Search in sources :

Example 1 with EncapsulatorIterator

use of il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.EncapsulatorIterator in project Main by SpartanRefactoring.

the class Matcher method getMatchingResult.

public MatchingResult getMatchingResult(PsiElement treeToMatch, Wrapper<Integer> numberOfNeighbors) {
    MatchingResult mr = new MatchingResult(false);
    for (int i = 1; i <= Utils.getNumberOfRootsPossible(treeToMatch); i++) {
        List<Encapsulator> potentialRoots = new ArrayList<>();
        PsiElement c = treeToMatch;
        MatchingResult m = new MatchingResult(true);
        for (int j = 0; j < i; j++) {
            potentialRoots.add(Encapsulator.buildTreeFromPsi(c));
            c = c.getNextSibling();
        }
        m.combineWith(matchingRecursion(new EncapsulatorIterator(roots), new EncapsulatorIterator(potentialRoots)));
        if (m.matches()) {
            mr.combineWith(m);
            mr.setMatches();
            numberOfNeighbors.set(i);
            break;
        }
    }
    if (mr.notMatches())
        return mr;
    Map<Integer, List<PsiElement>> info = mr.getMap();
    return info.keySet().stream().allMatch(id -> constrains.getOrDefault(id, new LinkedList<>()).stream().allMatch(c -> info.get(id).stream().allMatch(c::match))) ? mr : mr.setNotMatches();
}
Also used : Encapsulator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.Encapsulator) Utils(il.org.spartan.Leonidas.auxilary_layer.Utils) java.util(java.util) Wrapper(il.org.spartan.Leonidas.auxilary_layer.Wrapper) Leonidas.auxilary_layer.iz(il.org.spartan.Leonidas.auxilary_layer.iz) PsiElement(com.intellij.psi.PsiElement) GenericEncapsulator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.GenericEncapsulator) Leonidas.auxilary_layer.az(il.org.spartan.Leonidas.auxilary_layer.az) Collectors(java.util.stream.Collectors) EncapsulatorIterator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.EncapsulatorIterator) InvocationTargetException(java.lang.reflect.InvocationTargetException) Encapsulator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.Encapsulator) GenericEncapsulator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.GenericEncapsulator) PsiElement(com.intellij.psi.PsiElement) EncapsulatorIterator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.EncapsulatorIterator)

Example 2 with EncapsulatorIterator

use of il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.EncapsulatorIterator in project Main by SpartanRefactoring.

the class Matcher method matchingRecursion.

/**
     * Goes through each and every quantifier and tries all possible assignments for this quantifiers.
     *
     * @return MatchingResult false if the trees don't match and MatchingResult true with mapping between identifier
     * of generic elements to its instances if the trees match.
     */
@UpdatesIterator
private MatchingResult matchingRecursion(EncapsulatorIterator needle, EncapsulatorIterator cursor) {
    MatchingResult m = new MatchingResult(true);
    // base ground iterators
    EncapsulatorIterator bgNeedle = needle.clone(), bgCursor = cursor.clone();
    m.combineWith(matchBead(bgNeedle, bgCursor));
    if (m.notMatches())
        return m;
    if (!bgNeedle.hasNext() && !bgCursor.hasNext())
        return m.setMatches();
    // variant iterator for each attempt to match quantifier
    EncapsulatorIterator varNeedle, varCursor;
    if (iz.quantifier(bgNeedle.value())) {
        int n = az.quantifier(bgNeedle.value()).getNumberOfOccurrences(bgCursor);
        for (int i = 0; i <= n; i++) {
            varNeedle = bgNeedle.clone();
            varCursor = bgCursor.clone();
            varNeedle.setNumberOfOccurrences(i);
            MatchingResult mq = matchQuantifier(varNeedle, varCursor);
            if (mq.notMatches())
                continue;
            MatchingResult sr = matchingRecursion(varNeedle, varCursor);
            if (sr.notMatches())
                continue;
            return m.combineWith(mq).combineWith(sr);
        }
    }
    return m.setNotMatches();
}
Also used : EncapsulatorIterator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.EncapsulatorIterator)

Aggregations

EncapsulatorIterator (il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.EncapsulatorIterator)2 PsiElement (com.intellij.psi.PsiElement)1 Utils (il.org.spartan.Leonidas.auxilary_layer.Utils)1 Wrapper (il.org.spartan.Leonidas.auxilary_layer.Wrapper)1 Leonidas.auxilary_layer.az (il.org.spartan.Leonidas.auxilary_layer.az)1 Leonidas.auxilary_layer.iz (il.org.spartan.Leonidas.auxilary_layer.iz)1 Encapsulator (il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.Encapsulator)1 GenericEncapsulator (il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.GenericEncapsulator)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1