use of org.hl7.fhir.exceptions.PathEngineException in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method opPlus.
private List<Base> opPlus(List<Base> left, List<Base> right) throws PathEngineException {
if (left.size() == 0)
throw new PathEngineException("Error performing +: left operand has no value");
if (left.size() > 1)
throw new PathEngineException("Error performing +: left operand has more than one value");
if (!left.get(0).isPrimitive())
throw new PathEngineException(String.format("Error performing +: left operand has the wrong type (%s)", left.get(0).fhirType()));
if (right.size() == 0)
throw new PathEngineException("Error performing +: right operand has no value");
if (right.size() > 1)
throw new PathEngineException("Error performing +: right operand has more than one value");
if (!right.get(0).isPrimitive())
throw new PathEngineException(String.format("Error performing +: right operand has the wrong type (%s)", right.get(0).fhirType()));
List<Base> result = new ArrayList<Base>();
Base l = left.get(0);
Base r = right.get(0);
if (l.hasType("string", "id", "code", "uri") && r.hasType("string", "id", "code", "uri"))
result.add(new StringType(l.primitiveValue() + r.primitiveValue()));
else if (l.hasType("integer") && r.hasType("integer"))
result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) + Integer.parseInt(r.primitiveValue())));
else if (l.hasType("decimal", "integer") && r.hasType("decimal", "integer"))
result.add(new DecimalType(new BigDecimal(l.primitiveValue()).add(new BigDecimal(r.primitiveValue()))));
else
throw new PathEngineException(String.format("Error performing +: left and right operand have incompatible or illegal types (%s, %s)", left.get(0).fhirType(), right.get(0).fhirType()));
return result;
}
use of org.hl7.fhir.exceptions.PathEngineException in project org.hl7.fhir.core by hapifhir.
the class FluentPathTests method testWrong.
private void testWrong(Resource resource, String expression) throws FileNotFoundException, IOException, FHIRException {
if (TestingUtilities.context == null)
TestingUtilities.context = SimpleWorkerContext.fromPack("C:\\work\\org.hl7.fhir\\build\\publish\\validation-min.xml.zip");
FHIRPathEngine fp = new FHIRPathEngine(TestingUtilities.context);
try {
ExpressionNode node = fp.parse(expression);
fp.check(null, null, resource.getResourceType().toString(), node);
fp.evaluate(null, null, resource, node);
if (fp.hasLog())
System.out.println(fp.takeLog());
Assertions.fail("Fail expected");
} catch (PathEngineException e) {
// ok
}
}
use of org.hl7.fhir.exceptions.PathEngineException in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method opMod.
private List<Base> opMod(List<Base> left, List<Base> right, ExpressionNode expr) throws PathEngineException {
if (left.size() == 0 || right.size() == 0)
return new ArrayList<Base>();
if (left.size() > 1)
throw new PathEngineException("Error performing mod: left operand has more than one value", expr.getStart(), expr.toString());
if (!left.get(0).isPrimitive())
throw new PathEngineException(String.format("Error performing mod: left operand has the wrong type (%s)", left.get(0).fhirType()), expr.getStart(), expr.toString());
if (right.size() > 1)
throw new PathEngineException("Error performing mod: right operand has more than one value", expr.getStart(), expr.toString());
if (!right.get(0).isPrimitive())
throw new PathEngineException(String.format("Error performing mod: right operand has the wrong type (%s)", right.get(0).fhirType()), expr.getStart(), expr.toString());
List<Base> result = new ArrayList<Base>();
Base l = left.get(0);
Base r = right.get(0);
if (l.hasType("integer") && r.hasType("integer"))
result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) % Integer.parseInt(r.primitiveValue())));
else if (l.hasType("decimal", "integer") && r.hasType("decimal", "integer")) {
Decimal d1;
try {
d1 = new Decimal(l.primitiveValue());
Decimal d2 = new Decimal(r.primitiveValue());
result.add(new DecimalType(d1.modulo(d2).asDecimal()));
} catch (UcumException e) {
throw new PathEngineException(e);
}
} else
throw new PathEngineException(String.format("Error performing mod: left and right operand have incompatible or illegal types (%s, %s)", left.get(0).fhirType(), right.get(0).fhirType()), expr.getStart(), expr.toString());
return result;
}
use of org.hl7.fhir.exceptions.PathEngineException in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method getChildTypesByName.
private void getChildTypesByName(String type, String name, TypeDetails result, ExpressionNode expr) throws PathEngineException, DefinitionException {
if (Utilities.noString(type))
throw new PathEngineException("No type provided in BuildToolPathEvaluator.getChildTypesByName", expr.getStart(), expr.toString());
if (type.equals("http://hl7.org/fhir/StructureDefinition/xhtml"))
return;
if (type.startsWith(Constants.NS_SYSTEM_TYPE))
return;
if (type.equals(TypeDetails.FP_SimpleTypeInfo)) {
getSimpleTypeChildTypesByName(name, result);
} else if (type.equals(TypeDetails.FP_ClassInfo)) {
getClassInfoChildTypesByName(name, result);
} else {
String url = null;
if (type.contains("#")) {
url = type.substring(0, type.indexOf("#"));
} else {
url = type;
}
String tail = "";
StructureDefinition sd = worker.fetchResource(StructureDefinition.class, url);
if (sd == null)
// this really is an error, because we can only get to here if the internal infrastrucgture is wrong
throw new DefinitionException("Unknown type " + type);
List<StructureDefinition> sdl = new ArrayList<StructureDefinition>();
ElementDefinitionMatch m = null;
if (type.contains("#"))
m = getElementDefinition(sd, type.substring(type.indexOf("#") + 1), false, expr);
if (m != null && hasDataType(m.definition)) {
if (m.fixedType != null) {
StructureDefinition dt = worker.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(m.fixedType, worker.getOverrideVersionNs()));
if (dt == null)
throw new DefinitionException("unknown data type " + m.fixedType);
sdl.add(dt);
} else
for (TypeRefComponent t : m.definition.getType()) {
StructureDefinition dt = worker.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(t.getCode(), worker.getOverrideVersionNs()));
if (dt == null)
throw new DefinitionException("unknown data type " + t.getCode());
sdl.add(dt);
}
} else {
sdl.add(sd);
if (type.contains("#")) {
tail = type.substring(type.indexOf("#") + 1);
tail = tail.substring(tail.indexOf("."));
}
}
for (StructureDefinition sdi : sdl) {
String path = sdi.getSnapshot().getElement().get(0).getPath() + tail + ".";
if (name.equals("**")) {
assert (result.getCollectionStatus() == CollectionStatus.UNORDERED);
for (ElementDefinition ed : sdi.getSnapshot().getElement()) {
if (ed.getPath().startsWith(path))
for (TypeRefComponent t : ed.getType()) {
if (t.hasCode() && t.getCodeElement().hasValue()) {
String tn = null;
if (t.getCode().equals("Element") || t.getCode().equals("BackboneElement"))
tn = sdi.getType() + "#" + ed.getPath();
else
tn = t.getCode();
if (t.getCode().equals("Resource")) {
for (String rn : worker.getResourceNames()) {
if (!result.hasType(worker, rn)) {
getChildTypesByName(result.addType(rn), "**", result, expr);
}
}
} else if (!result.hasType(worker, tn)) {
getChildTypesByName(result.addType(tn), "**", result, expr);
}
}
}
}
} else if (name.equals("*")) {
assert (result.getCollectionStatus() == CollectionStatus.UNORDERED);
for (ElementDefinition ed : sdi.getSnapshot().getElement()) {
if (ed.getPath().startsWith(path) && !ed.getPath().substring(path.length()).contains("."))
for (TypeRefComponent t : ed.getType()) {
if (// Element.id or Extension.url
Utilities.noString(t.getCode()))
result.addType("System.string");
else if (t.getCode().equals("Element") || t.getCode().equals("BackboneElement"))
result.addType(sdi.getType() + "#" + ed.getPath());
else if (t.getCode().equals("Resource"))
result.addTypes(worker.getResourceNames());
else
result.addType(t.getCode());
}
}
} else {
path = sdi.getSnapshot().getElement().get(0).getPath() + tail + "." + name;
ElementDefinitionMatch ed = getElementDefinition(sdi, path, false, expr);
if (ed != null) {
if (!Utilities.noString(ed.getFixedType()))
result.addType(ed.getFixedType());
else
for (TypeRefComponent t : ed.getDefinition().getType()) {
if (Utilities.noString(t.getCode())) {
if (Utilities.existsInList(ed.getDefinition().getId(), "Element.id", "Extension.url") || Utilities.existsInList(ed.getDefinition().getBase().getPath(), "Resource.id", "Element.id", "Extension.url"))
result.addType(TypeDetails.FP_NS, "string");
// throw new PathEngineException("Illegal reference to primitive value attribute @ "+path);
break;
}
ProfiledType pt = null;
if (t.getCode().equals("Element") || t.getCode().equals("BackboneElement"))
pt = new ProfiledType(sdi.getUrl() + "#" + path);
else if (t.getCode().equals("Resource"))
result.addTypes(worker.getResourceNames());
else
pt = new ProfiledType(t.getCode());
if (pt != null) {
if (t.hasProfile())
pt.addProfiles(t.getProfile());
if (ed.getDefinition().hasBinding())
pt.addBinding(ed.getDefinition().getBinding());
result.addType(pt);
}
}
}
}
}
}
}
use of org.hl7.fhir.exceptions.PathEngineException in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method opMinus.
private List<Base> opMinus(List<Base> left, List<Base> right, ExpressionNode expr) throws PathEngineException {
if (left.size() == 0 || right.size() == 0)
return new ArrayList<Base>();
if (left.size() > 1)
throw new PathEngineException("Error performing -: left operand has more than one value", expr.getStart(), expr.toString());
if (!left.get(0).isPrimitive())
throw new PathEngineException(String.format("Error performing -: left operand has the wrong type (%s)", left.get(0).fhirType()), expr.getStart(), expr.toString());
if (right.size() > 1)
throw new PathEngineException("Error performing -: right operand has more than one value", expr.getStart(), expr.toString());
if (!right.get(0).isPrimitive() && !((left.get(0).isDateTime() || "0".equals(left.get(0).primitiveValue()) || left.get(0).hasType("Quantity")) && right.get(0).hasType("Quantity")))
throw new PathEngineException(String.format("Error performing -: right operand has the wrong type (%s)", right.get(0).fhirType()), expr.getStart(), expr.toString());
List<Base> result = new ArrayList<Base>();
Base l = left.get(0);
Base r = right.get(0);
if (l.hasType("integer") && r.hasType("integer"))
result.add(new IntegerType(Integer.parseInt(l.primitiveValue()) - Integer.parseInt(r.primitiveValue())));
else if (l.hasType("decimal", "integer") && r.hasType("decimal", "integer"))
result.add(new DecimalType(new BigDecimal(l.primitiveValue()).subtract(new BigDecimal(r.primitiveValue()))));
else if (l.isDateTime() && r.hasType("Quantity"))
result.add(dateAdd((BaseDateTimeType) l, (Quantity) r, true, expr));
else
throw new PathEngineException(String.format("Error performing -: left and right operand have incompatible or illegal types (%s, %s)", left.get(0).fhirType(), right.get(0).fhirType()), expr.getStart(), expr.toString());
return result;
}
Aggregations