Search in sources :

Example 1 with TMFile

use of org.drools.mvel.compiler.oopath.model.TMFile in project drools by kiegroup.

the class OOPathTest method testMiscListMethods.

@Test
public void testMiscListMethods() {
    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");
    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(0, Arrays.asList(file0, file1));
    y.getFiles().add(0, file2);
    y.getFiles().add(0, file0);
    y.getFiles().add(1, 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(1);
    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"));
    final TMFile file0R = new TMFile("File0R", 999);
    x.getFiles().set(0, file0R);
    ksession.fireAllRules();
    assertFalse(factsCollection(ksession).contains("X.File0"));
    assertTrue(factsCollection(ksession).contains("X.File0R"));
    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"));
}
Also used : TMFile(org.drools.mvel.compiler.oopath.model.TMFile) KieBase(org.kie.api.KieBase) TMDirectory(org.drools.mvel.compiler.oopath.model.TMDirectory) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 2 with TMFile

use of org.drools.mvel.compiler.oopath.model.TMFile in project drools by kiegroup.

the class OOPathTest method testListIteratorRemove.

@Test
public void testListIteratorRemove() {
    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");
    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"));
    final ListIterator<TMFile> xIterator = x.getFiles().listIterator(1);
    final ListIterator<TMFile> yIterator = y.getFiles().listIterator();
    xIterator.next();
    xIterator.remove();
    yIterator.next();
    yIterator.next();
    yIterator.next();
    yIterator.remove();
    ksession.fireAllRules();
    assertTrue(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"));
    assertFalse(factsCollection(ksession).contains("Y.File2"));
    assertTrue(xIterator.hasNext());
    assertTrue(xIterator.hasPrevious());
    assertFalse(yIterator.hasNext());
    assertTrue(yIterator.hasPrevious());
    xIterator.next();
    xIterator.remove();
    yIterator.previous();
    yIterator.remove();
    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"));
    assertFalse(xIterator.hasNext());
    assertTrue(xIterator.hasPrevious());
    assertFalse(yIterator.hasNext());
    assertTrue(yIterator.hasPrevious());
    xIterator.previous();
    xIterator.remove();
    yIterator.previous();
    yIterator.remove();
    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"));
    assertFalse(xIterator.hasNext());
    assertFalse(xIterator.hasPrevious());
    assertFalse(yIterator.hasNext());
    assertFalse(yIterator.hasPrevious());
}
Also used : TMFile(org.drools.mvel.compiler.oopath.model.TMFile) ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) KieBase(org.kie.api.KieBase) TMDirectory(org.drools.mvel.compiler.oopath.model.TMDirectory) KieSession(org.kie.api.runtime.KieSession) ObjectUpdatedEvent(org.kie.api.event.rule.ObjectUpdatedEvent) ObjectInsertedEvent(org.kie.api.event.rule.ObjectInsertedEvent) DefaultRuleRuntimeEventListener(org.kie.api.event.rule.DefaultRuleRuntimeEventListener) Test(org.junit.Test)

Example 3 with TMFile

use of org.drools.mvel.compiler.oopath.model.TMFile in project drools by kiegroup.

the class OOPathTest method testDeclarationInsideOOPath.

@Test
public void testDeclarationInsideOOPath() {
    // 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[name == $ic1.name, this != $ic1] )\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"));
}
Also used : TMFile(org.drools.mvel.compiler.oopath.model.TMFile) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) TMFileSet(org.drools.mvel.compiler.oopath.model.TMFileSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with TMFile

use of org.drools.mvel.compiler.oopath.model.TMFile in project drools by kiegroup.

the class OOPathTest method testDeclarationOutsideOOPath.

@Test
public void testDeclarationOutsideOOPath() {
    // 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[name == $ic1.name], $ic2 != $ic1 )\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"));
}
Also used : TMFile(org.drools.mvel.compiler.oopath.model.TMFile) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) TMFileSet(org.drools.mvel.compiler.oopath.model.TMFileSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with TMFile

use of org.drools.mvel.compiler.oopath.model.TMFile in project drools by kiegroup.

the class OOPathTest method testListIteratorMisc.

@Test
public void testListIteratorMisc() {
    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", 999);
    final TMFile file1 = new TMFile("File1", 999);
    final TMFile file2 = new TMFile("File2", 999);
    final ListIterator<TMFile> xIterator = x.getFiles().listIterator();
    final ListIterator<TMFile> yIterator = y.getFiles().listIterator();
    xIterator.add(file0);
    yIterator.add(file2);
    ksession.fireAllRules();
    assertTrue(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"));
    assertTrue(factsCollection(ksession).contains("Y.File2"));
    xIterator.add(file1);
    yIterator.previous();
    yIterator.add(file1);
    ksession.fireAllRules();
    assertTrue(factsCollection(ksession).contains("X.File0"));
    assertTrue(factsCollection(ksession).contains("X.File1"));
    assertFalse(factsCollection(ksession).contains("X.File2"));
    assertFalse(factsCollection(ksession).contains("Y.File0"));
    assertTrue(factsCollection(ksession).contains("Y.File1"));
    assertTrue(factsCollection(ksession).contains("Y.File2"));
    xIterator.add(file2);
    yIterator.previous();
    yIterator.add(file0);
    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"));
    System.out.println(x.getFiles());
    System.out.println(y.getFiles());
    xIterator.previous();
    xIterator.set(new TMFile("File2R", 999));
    yIterator.previous();
    yIterator.set(new TMFile("File0R", 999));
    ksession.fireAllRules();
    assertTrue(factsCollection(ksession).contains("X.File0"));
    assertTrue(factsCollection(ksession).contains("X.File1"));
    assertFalse(factsCollection(ksession).contains("X.File2"));
    assertFalse(factsCollection(ksession).contains("Y.File0"));
    assertTrue(factsCollection(ksession).contains("Y.File1"));
    assertTrue(factsCollection(ksession).contains("Y.File2"));
    assertTrue(factsCollection(ksession).contains("X.File2R"));
    assertTrue(factsCollection(ksession).contains("Y.File0R"));
}
Also used : TMFile(org.drools.mvel.compiler.oopath.model.TMFile) ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) KieBase(org.kie.api.KieBase) TMDirectory(org.drools.mvel.compiler.oopath.model.TMDirectory) KieSession(org.kie.api.runtime.KieSession) ObjectUpdatedEvent(org.kie.api.event.rule.ObjectUpdatedEvent) ObjectInsertedEvent(org.kie.api.event.rule.ObjectInsertedEvent) DefaultRuleRuntimeEventListener(org.kie.api.event.rule.DefaultRuleRuntimeEventListener) Test(org.junit.Test)

Aggregations

TMFile (org.drools.mvel.compiler.oopath.model.TMFile)9 Test (org.junit.Test)9 KieBase (org.kie.api.KieBase)9 KieSession (org.kie.api.runtime.KieSession)9 TMDirectory (org.drools.mvel.compiler.oopath.model.TMDirectory)5 TMFileSet (org.drools.mvel.compiler.oopath.model.TMFileSet)4 HashSet (java.util.HashSet)3 DefaultRuleRuntimeEventListener (org.kie.api.event.rule.DefaultRuleRuntimeEventListener)3 ObjectDeletedEvent (org.kie.api.event.rule.ObjectDeletedEvent)3 ObjectInsertedEvent (org.kie.api.event.rule.ObjectInsertedEvent)3 ObjectUpdatedEvent (org.kie.api.event.rule.ObjectUpdatedEvent)3