Search in sources :

Example 1 with ValidationResult

use of com.nedap.archie.archetypevalidator.ValidationResult 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 ValidationResult

use of com.nedap.archie.archetypevalidator.ValidationResult 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 3 with ValidationResult

use of com.nedap.archie.archetypevalidator.ValidationResult 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 4 with ValidationResult

use of com.nedap.archie.archetypevalidator.ValidationResult 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 5 with ValidationResult

use of com.nedap.archie.archetypevalidator.ValidationResult in project archetype-languageserver by nedap.

the class BroadcastingArchetypeRepository method extractADL2Info.

private void extractADL2Info(TextDocumentItem textDocumentItem) {
    try {
        ADL2SymbolExtractor adl2SymbolExtractor = new ADL2SymbolExtractor();
        DocumentInformation documentInformation = adl2SymbolExtractor.extractSymbols(textDocumentItem.getUri(), textDocumentItem.getText());
        symbolsByUri.put(textDocumentItem.getUri(), documentInformation);
        if (documentInformation.getArchetypeId() != null) {
            documentsByArchetypeId.put(documentInformation.getArchetypeId(), textDocumentItem);
        }
        if (documentInformation.getErrors().hasNoErrors()) {
            ADLParser adlParser = new ADLParser(BuiltinReferenceModels.getMetaModels());
            // no console output please :)
            adlParser.setLogEnabled(false);
            Archetype archetype = null;
            try {
                archetype = adlParser.parse(textDocumentItem.getText());
                addArchetype(archetype);
                // perform incremental compilation here
                invalidateAndRecompileArchetypes(archetype);
                ValidationResult result = getValidationResult(archetype.getArchetypeId().toString());
                Archetype archetypeForTerms = archetype;
                if (result != null && result.getFlattened() != null) {
                    archetypeForTerms = result.getFlattened();
                }
                String language = archetype.getOriginalLanguage() != null ? archetype.getOriginalLanguage().getCodeString() : null;
                if (language == null) {
                    language = "en";
                }
                documentInformation.setHoverInfo(new ArchetypeHoverInfo(documentInformation, archetype, archetypeForTerms, language));
                SymbolNameFromTerminologyHelper.giveNames(documentInformation.getSymbols(), archetypeForTerms, language);
            // diagnostics will now be pushed from within the invalidateArchetypesAndRecompile method
            } catch (ADLParseException e) {
                // this should have been checked in the previous step. But still, it could happen.
                textDocumentService.pushDiagnostics(new VersionedTextDocumentIdentifier(textDocumentItem.getUri(), textDocumentItem.getVersion()), e.getErrors());
            } catch (Exception ex) {
                // this particular exce[tion is a parse error, usually when extracting JSON. be sure to post taht
                textDocumentService.pushDiagnostics(new VersionedTextDocumentIdentifier(textDocumentItem.getUri(), textDocumentItem.getVersion()), ex);
            }
        } else {
            textDocumentService.pushDiagnostics(new VersionedTextDocumentIdentifier(textDocumentItem.getUri(), textDocumentItem.getVersion()), documentInformation.getErrors());
        }
    } catch (IOException e) {
        // shouldn't happen, ever, just in memory processing
        throw new RuntimeException(e);
    }
}
Also used : ADLParser(com.nedap.archie.adlparser.ADLParser) ADL2SymbolExtractor(com.nedap.openehr.lsp.symbolextractor.ADL2SymbolExtractor) Archetype(com.nedap.archie.aom.Archetype) DocumentInformation(com.nedap.openehr.lsp.document.DocumentInformation) IOException(java.io.IOException) ValidationResult(com.nedap.archie.archetypevalidator.ValidationResult) ArchetypeHoverInfo(com.nedap.openehr.lsp.document.ArchetypeHoverInfo) ADLParseException(com.nedap.archie.adlparser.ADLParseException) IOException(java.io.IOException) ADLParseException(com.nedap.archie.adlparser.ADLParseException)

Aggregations

Archetype (com.nedap.archie.aom.Archetype)5 ValidationResult (com.nedap.archie.archetypevalidator.ValidationResult)5 ArchetypeModelObject (com.nedap.archie.aom.ArchetypeModelObject)3 CAttribute (com.nedap.archie.aom.CAttribute)3 CObject (com.nedap.archie.aom.CObject)3 ADLParseException (com.nedap.archie.adlparser.ADLParseException)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 ArrayList (java.util.ArrayList)2 BmmClass (org.openehr.bmm.core.BmmClass)2 BmmProperty (org.openehr.bmm.core.BmmProperty)2 ADL14Parser (com.nedap.archie.adl14.ADL14Parser)1 ADLParser (com.nedap.archie.adlparser.ADLParser)1 ArchetypeHoverInfo (com.nedap.openehr.lsp.document.ArchetypeHoverInfo)1 DocumentInformation (com.nedap.openehr.lsp.document.DocumentInformation)1 HoverInfo (com.nedap.openehr.lsp.document.HoverInfo)1 ADL2SymbolExtractor (com.nedap.openehr.lsp.symbolextractor.ADL2SymbolExtractor)1