Search in sources :

Example 26 with PathEngineException

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;
}
Also used : IntegerType(org.hl7.fhir.dstu2016may.model.IntegerType) StringType(org.hl7.fhir.dstu2016may.model.StringType) ArrayList(java.util.ArrayList) DecimalType(org.hl7.fhir.dstu2016may.model.DecimalType) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) ParserBase(org.hl7.fhir.dstu2016may.metamodel.ParserBase) Base(org.hl7.fhir.dstu2016may.model.Base) BigDecimal(java.math.BigDecimal)

Example 27 with PathEngineException

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
    }
}
Also used : FHIRPathEngine(org.hl7.fhir.dstu2016may.utils.FHIRPathEngine) ExpressionNode(org.hl7.fhir.dstu2016may.model.ExpressionNode) PathEngineException(org.hl7.fhir.exceptions.PathEngineException)

Example 28 with PathEngineException

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;
}
Also used : BigDecimal(java.math.BigDecimal) Decimal(org.fhir.ucum.Decimal) UcumException(org.fhir.ucum.UcumException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException)

Example 29 with PathEngineException

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);
                            }
                        }
                }
            }
        }
    }
}
Also used : ProfiledType(org.hl7.fhir.r4.model.TypeDetails.ProfiledType) TypeRefComponent(org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException)

Example 30 with PathEngineException

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;
}
Also used : PathEngineException(org.hl7.fhir.exceptions.PathEngineException) BigDecimal(java.math.BigDecimal)

Aggregations

ArrayList (java.util.ArrayList)56 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)53 BigDecimal (java.math.BigDecimal)36 Base (org.hl7.fhir.dstu2.model.Base)22 UcumException (org.fhir.ucum.UcumException)21 Decimal (org.fhir.ucum.Decimal)18 Base (org.hl7.fhir.r4b.model.Base)11 Base (org.hl7.fhir.r5.model.Base)11 List (java.util.List)9 Pair (org.fhir.ucum.Pair)8 ParserBase (org.hl7.fhir.dstu2016may.metamodel.ParserBase)7 Base (org.hl7.fhir.dstu2016may.model.Base)7 ExpressionNode (org.hl7.fhir.dstu2.model.ExpressionNode)6 DecimalType (org.hl7.fhir.r4b.model.DecimalType)6 DecimalType (org.hl7.fhir.r5.model.DecimalType)6 DecimalType (org.hl7.fhir.dstu2.model.DecimalType)5 IntegerType (org.hl7.fhir.dstu2.model.IntegerType)5 DecimalType (org.hl7.fhir.dstu2016may.model.DecimalType)5 ExpressionNode (org.hl7.fhir.dstu2016may.model.ExpressionNode)5 TypeDetails (org.hl7.fhir.dstu2016may.model.ExpressionNode.TypeDetails)5