org.cqframework.cql.cql2elm.LibraryManager Java Examples

The following examples show how to use org.cqframework.cql.cql2elm.LibraryManager. 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: LibraryOperationsProvider.java    From cqf-ruler with Apache License 2.0 8 votes vote down vote up
@Operation(name = "$refresh-generated-content", type = Library.class)
public MethodOutcome refreshGeneratedContent(HttpServletRequest theRequest, RequestDetails theRequestDetails,
        @IdParam IdType theId) {
    Library theResource = this.libraryResourceProvider.getDao().read(theId);
    //this.formatCql(theResource);

    ModelManager modelManager = this.getModelManager();
    LibraryManager libraryManager = this.getLibraryManager(modelManager);

    CqlTranslator translator = this.dataRequirementsProvider.getTranslator(theResource, libraryManager, modelManager);
    if (translator.getErrors().size() > 0) {
        throw new RuntimeException("Errors during library compilation.");
    }
    
    this.dataRequirementsProvider.ensureElm(theResource, translator);
    this.dataRequirementsProvider.ensureRelatedArtifacts(theResource, translator, this);
    this.dataRequirementsProvider.ensureDataRequirements(theResource, translator);

    Narrative n = this.narrativeProvider.getNarrative(this.libraryResourceProvider.getContext(), theResource);
    theResource.setText(n);

    return this.libraryResourceProvider.update(theRequest, theResource, theId,
            theRequestDetails.getConditionalUrl(RestOperationTypeEnum.UPDATE), theRequestDetails);
}
 
Example #2
Source File: TranslatorHelper.java    From cqf-ruler with Apache License 2.0 6 votes vote down vote up
public static CqlTranslator getTranslator(InputStream cqlStream, LibraryManager libraryManager, ModelManager modelManager) {
    ArrayList<CqlTranslator.Options> options = new ArrayList<>();
    options.add(CqlTranslator.Options.EnableAnnotations);
    options.add(CqlTranslator.Options.EnableLocators);
    options.add(CqlTranslator.Options.DisableListDemotion);
    options.add(CqlTranslator.Options.DisableListPromotion);
    options.add(CqlTranslator.Options.DisableMethodInvocation);
    CqlTranslator translator;
    try {
        translator = CqlTranslator.fromStream(cqlStream, modelManager, libraryManager,
                options.toArray(new CqlTranslator.Options[options.size()]));
    } catch (IOException e) {
        throw new IllegalArgumentException(String.format("Errors occurred translating library: %s", e.getMessage()));
    }
    
    return translator;
}
 
Example #3
Source File: DataRequirementsProvider.java    From cqf-ruler with Apache License 2.0 6 votes vote down vote up
public CqlTranslator getTranslator(org.hl7.fhir.r4.model.Library library, LibraryManager libraryManager, ModelManager modelManager) {
    Attachment cql = null;
    for (Attachment a : library.getContent()) {
        if (a.getContentType().equals("text/cql")) {
            cql = a;
            break;
        }
    }

    if (cql == null) {
        return null;
    }

    return TranslatorHelper.getTranslator(
            new ByteArrayInputStream(Base64.getDecoder().decode(cql.getDataElement().getValueAsString())),
            libraryManager, modelManager);
}
 
Example #4
Source File: LibraryOperationsProvider.java    From cqf-ruler with Apache License 2.0 6 votes vote down vote up
@Operation(name = "$get-elm", idempotent = true, type = Library.class)
public Parameters getElm(@IdParam IdType theId, @OptionalParam(name="format") String format) {
    Library theResource = this.libraryResourceProvider.getDao().read(theId);
    // this.formatCql(theResource);

    ModelManager modelManager = this.getModelManager();
    LibraryManager libraryManager = this.getLibraryManager(modelManager);

    String elm = "";
    CqlTranslator translator = this.dataRequirementsProvider.getTranslator(theResource, libraryManager, modelManager);
    if (translator != null) {
        if (format.equals("json")) {
            elm = translator.toJson();
        }
        else {
            elm = translator.toXml();
        }
    }
    Parameters p = new Parameters();
    p.addParameter().setValue(new StringType(elm));
    return p;
}
 
Example #5
Source File: DataRequirementsProvider.java    From cqf-ruler with Apache License 2.0 6 votes vote down vote up
public CqlTranslator getTranslator(org.hl7.fhir.dstu3.model.Library library, LibraryManager libraryManager, ModelManager modelManager) {
    Attachment cql = null;
    for (Attachment a : library.getContent()) {
        if (a.getContentType().equals("text/cql")) {
            cql = a;
            break;
        }
    }

    if (cql == null) {
        return null;
    }

    CqlTranslator translator = TranslatorHelper.getTranslator(
            new ByteArrayInputStream(Base64.getDecoder().decode(cql.getDataElement().getValueAsString())),
            libraryManager, modelManager);

    return translator;
}
 
Example #6
Source File: LibraryOperationsProvider.java    From cqf-ruler with Apache License 2.0 6 votes vote down vote up
@Operation(name = "$get-elm", idempotent = true, type = Library.class)
public Parameters getElm(@IdParam IdType theId, @OptionalParam(name="format") String format) {
    Library theResource = this.libraryResourceProvider.getDao().read(theId);
    // this.formatCql(theResource);

    ModelManager modelManager = this.getModelManager();
    LibraryManager libraryManager = this.getLibraryManager(modelManager);


    String elm = "";
    CqlTranslator translator = this.dataRequirementsProvider.getTranslator(theResource, libraryManager, modelManager);
    if (translator != null) {
        if (format.equals("json")) {
            elm = translator.toJson();
        }
        else {
            elm = translator.toXml();
        }
    }
    Parameters p = new Parameters();
    p.addParameter().setValue(new StringType(elm));
    return p;
}
 
Example #7
Source File: EscapeSequenceWithBacktickTests.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    CqlTranslator translator = CqlTranslator.fromStream(org.cqframework.cql.cql2elm.EscapeSequenceTests.class.getResourceAsStream("EscapeSequenceWithBacktickTests.cql"), modelManager, libraryManager);
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #8
Source File: EscapeSequenceTests.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    CqlTranslator translator = CqlTranslator.fromStream(org.cqframework.cql.cql2elm.EscapeSequenceTests.class.getResourceAsStream("EscapeSequenceTests.cql"), modelManager, libraryManager);
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #9
Source File: DateTimeOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(DateTimeOperatorsTest.class.getResourceAsStream("../OperatorTests/DateTimeOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #10
Source File: StringOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(StringOperatorsTest.class.getResourceAsStream("../OperatorTests/StringOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #11
Source File: AgeOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(AgeOperatorsTest.class.getResourceAsStream("../OperatorTests/AgeOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    if (library.getStatements() != null) {
        for (ExpressionDef def : library.getStatements().getDef()) {
            defs.put(def.getName(), def);
        }
    }
}
 
Example #12
Source File: AggregateOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(AggregateOperatorsTest.class.getResourceAsStream("../OperatorTests/AggregateOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    if (library.getStatements() != null) {
        for (ExpressionDef def : library.getStatements().getDef()) {
            defs.put(def.getName(), def);
        }
    }
}
 
Example #13
Source File: CqlListOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(CqlListOperatorsTest.class.getResourceAsStream("../OperatorTests/CqlListOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    if (library.getStatements() != null) {
        for (ExpressionDef def : library.getStatements().getDef()) {
            defs.put(def.getName(), def);
        }
    }
}
 
Example #14
Source File: ArithmeticOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(ArithmeticOperatorsTest.class.getResourceAsStream("../OperatorTests/ArithmeticOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #15
Source File: TimeOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(TimeOperatorsTest.class.getResourceAsStream("../OperatorTests/TimeOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #16
Source File: CqlIntervalOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(CqlIntervalOperatorsTest.class.getResourceAsStream("../OperatorTests/CqlIntervalOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    if (library.getStatements() != null) {
        for (ExpressionDef def : library.getStatements().getDef()) {
            defs.put(def.getName(), def);
        }
    }
}
 
Example #17
Source File: QueryTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(QueryTest.class.getResourceAsStream("../OperatorTests/Query.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #18
Source File: SortingTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(QueryTest.class.getResourceAsStream("../OperatorTests/Sorting.cql"), modelManager, new LibraryManager(modelManager));
    
    // The alias test creates an error
    assertThat(translator.getErrors().size(), is(1));

    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #19
Source File: ListOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(ListOperatorsTest.class.getResourceAsStream("../OperatorTests/ListOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #20
Source File: TypeOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(TypeOperatorsTest.class.getResourceAsStream("../OperatorTests/TypeOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #21
Source File: NullologicalOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(NullologicalOperatorsTest.class.getResourceAsStream("../OperatorTests/NullologicalOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def: library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
 
Example #22
Source File: CqlComparisonOperatorsTest.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(CqlComparisonOperatorsTest.class.getResourceAsStream("../OperatorTests/CqlComparisonOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    if (library.getStatements() != null) {
        for (ExpressionDef def : library.getStatements().getDef()) {
            defs.put(def.getName(), def);
        }
    }
}
 
Example #23
Source File: TestLibraryLoader.java    From cql_engine with Apache License 2.0 5 votes vote down vote up
public TestLibraryLoader(LibraryManager libraryManager) {
    if (libraryManager == null) {
        throw new IllegalArgumentException("libraryManager is null");
    }

    this.libraryManager = libraryManager;
}
 
Example #24
Source File: LibraryOperationsProvider.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
@Operation(name = "$refresh-generated-content", type = Library.class)
  public MethodOutcome refreshGeneratedContent(HttpServletRequest theRequest, RequestDetails theRequestDetails,
          @IdParam IdType theId) {
      Library theResource = this.libraryResourceProvider.getDao().read(theId);
      //this.formatCql(theResource);

      ModelManager modelManager = this.getModelManager();
      LibraryManager libraryManager = this.getLibraryManager(modelManager);

      CqlTranslator translator = this.dataRequirementsProvider.getTranslator(theResource, libraryManager, modelManager);
      if (translator.getErrors().size() > 0) {
          throw new RuntimeException("Errors during library compilation.");
      }
      
      this.dataRequirementsProvider.ensureElm(theResource, translator);
      this.dataRequirementsProvider.ensureRelatedArtifacts(theResource, translator, this);
      this.dataRequirementsProvider.ensureDataRequirements(theResource, translator);

try {
	Narrative n = this.narrativeProvider.getNarrative(this.libraryResourceProvider.getContext(), theResource);
	theResource.setText(n);
} catch (Exception e) {
	//Ignore the exception so the resource still gets updated
}

      return this.libraryResourceProvider.update(theRequest, theResource, theId,
              theRequestDetails.getConditionalUrl(RestOperationTypeEnum.UPDATE), theRequestDetails);
  }
 
Example #25
Source File: LibraryOperationsProvider.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
private LibraryManager getLibraryManager(ModelManager modelManager)
{
    LibraryManager libraryManager = new LibraryManager(modelManager);
    libraryManager.getLibrarySourceLoader().clearProviders();
    libraryManager.getLibrarySourceLoader().registerProvider(getLibrarySourceProvider());

    return libraryManager;
}
 
Example #26
Source File: LibraryHelper.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
public static LibraryLoader createLibraryLoader(LibraryResolutionProvider<org.hl7.fhir.r4.model.Library> provider) {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    libraryManager.getLibrarySourceLoader().clearProviders();
    
    libraryManager.getLibrarySourceLoader().registerProvider(
        new LibrarySourceProvider<org.hl7.fhir.r4.model.Library, org.hl7.fhir.r4.model.Attachment>(
            provider, 
            x -> x.getContent(),
            x -> x.getContentType(),
            x -> x.getData()));

    return new LibraryLoader(libraryManager, modelManager);
}
 
Example #27
Source File: LibraryOperationsProvider.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
private LibraryManager getLibraryManager(ModelManager modelManager)
{
    LibraryManager libraryManager = new LibraryManager(modelManager);
    libraryManager.getLibrarySourceLoader().clearProviders();
    libraryManager.getLibrarySourceLoader().registerProvider(getLibrarySourceProvider());

    return libraryManager;
}
 
Example #28
Source File: LibraryHelper.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
public static LibraryLoader createLibraryLoader(LibraryResolutionProvider<org.hl7.fhir.dstu3.model.Library> provider) {
    ModelManager modelManager = new ModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    libraryManager.getLibrarySourceLoader().clearProviders();
    
    libraryManager.getLibrarySourceLoader().registerProvider(
        new LibrarySourceProvider<org.hl7.fhir.dstu3.model.Library, org.hl7.fhir.dstu3.model.Attachment>(
            provider, 
            x -> x.getContent(),
            x -> x.getContentType(),
            x -> x.getData()));

    return new LibraryLoader(libraryManager, modelManager);
}
 
Example #29
Source File: TestLibraryLoader.java    From cql_engine with Apache License 2.0 5 votes vote down vote up
public TestLibraryLoader(LibraryManager libraryManager) {
    if (libraryManager == null) {
        throw new IllegalArgumentException("libraryManager is null");
    }

    this.libraryManager = libraryManager;
}
 
Example #30
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));
}