use of il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.Encapsulator 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.Encapsulator in project Main by SpartanRefactoring.
the class MatcherTest method testMatch.
/**
* constraints(){
* element(1).isNot( () -> {
* if(booleanExpression(3){
* statement(4);
* }
* });
* element(2).is(() -> {
* return null;
* });
* element(3).isNot(() -> !booleanExpression(5));
* element(1).isNot(() -> {
* return null;
* });
* }
*
*
*
* matcher()
* if(booleanExpression(0){
* statement(1);
* statement(2);
* }
* @throws Exception
*/
public void testMatch() throws Exception {
Map<Integer, List<Matcher.Constraint>> constrains = new HashMap<>();
PsiIfStatement ifs = createTestIfStatement("booleanExpression(0)", "statement(1);\n statement(2);");
Encapsulator n = buildTemplate(ifs);
constrains.putIfAbsent(1, new LinkedList<>());
constrains.putIfAbsent(2, new LinkedList<>());
constrains.putIfAbsent(3, new LinkedList<>());
Encapsulator firstConstraint = buildTemplate(createTestIfStatement("booleanExpression(3)", "statement(4);"));
Encapsulator secondConstraint = buildTemplate(createTestReturnStatement("null"));
Encapsulator thirdConstraint = buildTemplate(createTestExpression("!booleanExpression(5)"));
Encapsulator forthConstraint = buildTemplate(createTestReturnStatement("null"));
constrains.get(1).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.ISNOT, Utils.wrapWithList(firstConstraint)));
constrains.get(2).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.IS, Utils.wrapWithList(secondConstraint)));
constrains.get(3).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.ISNOT, Utils.wrapWithList(thirdConstraint)));
constrains.get(1).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.ISNOT, Utils.wrapWithList(forthConstraint)));
Matcher m = new Matcher(Utils.wrapWithList(n), constrains);
PsiIfStatement tm1 = createTestIfStatement("x > 2", "\nx++; \nreturn null;");
assertTrue(m.match(tm1));
PsiIfStatement tm2 = createTestIfStatement("x > 2", "\nif(!(x > 4)){x--;} \nreturn null;");
assertTrue(m.match(tm2));
PsiIfStatement tm3 = createTestIfStatement("x > 2", "\nx++; \nx--;");
assertFalse(m.match(tm3));
PsiIfStatement tm4 = createTestIfStatement("x > 2", "\nreturn null; \nreturn null;");
assertFalse(m.match(tm4));
PsiIfStatement tm5 = createTestIfStatement("x > 2", "\nif(x < 3){x--;} \nreturn null;");
assertFalse(m.match(tm5));
PsiIfStatement tm6 = createTestIfStatement("x > 2", "\nif(x > 4){x--;} \nreturn null;");
assertFalse(m.match(tm6));
}
use of il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.Encapsulator in project Main by SpartanRefactoring.
the class MatcherTest method testExtractInfo.
public void testExtractInfo() throws Exception {
Map<Integer, List<Matcher.Constraint>> constrains = new HashMap<>();
PsiIfStatement ifs = createTestIfStatement("booleanExpression(0)", "statement(1);\n statement(2);");
Encapsulator n = buildTemplate(ifs);
constrains.putIfAbsent(1, new LinkedList<>());
constrains.putIfAbsent(2, new LinkedList<>());
constrains.putIfAbsent(3, new LinkedList<>());
Encapsulator firstConstraint = buildTemplate(createTestIfStatement("booleanExpression(3)", "statement(4);"));
Encapsulator secondConstraint = buildTemplate(createTestReturnStatement("null"));
Encapsulator thirdConstraint = buildTemplate(createTestExpression("!booleanExpression(5)"));
Encapsulator forthConstraint = buildTemplate(createTestReturnStatement("null"));
constrains.get(1).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.ISNOT, Utils.wrapWithList(firstConstraint)));
constrains.get(2).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.IS, Utils.wrapWithList(secondConstraint)));
constrains.get(3).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.ISNOT, Utils.wrapWithList(thirdConstraint)));
constrains.get(1).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.ISNOT, Utils.wrapWithList(forthConstraint)));
Matcher m = new Matcher(Utils.wrapWithList(n), constrains);
PsiIfStatement tm1 = createTestIfStatement("x > 2", "\nx++; \nreturn null;");
Wrapper<Integer> i = new Wrapper<>(0);
Map<Integer, List<PsiElement>> map = m.extractInfo(tm1, i);
assertEquals(map.get(0).get(0).getText(), "x > 2");
assertEquals(map.get(1).get(0).getText(), "x++;");
assertEquals(map.get(2).get(0).getText(), "return null;");
PsiIfStatement tm2 = createTestIfStatement("x > 2", "\nif(!(x > 4)){x--;} \nreturn null;");
map = m.extractInfo(tm2, i);
assertEquals(map.get(1).get(0).getText(), "if(!(x > 4)){x--;}");
assertEquals(map.get(2).get(0).getText(), "return null;");
PsiIfStatement tm3 = createTestIfStatement("x > 2", "\nx++; \nx--;");
map = m.extractInfo(tm3, i);
assertEquals(map.get(1).get(0).getText(), "x++;");
assertEquals(map.get(2).get(0).getText(), "x--;");
PsiIfStatement tm4 = createTestIfStatement("x > 2", "\nreturn null; \nreturn null;");
map = m.extractInfo(tm4, i);
assertEquals(map.get(1).get(0).getText(), "return null;");
assertEquals(map.get(2).get(0).getText(), "return null;");
PsiIfStatement tm5 = createTestIfStatement("x > 2", "\nif(x < 3){x--;} \nreturn null;");
map = m.extractInfo(tm5, i);
assertEquals(map.get(1).get(0).getText(), "if(x < 3){x--;}");
assertEquals(map.get(2).get(0).getText(), "return null;");
PsiIfStatement tm6 = createTestIfStatement("x > 2", "\nif(x > 4){x--;} \nreturn null;");
map = m.extractInfo(tm6, i);
assertEquals(map.get(1).get(0).getText(), "if(x > 4){x--;}");
assertEquals(map.get(2).get(0).getText(), "return null;");
}
use of il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.Encapsulator in project Main by SpartanRefactoring.
the class PruningTest method testPruneStatement.
public void testPruneStatement() throws Exception {
Encapsulator root = Encapsulator.buildTreeFromPsi(createTestIfStatement("x > 2", "statement(0);"));
Encapsulator pruned = Pruning.prune(root, new HashMap<>());
assertEquals(pruned.getChildren().size(), 7);
assertTrue(iz.generic(pruned.getChildren().get(6).getChildren().get(0).getChildren().get(1)));
assertFalse(iz.generic(pruned.getChildren().get(3)));
}
use of il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.Encapsulator in project Main by SpartanRefactoring.
the class PruningTest method testPruneExpression.
public void testPruneExpression() throws Exception {
Encapsulator root = Encapsulator.buildTreeFromPsi(createTestIfStatement("x > 2", "expression(0);"));
Encapsulator pruned = Pruning.prune(root, new HashMap<>());
assertEquals(pruned.getChildren().size(), 7);
assertTrue(iz.generic(pruned.getChildren().get(6).getChildren().get(0).getChildren().get(1).getChildren().get(0)));
assertFalse(iz.generic(pruned.getChildren().get(3)));
}
Aggregations