use of org.hl7.elm.r1.Expression in project org.hl7.fhir.core by hapifhir.
the class SearchParameter method setProperty.
@Override
public Base setProperty(String name, Base value) throws FHIRException {
if (name.equals("url")) {
// UriType
this.url = castToUri(value);
} else if (name.equals("version")) {
// StringType
this.version = castToString(value);
} else if (name.equals("name")) {
// StringType
this.name = castToString(value);
} else if (name.equals("status")) {
value = new PublicationStatusEnumFactory().fromType(castToCode(value));
// Enumeration<PublicationStatus>
this.status = (Enumeration) value;
} else if (name.equals("experimental")) {
// BooleanType
this.experimental = castToBoolean(value);
} else if (name.equals("date")) {
// DateTimeType
this.date = castToDateTime(value);
} else if (name.equals("publisher")) {
// StringType
this.publisher = castToString(value);
} else if (name.equals("contact")) {
this.getContact().add(castToContactDetail(value));
} else if (name.equals("useContext")) {
this.getUseContext().add(castToUsageContext(value));
} else if (name.equals("jurisdiction")) {
this.getJurisdiction().add(castToCodeableConcept(value));
} else if (name.equals("purpose")) {
// MarkdownType
this.purpose = castToMarkdown(value);
} else if (name.equals("code")) {
// CodeType
this.code = castToCode(value);
} else if (name.equals("base")) {
this.getBase().add(castToCode(value));
} else if (name.equals("type")) {
value = new SearchParamTypeEnumFactory().fromType(castToCode(value));
// Enumeration<SearchParamType>
this.type = (Enumeration) value;
} else if (name.equals("derivedFrom")) {
// UriType
this.derivedFrom = castToUri(value);
} else if (name.equals("description")) {
// MarkdownType
this.description = castToMarkdown(value);
} else if (name.equals("expression")) {
// StringType
this.expression = castToString(value);
} else if (name.equals("xpath")) {
// StringType
this.xpath = castToString(value);
} else if (name.equals("xpathUsage")) {
value = new XPathUsageTypeEnumFactory().fromType(castToCode(value));
// Enumeration<XPathUsageType>
this.xpathUsage = (Enumeration) value;
} else if (name.equals("target")) {
this.getTarget().add(castToCode(value));
} else if (name.equals("comparator")) {
value = new SearchComparatorEnumFactory().fromType(castToCode(value));
this.getComparator().add((Enumeration) value);
} else if (name.equals("modifier")) {
value = new SearchModifierCodeEnumFactory().fromType(castToCode(value));
this.getModifier().add((Enumeration) value);
} else if (name.equals("chain")) {
this.getChain().add(castToString(value));
} else if (name.equals("component")) {
this.getComponent().add((SearchParameterComponentComponent) value);
} else
return super.setProperty(name, value);
return value;
}
use of org.hl7.elm.r1.Expression in project org.hl7.fhir.core by hapifhir.
the class FluentPathTests method test.
@ParameterizedTest(name = "{index}: file {0}")
@MethodSource("data")
public void test(String name, Element element) throws IOException, FHIRException {
String input = element.getAttribute("inputfile");
String expression = XMLUtil.getNamedChild(element, "expression").getTextContent();
boolean fail = "true".equals(XMLUtil.getNamedChild(element, "expression").getAttribute("invalid"));
Resource res = null;
List<Base> outcome = new ArrayList<Base>();
ExpressionNode node = fp.parse(expression);
try {
if (Utilities.noString(input))
fp.check(null, null, node);
else {
res = new XmlParser().parse(new FileInputStream(Utilities.path("C:\\work\\org.hl7.fhir\\build\\publish", input)));
fp.check(res, res.getResourceType().toString(), res.getResourceType().toString(), node);
}
outcome = fp.evaluate(res, node);
Assertions.assertTrue(!fail, String.format("Expected exception parsing %s", expression));
} catch (Exception e) {
Assertions.assertTrue(fail, String.format("Unexpected exception parsing %s: " + e.getMessage(), expression));
}
if ("true".equals(element.getAttribute("predicate"))) {
boolean ok = fp.convertToBoolean(outcome);
outcome.clear();
outcome.add(new BooleanType(ok));
}
if (fp.hasLog())
System.out.println(fp.takeLog());
List<Element> expected = new ArrayList<Element>();
XMLUtil.getNamedChildren(element, "output", expected);
Assertions.assertTrue(outcome.size() == expected.size(), String.format("Expected %d objects but found %d", expected.size(), outcome.size()));
for (int i = 0; i < Math.min(outcome.size(), expected.size()); i++) {
String tn = expected.get(i).getAttribute("type");
if (!Utilities.noString(tn)) {
Assertions.assertTrue(tn.equals(outcome.get(i).fhirType()), String.format("Outcome %d: Type should be %s but was %s", i, tn, outcome.get(i).fhirType()));
}
String v = expected.get(i).getTextContent();
if (!Utilities.noString(v)) {
Assertions.assertTrue(outcome.get(i) instanceof PrimitiveType, String.format("Outcome %d: Value should be a primitive type but was %s", i, outcome.get(i).fhirType()));
Assertions.assertTrue(v.equals(((PrimitiveType) outcome.get(i)).asStringValue()), String.format("Outcome %d: Value should be %s but was %s", i, v, outcome.get(i).toString()));
}
}
}
use of org.hl7.elm.r1.Expression in project org.hl7.fhir.core by hapifhir.
the class RdfParser method composePlanDefinitionPlanDefinitionActionConditionComponent.
protected void composePlanDefinitionPlanDefinitionActionConditionComponent(Complex parent, String parentType, String name, PlanDefinition.PlanDefinitionActionConditionComponent element, int index) {
if (element == null)
return;
Complex t;
if (Utilities.noString(parentType))
t = parent;
else {
t = parent.predicate("fhir:" + parentType + '.' + name);
}
composeBackboneElement(t, "condition", name, element, index);
if (element.hasKindElement())
composeEnum(t, "PlanDefinition", "kind", element.getKindElement(), -1);
if (element.hasExpression())
composeExpression(t, "PlanDefinition", "expression", element.getExpression(), -1);
}
use of org.hl7.elm.r1.Expression in project org.hl7.fhir.core by hapifhir.
the class RdfParser method composePlanDefinitionPlanDefinitionActionDynamicValueComponent.
protected void composePlanDefinitionPlanDefinitionActionDynamicValueComponent(Complex parent, String parentType, String name, PlanDefinition.PlanDefinitionActionDynamicValueComponent element, int index) {
if (element == null)
return;
Complex t;
if (Utilities.noString(parentType))
t = parent;
else {
t = parent.predicate("fhir:" + parentType + '.' + name);
}
composeBackboneElement(t, "dynamicValue", name, element, index);
if (element.hasPathElement())
composeString(t, "PlanDefinition", "path", element.getPathElement(), -1);
if (element.hasExpression())
composeExpression(t, "PlanDefinition", "expression", element.getExpression(), -1);
}
use of org.hl7.elm.r1.Expression in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method evaluateDefinition.
/**
* given an element definition in a profile, what element contains the differentiating fixed
* for the element, given the differentiating expresssion. The expression is only allowed to
* use a subset of FHIRPath
*
* @param profile
* @param element
* @return
* @throws PathEngineException
* @throws DefinitionException
*/
public ElementDefinition evaluateDefinition(ExpressionNode expr, StructureDefinition profile, ElementDefinition element) throws DefinitionException {
StructureDefinition sd = profile;
ElementDefinition focus = null;
if (expr.getKind() == Kind.Name) {
if (element.hasSlicing()) {
ElementDefinition slice = pickMandatorySlice(sd, element);
if (slice == null)
throw new DefinitionException("Error in discriminator at " + element.getId() + ": found a sliced element while resolving the fixed value for one of the slices");
element = slice;
}
if (expr.getName().equals("$this")) {
focus = element;
} else {
List<ElementDefinition> childDefinitions;
childDefinitions = ProfileUtilities.getChildMap(sd, element);
// if that's empty, get the children of the type
if (childDefinitions.isEmpty()) {
sd = fetchStructureByType(element);
if (sd == null)
throw new DefinitionException("Problem with use of resolve() - profile '" + element.getType().get(0).getProfile() + "' on " + element.getId() + " could not be resolved");
childDefinitions = ProfileUtilities.getChildMap(sd, sd.getSnapshot().getElementFirstRep());
}
for (ElementDefinition t : childDefinitions) {
if (tailMatches(t, expr.getName())) {
focus = t;
break;
}
}
}
} else if (expr.getKind() == Kind.Function) {
if ("resolve".equals(expr.getName())) {
if (!element.hasType())
throw new DefinitionException("illegal use of resolve() in discriminator - no type on element " + element.getId());
if (element.getType().size() > 1)
throw new DefinitionException("illegal use of resolve() in discriminator - Multiple possible types on " + element.getId());
if (!element.getType().get(0).hasTarget())
throw new DefinitionException("illegal use of resolve() in discriminator - type on " + element.getId() + " is not Reference (" + element.getType().get(0).getCode() + ")");
if (element.getType().get(0).getTargetProfile().size() > 1)
throw new DefinitionException("illegal use of resolve() in discriminator - Multiple possible target type profiles on " + element.getId());
sd = worker.fetchResource(StructureDefinition.class, element.getType().get(0).getTargetProfile().get(0).getValue());
if (sd == null)
throw new DefinitionException("Problem with use of resolve() - profile '" + element.getType().get(0).getTargetProfile() + "' on " + element.getId() + " could not be resolved");
focus = sd.getSnapshot().getElementFirstRep();
} else if ("extension".equals(expr.getName())) {
String targetUrl = expr.getParameters().get(0).getConstant().primitiveValue();
List<ElementDefinition> childDefinitions = ProfileUtilities.getChildMap(sd, element);
for (ElementDefinition t : childDefinitions) {
if (t.getPath().endsWith(".extension") && t.hasSliceName()) {
StructureDefinition exsd = (t.getType() == null || t.getType().isEmpty()) ? null : worker.fetchResource(StructureDefinition.class, t.getType().get(0).getProfile().get(0).getValue());
while (exsd != null && !exsd.getBaseDefinition().equals("http://hl7.org/fhir/StructureDefinition/Extension")) exsd = worker.fetchResource(StructureDefinition.class, exsd.getBaseDefinition());
if (exsd.getUrl().equals(targetUrl)) {
if (ProfileUtilities.getChildMap(sd, t).isEmpty())
sd = exsd;
focus = t;
break;
}
}
}
} else
throw new DefinitionException("illegal function name " + expr.getName() + "() in discriminator");
} else if (expr.getKind() == Kind.Group) {
throw new DefinitionException("illegal expression syntax in discriminator (group)");
} else if (expr.getKind() == Kind.Constant) {
throw new DefinitionException("illegal expression syntax in discriminator (const)");
}
if (focus == null)
throw new DefinitionException("Unable to resolve discriminator in definitions: " + expr.toString());
else if (expr.getInner() == null)
return focus;
else {
return evaluateDefinition(expr.getInner(), sd, focus);
}
}
Aggregations