Search in sources :

Example 66 with Function

use of org.hl7.fhir.r5.model.ExpressionNode.Function in project org.hl7.fhir.core by hapifhir.

the class HTMLOutputGenerator method genHeader.

private String genHeader(long time) {
    int err = 0;
    int warn = 0;
    int info = 0;
    for (ValidationRecord f : records) {
        err = err + f.getErr();
        warn = warn + f.getWarn();
        info = info + f.getInfo();
    }
    return "<!DOCTYPE HTML>\r\n" + "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\r\n" + "<head>\r\n" + "  <title>Validation Results</title>\r\n" + "  <link href=\"http://hl7.org/fhir/fhir.css\" rel=\"stylesheet\"/>\r\n" + "  <style>\r\n" + "    span.flip  { background-color: #4CAF50; color: white; border: solid 1px #a6d8a8; padding: 2px }\r\n" + "  </style>\r\n" + "  <script>\r\n" + "    function flip(id) {\r\n" + "      var span = document.getElementById('s'+id);\r\n" + "      var div = document.getElementById(id);\r\n" + "      if (document.getElementById('s'+id).innerHTML == 'Show Reasoning') {\r\n" + "        div.style.display = 'block';\r\n" + "        span.innerHTML = 'Hide Reasoning';\r\n" + "      } else {\r\n" + "        div.style.display = 'none';\r\n" + "        span.innerHTML = 'Show Reasoning';\r\n" + "      }\r\n" + "    }\r\n" + "  </script>\r\n" + "</head>\r\n" + "<body style=\"margin: 20px; background-color: #ffffff\">\r\n" + " <h1>Validation Results</h1>\r\n" + " <p>" + err + " " + Utilities.pluralize("error", err) + ", " + warn + " " + Utilities.pluralize("warning", warn) + ", " + info + " " + Utilities.pluralize("hint", info) + ". Generated " + now() + " by Validator " + VersionUtil.getVersionString() + " (" + time + "ms)</p>\r\n" + " <table class=\"grid\">\r\n" + "   <tr>\r\n" + "     <td><b>Filename</b></td><td><b>Errors</b></td><td><b>Warnings</b></td><td><b>Hints</b></td>\r\n" + "   </tr>\r\n";
}
Also used : ValidationRecord(org.hl7.fhir.validation.ValidationRecord)

Example 67 with Function

use of org.hl7.fhir.r5.model.ExpressionNode.Function in project org.hl7.fhir.core by hapifhir.

the class RdfParser method composeImagingStudyImagingStudySeriesPerformerComponent.

protected void composeImagingStudyImagingStudySeriesPerformerComponent(Complex parent, String parentType, String name, ImagingStudy.ImagingStudySeriesPerformerComponent element, int index) {
    if (element == null)
        return;
    Complex t;
    if (Utilities.noString(parentType))
        t = parent;
    else {
        t = parent.predicate("fhir:" + parentType + '.' + name);
    }
    composeBackboneElement(t, "performer", name, element, index);
    if (element.hasFunction())
        composeCodeableConcept(t, "ImagingStudy", "function", element.getFunction(), -1);
    if (element.hasActor())
        composeReference(t, "ImagingStudy", "actor", element.getActor(), -1);
}
Also used : Complex(org.hl7.fhir.r4.utils.formats.Turtle.Complex)

Example 68 with Function

use of org.hl7.fhir.r5.model.ExpressionNode.Function in project org.hl7.fhir.core by hapifhir.

the class VersionConvertorPrimitiveTypeTests method testDirectConversion.

@ParameterizedTest(name = "Test index: {index} Source Class={1} First Value={3} Second Class={4} Second Value={6}")
@MethodSource("getDirectConversionParams")
public <K, L> void testDirectConversion(Class<K> srcTypeClazz, Class<L> tgtTypeClazz, Function<K, L> convertFunction, String[] srcStrings, String[] tgtStrings) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
    ConversionContext10_30.INSTANCE.init(mock(VersionConvertor_10_30.class), CONTEXT_PATH);
    ConversionContext10_40.INSTANCE.init(mock(VersionConvertor_10_40.class), CONTEXT_PATH);
    ConversionContext10_50.INSTANCE.init(mock(VersionConvertor_10_50.class), CONTEXT_PATH);
    ConversionContext14_50.INSTANCE.init(mock(VersionConvertor_14_50.class), CONTEXT_PATH);
    ConversionContext30_50.INSTANCE.init(mock(VersionConvertor_30_50.class), CONTEXT_PATH);
    ConversionContext40_50.INSTANCE.init(mock(VersionConvertor_40_50.class), CONTEXT_PATH);
    Method srcSetValueAsStringMethod = srcTypeClazz.getMethod("setValueAsString", String.class);
    Method srcGetValueAsString = srcTypeClazz.getMethod("getValueAsString");
    Method srcHasValue = srcTypeClazz.getMethod("hasValue");
    for (int i = 0; i < srcStrings.length; i++) {
        K srcInstance = srcTypeClazz.getDeclaredConstructor().newInstance();
        srcSetValueAsStringMethod.invoke(srcInstance, srcStrings[i]);
        String srcValueAsString = (String) srcGetValueAsString.invoke(srcInstance);
        Assertions.assertEquals(srcStrings[i], srcValueAsString);
        boolean srcHasValueReturn = (boolean) srcHasValue.invoke(srcInstance);
        L tgtInstance = convertFunction.apply(srcInstance);
        Method tgtGetValueAsString = tgtTypeClazz.getMethod("getValueAsString");
        Method tgtHasValue = tgtTypeClazz.getMethod("hasValue");
        if (srcHasValueReturn) {
            String tgtValueAsString = (String) tgtGetValueAsString.invoke(tgtInstance);
            Assertions.assertEquals(tgtStrings[i], tgtValueAsString);
        } else {
            boolean tgtHasValueReturn = (boolean) tgtHasValue.invoke(tgtInstance);
            Assertions.assertFalse(tgtHasValueReturn);
        }
    }
    ConversionContext10_30.INSTANCE.close(CONTEXT_PATH);
    ConversionContext10_40.INSTANCE.close(CONTEXT_PATH);
    ConversionContext10_50.INSTANCE.close(CONTEXT_PATH);
    ConversionContext14_50.INSTANCE.close(CONTEXT_PATH);
    ConversionContext30_50.INSTANCE.close(CONTEXT_PATH);
    ConversionContext40_50.INSTANCE.close(CONTEXT_PATH);
}
Also used : VersionConvertor_30_50(org.hl7.fhir.convertors.conv30_50.VersionConvertor_30_50) VersionConvertor_14_50(org.hl7.fhir.convertors.conv14_50.VersionConvertor_14_50) VersionConvertor_40_50(org.hl7.fhir.convertors.conv40_50.VersionConvertor_40_50) VersionConvertor_10_40(org.hl7.fhir.convertors.conv10_40.VersionConvertor_10_40) VersionConvertor_10_50(org.hl7.fhir.convertors.conv10_50.VersionConvertor_10_50) Method(java.lang.reflect.Method) VersionConvertor_10_30(org.hl7.fhir.convertors.conv10_30.VersionConvertor_10_30) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 69 with Function

use of org.hl7.fhir.r5.model.ExpressionNode.Function in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method parseExpression.

private ExpressionNode parseExpression(FHIRLexer lexer, boolean proximal) throws FHIRLexerException {
    ExpressionNode result = new ExpressionNode(lexer.nextId());
    ExpressionNode wrapper = null;
    SourceLocation c = lexer.getCurrentStartLocation();
    result.setStart(lexer.getCurrentLocation());
    // so we back correct for both +/- and as part of a numeric constant below.
    if (Utilities.existsInList(lexer.getCurrent(), "-", "+")) {
        wrapper = new ExpressionNode(lexer.nextId());
        wrapper.setKind(Kind.Unary);
        wrapper.setOperation(ExpressionNode.Operation.fromCode(lexer.take()));
        wrapper.setProximal(proximal);
    }
    if (lexer.isConstant()) {
        boolean isString = lexer.isStringConstant();
        if (!isString && (lexer.getCurrent().startsWith("-") || lexer.getCurrent().startsWith("+"))) {
            // the grammar says that this is a unary operation; it affects the correct processing order of the inner operations
            wrapper = new ExpressionNode(lexer.nextId());
            wrapper.setKind(Kind.Unary);
            wrapper.setOperation(ExpressionNode.Operation.fromCode(lexer.getCurrent().substring(0, 1)));
            wrapper.setProximal(proximal);
            lexer.setCurrent(lexer.getCurrent().substring(1));
        }
        result.setConstant(processConstant(lexer));
        result.setKind(Kind.Constant);
        if (!isString && !lexer.done() && (result.getConstant() instanceof IntegerType || result.getConstant() instanceof DecimalType) && (lexer.isStringConstant() || lexer.hasToken("year", "years", "month", "months", "week", "weeks", "day", "days", "hour", "hours", "minute", "minutes", "second", "seconds", "millisecond", "milliseconds"))) {
            // it's a quantity
            String ucum = null;
            if (lexer.hasToken("year", "years", "month", "months", "week", "weeks", "day", "days", "hour", "hours", "minute", "minutes", "second", "seconds", "millisecond", "milliseconds")) {
                String s = lexer.take();
                if (s.equals("year") || s.equals("years"))
                    ucum = "a";
                else if (s.equals("month") || s.equals("months"))
                    ucum = "mo";
                else if (s.equals("week") || s.equals("weeks"))
                    ucum = "wk";
                else if (s.equals("day") || s.equals("days"))
                    ucum = "d";
                else if (s.equals("hour") || s.equals("hours"))
                    ucum = "h";
                else if (s.equals("minute") || s.equals("minutes"))
                    ucum = "min";
                else if (s.equals("second") || s.equals("seconds"))
                    ucum = "s";
                else
                    // (s.equals("millisecond") || s.equals("milliseconds"))
                    ucum = "ms";
            } else
                ucum = lexer.readConstant("units");
            result.setConstant(new Quantity().setValue(new BigDecimal(result.getConstant().primitiveValue())).setSystem("http://unitsofmeasure.org").setCode(ucum));
        }
        result.setEnd(lexer.getCurrentLocation());
    } else if ("(".equals(lexer.getCurrent())) {
        lexer.next();
        result.setKind(Kind.Group);
        result.setGroup(parseExpression(lexer, true));
        if (!")".equals(lexer.getCurrent()))
            throw lexer.error("Found " + lexer.getCurrent() + " expecting a \")\"");
        result.setEnd(lexer.getCurrentLocation());
        lexer.next();
    } else {
        if (!lexer.isToken() && !lexer.getCurrent().startsWith("`"))
            throw lexer.error("Found " + lexer.getCurrent() + " expecting a token name");
        if (lexer.isFixedName())
            result.setName(lexer.readFixedName("Path Name"));
        else
            result.setName(lexer.take());
        result.setEnd(lexer.getCurrentLocation());
        if (!result.checkName())
            throw lexer.error("Found " + result.getName() + " expecting a valid token name");
        if ("(".equals(lexer.getCurrent())) {
            Function f = Function.fromCode(result.getName());
            FunctionDetails details = null;
            if (f == null) {
                if (hostServices != null)
                    details = hostServices.resolveFunction(result.getName());
                if (details == null)
                    throw lexer.error("The name " + result.getName() + " is not a valid function name");
                f = Function.Custom;
            }
            result.setKind(Kind.Function);
            result.setFunction(f);
            lexer.next();
            while (!")".equals(lexer.getCurrent())) {
                result.getParameters().add(parseExpression(lexer, true));
                if (",".equals(lexer.getCurrent()))
                    lexer.next();
                else if (!")".equals(lexer.getCurrent()))
                    throw lexer.error("The token " + lexer.getCurrent() + " is not expected here - either a \",\" or a \")\" expected");
            }
            result.setEnd(lexer.getCurrentLocation());
            lexer.next();
            checkParameters(lexer, c, result, details);
        } else
            result.setKind(Kind.Name);
    }
    ExpressionNode focus = result;
    if ("[".equals(lexer.getCurrent())) {
        lexer.next();
        ExpressionNode item = new ExpressionNode(lexer.nextId());
        item.setKind(Kind.Function);
        item.setFunction(ExpressionNode.Function.Item);
        item.getParameters().add(parseExpression(lexer, true));
        if (!lexer.getCurrent().equals("]"))
            throw lexer.error("The token " + lexer.getCurrent() + " is not expected here - a \"]\" expected");
        lexer.next();
        result.setInner(item);
        focus = item;
    }
    if (".".equals(lexer.getCurrent())) {
        lexer.next();
        focus.setInner(parseExpression(lexer, false));
    }
    result.setProximal(proximal);
    if (proximal) {
        while (lexer.isOp()) {
            focus.setOperation(ExpressionNode.Operation.fromCode(lexer.getCurrent()));
            focus.setOpStart(lexer.getCurrentStartLocation());
            focus.setOpEnd(lexer.getCurrentLocation());
            lexer.next();
            focus.setOpNext(parseExpression(lexer, false));
            focus = focus.getOpNext();
        }
        result = organisePrecedence(lexer, result);
    }
    if (wrapper != null) {
        wrapper.setOpNext(result);
        result.setProximal(false);
        result = wrapper;
    }
    return result;
}
Also used : SourceLocation(org.hl7.fhir.utilities.SourceLocation) FunctionDetails(org.hl7.fhir.r4.utils.FHIRPathEngine.IEvaluationContext.FunctionDetails) ExpressionNode(org.hl7.fhir.r4.model.ExpressionNode) BigDecimal(java.math.BigDecimal)

Example 70 with Function

use of org.hl7.fhir.r5.model.ExpressionNode.Function in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method execute.

private List<Base> execute(ExecutionContext context, List<Base> focus, ExpressionNode exp, boolean atEntry) throws FHIRException {
    // System.out.println("Evaluate {'"+exp.toString()+"'} on "+focus.toString());
    List<Base> work = new ArrayList<Base>();
    switch(exp.getKind()) {
        case Unary:
            work.add(new IntegerType(0));
            break;
        case Name:
            if (atEntry && exp.getName().equals("$this"))
                work.add(context.getThisItem());
            else if (atEntry && exp.getName().equals("$total"))
                work.addAll(context.getTotal());
            else
                for (Base item : focus) {
                    List<Base> outcome = execute(context, item, exp, atEntry);
                    for (Base base : outcome) if (base != null)
                        work.add(base);
                }
            break;
        case Function:
            List<Base> work2 = evaluateFunction(context, focus, exp);
            work.addAll(work2);
            break;
        case Constant:
            Base b = resolveConstant(context, exp.getConstant(), false, exp);
            if (b != null)
                work.add(b);
            break;
        case Group:
            work2 = execute(context, focus, exp.getGroup(), atEntry);
            work.addAll(work2);
    }
    if (exp.getInner() != null)
        work = execute(context, work, exp.getInner(), false);
    if (exp.isProximal() && exp.getOperation() != null) {
        ExpressionNode next = exp.getOpNext();
        ExpressionNode last = exp;
        while (next != null) {
            List<Base> work2 = preOperate(work, last.getOperation(), exp);
            if (work2 != null)
                work = work2;
            else if (last.getOperation() == Operation.Is || last.getOperation() == Operation.As) {
                work2 = executeTypeName(context, focus, next, false);
                work = operate(context, work, last.getOperation(), work2, last);
            } else {
                work2 = execute(context, focus, next, true);
                work = operate(context, work, last.getOperation(), work2, last);
            // System.out.println("Result of {'"+last.toString()+" "+last.getOperation().toCode()+" "+next.toString()+"'}: "+focus.toString());
            }
            last = next;
            next = next.getOpNext();
        }
    }
    // System.out.println("Result of {'"+exp.toString()+"'}: "+work.toString());
    return work;
}
Also used : ExpressionNode(org.hl7.fhir.r4.model.ExpressionNode)

Aggregations

Test (org.junit.jupiter.api.Test)17 List (java.util.List)8 FhirPath (au.csiro.pathling.fhirpath.FhirPath)7 Reference (org.hl7.fhir.r4.model.Reference)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 ParserContext (au.csiro.pathling.fhirpath.parser.ParserContext)6 MethodOutcome (ca.uhn.fhir.rest.api.MethodOutcome)6 URI (java.net.URI)6 Row (org.apache.spark.sql.Row)6 Session (org.eclipse.jetty.websocket.api.Session)6 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)6 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)6 IIdType (org.hl7.fhir.instance.model.api.IIdType)6 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)6 FhirReference (org.openmrs.module.fhir2.model.FhirReference)6 FhirTask (org.openmrs.module.fhir2.model.FhirTask)6 DatasetBuilder (au.csiro.pathling.test.builders.DatasetBuilder)5 ElementPathBuilder (au.csiro.pathling.test.builders.ElementPathBuilder)5 ParserContextBuilder (au.csiro.pathling.test.builders.ParserContextBuilder)5 ArrayList (java.util.ArrayList)5