org.fhir.ucum.UcumException Java Examples

The following examples show how to use org.fhir.ucum.UcumException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CqlTestSuite.java    From cql_engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testErrorSuite() throws IOException, JAXBException, UcumException {
    Library library = translate("portable/CqlErrorTestSuite.cql");
    Context context = new Context(library, new DateTime(TemporalHelper.getDefaultOffset(), 2018, 1, 1, 7, 0, 0, 0));
    if (library.getStatements() != null) {
        for (ExpressionDef expression : library.getStatements().getDef()) {
            try {
                expression.evaluate(context);
                logger.error("Test " + expression.getName() + " should result in an error");
                Assert.fail();
            }
            catch (Exception e) {
                // pass
                logger.info(expression.getName() + " TEST PASSED");
            }
        }
    }
}
 
Example #2
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<Base> opMod(List<Base> left, List<Base> right) throws PathEngineException {
  if (left.size() == 0)
    throw new PathEngineException("Error performing mod: left operand has no value");
  if (left.size() > 1)
    throw new PathEngineException("Error performing mod: left operand has more than one value");
  if (!left.get(0).isPrimitive())
    throw new PathEngineException(String.format("Error performing mod: left operand has the wrong type (%s)", left.get(0).fhirType()));
  if (right.size() == 0)
    throw new PathEngineException("Error performing mod: right operand has no value");
  if (right.size() > 1)
    throw new PathEngineException("Error performing mod: right operand has more than one value");
  if (!right.get(0).isPrimitive())
    throw new PathEngineException(String.format("Error performing mod: 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 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()));
  return result;
}
 
Example #3
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<Base> opMod(List<Base> left, List<Base> right) 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");
  if (!left.get(0).isPrimitive())
    throw new PathEngineException(String.format("Error performing mod: left operand has the wrong type (%s)", left.get(0).fhirType()));
  if (right.size() > 1)
    throw new PathEngineException("Error performing mod: right operand has more than one value");
  if (!right.get(0).isPrimitive())
    throw new PathEngineException(String.format("Error performing mod: 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 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()));
  return result;
}
 
Example #4
Source File: ResourceRoundTripTests.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
@Test
@Disabled
public void test() throws FileNotFoundException, IOException, FHIRException, EOperationOutcome, UcumException {
  Resource res = new XmlParser().parse(new FileInputStream(TestingUtilities.resourceNameToFile("unicode.xml")));
  new NarrativeGenerator("", "", TestingUtilities.context()).generate((DomainResource) res, null);
  new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(TestingUtilities.resourceNameToFile("gen", "unicode.out.xml")), res);
}
 
Example #5
Source File: ShexGeneratorTests.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private void doTest(String name) throws FileNotFoundException, IOException, FHIRException, UcumException {
  String workingDirectory = "C:\\work\\org.hl7.fhir\\build\\publish"; // FileSystems.getDefault().getPath(System.getProperty("user.dir"), "data").toString();
  // String workingDirectory = FileSystems.getDefault().getPath(System.getProperty("user.dir"), "..", "..", "..", "publish").toString();
  StructureDefinition sd = TestingUtilities.context().fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(name, null));
  if (sd == null) {
    throw new FHIRException("StructuredDefinition for " + name + "was null");
  }
  Path outPath = FileSystems.getDefault().getPath(workingDirectory, name.toLowerCase() + ".shex");
  TextFile.stringToFile(new ShExGenerator(TestingUtilities.context()).generate(HTMLLinkPolicy.NONE, sd), outPath.toString());
}
 
Example #6
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimple() throws FHIRException, FileNotFoundException, IOException, UcumException {

  StructureDefinition focus = new StructureDefinition();
  StructureDefinition base = TestingUtilities.context().fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/Patient").copy();
  focus.setUrl(Utilities.makeUuidUrn());
  focus.setBaseDefinition(base.getUrl());
  focus.setType("Patient");
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
  new ProfileUtilities(TestingUtilities.context(), messages, null).generateSnapshot(base, focus, focus.getUrl(), "http://hl7.org/fhir/R4", "Simple Test");

  boolean ok = base.getSnapshot().getElement().size() == focus.getSnapshot().getElement().size();
  for (int i = 0; i < base.getSnapshot().getElement().size(); i++) {
    ElementDefinition b = base.getSnapshot().getElement().get(i);
    ElementDefinition f = focus.getSnapshot().getElement().get(i);
    if (ok) {
      if (!f.hasBase())
        ok = false;
      else if (!b.getPath().equals(f.getPath()))
        ok = false;
      else {
        b.setBase(null);
        f.setBase(null);
        ok = Base.compareDeep(b, f, true);
      }
    }
  }

  if (!ok) {
    compareXml(base, focus);
    throw new FHIRException("Snap shot generation simple test failed");
  } else
    System.out.println("Snap shot generation simple test passed");
}
 
Example #7
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<Base> opDivideBy(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("integer", "decimal") && r.hasType("integer", "decimal")) {
    Decimal d1;
    try {
      d1 = new Decimal(l.primitiveValue());
      Decimal d2 = new Decimal(r.primitiveValue());
      result.add(new DecimalType(d1.divide(d2).asDecimal()));
    } catch (UcumException e) {
      throw new PathEngineException(e);
    }
  }
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;
}
 
Example #8
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
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;
 }
 
Example #9
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<Base> opMod(List<Base> left, List<Base> right) throws PathEngineException {
   if (left.size() == 0)
     throw new PathEngineException("Error performing mod: left operand has no value");
   if (left.size() > 1)
     throw new PathEngineException("Error performing mod: left operand has more than one value");
   if (!left.get(0).isPrimitive())
     throw new PathEngineException(String.format("Error performing mod: left operand has the wrong type (%s)", left.get(0).fhirType()));
   if (right.size() == 0)
     throw new PathEngineException("Error performing mod: right operand has no value");
   if (right.size() > 1)
     throw new PathEngineException("Error performing mod: right operand has more than one value");
   if (!right.get(0).isPrimitive())
     throw new PathEngineException(String.format("Error performing mod: 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 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()));
   return result;
}
 
Example #10
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<Base> opDivideBy(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("integer", "decimal", "unsignedInt", "positiveInt") && r.hasType("integer", "decimal", "unsignedInt", "positiveInt")) {
    Decimal d1;
    try {
      d1 = new Decimal(l.primitiveValue());
      Decimal d2 = new Decimal(r.primitiveValue());
      result.add(new DecimalType(d1.divide(d2).asDecimal()));
    } catch (UcumException e) {
      throw new PathEngineException(e);
    }
  }
  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;
}
 
Example #11
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #12
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private Pair qtyToPair(Quantity q) {
  if (!"http://unitsofmeasure.org".equals(q.getSystem()))
    return null;
  try {
    return new Pair(new Decimal(q.getValue().toPlainString()), q.getCode());
  } catch (UcumException e) {
    return null;
  }
}
 
Example #13
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<Base> opDivideBy(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("integer", "decimal") && r.hasType("integer", "decimal")) {
    Decimal d1;
    try {
      d1 = new Decimal(l.primitiveValue());
      Decimal d2 = new Decimal(r.primitiveValue());
      result.add(new DecimalType(d1.divide(d2).asDecimal()));
    } catch (UcumException e) {
      throw new PathEngineException(e);
    }
  }
  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;
}
 
Example #14
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #15
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<Base> opMod(List<Base> left, List<Base> right) throws PathEngineException {
  if (left.size() == 0)
    throw new PathEngineException("Error performing mod: left operand has no value");
  if (left.size() > 1)
    throw new PathEngineException("Error performing mod: left operand has more than one value");
  if (!left.get(0).isPrimitive())
    throw new PathEngineException(String.format("Error performing mod: left operand has the wrong type (%s)", left.get(0).fhirType()));
  if (right.size() == 0)
    throw new PathEngineException("Error performing mod: right operand has no value");
  if (right.size() > 1)
    throw new PathEngineException("Error performing mod: right operand has more than one value");
  if (!right.get(0).isPrimitive())
    throw new PathEngineException(String.format("Error performing mod: 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 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()));
  return result;
}
 
Example #16
Source File: TestFhirPath.java    From cql_engine with Apache License 2.0 5 votes vote down vote up
@Test
public void testFhirHelpersStu3() throws UcumException {
    String cql = getStringFromResourceStream("stu3/TestFHIRHelpers.cql");
    Library library = translate(cql);
    Context context = new Context(library);
    context.registerLibraryLoader(getLibraryLoader());

    Dstu3FhirModelResolver modelResolver = new Dstu3FhirModelResolver();
    FhirContext fhirContext = FhirContext.forDstu3();
    RestFhirRetrieveProvider retrieveProvider = new RestFhirRetrieveProvider(new SearchParameterResolver(fhirContext),
            fhirContext.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu3"));
    CompositeDataProvider provider = new CompositeDataProvider(modelResolver, retrieveProvider);
    // BaseFhirDataProvider provider = new
    // FhirDataProviderStu3().setEndpoint("http://fhirtest.uhn.ca/baseDstu3");
    context.registerDataProvider("http://hl7.org/fhir", provider);

    Object result = context.resolveExpressionRef("TestPeriodToInterval").getExpression().evaluate(context);
    // TODO - fix
    // Assert.assertEquals(((DateTime)((Interval) result).getStart()).getPartial(),
    // new Partial(DateTime.getFields(6), new int[] {2017, 5, 6, 18, 8, 0}));
    // Assert.assertEquals(((DateTime)((Interval) result).getEnd()).getPartial(),
    // new Partial(DateTime.getFields(6), new int[] {2017, 5, 6, 19, 8, 0}));
    result = context.resolveExpressionRef("TestToQuantity").getExpression().evaluate(context);
    // TODO: ModelInfo bug. Not aware of SimpleQuantity
    result = context.resolveExpressionRef("TestRangeToInterval").getExpression().evaluate(context);
    result = context.resolveExpressionRef("TestToCode").getExpression().evaluate(context);
    result = context.resolveExpressionRef("TestToConcept").getExpression().evaluate(context);
    result = context.resolveExpressionRef("TestToString").getExpression().evaluate(context);
    result = context.resolveExpressionRef("TestRequestStatusToString").getExpression().evaluate(context);
    result = context.resolveExpressionRef("TestToDateTime").getExpression().evaluate(context);
    result = context.resolveExpressionRef("TestToTime").getExpression().evaluate(context);
    result = context.resolveExpressionRef("TestToInteger").getExpression().evaluate(context);
    result = context.resolveExpressionRef("TestToDecimal").getExpression().evaluate(context);
    result = context.resolveExpressionRef("TestToBoolean").getExpression().evaluate(context);
}
 
Example #17
Source File: TestFhirPath.java    From cql_engine with Apache License 2.0 5 votes vote down vote up
public void testFhirHelpersDstu2() throws UcumException {
        String cql = getStringFromResourceStream("Dstu2/TestFHIRHelpersDstu2.cql");
        Library library = translate(cql);
        Context context = new Context(library);
        context.registerLibraryLoader(getLibraryLoader());
        Dstu2FhirModelResolver modelResolver = new Dstu2FhirModelResolver();
        RestFhirRetrieveProvider retrieveProvider = new RestFhirRetrieveProvider(
                new SearchParameterResolver(fhirContext), FhirContext.forDstu2().newRestfulGenericClient(""));
        CompositeDataProvider provider = new CompositeDataProvider(modelResolver, retrieveProvider);
        //BaseFhirDataProvider provider = new FhirDataProviderDstu2();
        context.registerDataProvider("http://hl7.org/fhir", provider);

        Object result = context.resolveExpressionRef("TestPeriodToInterval").getExpression().evaluate(context);
        // TODO - millis shouldn't be populated - issue with DateTime.fromJavaDate(Date date)
//        Assert.assertEquals(((DateTime)((Interval) result).getStart()).getPartial(), new Partial(DateTime.getFields(7), new int[] {2017, 5, 6, 18, 8, 0, 0}));
//        Assert.assertEquals(((DateTime)((Interval) result).getEnd()).getPartial(), new Partial(DateTime.getFields(7), new int[] {2017, 5, 6, 19, 8, 0, 0}));
        result = context.resolveExpressionRef("TestToQuantity").getExpression().evaluate(context);
        result = context.resolveExpressionRef("TestRangeToInterval").getExpression().evaluate(context);
        result = context.resolveExpressionRef("TestToCode").getExpression().evaluate(context);
        result = context.resolveExpressionRef("TestToConcept").getExpression().evaluate(context);
        result = context.resolveExpressionRef("TestToString").getExpression().evaluate(context);
        result = context.resolveExpressionRef("TestRequestStatusToString").getExpression().evaluate(context);
        result = context.resolveExpressionRef("TestToDateTime").getExpression().evaluate(context);
        result = context.resolveExpressionRef("TestToTime").getExpression().evaluate(context);
        result = context.resolveExpressionRef("TestToInteger").getExpression().evaluate(context);
        result = context.resolveExpressionRef("TestToDecimal").getExpression().evaluate(context);
        result = context.resolveExpressionRef("TestToBoolean").getExpression().evaluate(context);
    }
 
Example #18
Source File: CqlTestSuite.java    From cql_engine with Apache License 2.0 5 votes vote down vote up
@Test
public void testMainSuite() throws IOException, JAXBException, UcumException {
    Library library = translate("portable/CqlTestSuite.cql");
    Context context = new Context(library, new DateTime(TemporalHelper.getDefaultOffset(), 2018, 1, 1, 7, 0, 0, 0));
    if (library.getStatements() != null) {
        for (ExpressionDef expression : library.getStatements().getDef()) {
            if (expression instanceof FunctionDef) {
                continue;
            }
            if (expression.getName().startsWith("test")) {
                logger.info((String) expression.evaluate(context));
            }
        }
    }
}
 
Example #19
Source File: CqlTestSuite.java    From cql_engine with Apache License 2.0 5 votes vote down vote up
private Library translate(String file)  throws UcumException, JAXBException, IOException {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    UcumService ucumService = new UcumEssenceService(UcumEssenceService.class.getResourceAsStream("/ucum-essence.xml"));

    File cqlFile = new File(URLDecoder.decode(this.getClass().getResource(file).getFile(), "UTF-8"));

    CqlTranslator translator = CqlTranslator.fromFile(cqlFile, modelManager, libraryManager, ucumService);

    if (translator.getErrors().size() > 0) {
        System.err.println("Translation failed due to errors:");
        ArrayList<String> errors = new ArrayList<>();
        for (CqlTranslatorException error : translator.getErrors()) {
            TrackBack tb = error.getLocator();
            String lines = tb == null ? "[n/a]" : String.format("[%d:%d, %d:%d]",
                    tb.getStartLine(), tb.getStartChar(), tb.getEndLine(), tb.getEndChar());
            System.err.printf("%s %s%n", lines, error.getMessage());
            errors.add(lines + error.getMessage());
        }
        throw new IllegalArgumentException(errors.toString());
    }

    assertThat(translator.getErrors().size(), is(0));

    String xml = translator.toXml();

    return CqlLibraryReader.read(new StringReader(xml));
}
 
Example #20
Source File: TestUtils.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
private static void setup() {
    modelManager = new ModelManager();
    libraryManager = new LibraryManager(modelManager);
    libraryManager.getLibrarySourceLoader().registerProvider(new TestLibrarySourceProvider());
    libraryManager.getLibrarySourceLoader().registerProvider(new FhirLibrarySourceProvider());
    try {
        ucumService = new UcumEssenceService(UcumEssenceService.class.getResourceAsStream("/ucum-essence.xml"));
    }
    catch (UcumException e) {
        e.printStackTrace();
    }
}
 
Example #21
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<Base> opTimes(List<Base> left, List<Base> right) 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");
  if (!left.get(0).isPrimitive() && !(left.get(0) instanceof Quantity))
    throw new PathEngineException(String.format("Error performing +: left operand has the wrong type (%s)", left.get(0).fhirType()));
  if (right.size() > 1)
    throw new PathEngineException("Error performing *: right operand has more than one value");
  if (!right.get(0).isPrimitive() && !(right.get(0) instanceof Quantity))
    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("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()).multiply(new BigDecimal(r.primitiveValue()))));
  else if (l instanceof Quantity && r instanceof Quantity && worker.getUcumService() != null) {
    Pair pl = qtyToPair((Quantity) l);
    Pair pr = qtyToPair((Quantity) r);
    Pair p;
    try {
      p = worker.getUcumService().multiply(pl, pr);
      result.add(pairToQty(p));
    } catch (UcumException e) {
      throw new PathEngineException(e.getMessage(), e);
    }
  } 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;
}
 
Example #22
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private DecimalType qtyToCanonical(Quantity q) {
  if (!"http://unitsofmeasure.org".equals(q.getSystem()))
    return null;
  try {
    Pair p = new Pair(new Decimal(q.getValue().toPlainString()), q.getCode());
    Pair c = worker.getUcumService().getCanonicalForm(p);
    return new DecimalType(c.getValue().asDecimal());
  } catch (UcumException e) {
    return null;
  }
}
 
Example #23
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<Base> opTimes(List<Base> left, List<Base> right) 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");
  if (!left.get(0).isPrimitive() && !(left.get(0) instanceof Quantity))
    throw new PathEngineException(String.format("Error performing +: left operand has the wrong type (%s)", left.get(0).fhirType()));
  if (right.size() > 1)
    throw new PathEngineException("Error performing *: right operand has more than one value");
  if (!right.get(0).isPrimitive() && !(right.get(0) instanceof Quantity))
    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("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()).multiply(new BigDecimal(r.primitiveValue()))));
  else if (l instanceof Quantity && r instanceof Quantity && worker.getUcumService() != null) {
    Pair pl = qtyToPair((Quantity) l);
    Pair pr = qtyToPair((Quantity) r);
    Pair p;
    try {
      p = worker.getUcumService().multiply(pl, pr);
      result.add(pairToQty(p));
    } catch (UcumException e) {
      throw new PathEngineException(e.getMessage(), e);
    }
  } 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;
}
 
Example #24
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<Base> opDiv(List<Base> left, List<Base> right) throws PathEngineException {
  if (left.size() == 0 || right.size() == 0) 
    return new ArrayList<Base>();
  if (left.size() > 1)
    throw new PathEngineException("Error performing div: left operand has more than one value");
  if (!left.get(0).isPrimitive() && !(left.get(0) instanceof Quantity))
    throw new PathEngineException(String.format("Error performing div: left operand has the wrong type (%s)", left.get(0).fhirType()));
  if (right.size() > 1)
    throw new PathEngineException("Error performing div: right operand has more than one value");
  if (!right.get(0).isPrimitive() && !(right.get(0) instanceof Quantity))
    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;
}
 
Example #25
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<Base> opMod(List<Base> left, List<Base> right) 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");
  if (!left.get(0).isPrimitive())
    throw new PathEngineException(String.format("Error performing mod: left operand has the wrong type (%s)", left.get(0).fhirType()));
  if (right.size() > 1)
    throw new PathEngineException("Error performing mod: right operand has more than one value");
  if (!right.get(0).isPrimitive())
    throw new PathEngineException(String.format("Error performing mod: 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 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()));
  return result;
}
 
Example #26
Source File: ShexGeneratorTests.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private void doTest(String name) throws FileNotFoundException, IOException, FHIRException, UcumException {
  StructureDefinition sd = TestingUtilities.context().fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(name, null));
  if (sd == null) {
    throw new FHIRException("StructuredDefinition for " + name + "was null");
  }
  Path outPath = FileSystems.getDefault().getPath(System.getProperty("java.io.tmpdir"), name.toLowerCase() + ".shex");
  TextFile.stringToFile(new ShExGenerator(TestingUtilities.context()).generate(HTMLLinkPolicy.NONE, sd), outPath.toString());
}
 
Example #27
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimple() throws FHIRException, FileNotFoundException, IOException, UcumException {

  StructureDefinition focus = new StructureDefinition();
  StructureDefinition base = TestingUtilities.context().fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/Patient").copy();
  focus.setUrl(Utilities.makeUuidUrn());
  focus.setBaseDefinition(base.getUrl());
  focus.setType("Patient");
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
  new ProfileUtilities(TestingUtilities.context(), messages, null).generateSnapshot(base, focus, focus.getUrl(), "http://test.org/test", "Simple Test");

  boolean ok = base.getSnapshot().getElement().size() == focus.getSnapshot().getElement().size();
  for (int i = 0; i < base.getSnapshot().getElement().size(); i++) {
    ElementDefinition b = base.getSnapshot().getElement().get(i);
    ElementDefinition f = focus.getSnapshot().getElement().get(i);
    if (ok) {
      if (!f.hasBase())
        ok = false;
      else if (!b.getPath().equals(f.getPath()))
        ok = false;
      else {
        b.setBase(null);
        f.setBase(null);
        b.setRequirements(null);
        f.setRequirements(null);
        ok = Base.compareDeep(b, f, true);
      }
    }
  }

  if (!ok) {
    compareXml(base, focus);
    throw new FHIRException("Snap shot generation simple test failed");
  } else
    System.out.println("Snap shot generation simple test passed");
}
 
Example #28
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<Base> opDiv(List<Base> left, List<Base> right) throws PathEngineException {
  if (left.size() == 0 || right.size() == 0) 
    return new ArrayList<Base>();
  if (left.size() > 1)
    throw new PathEngineException("Error performing div: left operand has more than one value");
  if (!left.get(0).isPrimitive() && !(left.get(0) instanceof Quantity))
    throw new PathEngineException(String.format("Error performing div: left operand has the wrong type (%s)", left.get(0).fhirType()));
  if (right.size() > 1)
    throw new PathEngineException("Error performing div: right operand has more than one value");
  if (!right.get(0).isPrimitive() && !(right.get(0) instanceof Quantity))
    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;
}
 
Example #29
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private Pair qtyToPair(Quantity q) {
  if (!"http://unitsofmeasure.org".equals(q.getSystem()))
    return null;
  try {
    return new Pair(new Decimal(q.getValue().toPlainString()), q.getCode());
  } catch (UcumException e) {
    return null;
  }
}
 
Example #30
Source File: FHIRPathEngine.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private DecimalType qtyToCanonical(Quantity q) {
  if (!"http://unitsofmeasure.org".equals(q.getSystem()))
    return null;
  try {
    Pair p = new Pair(new Decimal(q.getValue().toPlainString()), q.getCode());
    Pair c = worker.getUcumService().getCanonicalForm(p);
    return new DecimalType(c.getValue().asDecimal());
  } catch (UcumException e) {
    return null;
  }
}