From 7d4e4b1d2ff271146a1768f44a962e3a1f16a6cd Mon Sep 17 00:00:00 2001 From: oliveregger Date: Wed, 16 Oct 2024 11:58:05 +0200 Subject: [PATCH] FML transform performance tuning #264 --- docs/changelog.md | 1 + .../TransformSupportServices.java | 6 +- .../org/hl7/fhir/r5/elementmodel/Element.java | 1652 +++++++++++++++++ .../org/hl7/fhir/r5/elementmodel/Manager.java | 161 ++ .../structuremap/ITransformerServices.java | 31 + .../structuremap/StructureMapUtilities.java | 8 +- .../validation/TransformSupportServices.java | 66 + updatehapi.sh | 4 + 8 files changed, 1924 insertions(+), 5 deletions(-) create mode 100644 matchbox-engine/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java create mode 100644 matchbox-engine/src/main/java/org/hl7/fhir/r5/elementmodel/Manager.java create mode 100644 matchbox-engine/src/main/java/org/hl7/fhir/r5/utils/structuremap/ITransformerServices.java create mode 100644 matchbox-engine/src/main/java/org/hl7/fhir/validation/TransformSupportServices.java diff --git a/docs/changelog.md b/docs/changelog.md index f315ddb890f..15361af9fab 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -6,6 +6,7 @@ Unreleased - Terminology: support CodeableConcept in ValueSet/$validate operation [#291](https://github.com/ahdis/matchbox/issues/291) - Upgrade hapifhir org.hl7.fhir.core to 6.3.32 - FML: Use FMLParser in StructureMapUtilities and support for identity transform [#289](https://github.com/ahdis/matchbox/issues/289) +- FML: FML transform performance tuning #264 (via @mrunibe) 2024/10/07 Release 3.9.3 diff --git a/matchbox-engine/src/main/java/ch/ahdis/matchbox/mappinglanguage/TransformSupportServices.java b/matchbox-engine/src/main/java/ch/ahdis/matchbox/mappinglanguage/TransformSupportServices.java index 20056fb9bb8..7d47c6a8baf 100644 --- a/matchbox-engine/src/main/java/ch/ahdis/matchbox/mappinglanguage/TransformSupportServices.java +++ b/matchbox-engine/src/main/java/ch/ahdis/matchbox/mappinglanguage/TransformSupportServices.java @@ -23,6 +23,7 @@ import java.util.List; import org.hl7.fhir.exceptions.FHIRException; +import org.hl7.fhir.r5.conformance.profile.ProfileUtilities; import org.hl7.fhir.r5.context.IWorkerContext; import org.hl7.fhir.r5.elementmodel.Manager; import org.hl7.fhir.r5.model.Base; @@ -42,8 +43,9 @@ public TransformSupportServices(IWorkerContext worker, List outputs) { this.outputs = outputs; } + // matchbox patch https://github.com/ahdis/matchbox/issues/264 @Override - public Base createType(Object appInfo, String name) throws FHIRException { + public Base createType(Object appInfo, String name, ProfileUtilities profileUtilities) throws FHIRException { StructureDefinition sd = context.fetchResource(StructureDefinition.class, name); if (sd == null) { if (Utilities.existsInList(name, "http://hl7.org/fhirpath/System.String")) { @@ -53,7 +55,7 @@ public Base createType(Object appInfo, String name) throws FHIRException { if (sd == null) { throw new FHIRException("Unable to create type "+name); } - return Manager.build(context, sd); + return Manager.build(context, sd, profileUtilities); } @Override diff --git a/matchbox-engine/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java b/matchbox-engine/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java new file mode 100644 index 00000000000..27e87c653a5 --- /dev/null +++ b/matchbox-engine/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java @@ -0,0 +1,1652 @@ +package org.hl7.fhir.r5.elementmodel; + +import java.io.PrintStream; + +/* + Copyright (c) 2011+, HL7, Inc. + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of HL7 nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + */ + + +import java.util.*; + +import org.apache.commons.lang3.Validate; +import org.hl7.fhir.exceptions.FHIRException; +import org.hl7.fhir.r5.conformance.profile.ProfileUtilities; +import org.hl7.fhir.r5.context.ContextUtilities; +import org.hl7.fhir.r5.elementmodel.Element.SliceDefinition; +import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat; +import org.hl7.fhir.r5.extensions.ExtensionsUtils; +import org.hl7.fhir.r5.model.Base; +import org.hl7.fhir.r5.model.DataType; +import org.hl7.fhir.r5.model.ElementDefinition; +import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent; +import org.hl7.fhir.r5.model.Enumerations.BindingStrength; +import org.hl7.fhir.r5.model.ICoding; +import org.hl7.fhir.r5.model.StringType; +import org.hl7.fhir.r5.model.StructureDefinition; +import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind; +import org.hl7.fhir.r5.model.TypeConvertor; +import org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionContainsComponent; +import org.hl7.fhir.r5.terminologies.expansion.ValueSetExpansionOutcome; +import org.hl7.fhir.r5.utils.ToolingExtensions; +import org.hl7.fhir.utilities.ElementDecoration; +import org.hl7.fhir.utilities.ElementDecoration.DecorationType; +import org.hl7.fhir.utilities.FhirPublication; +import org.hl7.fhir.utilities.NamedItemList; +import org.hl7.fhir.utilities.NamedItemList.NamedItem; +import org.hl7.fhir.utilities.SourceLocation; +import org.hl7.fhir.utilities.Utilities; +import org.hl7.fhir.utilities.validation.ValidationMessage; +import org.hl7.fhir.utilities.xhtml.XhtmlNode; + +/** + * This class represents the underlying reference model of FHIR + * + * A resource is nothing but a set of elements, where every element has a + * name, maybe a stated type, maybe an id, and either a value or child elements + * (one or the other, but not both or neither) + * + * @author Grahame Grieve + * + */ +public class Element extends Base implements NamedItem { + public class SliceDefinition { + + private StructureDefinition profile; + private ElementDefinition definition; + private ElementDefinition slice; + + public SliceDefinition(StructureDefinition profile, ElementDefinition definition, ElementDefinition slice) { + this.profile = profile; + this.definition = definition; + this.slice = slice; + } + + public StructureDefinition getProfile() { + return profile; + } + + public ElementDefinition getDefinition() { + return definition; + } + + public ElementDefinition getSlice() { + return slice; + } + + } + + private static final HashSet extensionList = new HashSet<>(Arrays.asList("extension", "modifierExtension")); + + public enum SpecialElement { + CONTAINED, BUNDLE_ENTRY, BUNDLE_OUTCOME, BUNDLE_ISSUES, PARAMETER, LOGICAL; + + public static SpecialElement fromProperty(Property property) { + if (property.getStructure().getType().equals("Parameters")) + return PARAMETER; + if (property.getStructure().getType().equals("Bundle") && property.getName().equals("resource")) + return BUNDLE_ENTRY; + if (property.getStructure().getType().equals("Bundle") && property.getName().equals("outcome")) + return BUNDLE_OUTCOME; + if (property.getStructure().getType().equals("Bundle") && property.getName().equals("issues")) + return BUNDLE_ISSUES; + if (property.getName().equals("contained")) + return CONTAINED; + if (property.getStructure().getKind() == StructureDefinitionKind.LOGICAL) + return LOGICAL; + throw new FHIRException("Unknown resource containing a native resource: "+property.getDefinition().getId()); + } + + public String toHuman() { + switch (this) { + case BUNDLE_ENTRY: return "entry"; + case BUNDLE_OUTCOME: return "outcome"; + case BUNDLE_ISSUES: return "issues"; + case CONTAINED: return "contained"; + case PARAMETER: return "parameter"; + case LOGICAL: return "logical"; + default: return "??"; + } + } + } + + private List comments;// not relevant for production, but useful in documentation + private String name; + private String type; + private String value; + private int index = -1; + private NamedItemList children; + private Property property; + private Property elementProperty; // this is used when special is set to true - it tracks the underlying element property which is used in a few places + private int line; + private int col; + private SpecialElement special; + private XhtmlNode xhtml; // if this is populated, then value will also hold the string representation + private String explicitType; // for xsi:type attribute + private Element parentForValidator; + private boolean hasParentForValidator; + private String path; + private List messages; + private boolean prohibited; + private boolean required; + private int descendentCount; + private int instanceId; + private boolean isNull; + private Base source; + private boolean ignorePropertyOrder; + private FhirFormat format; + private Object nativeObject; + private List sliceDefinitions; + private boolean elided; + + public Element(String name) { + super(); + this.name = name; + } + + public Element(Element other) { + super(); + name = other.name; + type = other.type; + property = other.property; + elementProperty = other.elementProperty; + special = other.special; + } + + public Element(String name, Property property) { + super(); + this.name = name; + this.property = property; + if (property.isResource()) { + children = new NamedItemList<>(); + } + } + + public Element(String name, Property property, String type, String value) { + super(); + this.name = name; + this.property = property; + this.type = type; + this.value = value; + } + + public void updateProperty(Property property, SpecialElement special, Property elementProperty) { + this.property = property; + this.elementProperty = elementProperty; + this.special = special; + } + + public SpecialElement getSpecial() { + return special; + } + + public String getName() { + return name; + } + + public String getType() { + if (type == null) + return property.getType(name); + else + return type; + } + + public String getValue() { + return value; + } + + public boolean hasChildren() { + return !(children == null || children.isEmpty()); + } + + public NamedItemList getChildren() { + if (children == null) + children = new NamedItemList(); + return children; + } + + public boolean hasComments() { + return !(comments == null || comments.isEmpty()); + } + + public List getComments() { + if (comments == null) + comments = new ArrayList(); + return comments; + } + + public Property getProperty() { + return property; + } + + public void setValue(String value) { + this.value = value; + } + + public Element setType(String type) { + this.type = type; + return this; + + } + + public boolean isNull() { + return isNull; + } + + public void setNull(boolean isNull) { + this.isNull = isNull; + } + + public boolean hasValue() { + return value != null; + } + + public List getChildrenByName(String name) { + return children.getByName(name); + } + + public void numberChildren() { + if (children == null) + return; + + String last = ""; + int index = 0; + for (Element child : children) { + if (child.getProperty().isList()) { + if (last.equals(child.getName())) { + index++; + } else { + last = child.getName(); + index = 0; + } + child.index = index; + } else { + child.index = -1; + } + // matchbox patch https://github.com/ahdis/matchbox/issues/264 + // child.numberChildren(); + } + } + + public int getIndex() { + return index; + } + + public boolean hasIndex() { + return index > -1; + } + + public void setIndex(int index) { + this.index = index; + } + + public String getChildValue(String name) { + if (children == null) + return null; + for (Element child : children) { + if (name.equals(child.getName())) + return child.getValue(); + } + for (Element child : children) { + if (name.equals(child.getNameBase())) + return child.getValue(); + } + return null; + } + + private String getNameBase() { + if (property.isChoice()) { + return property.getName().replace("[x]", ""); + } else { + return getName(); + } + } + + public void setChildValue(String name, String value) { + if (children == null) + children = new NamedItemList(); + for (Element child : children) { + if (name.equals(child.getName())) { + if (!child.isPrimitive()) + throw new Error("Cannot set a value of a non-primitive type ("+name+" on "+this.getName()+")"); + child.setValue(value); + } + } + + try { + setProperty(name.hashCode(), name, new StringType(value)); + } catch (FHIRException e) { + throw new Error(e); + } + } + + public void setChildValue(String name, Base value) { + if (children == null) + children = new NamedItemList(); + for (Element child : children) { + if (nameMatches(child.getName(), name)) { + if (!child.isPrimitive()) + throw new Error("Cannot set a value of a non-primitive type ("+name+" on "+this.getName()+")"); + child.setValue(value.primitiveValue()); + return; + } + } + + try { + setProperty(name.hashCode(), name, value); + } catch (FHIRException e) { + throw new Error(e); + } + } + + public List getChildren(String name) { + List res = new ArrayList(); + if (children.size() > 20) { + List l = children.getByName(name); + if (l != null) { + res.addAll(l); + } + } else { + if (children != null) + for (Element child : children) { + if (name.equals(child.getName())) + res.add(child); + } + } + return res; + } + + public boolean hasType() { + if (type == null) + return property.hasType(name); + else + return true; + } + + @Override + public String fhirType() { + return getType(); + } + + @Override + public Base[] getProperty(int hash, String name, boolean checkValid) throws FHIRException { + if (isPrimitive() && (hash == "value".hashCode()) && !Utilities.noString(value)) { +// String tn = getType(); +// throw new Error(tn+" not done yet"); + Base[] b = new Base[1]; + b[0] = new StringType(value); + return b; + } + + List result = new ArrayList(); + if (children != null) { + if (children.size() > 20) { + List l = children.getByName(name); + if (l != null) { + result.addAll(l); + } + } else { + for (Element child : children) { + if (child.getName().equals(name)) { + result.add(child); + } + if (child.getName().startsWith(name) && child.getProperty().isChoice() && child.getProperty().getName().equals(name+"[x]")) { + result.add(child); + } + } + } + } + if (result.isEmpty() && checkValid) { +// throw new FHIRException("not determined yet"); + } + return result.toArray(new Base[result.size()]); + } + + @Override + protected void listChildren(List childProps) { + if (children != null) { + Map map = new HashMap(); + for (Element c : children) { + org.hl7.fhir.r5.model.Property p = map.get(c.getName()); + if (p == null) { + p = new org.hl7.fhir.r5.model.Property(c.getName(), c.fhirType(), c.getProperty().getDefinition().getDefinition(), c.getProperty().getDefinition().getMin(), maxToInt(c.getProperty().getDefinition().getMax()), c); + childProps.add(p); + map.put(c.getName(), p); + + } else + p.getValues().add(c); + } + } + } + + @Override + public Base setProperty(int hash, String name, Base value) throws FHIRException { + if ("xhtml".equals(getType()) && (hash == "value".hashCode())) { + this.xhtml = TypeConvertor.castToXhtml(value); + this.value = TypeConvertor.castToXhtmlString(value); + return this; + } + if (isPrimitive() && (hash == "value".hashCode())) { + this.value = TypeConvertor.castToString(value).asStringValue(); + return this; + } + + if (!value.isPrimitive() && !(value instanceof Element)) { + if (isDataType(value)) + value = convertToElement(property.getChild(name), value); + else + throw new FHIRException("Cannot set property "+name+" on "+this.name+" - value is not a primitive type ("+value.fhirType()+") or an ElementModel type"); + } + + if (children == null) + children = new NamedItemList(); + Element childForValue = null; + + // look through existing children + for (Element child : children) { + if (child.getName().equals(name)) { + if (!child.isList()) { + childForValue = child; + break; + } else { + Element ne = new Element(child).setFormat(format); + children.add(ne); + numberChildren(); + childForValue = ne; + break; + } + } + } + + int i = 0; + if (childForValue == null) + for (Property p : property.getChildProperties(this.name, type)) { + int t = -1; + for (int c =0; c < children.size(); c++) { + Element e = children.get(c); + if (p.getName().equals(e.getName())) + t = c; + } + if (t >= i) + i = t+1; + if (p.getName().equals(name) || p.getName().equals(name+"[x]")) { + Element ne = new Element(name, p).setFormat(format); + children.add(i, ne); + childForValue = ne; + break; + } else if (p.getName().endsWith("[x]") && name.startsWith(p.getName().replace("[x]", ""))) { + Element ne = new Element(p.getName(), p).setFormat(format); + children.add(i, ne); + childForValue = ne; + break; + } + } + + if (childForValue == null) + throw new Error("Cannot set property "+name+" on "+this.name); + else if (value.isPrimitive()) { + if (childForValue.property.getName().endsWith("[x]")) + childForValue.name = childForValue.name.replace("[x]", "")+Utilities.capitalize(value.fhirType()); + childForValue.setValue(value.primitiveValue()); + } else { + Element ve = (Element) value; + childForValue.type = ve.getType(); + if (childForValue.property.getName().endsWith("[x]")) + childForValue.name = name+Utilities.capitalize(childForValue.type); + else if (value.isResource()) { + if (childForValue.elementProperty == null) + childForValue.elementProperty = childForValue.property; + childForValue.property = ve.property; + childForValue.special = SpecialElement.BUNDLE_ENTRY; + } + if (ve.children != null) { + if (childForValue.children == null) + childForValue.children = new NamedItemList(); + else + childForValue.children.clear(); + childForValue.children.addAll(ve.children); + } + } + return childForValue; + } + + private Base convertToElement(Property prop, Base v) throws FHIRException { + return new ObjectConverter(property.getContext()).convert(prop, (DataType) v); + } + + private boolean isDataType(Base v) { + return v instanceof DataType && property.getContextUtils().getTypeNames().contains(v.fhirType()); + } + + @Override + public Base makeProperty(int hash, String name) throws FHIRException { + if (isPrimitive() && (hash == "value".hashCode())) { + return new StringType(value); + } else { + return makeElement(name); + } + } + + public Element makeElement(String name) throws FHIRException { + if (children == null) + children = new NamedItemList(); + + // look through existing children + for (Element child : children) { + if (child.getName().equals(name)) { + if (!child.isList()) { + return child; + } else { + Element ne = new Element(child).setFormat(format); + children.add(ne); + numberChildren(); + return ne; + } + } + } + + for (Property p : property.getChildProperties(this.name, type)) { + if (p.getName().equals(name)) { + Element ne = new Element(name, p).setFormat(format); + children.add(ne); + return ne; + } else if (p.getDefinition().isChoice() && name.startsWith(p.getName().replace("[x]", ""))) { + String type = name.substring(p.getName().length()-3); + if (property.getContext().isPrimitiveType(Utilities.uncapitalize(type))) { + type = Utilities.uncapitalize(type); + } + Element ne = new Element(name, p).setFormat(format); + ne.setType(type); + children.add(ne); + return ne; + + } + } + + throw new Error("Unrecognised name "+name+" on "+this.name); + } + + public Element forceElement(String name) throws FHIRException { + if (children == null) + children = new NamedItemList(); + + // look through existing children + for (Element child : children) { + if (child.getName().equals(name)) { + return child; + } + } + + for (Property p : property.getChildProperties(this.name, type)) { + if (p.getName().equals(name)) { + Element ne = new Element(name, p).setFormat(format); + children.add(ne); + return ne; + } + } + + throw new Error("Unrecognised name "+name+" on "+this.name); + } + + + private int maxToInt(String max) { + if (max.equals("*")) + return Integer.MAX_VALUE; + else + return Integer.parseInt(max); + } + + @Override + public boolean isPrimitive() { + return type != null ? property.isPrimitive(type) : property.isPrimitive(property.getType(name)); + } + + @Override + public boolean isBooleanPrimitive() { + return isPrimitive() && ("boolean".equals(type) || "boolean".equals(property.getType(name))); + } + + @Override + public boolean isResource() { + return property.isResource(); + } + + + @Override + public boolean hasPrimitiveValue() { + //return property.isPrimitiveName(name) || property.IsLogicalAndHasPrimitiveValue(name); + return super.hasPrimitiveValue(); + } + + @Override + public boolean canHavePrimitiveValue() { + return property.isPrimitiveName(name) || property.IsLogicalAndHasPrimitiveValue(name); + } + + + @Override + public String primitiveValue() { + if (isPrimitive() || value != null) + return value; + else { + if (canHavePrimitiveValue() && children != null) { + for (Element c : children) { + if (c.getName().equals("value")) + return c.primitiveValue(); + } + } + return null; + } + } + + // for the validator + public int line() { + return line; + } + + public int col() { + return col; + } + + public Element markLocation(int line, int col) { + this.line = line; + this.col = col; + return this; + } + + public Element markLocation(SourceLocation loc) { + this.line = loc.getLine(); + this.col = loc.getColumn(); + return this; + } + + public Element markLocation(Element src) { + this.line = src.line(); + this.col = src.col(); + return this; + } + + public void clearDecorations() { + clearUserData("fhir.decorations"); + for (Element e : children) { + e.clearDecorations(); + } + } + + public void markValidation(StructureDefinition profile, ElementDefinition definition) { + @SuppressWarnings("unchecked") + List decorations = (List) getUserData("fhir.decorations"); + if (decorations == null) { + decorations = new ArrayList<>(); + setUserData("fhir.decorations", decorations); + } + decorations.add(new ElementDecoration(DecorationType.TYPE, profile.getWebPath(), definition.getPath())); + if (definition.getId() != null && tail(definition.getId()).contains(":")) { + String[] details = tail(definition.getId()).split(":"); + decorations.add(new ElementDecoration(DecorationType.SLICE, null, details[1])); + } + } + + private String tail(String id) { + return id.contains(".") ? id.substring(id.lastIndexOf(".")+1) : id; + } + + public Element getNamedChild(String name) { + return getNamedChild(name, true); + } + + public Element getNamedChild(String name, boolean exception) { + if (children == null) + return null; + if (children.size() > 20) { + List l = children.getByName(name); + if (l == null || l.size() == 0) { + // try the other way (in case of complicated naming rules) + } else if (l.size() > 1 && exception) { + throw new Error("Attempt to read a single element when there is more than one present ("+name+")"); + } else { + return l.get(0); + } + } else { + + } + Element result = null; + + for (Element child : children) { + if (child.getName() != null && name != null && child.getProperty() != null && child.getProperty().getDefinition() != null && child.fhirType() != null) { + if (child.getName().equals(name) || (child.getName().length() > child.fhirType().length() && child.getName().substring(0, child.getName().length() - child.fhirType().length()).equals(name) && child.getProperty().getDefinition().isChoice())) { + if (result == null) + result = child; + else + throw new Error("Attempt to read a single element when there is more than one present ("+name+")"); + } + } + } + return result; + } + + public void getNamedChildren(String name, List list) { + if (children != null) + if (children.size() > 20) { + List l = children.getByName(name); + if (l != null) { + list.addAll(l); + } + } else { + for (Element child : children) + if (child.getName().equals(name)) + list.add(child); + } + } + + public String getNamedChildValue(String name) { + return getNamedChildValue(name, true); + } + + public String getNamedChildValue(String name, boolean exception) { + Element child = getNamedChild(name, exception); + return child == null ? null : child.value; + } + + public void getNamedChildrenWithWildcard(String string, List values) { + Validate.isTrue(string.endsWith("[x]")); + + String start = string.substring(0, string.length() - 3); + if (children != null) { + for (Element child : children) { + if (child.getName().startsWith(start)) { + values.add(child); + } + } + } + } + + + public XhtmlNode getXhtml() { + return xhtml; + } + + public Element setXhtml(XhtmlNode xhtml) { + this.xhtml = xhtml; + return this; + } + + @Override + public boolean isEmpty() { + // GG: this used to also test !"".equals(value). + // the condition where "" is empty and there are no children is an error, and so this really only manifested as an issue in corner cases technical testing of the validator / FHIRPath. + // it should not cause any problems in real life. + if (value != null) { + return false; + } + for (Element next : getChildren()) { + if (!next.isEmpty()) { + return false; + } + } + return true; + } + + public Property getElementProperty() { + return elementProperty; + } + + public boolean hasElementProperty() { + return elementProperty != null; + } + + public boolean hasChild(String name) { + return getNamedChild(name, true) != null; + } + + public boolean hasChild(String name, boolean exception) { + return getNamedChild(name, exception) != null; + } + + public boolean hasChildren(String name) { + if (children != null) + for (Element child : children) + if (child.getName().equals(name)) + return true; + return false; + } + + @Override + public String toString() { + if (name.equals(fhirType()) && isResource()) { + return fhirType()+"/"+getIdBase() + "["+(children == null || hasValue() ? value : Integer.toString(children.size())+" children")+"]"; + + } else if (isResource()) { + return name+"="+fhirType()+"/"+getIdBase()+ "["+(children == null || hasValue() ? value : Integer.toString(children.size())+" children")+"]"; + } else { + return name+"="+fhirType() + "["+(children == null || hasValue() ? value : Integer.toString(children.size())+" children")+"]"; + } + } + + @Override + public String getIdBase() { + return getChildValue("id"); + } + + @Override + public void setIdBase(String value) { + setChildValue("id", value); + } + + + @Override + public boolean equalsDeep(Base other) { + if (!super.equalsDeep(other)) + return false; + if (isPrimitive() && primitiveValue() != null && other.isPrimitive()) + return primitiveValue().equals(other.primitiveValue()); + if (isPrimitive() || other.isPrimitive()) + return false; + Set processed = new HashSet(); + for (org.hl7.fhir.r5.model.Property p : children()) { + String name = p.getName(); + processed.add(name); + org.hl7.fhir.r5.model.Property o = other.getChildByName(name); + if (!equalsDeep(p, o)) + return false; + } + for (org.hl7.fhir.r5.model.Property p : children()) { + String name = p.getName(); + if (!processed.contains(name)) { + org.hl7.fhir.r5.model.Property o = other.getChildByName(name); + if (!equalsDeep(p, o)) + return false; + } + } + return true; + } + + private boolean equalsDeep(org.hl7.fhir.r5.model.Property p, org.hl7.fhir.r5.model.Property o) { + if (o == null || p == null) + return false; + if (p.getValues().size() != o.getValues().size()) + return false; + for (int i = 0; i < p.getValues().size(); i++) + if (!Base.compareDeep(p.getValues().get(i), o.getValues().get(i), true)) + return false; + return true; + } + + @Override + public boolean equalsShallow(Base other) { + if (!super.equalsShallow(other)) + return false; + if (isPrimitive() && other.isPrimitive()) + return primitiveValue().equals(other.primitiveValue()); + if (isPrimitive() || other.isPrimitive()) + return false; + return true; //? + } + + public DataType asType() throws FHIRException { + return new ObjectConverter(property.getContext()).convertToType(this); + } + + @Override + public boolean isMetadataBased() { + return true; + } + + public boolean isList() { + if (elementProperty != null) + return elementProperty.isList(); + else + return property.isList(); + } + + public boolean isBaseList() { + if (elementProperty != null) + return elementProperty.isBaseList(); + else + return property.isBaseList(); + } + + @Override + public String[] getTypesForProperty(int hash, String name) throws FHIRException { + Property p = property.getChildSimpleName(this.name, name); + if (p != null) { + Set types = new HashSet(); + for (TypeRefComponent tr : p.getDefinition().getType()) { + types.add(tr.getCode()); + } + return types.toArray(new String[]{}); + } + return super.getTypesForProperty(hash, name); + + } + + public void sort() { + if (children != null) { + List remove = new ArrayList(); + for (Element child : children) { + child.sort(); + if (child.isEmpty()) + remove.add(child); + } + children.removeAll(remove); + children.sort(new ElementSortComparator(this, this.property)); + } + } + + public class ElementSortComparator implements Comparator { + private List children; + public ElementSortComparator(Element e, Property property) { + String tn = e.getType(); + StructureDefinition sd = property.getContext().fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(tn, null)); + if (sd != null && !sd.getAbstract()) + children = sd.getSnapshot().getElement(); + else + children = property.getStructure().getSnapshot().getElement(); + } + + @Override + public int compare(Element e0, Element e1) { + int i0 = find(e0); + int i1 = find(e1); + return Integer.compare(i0, i1); + } + private int find(Element e0) { + int i = e0.elementProperty != null ? children.indexOf(e0.elementProperty.getDefinition()) : children.indexOf(e0.property.getDefinition()); + return i; + } + + } + + public class ICodingImpl implements ICoding { + private String system; + private String version; + private String code; + private String display; + private boolean doesSystem; + private boolean doesVersion; + private boolean doesCode; + private boolean doesDisplay; + public ICodingImpl(boolean doesCode, boolean doesSystem, boolean doesVersion, boolean doesDisplay) { + super(); + this.doesCode = doesCode; + this.doesSystem = doesSystem; + this.doesVersion = doesVersion; + this.doesDisplay = doesDisplay; + } + public String getSystem() { + return system; + } + public String getVersion() { + return version; + } + public String getCode() { + return code; + } + public String getDisplay() { + return display; + } + public boolean hasSystem() { + return !Utilities.noString(system); + } + public boolean hasVersion() { + return !Utilities.noString(version); + } + public boolean hasCode() { + return !Utilities.noString(code); + } + public boolean hasDisplay() { + return !Utilities.noString(display); + } + public boolean supportsSystem() { + return doesSystem; + } + public boolean supportsVersion() { + return doesVersion; + } + public boolean supportsCode() { + return doesCode; + } + public boolean supportsDisplay() { + return doesDisplay; + } + } + + public ICoding getAsICoding() throws FHIRException { + if ("code".equals(fhirType())) { + if (property.getDefinition().getBinding().getStrength() != BindingStrength.REQUIRED) + return null; + ICodingImpl c = new ICodingImpl(true, true, false, false); + c.code = primitiveValue(); + ValueSetExpansionOutcome vse = property.getContext().expandVS(property.getStructure(), property.getDefinition().getBinding(), true, false); + if (vse.getValueset() == null) + return null; + for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) { + if (cc.getCode().equals(c.code)) { + c.system = cc.getSystem(); + if (cc.hasVersion()) { + c.doesVersion = true; + c.version = cc.getVersion(); + } + if (cc.hasDisplay()) { + c.doesDisplay = true; + c.display = cc.getDisplay(); + } + } + } + if (c.system == null) + return null; + return c; + } else if ("Coding".equals(fhirType())) { + ICodingImpl c = new ICodingImpl(true, true, true, true); + c.system = getNamedChildValue("system", false); + c.code = getNamedChildValue("code", false); + c.display = getNamedChildValue("display", false); + c.version = getNamedChildValue("version", false); + return c; + } else if ("Quantity".equals(fhirType())) { + ICodingImpl c = new ICodingImpl(true, true, false, false); + c.system = getNamedChildValue("system", false); + c.code = getNamedChildValue("code", false); + return c; + } else + return null; + } + + public String getExplicitType() { + return explicitType; + } + + public void setExplicitType(String explicitType) { + this.explicitType = explicitType; + } + + public boolean hasDescendant(Element element) { + if (children != null) { + for (Element child : children) { + if (element == child || child.hasDescendant(element)) { + return true; + } + } + } + return false; + } + + public Element getExtension(String url) { + if (children != null) { + for (Element child : children) { + if (extensionList.contains(child.getName())) { + String u = child.getChildValue("url"); + if (url.equals(u)) { + return child; + } + } + } + } + return null; + } + + public List getExtensions(String url) { + List list = new ArrayList<>(); + if (children != null) { + for (Element child : children) { + if (extensionList.contains(child.getName())) { + String u = child.getChildValue("url"); + if (url.equals(u)) { + list.add(child); + } + } + } + } + return list; + } + + public Base getExtensionValue(String url) { + if (children != null) { + for (Element child : children) { + if (Utilities.existsInList(child.getName(), "extension", "modifierExtension")) { + String u = child.getChildValue("url"); + if (url.equals(u)) { + return child.getNamedChild("value", false); + } + } + } + } + return null; + } + + public boolean hasExtension(String url) { + if (children != null) { + for (Element child : children) { + if (Utilities.existsInList(child.getName(), "extension", "modifierExtension")) { + String u = child.getChildValue("url"); + if (url.equals(u)) { + return true; + } + } + } + } + return false; + } + + /** + * this is set by the instance validator. There's no reason to maintain this when working with an element tree, and so it should be ignored outside the validator + */ + public Element getParentForValidator() { + if (!hasParentForValidator) { + throw new Error("Parent not set"); + } + return parentForValidator; + } + + public void setParentForValidator(Element parentForValidator) { + this.parentForValidator = parentForValidator; + this.hasParentForValidator = true; + } + + public boolean hasParentForValidator() { + return hasParentForValidator; + } + + public void clear() { + comments = null; + children.clear(); + property = null; + elementProperty = null; + xhtml = null; + path = null; + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public void addMessage(ValidationMessage vm) { + if (messages == null) { + messages = new ArrayList<>(); + } + messages.add(vm); + } + + public boolean hasMessages() { + return messages != null && !messages.isEmpty(); + } + + public List getMessages() { + return messages; + } + + public void removeChild(String name) { + if (children.removeIf(n -> name.equals(n.getName()))) { + children.clearMap(); + } + } + + public boolean isProhibited() { + return prohibited; + } + + public void setProhibited(boolean prohibited) { + this.prohibited = prohibited; + } + + public boolean isRequired() { + return required; + } + + public void setRequired(boolean required) { + this.required = required; + } + + public int getDescendentCount() { + return descendentCount; + } + + public void setDescendentCount(int descendentCount) { + this.descendentCount = descendentCount; + } + + public int countDescendents() { + if (descendentCount > 0) { + return descendentCount; + } else if (children != null) { + descendentCount = children.size(); + for (Element e : children) { + descendentCount = descendentCount + e.countDescendents(); + } + } else { + descendentCount = 0; + } + return descendentCount; + } + + public int getInstanceId() { + return instanceId; + } + + public void setInstanceId(int instanceId) { + this.instanceId = instanceId; + } + + + @Override + public boolean hasValidationInfo() { + return hasSource() ? source.hasValidationInfo() : super.hasValidationInfo(); + } + + @Override + public List getValidationInfo() { + return hasSource() ? source.getValidationInfo() : super.getValidationInfo(); + } + + @Override + public ValidationInfo addDefinition(StructureDefinition source, ElementDefinition defn, ValidationMode mode) { + if (this.source != null) { + return this.source.addDefinition(source, defn, mode); + } else { + return super.addDefinition(source, defn, mode); + } + } + + public boolean hasSource() { + return source != null; + } + + + public Base getSource() { + return source; + } + + public void setSource(Base source) { + this.source = source; + } + + public void printToOutput() { + printToOutput(System.out, ""); + + } + + public void printToOutput(PrintStream stream) { + printToOutput(stream, ""); + + } + + private void printToOutput(PrintStream out, String indent) { + String s = indent+name +(index == -1 ? "" : "["+index+"]") +(special != null ? "$"+special.toHuman(): "")+ (type!= null || explicitType != null ? " : "+type+(explicitType != null ? "/'"+explicitType+"'" : "") : ""); + if (isNull) { + s = s + " = (null)"; + } else if (value != null) { + s = s + " = '"+value+"'"; + } else if (xhtml != null) { + s = s + " = (xhtml)"; + } + if (property != null) { + s = s +" {"+property.summary(); + if (elementProperty != null) { + s = s +" -> "+elementProperty.summary(); + } + s = s + "}"; + } + if (line > 0) { + s = s + " (l"+line+":c"+col+")"; + } + out.println(s); + if (children != null) { + for (Element child : children) { + child.printToOutput(out, indent+" "); + } + } + + } + + private String msgCounts() { + int e = 0; + int w = 0; + int h = 0; + for (ValidationMessage msg : messages) { + switch (msg.getLevel()) { + case ERROR: + e++; + break; + case FATAL: + e++; + break; + case INFORMATION: + h++; + break; + case NULL: + break; + case WARNING: + w++; + break; + default: + break; + } + } + return "e:"+e+",w:"+w+",h:"+h; + } + + public void populatePaths(String path) { + if (path == null) { + path = fhirType(); + } + setPath(path); + if (children != null) { + for (Element n : children) { + n.populatePaths(path+"."+n.getName()); + } + } + + } + + public String fhirTypeRoot() { + if (fhirType().contains("/")) { + return fhirType().substring(fhirType().lastIndexOf("/")+1); + } else { + return fhirType(); + } + } + + public void setElement(String string, Element map) { + throw new Error("Not done yet"); + } + + public Element addElement(String name) { + if (children == null) + children = new NamedItemList(); + int insertionPoint = 0; + + for (Property p : property.getChildProperties(this.name, type)) { + while (insertionPoint < children.size() && nameMatches(children.get(insertionPoint).getName(), p.getName())) { + insertionPoint++; + } + if (p.getName().equals(name)) { + if (!p.isList() && hasChild(name, false)) { + throw new Error(name+" on "+this.name+" is not a list, so can't add an element"); + } + Element ne = new Element(name, p).setFormat(format); + children.add(insertionPoint, ne); + return ne; + } + // polymorphic support + if (p.getName().endsWith("[x]")) { + String base = p.getName().substring(0, p.getName().length()-3); + + if (name.startsWith(base)) { + String type = name.substring(base.length()); + if (p.getContextUtils().isPrimitiveType(Utilities.uncapitalize(type))) { + type = Utilities.uncapitalize(type); + } + if (p.canBeType(type)) { + Element ne = new Element(name, p).setFormat(format); + ne.setType(type); + children.add(insertionPoint, ne); + return ne; + } + } + } + } + + throw new Error("Unrecognised property '"+name+"' on "+this.name); + } + + private boolean nameMatches(String elementName, String propertyName) { + if (propertyName.endsWith("[x]")) { + String base = propertyName.replace("[x]", ""); + return elementName.startsWith(base); + } else { + return elementName.equals(propertyName); + } + } + + @Override + public Base copy() { + Element element = new Element(this); + this.copyValues(element); + if (this.isElided()) + element.setElided(true); + return element; + } + + @Override + public void copyValues(Base dst) { + super.copyValues(dst); + + Element dest = (Element) dst; + if (comments != null) { + dest.comments = new ArrayList<>(); + dest.comments.addAll(comments); + } else { + dest.comments = null; + } + dest.value = value; + if (children != null) { + dest.children = new NamedItemList<>(); + for (Element child : children) { + dest.children.add((Element) child.copy()); + } + } else { + dest.children = null; + } + dest.line = line; + dest.col = col; + dest.xhtml = xhtml; + dest.explicitType = explicitType; + dest.hasParentForValidator = false; + dest.path = path; + dest.messages = null; + dest.prohibited = prohibited; + dest.required = required; + dest.descendentCount = descendentCount; + dest.instanceId = instanceId; + dest.isNull = isNull; + dest.source = source; + dest.format = format; + } + + public Base setProperty(String name, Base value) throws FHIRException { + setChildValue(name, value.primitiveValue()); + return this; + } + + public boolean isIgnorePropertyOrder() { + return ignorePropertyOrder; + } + + public void setIgnorePropertyOrder(boolean ignorePropertyOrder) { + this.ignorePropertyOrder = ignorePropertyOrder; + if (children != null) { + for (Element e : children) { + e.setIgnorePropertyOrder(ignorePropertyOrder); + } + } + } + + + private String webPath; + public boolean hasWebPath() { + return webPath != null; + } + public String getWebPath() { + return webPath; + } + public void setWebPath(String webPath) { + this.webPath = webPath; + } + + public String getTranslation(String lang) { + for (Element e : getChildren()) { + if (e.fhirType().equals("Extension")) { + String url = e.getNamedChildValue("url", false); + if (ToolingExtensions.EXT_TRANSLATION.equals(url)) { + String l = null; + String v = null; + for (Element g : e.getChildren()) { + if (g.fhirType().equals("Extension")) { + String u = g.getNamedChildValue("url", false); + if ("lang".equals(u)) { + l = g.getNamedChildValue("value", false); + } else if ("value".equals(u)) { + v = g.getNamedChildValue("value", false); + } + } + } + if (LanguageUtils.langsMatch(lang, l)) { + return v; + } + } + } + } + return null; + } + + public String getBasePath() { + if (property.getStructure().hasExtension(ToolingExtensions.EXT_RESOURCE_IMPLEMENTS)) { + StructureDefinition sd = property.getContext().fetchResource(StructureDefinition.class, ExtensionsUtils.getExtensionString(property.getStructure(), ToolingExtensions.EXT_RESOURCE_IMPLEMENTS)); + if (sd != null) { + ElementDefinition ed = sd.getSnapshot().getElementByPath(property.getDefinition().getPath().replace(property.getStructure().getType(), sd.getType())); + if (ed != null) { + return ed.getBase().getPath(); + } + } + } + return property.getDefinition().getBase().getPath(); + } + + public void setTranslation(String lang, String translation) { + for (Element e : getChildren()) { + if (e.fhirType().equals("Extension")) { + String url = e.getNamedChildValue("url", false); + if (ToolingExtensions.EXT_TRANSLATION.equals(url)) { + String l = null; + Element v = null; + for (Element g : e.getChildren()) { + if (g.fhirType().equals("Extension")) { + String u = g.getNamedChildValue("url", false); + if ("lang".equals(u)) { + l = g.getNamedChildValue("value", false); + } else if ("value".equals(u)) { + v = g.getNamedChild("value", false); + } + } + } + if (LanguageUtils.langsMatch(lang, l)) { + if (v == null) { + Element ext = e.addElement("extension"); + ext.addElement("url").setValue("value"); + ext.addElement("valueString").setValue(translation); + } else { + v.setValue(translation); + } + } + } + } + } + Element t = addElement("extension"); + t.addElement("url").setValue(ToolingExtensions.EXT_TRANSLATION); + + Element ext = t.addElement("extension"); + ext.addElement("url").setValue("lang"); + ext.addElement("valueCode").setValue(lang); + + ext = t.addElement("extension"); + ext.addElement("url").setValue("content"); + ext.addElement("valueString").setValue(translation); + } + + @Override + public String getListName() { + if (getProperty().getName().endsWith("[x]")) { + String n = getProperty().getName(); + return n.substring(0, n.length()-3); + } else { + return getName(); + } + } + + public FhirFormat getFormat() { + return format; + } + + public Element setFormat(FhirFormat format) { + this.format = format; + return this; + } + + public Object getNativeObject() { + return nativeObject; + } + + public Element setNativeObject(Object nativeObject) { + this.nativeObject = nativeObject; + return this; + } + + public void removeExtension(String url) { + List rem = new ArrayList<>(); + for (Element e : children) { + if ("extension".equals(e.getName()) && url.equals(e.getChildValue("url"))) { + rem.add(e); + } + } + children.removeAll(rem); + } + + public void addSliceDefinition(StructureDefinition profile, ElementDefinition definition, ElementDefinition slice) { + if (sliceDefinitions == null) { + sliceDefinitions = new ArrayList<>(); + } + sliceDefinitions.add(new SliceDefinition(profile, definition, slice)); + } + + public boolean hasSlice(StructureDefinition sd, String sliceName) { + if (sliceDefinitions != null) { + for (SliceDefinition def : sliceDefinitions) { + if (def.profile == sd && sliceName.equals(def.definition.getSliceName())) { + return true; + } + } + } + return false; + } + + public FhirPublication getFHIRPublicationVersion() { + return FhirPublication.fromCode(property.getStructure().getVersion()); + } + + public void setElided(boolean elided) { + this.elided = elided; + } + + public boolean isElided() { + return this.elided; + } +} \ No newline at end of file diff --git a/matchbox-engine/src/main/java/org/hl7/fhir/r5/elementmodel/Manager.java b/matchbox-engine/src/main/java/org/hl7/fhir/r5/elementmodel/Manager.java new file mode 100644 index 00000000000..7d541332573 --- /dev/null +++ b/matchbox-engine/src/main/java/org/hl7/fhir/r5/elementmodel/Manager.java @@ -0,0 +1,161 @@ +package org.hl7.fhir.r5.elementmodel; + +/* + Copyright (c) 2011+, HL7, Inc. + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of HL7 nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + */ + + + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.List; + +import org.hl7.fhir.exceptions.DefinitionException; +import org.hl7.fhir.exceptions.FHIRException; +import org.hl7.fhir.exceptions.FHIRFormatError; +// matchbox patch https://github.com/ahdis/matchbox/issues/264 +import org.hl7.fhir.r5.conformance.profile.ProfileUtilities; +import org.hl7.fhir.r5.context.ContextUtilities; +import org.hl7.fhir.r5.context.IWorkerContext; +import org.hl7.fhir.r5.formats.IParser.OutputStyle; +import org.hl7.fhir.r5.model.StructureDefinition; + +public class Manager { + + //TODO use EnumMap + public enum FhirFormat { XML, JSON, TURTLE, TEXT, VBAR, SHC, SHL, FML, NDJSON; + // SHC = smart health cards, including as text versions of QR codes + // SHL = smart health links, also a text version of the QR code + + public String getExtension() { + switch (this) { + case JSON: + return "json"; + case TURTLE: + return "ttl"; + case XML: + return "xml"; + case TEXT: + return "txt"; + case VBAR: + return "hl7"; + case SHC: + return "shc"; + case SHL: + return "shl"; + case FML: + return "fml"; + case NDJSON: + return "ndjson"; + } + return null; + } + + public static FhirFormat getFhirFormat(String code) { + switch (code) { + case "json": + return JSON; + case "ttl": + return TURTLE; + case "xml": + return XML; + case "txt": + return TEXT; + case "hl7": + return VBAR; + case "shc": + return SHC; + case "shl": + return SHL; + case "fml": + return FML; + case "ndjson": + return NDJSON; + } + return null; + } + public static FhirFormat readFromMimeType(String mt) { + if (mt == null) { + return null; + } + if (mt.contains("/xml") || mt.contains("+xml")) { + return FhirFormat.XML; + } + if (mt.contains("/json") || mt.contains("+json")) { + return FhirFormat.JSON; + } + return null; + } + } + + public static List parse(IWorkerContext context, InputStream source, FhirFormat inputFormat) throws FHIRFormatError, DefinitionException, IOException, FHIRException { + return makeParser(context, inputFormat).parse(source); + } + + public static Element parseSingle(IWorkerContext context, InputStream source, FhirFormat inputFormat) throws FHIRFormatError, DefinitionException, IOException, FHIRException { + return makeParser(context, inputFormat).parseSingle(source, null); + } + + + public static void compose(IWorkerContext context, Element e, OutputStream destination, FhirFormat outputFormat, OutputStyle style, String base) throws FHIRException, IOException { + makeParser(context, outputFormat).compose(e, destination, style, base); + } + + public static ParserBase makeParser(IWorkerContext context, FhirFormat format) { + if (format == null) { + throw new Error("Programming logic error: no format known"); + } + switch (format) { + case JSON : return new JsonParser(context); + case NDJSON : return new NDJsonParser(context); + case XML : return new XmlParser(context); + case TURTLE : return new TurtleParser(context); + case VBAR : return new VerticalBarParser(context); + case SHC : return new SHCParser(context); + case SHL : return new SHLParser(context); + case FML : return new FmlParser(context); + case TEXT : throw new Error("Programming logic error: do not call makeParser for a text resource"); + } + return null; + } + + public static Element build(IWorkerContext context, StructureDefinition sd) { + // matchbox patch https://github.com/ahdis/matchbox/issues/264 + return build(context, sd, new ProfileUtilities(context, null, null)); + } + + // matchbox patch https://github.com/ahdis/matchbox/issues/264 + public static Element build(IWorkerContext context, StructureDefinition sd, ProfileUtilities profileUtilities) { + Property p = new Property(context, sd.getSnapshot().getElementFirstRep(), sd, profileUtilities, new ContextUtilities(context)); + Element e = new Element(p.getName(), p); + e.setPath(sd.getType()); + return e; + } + +} \ No newline at end of file diff --git a/matchbox-engine/src/main/java/org/hl7/fhir/r5/utils/structuremap/ITransformerServices.java b/matchbox-engine/src/main/java/org/hl7/fhir/r5/utils/structuremap/ITransformerServices.java new file mode 100644 index 00000000000..3f8e5ac31ab --- /dev/null +++ b/matchbox-engine/src/main/java/org/hl7/fhir/r5/utils/structuremap/ITransformerServices.java @@ -0,0 +1,31 @@ +package org.hl7.fhir.r5.utils.structuremap; + +import org.hl7.fhir.exceptions.FHIRException; +// matchbox patch https://github.com/ahdis/matchbox/issues/264 +import org.hl7.fhir.r5.conformance.profile.ProfileUtilities; +import org.hl7.fhir.r5.model.Base; +import org.hl7.fhir.r5.model.Coding; + +import java.util.List; + +public interface ITransformerServices { + // public boolean validateByValueSet(Coding code, String valuesetId); + public void log(String message); // log internal progress + +// matchbox patch https://github.com/ahdis/matchbox/issues/264 +public Base createType(Object appInfo, String name, ProfileUtilities profileUtilities) throws FHIRException; + + public Base createResource(Object appInfo, Base res, boolean atRootofTransform); // an already created resource is provided; this is to identify/store it + + public Coding translate(Object appInfo, Coding source, String conceptMapUrl) throws FHIRException; + + // public Coding translate(Coding code) + // ValueSet validation operation + // Translation operation + // Lookup another tree of data + // Create an instance tree + // Return the correct string format to refer to a tree (input or output) + public Base resolveReference(Object appContext, String url) throws FHIRException; + + public List performSearch(Object appContext, String url) throws FHIRException; +} diff --git a/matchbox-engine/src/main/java/org/hl7/fhir/r5/utils/structuremap/StructureMapUtilities.java b/matchbox-engine/src/main/java/org/hl7/fhir/r5/utils/structuremap/StructureMapUtilities.java index cb3b0a92421..f02b1e881e8 100644 --- a/matchbox-engine/src/main/java/org/hl7/fhir/r5/utils/structuremap/StructureMapUtilities.java +++ b/matchbox-engine/src/main/java/org/hl7/fhir/r5/utils/structuremap/StructureMapUtilities.java @@ -345,7 +345,7 @@ public static String ruleToString(StructureMapGroupRuleComponent r) { } private static void renderRule(StringBuilder b, StructureMapGroupRuleComponent r, int indent) { - // matchbox pr https://github.com/hapifhir/org.hl7.fhir.core/issues/1777 + // matchbox pr https://github.com/hapifhir/org.hl7.fhir.core/issues/1777 if (r.hasDocumentation()) { renderMultilineDoco(b, r.getDocumentation(), indent, true); } @@ -1249,7 +1249,8 @@ else if (srcVar != null) { } } } - Base res = services != null ? services.createType(context.getAppInfo(), tn) : typeFactory(tn); + // matchbox patch https://github.com/ahdis/matchbox/issues/264 + Base res = services != null ? services.createType(context.getAppInfo(), tn, profileUtilities) : typeFactory(tn); if (res.isResource() && !res.fhirType().equals("Parameters")) { // res.setIdBase(tgt.getParameter().size() > 1 ? getParamString(vars, tgt.getParameter().get(0)) : UUID.randomUUID().toString().toLowerCase()); if (services != null) @@ -1390,7 +1391,8 @@ private Base typeFactory(String tn) { if (sd == null) { throw new FHIRException("Unable to create type "+tn); } else { - return Manager.build(worker, sd); + // matchbox pr https://github.com/hapifhir/org.hl7.fhir.core/issues/1777 + return Manager.build(worker, sd, profileUtilities); } } else { return ResourceFactory.createResourceOrType(tn); diff --git a/matchbox-engine/src/main/java/org/hl7/fhir/validation/TransformSupportServices.java b/matchbox-engine/src/main/java/org/hl7/fhir/validation/TransformSupportServices.java new file mode 100644 index 00000000000..3a5854d0835 --- /dev/null +++ b/matchbox-engine/src/main/java/org/hl7/fhir/validation/TransformSupportServices.java @@ -0,0 +1,66 @@ +package org.hl7.fhir.validation; + +import java.io.PrintWriter; +import java.util.List; + +import org.hl7.fhir.exceptions.FHIRException; +import org.hl7.fhir.r5.conformance.profile.ProfileUtilities; +import org.hl7.fhir.r5.context.SimpleWorkerContext; +import org.hl7.fhir.r5.elementmodel.Manager; +import org.hl7.fhir.r5.model.Base; +import org.hl7.fhir.r5.model.Coding; +import org.hl7.fhir.r5.model.StructureDefinition; +import org.hl7.fhir.r5.terminologies.ConceptMapEngine; +import org.hl7.fhir.r5.utils.structuremap.ITransformerServices; + +public class TransformSupportServices implements ITransformerServices { + + private final PrintWriter mapLog; + private final SimpleWorkerContext context; + private List outputs; + + public TransformSupportServices(List outputs, + PrintWriter mapLog, + SimpleWorkerContext context) { + this.outputs = outputs; + this.mapLog = mapLog; + this.context = context; + } + + @Override + public void log(String message) { + if (mapLog != null) + mapLog.println(message); + System.out.println(message); + } + +// matchbox patch https://github.com/ahdis/matchbox/issues/264 + @Override + public Base createType(Object appInfo, String name, ProfileUtilities profileUtilities) throws FHIRException { + StructureDefinition sd = context.fetchResource(StructureDefinition.class, name); + return Manager.build(context, sd, profileUtilities); + } + + @Override + public Base createResource(Object appInfo, Base res, boolean atRootofTransform) { + if (atRootofTransform) + outputs.add(res); + return res; + } + + @Override + public Coding translate(Object appInfo, Coding source, String conceptMapUrl) throws FHIRException { + ConceptMapEngine cme = new ConceptMapEngine(context); + return cme.translate(source, conceptMapUrl); + } + + @Override + public Base resolveReference(Object appContext, String url) throws FHIRException { + throw new FHIRException("resolveReference is not supported yet"); + } + + @Override + public List performSearch(Object appContext, String url) throws FHIRException { + throw new FHIRException("performSearch is not supported yet"); + } +} diff --git a/updatehapi.sh b/updatehapi.sh index 1965629d378..cb59be882d3 100755 --- a/updatehapi.sh +++ b/updatehapi.sh @@ -1,11 +1,15 @@ cp ../org.hl7.fhir.core/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java matchbox-engine/src/main/java/org/hl7/fhir/utilities/npm cp ../org.hl7.fhir.core/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java matchbox-engine/src/main/java/org/hl7/fhir/r5/elementmodel cp ../org.hl7.fhir.core/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/FmlParser.java matchbox-engine/src/main/java/org/hl7/fhir/r5/elementmodel +cp ../org.hl7.fhir.core/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/Manager.java matchbox-engine/src/main/java/org/hl7/fhir/r5/elementmodel +cp ../org.hl7.fhir.core/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/Element.java matchbox-engine/src/main/java/org/hl7/fhir/r5/elementmodel cp ../org.hl7.fhir.core/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/structuremap/FHIRPathHostServices.java matchbox-engine/src/main/java/org/hl7/fhir/r5/utils/structuremap/ cp ../org.hl7.fhir.core/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/structuremap/StructureMapUtilities.java matchbox-engine/src/main/java/org/hl7/fhir/r5/utils/structuremap/ +cp ../org.hl7.fhir.core/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/structuremap/ITransformerServices.java matchbox-engine/src/main/java/org/hl7/fhir/r5/utils/structuremap/ cp ../org.hl7.fhir.core/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java matchbox-engine/src/main/java/org/hl7/fhir/r5/context/ cp ../org.hl7.fhir.core/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java matchbox-engine/src/main/java/org/hl7/fhir/utilities/npm/ cp ../org.hl7.fhir.core/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/instance/InstanceValidator.java matchbox-engine/src/main/java/org/hl7/fhir/validation/instance/ +cp ../org.hl7.fhir.core/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/TransformSupportServices.java matchbox-engine/src/main/java/org/hl7/fhir/validation/ # cp ../hapi-fhir/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/ResourceParameter.java matchbox-server/src/main/java/ca/uhn/fhir/rest/server/method # cp ../hapi-fhir/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java matchbox-server/src/main/java/ca/uhn/fhir/rest/server # cp ../hapi-fhir/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/packages/loader/PackageLoaderSvc.java matchbox-server/src/main/java/ca/uhn/fhir/jpa/packages/loader/ \ No newline at end of file