Search in sources :

Example 1 with Archetype

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

the class AQLStorage method completion.

public Either<List<CompletionItem>, CompletionList> completion(CompletionParams position) {
    String uri = position.getTextDocument().getUri();
    AQLDocument aqlDocument = this.aqlDocumentsByUri.get(uri);
    if (aqlDocument == null) {
        return Either.forRight(new CompletionList());
    }
    List<CompletionItem> items = new ArrayList<>();
    CompletionList result = new CompletionList(items);
    MetaModels metaModels = BuiltinReferenceModels.getMetaModels();
    for (ArchetypePathReference reference : aqlDocument.getArchetypePathReferences()) {
        // copy the range, adding one because typing at the end of course
        Range range = new Range();
        range.setStart(new Position(reference.getRange().getStart().getLine(), reference.getRange().getStart().getCharacter()));
        range.setEnd(new Position(reference.getRange().getEnd().getLine(), reference.getRange().getEnd().getCharacter() + 1));
        if (reference.getArchetypeId() != null && Ranges.containsPosition(range, position.getPosition())) {
            // TODO: get operational template here. I think that should be cached?
            ValidationResult validationResult = archetypeRepository.getValidationResult(reference.getArchetypeId());
            if (validationResult != null && validationResult.getFlattened() != null) {
                // ok we have code completion!
                // now to find the correct archetype path
                Archetype flat = validationResult.getFlattened();
                metaModels.selectModel(flat);
                PartialAOMPathQuery partialAOMPathQuery = new PartialAOMPathQuery(reference.getPath());
                // TODO: get path UP TO where we are typing!
                PartialAOMPathQuery.PartialMatch partial = partialAOMPathQuery.findLSPPartial(flat.getDefinition());
                // TODO: add BMM path traversal here as well, so you can do /value/magnitude
                for (ArchetypeModelObject object : partial.getMatches()) {
                    if (object instanceof CObject) {
                        for (CAttribute attribute : ((CObject) object).getAttributes()) {
                            for (CObject child : attribute.getChildren()) {
                                if (child.getNodeId() != null && !"id9999".equalsIgnoreCase(child.getNodeId())) {
                                    String text = child.getTerm() == null ? attribute.getRmAttributeName() + "[" + child.getNodeId() + "] (" + child.getRmTypeName() + ")" : child.getTerm().getText() + " (" + attribute.getRmAttributeName() + " " + child.getRmTypeName() + "[" + child.getNodeId() + "])";
                                    CompletionItem completionItem = new CompletionItem(text);
                                    completionItem.setInsertText(attribute.getRmAttributeName() + "[" + child.getNodeId() + "]");
                                    completionItem.setFilterText(attribute.getRmAttributeName() + "[" + child.getNodeId() + "]" + text);
                                    // the 0 is to sort this before others
                                    completionItem.setSortText(attribute.getRmAttributeName() + "0" + text);
                                    completionItem.setKind(CompletionItemKind.Reference);
                                    items.add(completionItem);
                                }
                            }
                        }
                        BmmClass classDefinition = metaModels.getSelectedBmmModel().getClassDefinition(BmmDefinitions.typeNameToClassKey(((CObject) object).getRmTypeName()));
                        if (classDefinition != null) {
                            for (BmmProperty property : classDefinition.getFlatProperties().values()) {
                                CompletionItem completionItem = new CompletionItem(property.getName() + "(" + property.getType().toDisplayString() + ")");
                                completionItem.setInsertText(property.getName());
                                // sort this last please
                                completionItem.setSortText(property.getName() + "zzzz");
                                completionItem.setKind(CompletionItemKind.Reference);
                                items.add(completionItem);
                            }
                        }
                    } else if (object instanceof CAttribute) {
                        for (CObject child : ((CAttribute) object).getChildren()) {
                            if (child.getNodeId() != null && !child.getNodeId().equalsIgnoreCase("id9999")) {
                                CompletionItem completionItem = new CompletionItem();
                                completionItem.setLabel(child.getTerm() == null ? child.getRmTypeName() : child.getTerm().getText());
                                completionItem.setInsertText("[" + child.getNodeId() + "]");
                                completionItem.setKind(CompletionItemKind.Reference);
                                // completionItem.setInsertText();
                                items.add(completionItem);
                            }
                        }
                    }
                }
            }
        }
    }
    return Either.forRight(result);
}
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) ArrayList(java.util.ArrayList) ArchetypeModelObject(com.nedap.archie.aom.ArchetypeModelObject) CAttribute(com.nedap.archie.aom.CAttribute) ValidationResult(com.nedap.archie.archetypevalidator.ValidationResult) BmmProperty(org.openehr.bmm.core.BmmProperty)

Example 2 with Archetype

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

the class ConvertToOptCommand method apply.

public void apply() {
    String documentUri = ((JsonPrimitive) params.getArguments().get(0)).getAsString();
    String format = ((JsonPrimitive) params.getArguments().get(1)).getAsString();
    String serializedOpt = 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);
    String extension;
    switch(format) {
        case "adl":
            extension = ".opt2";
            serializedOpt = ADLArchetypeSerializer.serialize(opt);
            break;
        case "json":
            extension = "_opt.json";
            try {
                serializedOpt = JacksonUtil.getObjectMapper().writeValueAsString(opt);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            break;
        case "xml":
            {
                extension = "_opt.xml";
                StringWriter sw = new StringWriter();
                try {
                    Marshaller marshaller = JAXBUtil.getArchieJAXBContext().createMarshaller();
                    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                    marshaller.marshal(opt, sw);
                } catch (JAXBException e) {
                    throw new RuntimeException(e);
                }
                serializedOpt = sw.toString();
                break;
            }
        // 
        default:
            throw new UnsupportedOperationException("unsupported format: " + format);
    }
    String uriToWrite = documentUri.substring(0, documentUri.lastIndexOf("/")) + "/opt/" + opt.getArchetypeId() + extension;
    textDocumentService.writeFile(uriToWrite, "opt in " + format, serializedOpt);
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) 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) OperationalTemplate(com.nedap.archie.aom.OperationalTemplate) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with Archetype

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

the class DocumentLinks method resolveLinkInternal.

/**
 * Re-resolve all document links.
 * TODO: add incremental compile?
 * @param repository
 * @param documentLink
 * @return
 */
private DocumentLink resolveLinkInternal(BroadcastingArchetypeRepository repository, DocumentLink documentLink) {
    if (documentLink.getData() != null) {
        String ref = (String) documentLink.getData();
        TextDocumentItem document = repository.getDocument(ref);
        if (document != null) {
            documentLink.setTarget(document.getUri());
            DocumentInformation information = repository.getDocumentInformation(document.getUri());
            if (information != null && information.getArchetypeId() != null) {
                Archetype archetype = repository.getArchetype(information.getArchetypeId());
                if (archetype != null && archetype.getOriginalLanguage() != null) {
                    ArchetypeTerm term = archetype.getTerm(archetype.getDefinition(), archetype.getOriginalLanguage().getCodeString());
                    if (term != null) {
                        documentLink.setTooltip(term.getText() + "\n" + term.getDescription());
                    }
                }
            }
        }
    }
    return documentLink;
}
Also used : TextDocumentItem(org.eclipse.lsp4j.TextDocumentItem) ArchetypeTerm(com.nedap.archie.aom.terminology.ArchetypeTerm) Archetype(com.nedap.archie.aom.Archetype)

Example 4 with Archetype

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

the class ADL14ConvertingStorage method addFile.

public void addFile(TextDocumentItem item) {
    ADL14Parser adl14Parser = new ADL14Parser(BuiltinReferenceModels.getMetaModels());
    Archetype archetype = null;
    try {
        archetype = adl14Parser.parse(item.getText(), configuration);
        textService.pushDiagnostics(new VersionedTextDocumentIdentifier(item.getUri(), item.getVersion()), null, new ValidationResult(archetype));
        adl14Files.put(item.getUri(), archetype);
    } catch (ADLParseException ex) {
        textService.pushDiagnostics(new VersionedTextDocumentIdentifier(item.getUri(), item.getVersion()), ex.getErrors());
    }
}
Also used : VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) Archetype(com.nedap.archie.aom.Archetype) ADL14Parser(com.nedap.archie.adl14.ADL14Parser) ValidationResult(com.nedap.archie.archetypevalidator.ValidationResult) ADLParseException(com.nedap.archie.adlparser.ADLParseException)

Example 5 with Archetype

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

the class BroadcastingArchetypeRepository method isDescendant.

private boolean isDescendant(Archetype archetype, String parent) {
    Archetype archetypeToCheck = archetype;
    int maxTreeDepth = 75;
    int i = 0;
    while (archetypeToCheck != null && i < maxTreeDepth && archetypeToCheck.getParentArchetypeId() != null) {
        if (archetypeToCheck.getParentArchetypeId().startsWith(parent)) {
            return true;
        }
        String parentArchetypeId = archetypeToCheck.getParentArchetypeId();
        archetypeToCheck = getArchetype(parentArchetypeId);
        i++;
    }
    return false;
}
Also used : Archetype(com.nedap.archie.aom.Archetype)

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