Search in sources :

Example 6 with Archetype

use of com.nedap.archie.aom.Archetype in project archetype-languageserver by nedap.

the class BroadcastingArchetypeRepository method invalidateAndRecompileArchetypes.

public void invalidateAndRecompileArchetypes(Archetype newArchetype) {
    // now invalidate all related archetypes
    Set<String> archetypesToInvalidate = new HashSet<>();
    archetypesToInvalidate.add(newArchetype.getArchetypeId().toString());
    for (Archetype result : getAllArchetypes()) {
        if (isDescendant(result, newArchetype.getArchetypeId().getFullId())) {
            archetypesToInvalidate.add(result.getArchetypeId().toString());
        }
        // this assumes templates cannot be further specialized
        if (result instanceof Template) {
            Template template = (Template) result;
            for (TemplateOverlay overlay : template.getTemplateOverlays()) {
                // TODO: not only direct descendants, but also those far below should be checked with (a variant of?) isDescendant
                if (archetypesToInvalidate.contains(overlay.getParentArchetypeId())) {
                    archetypesToInvalidate.add(result.getArchetypeId().getFullId());
                }
            }
        }
    }
    for (String archetypeId : archetypesToInvalidate) {
        removeValidationResult(archetypeId);
    }
    // TODO: for operational templates we need to scan way more, all archetype roots as well. future addition
    if (compile) {
        for (String archetypeId : archetypesToInvalidate) {
            Archetype archetype = getArchetype(archetypeId);
            if (archetype != null && getValidationResult(archetypeId) == null) {
                validator.validate(archetype, this);
            }
        }
        resolveDocumentLinks();
    }
}
Also used : TemplateOverlay(com.nedap.archie.aom.TemplateOverlay) Archetype(com.nedap.archie.aom.Archetype) HashSet(java.util.HashSet) Template(com.nedap.archie.aom.Template)

Example 7 with Archetype

use of com.nedap.archie.aom.Archetype in project archetype-languageserver by nedap.

the class AQLStorage method extractHoverInfo.

private void extractHoverInfo(AQLDocument document) {
    HoverInfo hoverInfo = new HoverInfo("aql");
    MetaModels metaModels = BuiltinReferenceModels.getMetaModels();
    for (ArchetypePathReference reference : document.getArchetypePathReferences()) {
        if (reference.getArchetypeId() == null) {
            continue;
        }
        try {
            // TODO: get operational template here. I think that should be cached?
            ValidationResult validationResult = archetypeRepository.getValidationResult(reference.getArchetypeId());
            if (validationResult != null) {
                Archetype flattened = validationResult.getFlattened();
                metaModels.selectModel(flattened);
                if (flattened != null) {
                    PartialAOMPathQuery aomPathQuery = new PartialAOMPathQuery(reference.getPath());
                    PartialAOMPathQuery.PartialMatch partial = aomPathQuery.findLSPPartial(flattened.getDefinition());
                    if (partial.getMatches().size() > 0) {
                        ArchetypeModelObject archetypeModelObject = partial.getMatches().get(0);
                        String content = null;
                        String description = null;
                        String typeName = "";
                        if (archetypeModelObject instanceof CAttribute) {
                            CAttribute attribute = (CAttribute) archetypeModelObject;
                            content = findNearestText((CAttribute) archetypeModelObject);
                            description = findNearestDescription((CAttribute) archetypeModelObject);
                            CObject parent = attribute.getParent();
                            if (partial.getRemainingQuery().isEmpty()) {
                                // TODO: proper path lookup here
                                BmmClass classDefinition = metaModels.getSelectedBmmModel().getClassDefinition(BmmDefinitions.typeNameToClassKey(parent.getRmTypeName()));
                                if (classDefinition != null) {
                                    BmmProperty bmmProperty = classDefinition.getFlatProperties().get(attribute.getRmAttributeName());
                                    if (bmmProperty != null) {
                                        bmmProperty.getType().toDisplayString();
                                    }
                                }
                            }
                        } else if (archetypeModelObject instanceof CObject) {
                            content = findNearestText((CObject) archetypeModelObject);
                            description = findNearestDescription((CObject) archetypeModelObject);
                            if (partial.getRemainingQuery().isEmpty()) {
                                // TODO: proper path lookup here.
                                typeName = ((CObject) archetypeModelObject).getRmTypeName();
                            }
                        }
                        String text = content + partial.getRemainingQuery().stream().map(PathSegment::toString).collect(Collectors.joining("/"));
                        text += "\n\n" + typeName;
                        text += "\n\n" + description;
                        text += "\n\nIn Archetype " + flattened.getDefinition().getTerm().getText() + " (" + reference.getArchetypeId() + ")";
                        hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, text)));
                    } else {
                        hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "path " + reference.getPath() + " not found")));
                    }
                } else {
                    hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "flattened archetype not found")));
                }
            } else {
                hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "archetype not found")));
            }
        } catch (Exception e) {
            // ok... report as diagnostics or log
            e.printStackTrace();
        }
    }
    document.setHoverInfo(hoverInfo);
}
Also used : BmmClass(org.openehr.bmm.core.BmmClass) MetaModels(com.nedap.archie.rminfo.MetaModels) Archetype(com.nedap.archie.aom.Archetype) CObject(com.nedap.archie.aom.CObject) HoverInfo(com.nedap.openehr.lsp.document.HoverInfo) ArchetypeModelObject(com.nedap.archie.aom.ArchetypeModelObject) CAttribute(com.nedap.archie.aom.CAttribute) PathSegment(com.nedap.archie.paths.PathSegment) ValidationResult(com.nedap.archie.archetypevalidator.ValidationResult) AQLValidationException(com.nedap.healthcare.aqlparser.exception.AQLValidationException) AQLRuntimeException(com.nedap.healthcare.aqlparser.exception.AQLRuntimeException) AQLUnsupportedFeatureException(com.nedap.healthcare.aqlparser.exception.AQLUnsupportedFeatureException) BmmProperty(org.openehr.bmm.core.BmmProperty)

Example 8 with Archetype

use of com.nedap.archie.aom.Archetype in project archetype-languageserver by nedap.

the class AQLStorage method getCodeLens.

public List<? extends CodeLens> getCodeLens(CodeLensParams params) {
    AQLDocument document = this.aqlDocumentsByUri.get(params.getTextDocument().getUri());
    if (document == null) {
        return new ArrayList<>();
    }
    List<CodeLens> result = new ArrayList<>();
    for (ArchetypePathReference reference : document.getArchetypePathReferences()) {
        if (reference.getArchetypeId() == null) {
            continue;
        }
        try {
            // TODO: get operational template here. I think that should be cached?
            ValidationResult validationResult = archetypeRepository.getValidationResult(reference.getArchetypeId());
            if (validationResult != null) {
                Archetype flattened = validationResult.getFlattened();
                if (flattened != null) {
                    PartialAOMPathQuery aomPathQuery = new PartialAOMPathQuery(reference.getPath());
                    PartialAOMPathQuery.PartialMatch partial = aomPathQuery.findLSPPartial(flattened.getDefinition());
                    if (partial.getMatches().size() > 0) {
                        ArchetypeModelObject archetypeModelObject = partial.getMatches().get(0);
                        String content = null;
                        String description = null;
                        if (archetypeModelObject instanceof CAttribute) {
                            content = findNearestText((CAttribute) archetypeModelObject);
                            description = findNearestDescription((CAttribute) archetypeModelObject);
                        } else if (archetypeModelObject instanceof CObject) {
                            content = findNearestText((CObject) archetypeModelObject);
                            description = findNearestDescription((CObject) archetypeModelObject);
                        }
                        String text = content + partial.getRemainingQuery().stream().map(PathSegment::toString).collect(Collectors.joining("/"));
                        String extraText = description;
                        extraText += "\n\nIn Archetype " + flattened.getDefinition().getTerm().getText() + " (" + reference.getArchetypeId() + ")";
                        // result.add(new CodeLens(reference.getRange(), new Command(text, ADL2TextDocumentService.SHOW_INFO_COMMAND, Lists.newArrayList(extraText)), null));
                        result.add(new CodeLens(reference.getRange(), new Command(text, ADL2TextDocumentService.SHOW_INFO_COMMAND, Lists.newArrayList(extraText)), null));
                    } else {
                    // hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "path " + reference.getPath() + " not found")));
                    }
                } else {
                // hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "flattened archetype not found")));
                }
            } else {
            // hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "archetype not found")));
            }
        } catch (Exception e) {
            // ok... report as diagnostics or log
            e.printStackTrace();
        }
    }
    return result;
}
Also used : Archetype(com.nedap.archie.aom.Archetype) CObject(com.nedap.archie.aom.CObject) ArrayList(java.util.ArrayList) ArchetypeModelObject(com.nedap.archie.aom.ArchetypeModelObject) CAttribute(com.nedap.archie.aom.CAttribute) PathSegment(com.nedap.archie.paths.PathSegment) ValidationResult(com.nedap.archie.archetypevalidator.ValidationResult) AQLValidationException(com.nedap.healthcare.aqlparser.exception.AQLValidationException) AQLRuntimeException(com.nedap.healthcare.aqlparser.exception.AQLRuntimeException) AQLUnsupportedFeatureException(com.nedap.healthcare.aqlparser.exception.AQLUnsupportedFeatureException)

Example 9 with Archetype

use of com.nedap.archie.aom.Archetype in project archetype-languageserver by nedap.

the class GenerateExampleCommand method apply.

public void apply() {
    String documentUri = ((JsonPrimitive) params.getArguments().get(0)).getAsString();
    String format = ((JsonPrimitive) params.getArguments().get(1)).getAsString();
    String serializedExample = null;
    Flattener flattener = new Flattener(storage, BuiltinReferenceModels.getMetaModels(), FlattenerConfiguration.forOperationalTemplate());
    DocumentInformation documentInformation = storage.getDocumentInformation(documentUri);
    Archetype archetype = storage.getArchetype(documentInformation.getArchetypeId());
    OperationalTemplate opt = (OperationalTemplate) flattener.flatten(archetype);
    ExampleJsonInstanceGenerator exampleJsonInstanceGenerator = new ExampleJsonInstanceGenerator(BuiltinReferenceModels.getMetaModels(), archetype.getOriginalLanguage().getCodeString());
    Map<String, Object> exampleMap = exampleJsonInstanceGenerator.generate(opt);
    String extension;
    ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
    String jsonRmObject = null;
    OpenEHRBase example;
    try {
        jsonRmObject = objectMapper.writeValueAsString(exampleMap);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    switch(format) {
        case "json":
            extension = "_example.json";
            serializedExample = jsonRmObject;
            break;
        case "flat_json":
            extension = "_flat_example.json";
            FlatJsonGenerator flatJsonGenerator = new FlatJsonGenerator(ArchieRMInfoLookup.getInstance(), FlatJsonFormatConfiguration.nedapInternalFormat());
            try {
                example = objectMapper.readValue(jsonRmObject, OpenEHRBase.class);
                Map<String, Object> flatExample = flatJsonGenerator.buildPathsAndValues(example);
                serializedExample = JacksonUtil.getObjectMapper().writeValueAsString(flatExample);
            } catch (JsonProcessingException | DuplicateKeyException e) {
                throw new RuntimeException(e);
            }
            break;
        case "xml":
            {
                extension = "_example.xml";
                try {
                    example = objectMapper.readValue(jsonRmObject, OpenEHRBase.class);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
                StringWriter sw = new StringWriter();
                try {
                    Marshaller marshaller = JAXBUtil.getArchieJAXBContext().createMarshaller();
                    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                    marshaller.marshal(example, sw);
                } catch (JAXBException e) {
                    throw new RuntimeException(e);
                }
                serializedExample = sw.toString();
                break;
            }
        // 
        default:
            throw new UnsupportedOperationException("unsupported format: " + format);
    }
    String uriToWrite = documentUri.substring(0, documentUri.lastIndexOf("/")) + "/example/" + opt.getArchetypeId() + extension;
    textDocumentService.writeFile(uriToWrite, "opt in " + format, serializedExample);
}
Also used : FlatJsonGenerator(com.nedap.archie.json.flat.FlatJsonGenerator) Marshaller(javax.xml.bind.Marshaller) JsonPrimitive(com.google.gson.JsonPrimitive) Archetype(com.nedap.archie.aom.Archetype) Flattener(com.nedap.archie.flattener.Flattener) DocumentInformation(com.nedap.openehr.lsp.document.DocumentInformation) JAXBException(javax.xml.bind.JAXBException) DuplicateKeyException(com.nedap.archie.json.flat.DuplicateKeyException) StringWriter(java.io.StringWriter) OpenEHRBase(com.nedap.archie.base.OpenEHRBase) OperationalTemplate(com.nedap.archie.aom.OperationalTemplate) ExampleJsonInstanceGenerator(com.nedap.archie.creation.ExampleJsonInstanceGenerator) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 10 with Archetype

use of com.nedap.archie.aom.Archetype in project archetype-languageserver by nedap.

the class ADL14ConvertingStorage method getAllToConvertIncludingParents.

private List<Archetype> getAllToConvertIncludingParents(Archetype archetype) {
    List<Archetype> toConvert = new ArrayList<>();
    toConvert.add(archetype);
    Stack<Archetype> toAdd = new Stack();
    toAdd.push(archetype);
    while (!toAdd.isEmpty()) {
        Archetype a = toAdd.pop();
        // user can override using the convert all adl 1.4 archetypes
        if (a.getParentArchetypeId() != null && repository.getArchetype(a.getParentArchetypeId()) == null) {
            for (Archetype possibleParent : this.adl14Files.values()) {
                // ADL 1.4, so simple string comparison is enough
                if (a.getParentArchetypeId().equalsIgnoreCase(possibleParent.getArchetypeId().toString())) {
                    toConvert.add(possibleParent);
                    toAdd.push(possibleParent);
                }
            }
        }
    }
    return toConvert;
}
Also used : Archetype(com.nedap.archie.aom.Archetype) ArrayList(java.util.ArrayList) Stack(java.util.Stack)

Aggregations

Archetype (com.nedap.archie.aom.Archetype)13 ValidationResult (com.nedap.archie.archetypevalidator.ValidationResult)5 DocumentInformation (com.nedap.openehr.lsp.document.DocumentInformation)4 ArchetypeModelObject (com.nedap.archie.aom.ArchetypeModelObject)3 CAttribute (com.nedap.archie.aom.CAttribute)3 CObject (com.nedap.archie.aom.CObject)3 ArrayList (java.util.ArrayList)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 ADLParseException (com.nedap.archie.adlparser.ADLParseException)2 OperationalTemplate (com.nedap.archie.aom.OperationalTemplate)2 Flattener (com.nedap.archie.flattener.Flattener)2 PathSegment (com.nedap.archie.paths.PathSegment)2 MetaModels (com.nedap.archie.rminfo.MetaModels)2 AQLRuntimeException (com.nedap.healthcare.aqlparser.exception.AQLRuntimeException)2 AQLUnsupportedFeatureException (com.nedap.healthcare.aqlparser.exception.AQLUnsupportedFeatureException)2 AQLValidationException (com.nedap.healthcare.aqlparser.exception.AQLValidationException)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 JAXBException (javax.xml.bind.JAXBException)2