Search in sources :

Example 1 with APathQuery

use of com.nedap.archie.query.APathQuery 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 APathQuery

use of com.nedap.archie.query.APathQuery 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)2 APathQuery (com.nedap.archie.query.APathQuery)2 Lists (com.google.common.collect.Lists)1 ArchetypeModelObject (com.nedap.archie.aom.ArchetypeModelObject)1 CAttribute (com.nedap.archie.aom.CAttribute)1 CComplexObject (com.nedap.archie.aom.CComplexObject)1 AOMPathQuery (com.nedap.archie.query.AOMPathQuery)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 DocumentSymbol (org.eclipse.lsp4j.DocumentSymbol)1