Search in sources :

Example 1 with TMFileSet

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

the class OOPathTest method testDereferencedDeclarationOutsideOOPath.

@Test
public void testDereferencedDeclarationOutsideOOPath() {
    // DROOLS-1411
    final String drl = "import org.drools.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" + "  System.out.println( $dir1 + \".: \" + $ic1 + \" \" + $ic2 );\n" + "  duplicateNames.add( $ic1.getName() );\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().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.compiler.oopath.model.TMFile) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) TMFileSet(org.drools.compiler.oopath.model.TMFileSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with TMFileSet

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

the class OOPathTest method testOOPathWithLocalDeclaration.

@Test
public void testOOPathWithLocalDeclaration() {
    // DROOLS-1411
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.Set duplicateNames; \n" + "\n" + "rule DIFF_FILES_BUT_WITH_SAME_FILENAME when\n" + "  $ic1 : TMFileWithParentObj( $curName : name,\n" + "                              $ic2: /parent#TMFileSet/files[name == $curName, this != $ic1 ] )\n" + "then\n" + "  System.out.println( $ic1 + \" \" + $ic2 );\n" + "  duplicateNames.add( $ic1.getName() );\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    final Set<String> duplicateNames = new HashSet<>();
    ksession.setGlobal("duplicateNames", duplicateNames);
    final TMFileSet x = new TMFileSet("X");
    final TMFileWithParentObj file0 = new TMFileWithParentObj(0, "File0", 47, x);
    final TMFileWithParentObj file1 = new TMFileWithParentObj(1, "File1", 47, x);
    final TMFileWithParentObj file2 = new TMFileWithParentObj(2, "File0", 47, x);
    x.getFiles().addAll(Arrays.asList(file0, file1, file2));
    ksession.insert(x);
    ksession.insert(file0);
    ksession.insert(file1);
    ksession.insert(file2);
    ksession.fireAllRules();
    assertTrue(duplicateNames.contains("File0"));
    assertFalse(duplicateNames.contains("File1"));
}
Also used : TMFileWithParentObj(org.drools.compiler.oopath.model.TMFileWithParentObj) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) TMFileSet(org.drools.compiler.oopath.model.TMFileSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with TMFileSet

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

the class OOPathTest method testMiscSetMethods.

@Test
public void testMiscSetMethods() {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "\n" + "rule R2 when\n" + "  TMFileSet( $id: name, $p: /files[size >= 100] )\n" + "then\n" + "  System.out.println( $id + \".\" + $p.getName() );\n" + "  insertLogical(      $id + \".\" + $p.getName() );\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().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"));
}
Also used : TMFile(org.drools.compiler.oopath.model.TMFile) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) TMFileSet(org.drools.compiler.oopath.model.TMFileSet) Test(org.junit.Test)

Example 4 with TMFileSet

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

the class OOPathTest method testDeclarationInsideOOPath.

@Test
public void testDeclarationInsideOOPath() {
    // DROOLS-1411
    final String drl = "import org.drools.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" + "  System.out.println( $dir1 + \".: \" + $ic1 + \" \" + $ic2 );\n" + "  duplicateNames.add( $ic1.getName() );\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().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.compiler.oopath.model.TMFile) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) TMFileSet(org.drools.compiler.oopath.model.TMFileSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with TMFileSet

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

the class OOPathTest method testDeclarationOutsideOOPath.

@Test
public void testDeclarationOutsideOOPath() {
    // DROOLS-1411
    final String drl = "import org.drools.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" + "  System.out.println( $dir1 + \".: \" + $ic1 + \" \" + $ic2 );\n" + "  duplicateNames.add( $ic1.getName() );\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().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.compiler.oopath.model.TMFile) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) TMFileSet(org.drools.compiler.oopath.model.TMFileSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

TMFileSet (org.drools.compiler.oopath.model.TMFileSet)5 Test (org.junit.Test)5 KieSession (org.kie.api.runtime.KieSession)5 KieHelper (org.kie.internal.utils.KieHelper)5 HashSet (java.util.HashSet)4 TMFile (org.drools.compiler.oopath.model.TMFile)4 TMFileWithParentObj (org.drools.compiler.oopath.model.TMFileWithParentObj)1