use of org.hl7.fhir.dstu2.model.PrimitiveType in project org.hl7.fhir.core by hapifhir.
the class CanonicalResourceComparer method comparePrimitives.
@SuppressWarnings("rawtypes")
protected void comparePrimitives(String name, PrimitiveType l, PrimitiveType r, Map<String, StructuralMatch<String>> comp, IssueSeverity level, CanonicalResourceComparison<? extends CanonicalResource> res) {
StructuralMatch<String> match = null;
if (l.isEmpty() && r.isEmpty()) {
match = new StructuralMatch<>(null, null, null);
} else if (l.isEmpty()) {
match = new StructuralMatch<>(null, r.primitiveValue(), vmI(IssueSeverity.INFORMATION, "Added the item '" + r.primitiveValue() + "'", fhirType() + "." + name));
} else if (r.isEmpty()) {
match = new StructuralMatch<>(l.primitiveValue(), null, vmI(IssueSeverity.INFORMATION, "Removed the item '" + l.primitiveValue() + "'", fhirType() + "." + name));
} else if (!l.hasValue() && !r.hasValue()) {
match = new StructuralMatch<>(null, null, vmI(IssueSeverity.INFORMATION, "No Value", fhirType() + "." + name));
} else if (!l.hasValue()) {
match = new StructuralMatch<>(null, r.primitiveValue(), vmI(IssueSeverity.INFORMATION, "No Value on Left", fhirType() + "." + name));
} else if (!r.hasValue()) {
match = new StructuralMatch<>(l.primitiveValue(), null, vmI(IssueSeverity.INFORMATION, "No Value on Right", fhirType() + "." + name));
} else if (l.getValue().equals(r.getValue())) {
match = new StructuralMatch<>(l.primitiveValue(), r.primitiveValue(), null);
} else {
match = new StructuralMatch<>(l.primitiveValue(), r.primitiveValue(), vmI(level, "Values Differ", fhirType() + "." + name));
if (level != IssueSeverity.NULL) {
res.getMessages().add(new ValidationMessage(Source.ProfileComparer, IssueType.INFORMATIONAL, fhirType() + "." + name, "Values for " + name + " differ: '" + l.primitiveValue() + "' vs '" + r.primitiveValue() + "'", level));
}
}
comp.put(name, match);
}
use of org.hl7.fhir.dstu2.model.PrimitiveType in project org.hl7.fhir.core by hapifhir.
the class FHIRPathTests method test.
@SuppressWarnings("deprecation")
@ParameterizedTest(name = "{index}: file {0}")
@MethodSource("data")
public void test(String name, Element test) throws FileNotFoundException, IOException, FHIRException, org.hl7.fhir.exceptions.FHIRException, UcumException {
// Setting timezone for this test. Grahame is in UTC+11, Travis is in GMT, and I'm here in Toronto, Canada with
// all my time based tests failing locally...
TimeZone.setDefault(TimeZone.getTimeZone("UTC+1100"));
fp.setHostServices(new FHIRPathTestEvaluationServices());
String input = test.getAttribute("inputfile");
String expression = XMLUtil.getNamedChild(test, "expression").getTextContent();
TestResultType fail = TestResultType.OK;
if ("syntax".equals(XMLUtil.getNamedChild(test, "expression").getAttribute("invalid"))) {
fail = TestResultType.SYNTAX;
} else if ("semantic".equals(XMLUtil.getNamedChild(test, "expression").getAttribute("invalid"))) {
fail = TestResultType.SEMANTICS;
} else if ("execution".equals(XMLUtil.getNamedChild(test, "expression").getAttribute("invalid"))) {
fail = TestResultType.EXECUTION;
}
;
fp.setAllowPolymorphicNames("lenient/polymorphics".equals(test.getAttribute("mode")));
Resource res = null;
List<Base> outcome = new ArrayList<Base>();
System.out.println(name);
ExpressionNode node = null;
try {
node = fp.parse(expression);
Assertions.assertTrue(fail != TestResultType.SYNTAX, String.format("Expected exception didn't occur parsing %s", expression));
} catch (Exception e) {
System.out.println("Parsing Error: " + e.getMessage());
Assertions.assertTrue(fail == TestResultType.SYNTAX, String.format("Unexpected exception parsing %s: " + e.getMessage(), expression));
}
if (node != null) {
try {
if (Utilities.noString(input)) {
fp.check(null, null, node);
} else {
res = resources.get(input);
if (res == null) {
if (input.endsWith(".json")) {
res = new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", input));
} else {
res = new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", input));
}
resources.put(input, res);
}
fp.check(res, res.getResourceType().toString(), res.getResourceType().toString(), node);
}
Assertions.assertTrue(fail != TestResultType.SEMANTICS, String.format("Expected exception didn't occur checking %s", expression));
} catch (Exception e) {
System.out.println("Checking Error: " + e.getMessage());
Assertions.assertTrue(fail == TestResultType.SEMANTICS, String.format("Unexpected exception checking %s: " + e.getMessage(), expression));
node = null;
}
}
if (node != null) {
try {
outcome = fp.evaluate(res, node);
Assertions.assertTrue(fail == TestResultType.OK, String.format("Expected exception didn't occur executing %s", expression));
} catch (Exception e) {
System.out.println("Execution Error: " + e.getMessage());
Assertions.assertTrue(fail == TestResultType.EXECUTION, String.format("Unexpected exception executing %s: " + e.getMessage(), expression));
node = null;
}
}
if (fp.hasLog()) {
System.out.println(name);
System.out.println(fp.takeLog());
}
if (node != null) {
if ("true".equals(test.getAttribute("predicate"))) {
boolean ok = fp.convertToBoolean(outcome);
outcome.clear();
outcome.add(new BooleanType(ok));
}
List<Element> expected = new ArrayList<Element>();
XMLUtil.getNamedChildren(test, "output", expected);
Assertions.assertEquals(outcome.size(), expected.size(), String.format("Expected %d objects but found %d for expression %s", expected.size(), outcome.size(), expression));
if ("false".equals(test.getAttribute("ordered"))) {
for (int i = 0; i < Math.min(outcome.size(), expected.size()); i++) {
String tn = outcome.get(i).fhirType();
String s;
if (outcome.get(i) instanceof Quantity) {
s = fp.convertToString(outcome.get(i));
} else {
s = ((PrimitiveType) outcome.get(i)).asStringValue();
}
boolean found = false;
for (Element e : expected) {
if ((Utilities.noString(e.getAttribute("type")) || e.getAttribute("type").equals(tn)) && (Utilities.noString(e.getTextContent()) || e.getTextContent().equals(s))) {
found = true;
}
}
Assertions.assertTrue(found, String.format("Outcome %d: Value %s of type %s not expected for %s", i, s, tn, expression));
}
} else {
for (int i = 0; i < Math.min(outcome.size(), expected.size()); i++) {
String tn = expected.get(i).getAttribute("type");
if (!Utilities.noString(tn)) {
Assertions.assertEquals(tn, 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)) {
if (outcome.get(i) instanceof Quantity) {
Quantity q = fp.parseQuantityString(v);
Assertions.assertTrue(outcome.get(i).equalsDeep(q), String.format("Outcome %d: Value should be %s but was %s", i, v, outcome.get(i).toString()));
} else {
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()));
if (!(v.equals(((PrimitiveType) outcome.get(i)).fpValue()))) {
System.out.println(name);
System.out.println(String.format("Outcome %d: Value should be %s but was %s for expression %s", i, v, ((PrimitiveType) outcome.get(i)).fpValue(), expression));
}
Assertions.assertEquals(v, ((PrimitiveType) outcome.get(i)).fpValue(), String.format("Outcome %d: Value should be %s but was %s for expression %s", i, v, ((PrimitiveType) outcome.get(i)).fpValue(), expression));
}
}
}
}
}
}
use of org.hl7.fhir.dstu2.model.PrimitiveType in project org.hl7.fhir.core by hapifhir.
the class InstanceValidator method buildPattternExpression.
private void buildPattternExpression(ElementDefinition ed, StringBuilder expression, String discriminator, ElementDefinition criteriaElement) throws DefinitionException {
DataType pattern = criteriaElement.getPattern();
if (pattern instanceof CodeableConcept) {
CodeableConcept cc = (CodeableConcept) pattern;
expression.append(" and ");
buildCodeableConceptExpression(ed, expression, discriminator, cc);
} else if (pattern instanceof Coding) {
Coding c = (Coding) pattern;
expression.append(" and ");
buildCodingExpression(ed, expression, discriminator, c);
} else if (pattern instanceof BooleanType || pattern instanceof IntegerType || pattern instanceof DecimalType) {
expression.append(" and ");
buildPrimitiveExpression(ed, expression, discriminator, pattern, false);
} else if (pattern instanceof PrimitiveType) {
expression.append(" and ");
buildPrimitiveExpression(ed, expression, discriminator, pattern, true);
} else if (pattern instanceof Identifier) {
Identifier ii = (Identifier) pattern;
expression.append(" and ");
buildIdentifierExpression(ed, expression, discriminator, ii);
} else if (pattern instanceof HumanName) {
HumanName name = (HumanName) pattern;
expression.append(" and ");
buildHumanNameExpression(ed, expression, discriminator, name);
} else if (pattern instanceof Address) {
Address add = (Address) pattern;
expression.append(" and ");
buildAddressExpression(ed, expression, discriminator, add);
} else {
throw new DefinitionException(context.formatMessage(I18nConstants.UNSUPPORTED_FIXED_PATTERN_TYPE_FOR_DISCRIMINATOR_FOR_SLICE__, discriminator, ed.getId(), pattern.fhirType()));
}
}
use of org.hl7.fhir.dstu2.model.PrimitiveType in project org.hl7.fhir.core by hapifhir.
the class NarrativeGenerator method displayLeaf.
private boolean displayLeaf(ResourceWrapper res, BaseWrapper ew, ElementDefinition defn, XhtmlNode x, String name, boolean showCodeDetails) throws FHIRException, UnsupportedEncodingException, IOException {
if (ew == null)
return false;
Base e = ew.getBase();
Map<String, String> displayHints = readDisplayHints(defn);
if (name.endsWith("[x]"))
name = name.substring(0, name.length() - 3);
if (!showCodeDetails && e instanceof PrimitiveType && isDefault(displayHints, ((PrimitiveType) e)))
return false;
if (e instanceof StringType) {
x.addText(name + ": " + ((StringType) e).getValue());
return true;
} else if (e instanceof CodeType) {
x.addText(name + ": " + ((CodeType) e).getValue());
return true;
} else if (e instanceof IdType) {
x.addText(name + ": " + ((IdType) e).getValue());
return true;
} else if (e instanceof DateTimeType) {
x.addText(name + ": " + ((DateTimeType) e).toHumanDisplay());
return true;
} else if (e instanceof InstantType) {
x.addText(name + ": " + ((InstantType) e).toHumanDisplay());
return true;
} else if (e instanceof Extension) {
x.addText("Extensions: todo");
return true;
} else if (e instanceof org.hl7.fhir.dstu2.model.DateType) {
x.addText(name + ": " + ((org.hl7.fhir.dstu2.model.DateType) e).toHumanDisplay());
return true;
} else if (e instanceof Enumeration) {
// todo: look up a display name if there is one
x.addText(((Enumeration<?>) e).getValue().toString());
return true;
} else if (e instanceof BooleanType) {
if (((BooleanType) e).getValue()) {
x.addText(name);
return true;
}
} else if (e instanceof CodeableConcept) {
renderCodeableConcept((CodeableConcept) e, x, showCodeDetails);
return true;
} else if (e instanceof Coding) {
renderCoding((Coding) e, x, showCodeDetails);
return true;
} else if (e instanceof Annotation) {
renderAnnotation((Annotation) e, x, showCodeDetails);
return true;
} else if (e instanceof org.hl7.fhir.dstu2.model.IntegerType) {
x.addText(Integer.toString(((org.hl7.fhir.dstu2.model.IntegerType) e).getValue()));
return true;
} else if (e instanceof org.hl7.fhir.dstu2.model.DecimalType) {
x.addText(((org.hl7.fhir.dstu2.model.DecimalType) e).getValue().toString());
return true;
} else if (e instanceof Identifier) {
renderIdentifier((Identifier) e, x);
return true;
} else if (e instanceof HumanName) {
renderHumanName((HumanName) e, x);
return true;
} else if (e instanceof SampledData) {
renderSampledData((SampledData) e, x);
return true;
} else if (e instanceof Address) {
renderAddress((Address) e, x);
return true;
} else if (e instanceof ContactPoint) {
renderContactPoint((ContactPoint) e, x);
return true;
} else if (e instanceof Timing) {
renderTiming((Timing) e, x);
return true;
} else if (e instanceof Quantity) {
renderQuantity((Quantity) e, x, showCodeDetails);
return true;
} else if (e instanceof Ratio) {
renderQuantity(((Ratio) e).getNumerator(), x, showCodeDetails);
x.addText("/");
renderQuantity(((Ratio) e).getDenominator(), x, showCodeDetails);
return true;
} else if (e instanceof Period) {
Period p = (Period) e;
x.addText(name + ": ");
x.addText(!p.hasStart() ? "??" : p.getStartElement().toHumanDisplay());
x.addText(" --> ");
x.addText(!p.hasEnd() ? "(ongoing)" : p.getEndElement().toHumanDisplay());
return true;
} else if (e instanceof Reference) {
Reference r = (Reference) e;
if (r.hasDisplayElement())
x.addText(r.getDisplay());
else if (r.hasReferenceElement()) {
ResourceWithReference tr = resolveReference(res, r.getReference());
// getDisplayForReference(tr.getReference()));
x.addText(tr == null ? r.getReference() : "????");
} else
x.addText("??");
return true;
} else if (e instanceof Narrative) {
return false;
} else if (e instanceof Resource) {
return false;
} else if (!(e instanceof Attachment))
throw new NotImplementedException("type " + e.getClass().getName() + " not handled yet");
return false;
}
use of org.hl7.fhir.dstu2.model.PrimitiveType in project org.hl7.fhir.core by hapifhir.
the class VersionConvertorPrimitiveType40_50Test method testNullValueDstu2Primitive.
@ParameterizedTest(name = "Testing r4 -> r5 conversion of null value {0}.")
@MethodSource("r4PrimitiveTypes")
public <T extends PrimitiveType> void testNullValueDstu2Primitive(String classname, T obj) {
obj.addExtension().setUrl("http://example.com/AnyValue").setValue(new StringType("A value"));
Assertions.assertNull(((org.hl7.fhir.r5.model.PrimitiveType) VersionConvertorFactory_40_50.convertType(obj)).getValue());
}
Aggregations