use of org.drools.mvel.compiler.oopath.model.TMFile in project drools by kiegroup.
the class OOPathTest method testCollectionIteratorRemove.
@Test
public void testCollectionIteratorRemove() {
final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "\n" + "rule R2 when\n" + " TMDirectory( $id: name, $p: /files[size >= 100] )\n" + "then\n" + " insertLogical( $id + \".\" + $p.getName() );\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession();
final TMDirectory x = new TMDirectory("X");
final TMDirectory y = new TMDirectory("Y");
final TMFile file0 = new TMFile("File0", 999);
final TMFile file1 = new TMFile("File1", 999);
final TMFile file2 = new TMFile("File2", 999);
x.getFiles().addAll(Arrays.asList(file0, file1, file2));
y.getFiles().addAll(Arrays.asList(file0, file1, file2));
ksession.insert(x);
ksession.insert(y);
ksession.fireAllRules();
assertTrue(factsCollection(ksession).contains("X.File0"));
assertTrue(factsCollection(ksession).contains("X.File1"));
assertTrue(factsCollection(ksession).contains("X.File2"));
assertTrue(factsCollection(ksession).contains("Y.File0"));
assertTrue(factsCollection(ksession).contains("Y.File1"));
assertTrue(factsCollection(ksession).contains("Y.File2"));
java.util.Iterator<TMFile> iterator = x.getFiles().iterator();
iterator.next();
iterator.remove();
ksession.fireAllRules();
assertFalse(factsCollection(ksession).contains("X.File0"));
assertTrue(factsCollection(ksession).contains("X.File1"));
assertTrue(factsCollection(ksession).contains("X.File2"));
assertTrue(factsCollection(ksession).contains("Y.File0"));
assertTrue(factsCollection(ksession).contains("Y.File1"));
assertTrue(factsCollection(ksession).contains("Y.File2"));
iterator.next();
iterator.remove();
ksession.fireAllRules();
assertFalse(factsCollection(ksession).contains("X.File0"));
assertFalse(factsCollection(ksession).contains("X.File1"));
assertTrue(factsCollection(ksession).contains("X.File2"));
assertTrue(factsCollection(ksession).contains("Y.File0"));
assertTrue(factsCollection(ksession).contains("Y.File1"));
assertTrue(factsCollection(ksession).contains("Y.File2"));
iterator.next();
iterator.remove();
ksession.fireAllRules();
assertFalse(factsCollection(ksession).contains("X.File0"));
assertFalse(factsCollection(ksession).contains("X.File1"));
assertFalse(factsCollection(ksession).contains("X.File2"));
assertTrue(factsCollection(ksession).contains("Y.File0"));
assertTrue(factsCollection(ksession).contains("Y.File1"));
assertTrue(factsCollection(ksession).contains("Y.File2"));
assertFalse(iterator.hasNext());
}
use of org.drools.mvel.compiler.oopath.model.TMFile in project drools by kiegroup.
the class OOPathTest method testRemoveIfSupport.
@Test
public void testRemoveIfSupport() {
final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "\n" + "rule R2 when\n" + " TMDirectory( $id: name, $p: /files[size >= 100] )\n" + "then\n" + " insertLogical( $id + \".\" + $p.getName() );\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession();
ksession.addEventListener(new DefaultRuleRuntimeEventListener() {
@Override
public void objectDeleted(ObjectDeletedEvent event) {
System.out.println(event.getOldObject() + " -> " + "_");
}
@Override
public void objectInserted(ObjectInsertedEvent event) {
System.out.println("_" + " -> " + event.getObject());
}
@Override
public void objectUpdated(ObjectUpdatedEvent event) {
System.out.println(event.getOldObject() + " -> " + event.getObject());
}
});
final TMDirectory x = new TMDirectory("X");
final TMDirectory y = new TMDirectory("Y");
ksession.insert(x);
ksession.insert(y);
ksession.fireAllRules();
assertFalse(factsCollection(ksession).contains("X.File0"));
assertFalse(factsCollection(ksession).contains("X.File1"));
assertFalse(factsCollection(ksession).contains("X.File2"));
assertFalse(factsCollection(ksession).contains("Y.File0"));
assertFalse(factsCollection(ksession).contains("Y.File1"));
assertFalse(factsCollection(ksession).contains("Y.File2"));
final TMFile file0 = new TMFile("File0", 1000);
final TMFile file1 = new TMFile("File1", 1001);
final TMFile file2 = new TMFile("File2", 1002);
x.getFiles().addAll(Arrays.asList(file0, file1, file2));
y.getFiles().addAll(Arrays.asList(file0, file1, file2));
ksession.fireAllRules();
assertTrue(factsCollection(ksession).contains("X.File0"));
assertTrue(factsCollection(ksession).contains("X.File1"));
assertTrue(factsCollection(ksession).contains("X.File2"));
assertTrue(factsCollection(ksession).contains("Y.File0"));
assertTrue(factsCollection(ksession).contains("Y.File1"));
assertTrue(factsCollection(ksession).contains("Y.File2"));
x.getFiles().removeIf(f -> f.getSize() % 2 == 0);
y.getFiles().removeIf(f -> f.getSize() % 2 == 1);
ksession.fireAllRules();
assertFalse(factsCollection(ksession).contains("X.File0"));
assertTrue(factsCollection(ksession).contains("X.File1"));
assertFalse(factsCollection(ksession).contains("X.File2"));
assertTrue(factsCollection(ksession).contains("Y.File0"));
assertFalse(factsCollection(ksession).contains("Y.File1"));
assertTrue(factsCollection(ksession).contains("Y.File2"));
}
use of org.drools.mvel.compiler.oopath.model.TMFile in project drools by kiegroup.
the class OOPathTest method testMiscSetMethods.
@Test
public void testMiscSetMethods() {
final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "\n" + "rule R2 when\n" + " TMFileSet( $id: name, $p: /files[size >= 100] )\n" + "then\n" + " insertLogical( $id + \".\" + $p.getName() );\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession();
final TMFileSet x = new TMFileSet("X");
final TMFileSet y = new TMFileSet("Y");
ksession.insert(x);
ksession.insert(y);
ksession.fireAllRules();
assertFalse(factsCollection(ksession).contains("X.File0"));
assertFalse(factsCollection(ksession).contains("X.File1"));
assertFalse(factsCollection(ksession).contains("Y.File0"));
assertFalse(factsCollection(ksession).contains("Y.File1"));
final TMFile file0 = new TMFile("File0", 47);
final TMFile file1 = new TMFile("File1", 47);
final TMFile file2 = new TMFile("File2", 47);
x.getFiles().add(file2);
x.getFiles().addAll(Arrays.asList(file0, file1));
y.getFiles().add(file2);
y.getFiles().add(file0);
y.getFiles().add(file1);
ksession.fireAllRules();
assertFalse(factsCollection(ksession).contains("X.File0"));
assertFalse(factsCollection(ksession).contains("X.File1"));
assertFalse(factsCollection(ksession).contains("X.File2"));
assertFalse(factsCollection(ksession).contains("Y.File0"));
assertFalse(factsCollection(ksession).contains("Y.File1"));
assertFalse(factsCollection(ksession).contains("Y.File2"));
file0.setSize(999);
ksession.fireAllRules();
assertTrue(factsCollection(ksession).contains("X.File0"));
assertFalse(factsCollection(ksession).contains("X.File1"));
assertFalse(factsCollection(ksession).contains("X.File2"));
assertTrue(factsCollection(ksession).contains("Y.File0"));
assertFalse(factsCollection(ksession).contains("Y.File1"));
assertFalse(factsCollection(ksession).contains("Y.File2"));
// removing File1 from Y
y.getFiles().remove(file1);
file1.setSize(999);
ksession.fireAllRules();
assertTrue(factsCollection(ksession).contains("X.File0"));
assertTrue(factsCollection(ksession).contains("X.File1"));
assertFalse(factsCollection(ksession).contains("X.File2"));
assertTrue(factsCollection(ksession).contains("Y.File0"));
assertFalse(factsCollection(ksession).contains("Y.File1"));
assertFalse(factsCollection(ksession).contains("Y.File2"));
file2.setSize(999);
ksession.fireAllRules();
assertTrue(factsCollection(ksession).contains("X.File0"));
assertTrue(factsCollection(ksession).contains("X.File1"));
assertTrue(factsCollection(ksession).contains("X.File2"));
assertTrue(factsCollection(ksession).contains("Y.File0"));
assertFalse(factsCollection(ksession).contains("Y.File1"));
assertTrue(factsCollection(ksession).contains("Y.File2"));
}
use of org.drools.mvel.compiler.oopath.model.TMFile in project drools by kiegroup.
the class OOPathTest method testDereferencedDeclarationOutsideOOPath.
@Test
public void testDereferencedDeclarationOutsideOOPath() {
// DROOLS-1411
final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "global java.util.Set duplicateNames; \n" + "\n" + "rule DIFF_FILES_BUT_WITH_SAME_FILENAME when\n" + " $dir1 : TMFileSet( $ic1 : /files )\n" + " TMFileSet( this == $dir1, $ic2 : /files[this != $ic1], $ic2.name == $ic1.name )\n" + "then\n" + " duplicateNames.add( $ic1.getName() );\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession();
final Set<String> duplicateNames = new HashSet<>();
ksession.setGlobal("duplicateNames", duplicateNames);
final TMFileSet x = new TMFileSet("X");
final TMFile file0 = new TMFile("File0", 47);
final TMFile file1 = new TMFile("File1", 47);
final TMFile file2 = new TMFile("File0", 47);
x.getFiles().addAll(Arrays.asList(file0, file1, file2));
ksession.insert(x);
ksession.fireAllRules();
assertTrue(duplicateNames.contains("File0"));
assertFalse(duplicateNames.contains("File1"));
}
Aggregations