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();
}
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();
}
Aggregations