org.hl7.elm.r1.VersionedIdentifier Java Examples

The following examples show how to use org.hl7.elm.r1.VersionedIdentifier. 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: PriorityLibrarySourceLoader.java    From clinical_quality_language with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream getLibrarySource(VersionedIdentifier libraryIdentifier) {
    if (libraryIdentifier == null) {
        throw new IllegalArgumentException("libraryIdentifier is null.");
    }

    if (libraryIdentifier.getId() == null || libraryIdentifier.getId().equals("")) {
        throw new IllegalArgumentException("libraryIdentifier Id is null.");
    }

    InputStream source = null;
    for (LibrarySourceProvider provider : PROVIDERS) {
        InputStream localSource = provider.getLibrarySource(libraryIdentifier);
        if (localSource != null) {
            return localSource;
        }
    }

    throw new IllegalArgumentException(String.format("Could not load source for library %s, version %s.",
            libraryIdentifier.getId(), libraryIdentifier.getVersion()));
}
 
Example #2
Source File: ModelManager.java    From clinical_quality_language with Apache License 2.0 6 votes vote down vote up
private Model buildModel(VersionedIdentifier identifier) {
    Model model = null;
    try {
        ModelInfoProvider provider = ModelInfoLoader.getModelInfoProvider(identifier);
        if (identifier.getId().equals("System")) {
            model = new SystemModel(provider.load());
        }
        else {
            model = new Model(provider.load(), resolveModel("System"));
        }
    } catch (ClassNotFoundException e) {
        throw new IllegalArgumentException(String.format("Could not load model information for model %s, version %s.",
                identifier.getId(), identifier.getVersion()));
    }

    return model;
}
 
Example #3
Source File: NamespaceTests.java    From clinical_quality_language with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream getLibrarySource(VersionedIdentifier libraryIdentifier) {
    String namespacePath = "NamespaceTests/";
    if (libraryIdentifier.getSystem() != null) {
        NamespaceInfo namespaceInfo = libraryManager.getNamespaceManager().getNamespaceInfoFromUri(libraryIdentifier.getSystem());
        if (namespaceInfo != null && !namespaceInfo.getName().equals("Public")) {
            namespacePath += namespaceInfo.getName() + "/";

        }
    }
    String libraryFileName = String.format("%s%s%s.cql",
            namespacePath,
            libraryIdentifier.getId(),
            libraryIdentifier.getVersion() != null ? ("-" + libraryIdentifier.getVersion()) : "");
    return org.cqframework.cql.cql2elm.NamespaceTests.class.getResourceAsStream(libraryFileName);
}
 
Example #4
Source File: FhirLibrarySourceProvider.java    From clinical_quality_language with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream getLibrarySource(VersionedIdentifier libraryIdentifier) {
    // If the FHIRHelpers library is referenced in a namespace-enabled context,
    // set the namespace to the FHIR namespace URI
    if (namespaceManager != null && namespaceManager.hasNamespaces()) {
        // If the context already has a namespace registered for FHIR, use that.
        NamespaceInfo namespaceInfo = namespaceManager.getNamespaceInfoFromUri(namespaceUri);
        if (namespaceInfo == null) {
            namespaceInfo = new NamespaceInfo(namespaceName, namespaceUri);
            namespaceManager.ensureNamespaceRegistered(namespaceInfo);
        }
        libraryIdentifier.setSystem(namespaceUri);
    }

    return FhirLibrarySourceProvider.class.getResourceAsStream(String.format("/org/hl7/fhir/%s-%s.cql", libraryIdentifier.getId(),
            libraryIdentifier.getVersion()));
}
 
Example #5
Source File: ModelInfoLoader.java    From clinical_quality_language with Apache License 2.0 6 votes vote down vote up
public static void registerModelInfoProvider(VersionedIdentifier modelIdentifier, ModelInfoProvider provider) {
    checkModelIdentifier(modelIdentifier);

    if (provider == null) {
        throw new IllegalArgumentException("Provider is null");
    }

    PROVIDERS.put(modelIdentifier, provider);

    if (modelIdentifier.getVersion() != null) {
        VersionedIdentifier versionlessIdentifier = new VersionedIdentifier().withId(modelIdentifier.getId());
        if (!PROVIDERS.containsKey(versionlessIdentifier)) {
            PROVIDERS.put(versionlessIdentifier, provider);
        }
    }
}
 
Example #6
Source File: ModelInfoLoader.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
public static ModelInfoProvider getModelInfoProvider(VersionedIdentifier modelIdentifier) {
    checkModelIdentifier(modelIdentifier);

    ModelInfoProvider provider = PROVIDERS.get(modelIdentifier);
    if (provider == null) {
        throw new IllegalArgumentException(String.format("Could not resolve model info provider for model %s, version %s.",
                modelIdentifier.getId(), modelIdentifier.getVersion()));
    }

    return provider;
}
 
Example #7
Source File: CqlTranslator.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@Override
public void syntaxError(@NotNull Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, @NotNull String msg, RecognitionException e) {
    VersionedIdentifier libraryIdentifier = builder.getLibraryIdentifier();
    if (libraryIdentifier == null) {
        // Attempt to extract a libraryIdentifier from the currently parsed content
        if (recognizer instanceof cqlParser) {
            libraryIdentifier = extractLibraryIdentifier((cqlParser)recognizer);
        }
        if (libraryIdentifier == null) {
            libraryIdentifier = sourceInfo;
        }
    }
    TrackBack trackback = new TrackBack(libraryIdentifier, line, charPositionInLine, line, charPositionInLine);

    if (detailedErrors) {
        builder.recordParsingException(new CqlSyntaxException(msg, trackback, e));
        builder.recordParsingException(new CqlTranslatorException(msg, trackback, e));
    }
    else {
        if (offendingSymbol instanceof CommonToken) {
            CommonToken token = (CommonToken) offendingSymbol;
            builder.recordParsingException(new CqlSyntaxException(String.format("Syntax error at %s", token.getText()), trackback, e));
        } else {
            builder.recordParsingException(new CqlSyntaxException("Syntax error", trackback, e));
        }
    }
}
 
Example #8
Source File: CqlTranslator.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
private VersionedIdentifier extractLibraryIdentifier(cqlParser parser) {
    RuleContext context = parser.getContext();
    while (context != null && !(context instanceof cqlParser.LibraryContext)) {
        context = context.parent;
    }

    if (context instanceof cqlParser.LibraryContext) {
        cqlParser.LibraryDefinitionContext ldc = ((cqlParser.LibraryContext)context).libraryDefinition();
        if (ldc != null && ldc.qualifiedIdentifier() != null && ldc.qualifiedIdentifier().identifier() != null) {
            return new VersionedIdentifier().withId(StringEscapeUtils.unescapeCql(ldc.qualifiedIdentifier().identifier().getText()));
        }
    }

    return null;
}
 
Example #9
Source File: CqlTranslator.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
private static VersionedIdentifier getSourceInfo(File cqlFile) {
    String name = cqlFile.getName();
    int extensionIndex = name.lastIndexOf('.');
    if (extensionIndex > 0) {
        name = name.substring(0, extensionIndex);
    }
    String system = null;
    try {
        system = cqlFile.getCanonicalPath();
    } catch (IOException e) {
        system = cqlFile.getAbsolutePath();
    }

    return new VersionedIdentifier().withId(name).withSystem(system);
}
 
Example #10
Source File: LibraryManager.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
public TranslatedLibrary resolveLibrary(VersionedIdentifier libraryIdentifier, CqlTranslatorOptions options, List<CqlTranslatorException> errors) {
    if (libraryIdentifier == null) {
        throw new IllegalArgumentException("libraryIdentifier is null.");
    }

    if (libraryIdentifier.getId() == null || libraryIdentifier.getId().equals("")) {
        throw new IllegalArgumentException("libraryIdentifier Id is null");
    }

    String libraryPath = NamespaceManager.getPath(libraryIdentifier.getSystem(), libraryIdentifier.getId());
    TranslatedLibrary library = libraries.get(libraryPath);

    if (library != null
            && libraryIdentifier.getVersion() != null
            && !libraryIdentifier.getVersion().equals(library.getIdentifier().getVersion())) {
        throw new CqlTranslatorIncludeException(String.format("Could not resolve reference to library %s, version %s because version %s is already loaded.",
                libraryPath, libraryIdentifier.getVersion(), library.getIdentifier().getVersion()), libraryIdentifier.getSystem(), libraryIdentifier.getId(), libraryIdentifier.getVersion());
    }

    else if (library != null) {
        return library;
    }

    else {
        library = translateLibrary(libraryIdentifier, options, errors);
        if (!HasErrors(errors)) {
            libraries.put(libraryPath, library);
        }
    }

    return library;
}
 
Example #11
Source File: CqlTranslator.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
private CqlTranslator(NamespaceInfo namespaceInfo, VersionedIdentifier sourceInfo, ANTLRInputStream is, ModelManager modelManager,
                      LibraryManager libraryManager, UcumService ucumService, CqlTranslatorOptions options) {
    this.sourceInfo = sourceInfo;
    this.namespaceInfo = namespaceInfo;
    this.modelManager = modelManager;
    this.libraryManager = libraryManager;
    this.ucumService = ucumService;
    this.errorLevel = options.getErrorLevel();

    if (this.sourceInfo == null) {
        this.sourceInfo = new VersionedIdentifier().withId("Anonymous").withSystem("text/cql");
    }

    if (this.namespaceInfo != null) {
        libraryManager.getNamespaceManager().ensureNamespaceRegistered(this.namespaceInfo);
    }

    if (libraryManager.getNamespaceManager().hasNamespaces() && libraryManager.getLibrarySourceLoader() instanceof NamespaceAware) {
        ((NamespaceAware)libraryManager.getLibrarySourceLoader()).setNamespaceManager(libraryManager.getNamespaceManager());
    }

    if (libraryManager.getUcumService() == null) {
        libraryManager.setUcumService(this.ucumService);
    }

    translateToELM(is, options);
}
 
Example #12
Source File: DefaultLibrarySourceLoader.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream getLibrarySource(VersionedIdentifier libraryIdentifier) {
    if (libraryIdentifier == null) {
        throw new IllegalArgumentException("libraryIdentifier is null.");
    }

    if (libraryIdentifier.getId() == null || libraryIdentifier.getId().equals("")) {
        throw new IllegalArgumentException("libraryIdentifier Id is null.");
    }

    InputStream source = null;
    for (LibrarySourceProvider provider : PROVIDERS) {
        InputStream localSource = provider.getLibrarySource(libraryIdentifier);
        if (localSource != null) {
            if (source != null) {
                throw new IllegalArgumentException(String.format("Multiple sources found for library %s, version %s.",
                        libraryIdentifier.getId(), libraryIdentifier.getVersion()));
            }

            source = localSource;
        }
    }

    if (source == null) {
        throw new IllegalArgumentException(String.format("Could not load source for library %s, version %s.",
                libraryIdentifier.getId(), libraryIdentifier.getVersion()));
    }

    return source;
}
 
Example #13
Source File: PathTests.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void setup() {
    modelManager = new ModelManager();
    libraryManager = new LibraryManager(modelManager);
    libraryManager.getLibrarySourceLoader().clearProviders();
    libraryManager.getLibrarySourceLoader().registerProvider(new TestLibrarySourceProvider());
    libraryManager.getLibrarySourceLoader().registerProvider(new FhirLibrarySourceProvider());
    ModelInfoLoader.registerModelInfoProvider(new VersionedIdentifier().withId("FHIR").withVersion("1.8"),
            new TestFhirModelInfoProvider(PathTests.class));
}
 
Example #14
Source File: ModelInfoLoader.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
private static void checkModelIdentifier(VersionedIdentifier modelIdentifier) {
    if (modelIdentifier == null) {
        throw new IllegalArgumentException("modelIdentifier is null.");
    }

    if (modelIdentifier.getId() == null || modelIdentifier.getId().equals("")) {
        throw new IllegalArgumentException("modelIdentifier Id is null.");
    }
}
 
Example #15
Source File: ModelManager.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
public Model resolveModel(VersionedIdentifier modelIdentifier) {
    Model model = models.get(modelIdentifier.getId());
    if (model == null) {
        model = buildModel(modelIdentifier);
        models.put(modelIdentifier.getId(), model);
    }

    if (modelIdentifier.getVersion() != null && !modelIdentifier.getVersion().equals(model.getModelInfo().getVersion())) {
        throw new IllegalArgumentException(String.format("Could not load model information for model %s, version %s because version %s is already loaded.",
                modelIdentifier.getId(), modelIdentifier.getVersion(), model.getModelInfo().getVersion()));
    }

    return model;
}
 
Example #16
Source File: LibrarySourceProvider.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream getLibrarySource(VersionedIdentifier versionedIdentifier) {
    try {
        LibraryType lib = this.provider.resolveLibraryByName(versionedIdentifier.getId(), versionedIdentifier.getVersion());
        for (AttachmentType attachment : this.getAttachments.apply(lib)) {
            if (this.getContentType.apply(attachment).equals("text/cql")) {
                return new ByteArrayInputStream(this.getContent.apply(attachment));
            }
        }
    }
    catch(Exception e){}

    return this.innerProvider.getLibrarySource(versionedIdentifier);
}
 
Example #17
Source File: TrackBack.java    From clinical_quality_language with Apache License 2.0 5 votes vote down vote up
public TrackBack(VersionedIdentifier library, int startLine, int startChar, int endLine, int endChar) {
    this.library = library;
    this.startLine = startLine;
    this.startChar = startChar;
    this.endLine = endLine;
    this.endChar = endChar;
}
 
Example #18
Source File: TestR4ModelResolver.java    From cql_engine with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveModelInfoTests() {
    ModelResolver resolver = new R4FhirModelResolver();
    ModelManager mm = new ModelManager();
    Model m = mm.resolveModel(new VersionedIdentifier().withId("FHIR").withVersion("4.0.0"));

    List<TypeInfo> typeInfos = m.getModelInfo().getTypeInfo();

    for (TypeInfo ti : typeInfos) {
        ClassInfo ci = (ClassInfo)ti;
        if (ci != null) {
            switch (ci.getBaseType()) {
                // Astract classes
                case "FHIR.BackboneElement":
                case "FHIR.Element": continue;
            }

            switch (ci.getName()) {
                // TODO: HAPI Doesn't have a ResourceContainer type
                case "ResourceContainer": continue;
            }


            // TODO: The cause of failure for this is unknown.
            // Need to figure out if it's a gap in happy,
            // or if a manual mapping is required, or what.
            switch(ci.getName()) {
                case "ItemInstance" : continue;
            }

            resolver.resolveType(ci.getName());
        }
    }
}
 
Example #19
Source File: TestDstu3ModelResolver.java    From cql_engine with Apache License 2.0 5 votes vote down vote up
@Test
// This tests all the types that are present in the modelinfo
public void resolveModelInfoTests() {
    ModelResolver resolver = new Dstu3FhirModelResolver();
    ModelManager mm = new ModelManager();
    Model m = mm.resolveModel(new VersionedIdentifier().withId("FHIR").withVersion("3.0.0"));

    List<TypeInfo> typeInfos = m.getModelInfo().getTypeInfo();

    for (TypeInfo ti : typeInfos) {
        ClassInfo ci = (ClassInfo)ti;
        if (ci != null) {
            switch (ci.getBaseType()) {
                // Astract classes
                case "FHIR.BackboneElement":
                case "FHIR.Element": continue;
            }

            switch (ci.getName()) {
                // TODO: HAPI Doesn't have a ResourceContainer type for Dstu3
                case "ResourceContainer": continue;
            }

            resolver.resolveType(ci.getName());
        }
    }
}
 
Example #20
Source File: TestLibrarySourceProvider.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
@Override
public InputStream getLibrarySource(VersionedIdentifier libraryIdentifier) {
    String libraryFileName = String.format("LibraryTests/%s%s.cql",
            libraryIdentifier.getId(), libraryIdentifier.getVersion() != null ? ("-" + libraryIdentifier.getVersion()) : "");
    return TestLibrarySourceProvider.class.getResourceAsStream(libraryFileName);
}
 
Example #21
Source File: CqlTranslator.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public static CqlTranslator fromFile(NamespaceInfo namespaceInfo, VersionedIdentifier sourceInfo, File cqlFile, ModelManager modelManager,
                                     LibraryManager libraryManager, UcumService ucumService, CqlTranslatorOptions options) throws IOException {
    return new CqlTranslator(namespaceInfo, sourceInfo, new ANTLRInputStream(new FileInputStream(cqlFile)), modelManager, libraryManager, ucumService, options);
}
 
Example #22
Source File: PathTests.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
@AfterClass
public void tearDown() {
    ModelInfoLoader.unregisterModelInfoProvider(new VersionedIdentifier().withId("FHIR").withVersion("1.8"));
}
 
Example #23
Source File: ModelTests.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public void setup() {
    ModelInfoLoader.registerModelInfoProvider(new VersionedIdentifier().withId("Test").withVersion("1"),
            new TestModelInfoProvider());
}
 
Example #24
Source File: ModelTests.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
@AfterClass
public void tearDown() {
    ModelInfoLoader.unregisterModelInfoProvider(new VersionedIdentifier().withId("Test").withVersion("1"));
}
 
Example #25
Source File: TrackBack.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public VersionedIdentifier getLibrary() {
    return library;
}
 
Example #26
Source File: CqlTranslator.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public static void loadModelInfo(File modelInfoXML) {
    final ModelInfo modelInfo = JAXB.unmarshal(modelInfoXML, ModelInfo.class);
    final VersionedIdentifier modelId = new VersionedIdentifier().withId(modelInfo.getName()).withVersion(modelInfo.getVersion());
    final ModelInfoProvider modelProvider = () -> modelInfo;
    ModelInfoLoader.registerModelInfoProvider(modelId, modelProvider);
}
 
Example #27
Source File: CqlTranslator.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
private static VersionedIdentifier getSourceInfo(String cqlFileName) {
    return getSourceInfo(new File(cqlFileName));
}
 
Example #28
Source File: CqlTranslator.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
private CqlTranslator(NamespaceInfo namespaceInfo, VersionedIdentifier sourceInfo, ANTLRInputStream is, ModelManager modelManager, LibraryManager libraryManager, UcumService ucumService,
                      CqlTranslatorException.ErrorSeverity errorLevel, LibraryBuilder.SignatureLevel signatureLevel, CqlTranslator.Options... options) {
    this(namespaceInfo, sourceInfo, is, modelManager, libraryManager, ucumService, new CqlTranslatorOptions(errorLevel, signatureLevel, options));
}
 
Example #29
Source File: TestLibrarySourceProvider.java    From cql_engine with Apache License 2.0 4 votes vote down vote up
@Override
public InputStream getLibrarySource(VersionedIdentifier libraryIdentifier) {
    String libraryFileName = String.format("stu3/%s%s.cql",
            libraryIdentifier.getId(), libraryIdentifier.getVersion() != null ? ("-" + libraryIdentifier.getVersion()) : "");
    return TestLibrarySourceProvider.class.getResourceAsStream(libraryFileName);
}
 
Example #30
Source File: CqlTranslator.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
public static CqlTranslator fromStream(NamespaceInfo namespaceInfo, VersionedIdentifier sourceInfo, InputStream cqlStream, ModelManager modelManager,
                                       LibraryManager libraryManager, UcumService ucumService, CqlTranslatorOptions options) throws IOException {
    return new CqlTranslator(namespaceInfo, sourceInfo, new ANTLRInputStream(cqlStream), modelManager, libraryManager, ucumService, options);
}