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) throws PathEngineException, DefinitionException {
if (Utilities.noString(type))
throw new PathEngineException("No type provided in BuildToolPathEvaluator.getChildTypesByName");
if (type.equals("xhtml"))
return;
String url = null;
if (type.contains(".")) {
url = "http://hl7.org/fhir/StructureDefinition/" + type.substring(0, type.indexOf("."));
} else {
url = "http://hl7.org/fhir/StructureDefinition/" + 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, false);
if (m != null && hasDataType(m.definition)) {
if (m.fixedType != null) {
StructureDefinition dt = worker.fetchTypeDefinition(m.fixedType);
if (dt == null)
throw new DefinitionException("unknown data type " + m.fixedType);
sdl.add(dt);
} else
for (TypeRefComponent t : m.definition.getType()) {
StructureDefinition dt = worker.fetchTypeDefinition(t.getCode());
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("."));
}
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 = ed.getPath();
else
tn = t.getCode();
if (t.getCode().equals("Resource")) {
for (String rn : worker.getResourceNames()) {
if (!result.hasType(worker, rn)) {
result.addType(rn);
getChildTypesByName(rn, "**", result);
}
}
} else if (!result.hasType(worker, tn)) {
result.addType(tn);
getChildTypesByName(tn, "**", result);
}
}
}
}
} 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 (t.getCode().equals("Element") || t.getCode().equals("BackboneElement"))
result.addType(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);
if (ed != null) {
if (!Utilities.noString(ed.getFixedType()))
result.addType(ed.getFixedType());
else
for (TypeRefComponent t : ed.getDefinition().getType()) {
if (Utilities.noString(t.getCode()))
// throw new PathEngineException("Illegal reference to primitive value attribute @ "+path);
break;
if (t.getCode().equals("Element") || t.getCode().equals("BackboneElement"))
result.addType(path);
else if (t.getCode().equals("Resource"))
result.addTypes(worker.getResourceNames());
else
result.addType(t.getCode());
}
}
}
}
}
use of org.hl7.fhir.exceptions.PathEngineException in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method funcSubsetOf.
private List<Base> funcSubsetOf(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws PathEngineException {
List<Base> target = execute(context, focus, exp.getParameters().get(0), true);
boolean valid = true;
for (Base item : focus) {
boolean found = false;
for (Base t : target) {
if (Base.compareDeep(item, t, false)) {
found = true;
break;
}
}
if (!found) {
valid = false;
break;
}
}
List<Base> result = new ArrayList<Base>();
result.add(new BooleanType(valid));
return result;
}
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 FHIRPathEngine method funcIs.
private List<Base> funcIs(ExecutionContext context, List<Base> focus, ExpressionNode exp) throws PathEngineException {
List<Base> result = new ArrayList<Base>();
if (focus.size() == 0 || focus.size() > 1)
result.add(new BooleanType(false));
else {
String tn = exp.getParameters().get(0).getName();
result.add(new BooleanType(focus.get(0).hasType(tn)));
}
return result;
}
use of org.hl7.fhir.exceptions.PathEngineException in project org.hl7.fhir.core by hapifhir.
the class FHIRPathEngine method opDiv.
private List<Base> opDiv(List<Base> left, List<Base> right) throws PathEngineException {
if (left.size() == 0)
throw new PathEngineException("Error performing div: left operand has no value");
if (left.size() > 1)
throw new PathEngineException("Error performing div: left operand has more than one value");
if (!left.get(0).isPrimitive())
throw new PathEngineException(String.format("Error performing div: left operand has the wrong type (%s)", left.get(0).fhirType()));
if (right.size() == 0)
throw new PathEngineException("Error performing div: right operand has no value");
if (right.size() > 1)
throw new PathEngineException("Error performing div: right operand has more than one value");
if (!right.get(0).isPrimitive())
throw new PathEngineException(String.format("Error performing div: 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("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 IntegerType(d1.divInt(d2).asDecimal()));
} catch (UcumException e) {
throw new PathEngineException(e);
}
} else
throw new PathEngineException(String.format("Error performing div: left and right operand have incompatible or illegal types (%s, %s)", left.get(0).fhirType(), right.get(0).fhirType()));
return result;
}
Aggregations