Search in sources :

Example 1 with PathSegment

use of com.nedap.archie.paths.PathSegment in project archetype-languageserver by nedap.

the class PartialAOMPathQuery method findLSPPartial.

// TODO: check if/how this is different from AOMPathQuery.findPartial, and potentially remove
public PartialMatch findLSPPartial(CComplexObject root) {
    List<ArchetypeModelObject> result = new ArrayList<>();
    boolean findThroughDifferentialPaths = true, matchSpecializedNodes = true;
    result.add(root);
    List<PathSegment> pathSegments = getPathSegments();
    for (int i = 0; i < pathSegments.size(); i++) {
        List<ArchetypeModelObject> previousResult = result;
        PathSegment segment = pathSegments.get(i);
        CAttribute differentialAttribute = null;
        if (findThroughDifferentialPaths) {
            differentialAttribute = findMatchingDifferentialPath(pathSegments.subList(i, pathSegments.size()), result);
        }
        if (differentialAttribute != null) {
            // skip a few pathsegments for this differential path match
            i = i + new APathQuery(differentialAttribute.getDifferentialPath()).getPathSegments().size() - 1;
            PathSegment lastPathSegment = pathSegments.get(i);
            ArchetypeModelObject oneMatchingObject = findOneMatchingObject(differentialAttribute, lastPathSegment, matchSpecializedNodes);
            if (oneMatchingObject != null) {
                result = Lists.newArrayList(oneMatchingObject);
            } else {
                result = findOneSegment(segment, result, matchSpecializedNodes);
            }
        } else {
            result = findOneSegment(segment, result, matchSpecializedNodes);
        }
        List<ArchetypeModelObject> returnValue = previousResult.stream().filter((object) -> object != null).collect(Collectors.toList());
        if (result.isEmpty()) {
            // we have to return our partial match here
            return new PartialMatch(pathSegments, pathSegments.subList(i, pathSegments.size()), returnValue);
        }
    }
    List<ArchetypeModelObject> returnValue = result.stream().filter((object) -> object != null).collect(Collectors.toList());
    return new PartialMatch(pathSegments, new ArrayList<>(), returnValue);
}
Also used : CAttribute(com.nedap.archie.aom.CAttribute) ArchetypeModelObject(com.nedap.archie.aom.ArchetypeModelObject) List(java.util.List) Lists(com.google.common.collect.Lists) AOMPathQuery(com.nedap.archie.query.AOMPathQuery) CComplexObject(com.nedap.archie.aom.CComplexObject) PathSegment(com.nedap.archie.paths.PathSegment) APathQuery(com.nedap.archie.query.APathQuery) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) APathQuery(com.nedap.archie.query.APathQuery) ArrayList(java.util.ArrayList) ArchetypeModelObject(com.nedap.archie.aom.ArchetypeModelObject) PathSegment(com.nedap.archie.paths.PathSegment) CAttribute(com.nedap.archie.aom.CAttribute)

Example 2 with PathSegment

use of com.nedap.archie.paths.PathSegment in project archetype-languageserver by nedap.

the class DocumentInformation method checkDifferentialMatch.

private boolean checkDifferentialMatch(List<PathSegment> pathSegments, List<PathSegment> differentialPathSegments) {
    if (differentialPathSegments.size() <= pathSegments.size()) {
        for (int i = 0; i < differentialPathSegments.size(); i++) {
            PathSegment segment = pathSegments.get(i);
            PathSegment differentialPathSegment = differentialPathSegments.get(i);
            if (!matches(segment, differentialPathSegment)) {
                return false;
            }
        }
        return true;
    }
    return false;
}
Also used : PathSegment(com.nedap.archie.paths.PathSegment)

Example 3 with PathSegment

use of com.nedap.archie.paths.PathSegment 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 PathSegment

use of com.nedap.archie.paths.PathSegment 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 PathSegment

use of com.nedap.archie.paths.PathSegment in project archetype-languageserver by nedap.

the class DocumentInformation method lookupCObjectOrAttribute.

/**
 * Archetype path lookup. Definition only for now. Sorry for the ugly code, but this works rather well :)
 * @param path
 * @return
 */
public DocumentSymbol lookupCObjectOrAttribute(String path, boolean returnResultSoFar) {
    APathQuery query = new APathQuery(path);
    List<PathSegment> pathSegments = query.getPathSegments();
    List<DocumentSymbol> documentSymbols = DocumentSymbolUtils.getDocumentSymbols(symbols);
    if (documentSymbols.isEmpty()) {
        return null;
    }
    DocumentSymbol definitionSections = DocumentSymbolUtils.getDocumentSymbolOrThrow(documentSymbols.get(0).getChildren(), DEFINITION_SECTION_NAME);
    DocumentSymbol rootNode = definitionSections.getChildren().get(0);
    DocumentSymbol currentSymbol = rootNode;
    for (int i = 0; i < pathSegments.size(); i++) {
        PathSegment segment = pathSegments.get(i);
        // search attribute
        if (currentSymbol.getChildren() == null || currentSymbol.getChildren().isEmpty()) {
            return currentSymbol;
        }
        {
            DocumentSymbol foundAttribute = findAttribute(currentSymbol, segment, pathSegments, i);
            if (foundAttribute == null) {
                return returnResultSoFar ? currentSymbol : null;
            } else if (foundAttribute.getName().startsWith("/")) {
                // differential path. Skip a couple of path segments
                int numberOfSegments = new APathQuery(foundAttribute.getName()).getPathSegments().size();
                i = i + numberOfSegments - 1;
                segment = pathSegments.get(i);
            }
            currentSymbol = foundAttribute;
        }
        if (currentSymbol.getChildren() == null || currentSymbol.getChildren().isEmpty()) {
            return returnCurrentSymbolIfPossible(returnResultSoFar, pathSegments, currentSymbol, i);
        }
        if (segment.getIndex() != null) {
            if (segment.getIndex() > currentSymbol.getChildren().size()) {
                return returnResultSoFar ? currentSymbol : null;
            }
            currentSymbol = currentSymbol.getChildren().get(segment.getIndex() - 1);
        } else if (segment.hasIdCode() || segment.hasArchetypeRef()) {
            DocumentSymbol foundCObject = findCObject(currentSymbol, segment);
            if (foundCObject == null) {
                return returnCurrentSymbolIfPossible(returnResultSoFar, pathSegments, currentSymbol, i);
            }
            currentSymbol = foundCObject;
        } else {
            if (i == pathSegments.size() - 1) {
                // this just points at an attribute, that's ok.
                return returnCurrentSymbolIfPossible(returnResultSoFar, pathSegments, currentSymbol, i);
            } else // let's hope it's size 1
            if (currentSymbol.getChildren() == null || currentSymbol.getChildren().isEmpty()) {
                return returnCurrentSymbolIfPossible(returnResultSoFar, pathSegments, currentSymbol, i);
            } else if (currentSymbol.getChildren().size() == 1) {
                currentSymbol = currentSymbol.getChildren().get(0);
            } else {
                throw new RuntimeException("cannot find path, it has too many possible values without an id at segment " + segment);
            }
        }
    }
    return currentSymbol;
}
Also used : APathQuery(com.nedap.archie.query.APathQuery) DocumentSymbol(org.eclipse.lsp4j.DocumentSymbol) PathSegment(com.nedap.archie.paths.PathSegment)

Aggregations

PathSegment (com.nedap.archie.paths.PathSegment)5 ArchetypeModelObject (com.nedap.archie.aom.ArchetypeModelObject)3 CAttribute (com.nedap.archie.aom.CAttribute)3 Archetype (com.nedap.archie.aom.Archetype)2 CObject (com.nedap.archie.aom.CObject)2 ValidationResult (com.nedap.archie.archetypevalidator.ValidationResult)2 APathQuery (com.nedap.archie.query.APathQuery)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 Lists (com.google.common.collect.Lists)1 CComplexObject (com.nedap.archie.aom.CComplexObject)1 AOMPathQuery (com.nedap.archie.query.AOMPathQuery)1 MetaModels (com.nedap.archie.rminfo.MetaModels)1 HoverInfo (com.nedap.openehr.lsp.document.HoverInfo)1 Collections (java.util.Collections)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 DocumentSymbol (org.eclipse.lsp4j.DocumentSymbol)1