Ejemplo n.º 1
0
  @Override
  protected Collection<RuleBaseElementId> listElementsInPackage(KnowledgePackage p) {
    Collection<RuleBaseElementId> result = new HashSet<RuleBaseElementId>();
    String path = p.getName().replace(".", File.separator);
    File directory = new File(source.getPath(), path);
    @SuppressWarnings("unchecked")
    Collection<File> listFiles =
        FileUtils.listFiles(directory, new String[] {DirectoryRuleSource.FUNC_EXTENSION}, false);

    for (File f : listFiles) {
      String s = f.getName().replace("." + DirectoryRuleSource.FUNC_EXTENSION, "");
      result.add(new RuleBaseElementId(RuleBaseElementType.Function, p.getName(), s));
    }
    return result;
  }
Ejemplo n.º 2
0
  @Test
  public void testDslRulesKnowledge() throws IOException, ClassNotFoundException {
    try {
      executeTarget("dslRulesKnowledge");
    } catch (Exception e) {
      e.printStackTrace();
      fail("Should not throw any exception: " + e.getMessage());
    }

    KnowledgePackage kpackage1 =
        (KnowledgePackage)
            DroolsStreamUtils.streamIn(new FileInputStream("target/cheese.rules.pkg"));

    assertNotNull(kpackage1);
    assertEquals(1, kpackage1.getRules().size());
  }
Ejemplo n.º 3
0
  @Test
  @Ignore
  public void testDeletePackageUrl() throws Exception {
    String rule1 = this.createDefaultRule("rule1", "org.drools.test1");

    String rule2 = this.createDefaultRule("rule2", "org.drools.test2");

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(rule1.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(rule2.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
      fail(kbuilder.getErrors().toString());
    }

    Map<String, KnowledgePackage> map = new HashMap<String, KnowledgePackage>();
    for (KnowledgePackage pkg : kbuilder.getKnowledgePackages()) {
      map.put(pkg.getName(), pkg);
    }
    writePackage((KnowledgePackage) map.get("org.drools.test1"), fileManager.newFile("pkg1.pkg"));
    writePackage((KnowledgePackage) map.get("org.drools.test2"), fileManager.newFile("pkg2.pkg"));

    String xml = "";
    xml += "<change-set xmlns='http://drools.org/drools-5.0/change-set'";
    xml += "    xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'";
    xml +=
        "    xs:schemaLocation='http://drools.org/drools-5.0/change-set http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd' >";
    xml += "    <add> ";
    xml +=
        "        <resource source='http://localhost:" + this.getPort() + "/pkg1.pkg' type='PKG' />";
    xml +=
        "        <resource source='http://localhost:" + this.getPort() + "/pkg2.pkg' type='PKG' />";
    xml += "    </add> ";
    xml += "</change-set>";

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

    KnowledgeAgent kagent = this.createKAgent(kbase, false);

    applyChangeSet(kagent, ResourceFactory.newByteArrayResource(xml.getBytes()));

    StatefulKnowledgeSession ksession = kagent.getKnowledgeBase().newStatefulKnowledgeSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    ksession.dispose();

    assertEquals(2, list.size());
    assertTrue(list.contains("rule1"));
    assertTrue(list.contains("rule2"));

    list.clear();

    xml = "";
    xml += "<change-set xmlns='http://drools.org/drools-5.0/change-set'";
    xml += "    xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'";
    xml +=
        "    xs:schemaLocation='http://drools.org/drools-5.0/change-set http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd' >";
    xml += "    <remove> ";
    xml +=
        "        <resource source='http://localhost:" + this.getPort() + "/pkg2.pkg' type='PKG' />";
    xml += "    </remove> ";
    xml += "</change-set>";

    applyChangeSet(kagent, xml);

    ksession = kagent.getKnowledgeBase().newStatefulKnowledgeSession();
    list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    ksession.dispose();

    assertEquals(1, list.size());

    assertTrue(list.contains("rule1"));
    kagent.dispose();
  }
  @Test
  public void testLogicalInsertionsDynamicRule() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(
        ResourceFactory.newClassPathResource("test_LogicalInsertionsDynamicRule.drl", getClass()),
        ResourceType.DRL);

    if (kbuilder.hasErrors()) {
      fail(kbuilder.getErrors().toString());
    }

    Collection<KnowledgePackage> kpkgs = kbuilder.getKnowledgePackages();
    KnowledgeBase kbase = getKnowledgeBase();
    kbase.addKnowledgePackages(kpkgs);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

    final Cheese c1 = new Cheese("a", 1);
    final Cheese c2 = new Cheese("b", 2);
    final Cheese c3 = new Cheese("c", 3);
    List list;

    ksession.insert(c1);
    FactHandle h = ksession.insert(c2);
    ksession.insert(c3);
    ksession.fireAllRules();

    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);

    // Check logical Insertions where made for c2 and c3
    list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
    assertEquals(2, list.size());
    assertFalse(list.contains(new Person(c1.getType())));
    assertTrue(list.contains(new Person(c2.getType())));
    assertTrue(list.contains(new Person(c3.getType())));

    // this rule will make a logical assertion for c1 too
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(
        ResourceFactory.newClassPathResource("test_LogicalInsertionsDynamicRule2.drl", getClass()),
        ResourceType.DRL);
    if (kbuilder.hasErrors()) {
      fail(kbuilder.getErrors().toString());
    }
    Collection<KnowledgePackage> kpkgs2 = kbuilder.getKnowledgePackages();
    kbase.addKnowledgePackages(kpkgs2);
    kbase = SerializationHelper.serializeObject(kbase);

    ksession.fireAllRules();

    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);

    kbase = ksession.getKnowledgeBase();

    // check all now have just one logical assertion each
    list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
    assertEquals(3, list.size());
    assertTrue(list.contains(new Person(c1.getType())));
    assertTrue(list.contains(new Person(c2.getType())));
    assertTrue(list.contains(new Person(c3.getType())));

    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);

    // check the packages are correctly populated
    assertEquals(2, kbase.getKnowledgePackages().size());
    KnowledgePackage test = null, test2 = null;
    // different JVMs return the package list in different order
    for (KnowledgePackage kpkg : kbase.getKnowledgePackages()) {
      if (kpkg.getName().equals("org.drools.test")) {
        test = kpkg;
      } else if (kpkg.getName().equals("org.drools.test2")) {
        test2 = kpkg;
      }
    }

    assertNotNull(test);
    assertNotNull(test2);
    assertEquals("rule1", test.getRules().iterator().next().getName());
    assertEquals("rule2", test2.getRules().iterator().next().getName());

    // now remove the first rule
    kbase.removeRule(test.getName(), test.getRules().iterator().next().getName());
    // different JVMs return the package list in different order
    for (KnowledgePackage kpkg : kbase.getKnowledgePackages()) {
      if (kpkg.getName().equals("org.drools.test")) {
        test = kpkg;
      } else if (kpkg.getName().equals("org.drools.test2")) {
        test2 = kpkg;
      }
    }
    assertNotNull(test);
    assertNotNull(test2);

    // Check the rule was correctly remove
    assertEquals(0, test.getRules().size());
    assertEquals(1, test2.getRules().size());
    assertEquals("rule2", test2.getRules().iterator().next().getName());

    list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
    assertEquals(
        "removal of the rule should result in retraction of c3's logical assertion",
        2,
        list.size());
    assertTrue(
        "c1's logical assertion should not be retracted", list.contains(new Person(c1.getType())));
    assertTrue(
        "c2's logical assertion should  not be retracted", list.contains(new Person(c2.getType())));
    assertFalse(
        "c3's logical assertion should be  retracted", list.contains(new Person(c3.getType())));

    c2.setPrice(3);
    h = getFactHandle(h, ksession);
    ksession.update(h, c2);
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
    assertEquals(
        "c2 now has a higher price, its logical assertion should  be cancelled", 1, list.size());
    assertFalse(
        "The logical assertion cor c2 should have been retracted",
        list.contains(new Person(c2.getType())));
    assertTrue(
        "The logical assertion  for c1 should exist", list.contains(new Person(c1.getType())));

    // different JVMs return the package list in different order
    for (KnowledgePackage kpkg : kbase.getKnowledgePackages()) {
      if (kpkg.getName().equals("org.drools.test")) {
        test = kpkg;
      } else if (kpkg.getName().equals("org.drools.test2")) {
        test2 = kpkg;
      }
    }
    assertNotNull(test);
    assertNotNull(test2);

    kbase.removeRule(test2.getName(), test2.getRules().iterator().next().getName());
    kbase = SerializationHelper.serializeObject(kbase);

    // different JVMs return the package list in different order
    for (KnowledgePackage kpkg : kbase.getKnowledgePackages()) {
      if (kpkg.getName().equals("org.drools.test")) {
        test = kpkg;
      } else if (kpkg.getName().equals("org.drools.test2")) {
        test2 = kpkg;
      }
    }
    assertNotNull(test);
    assertNotNull(test2);

    assertEquals(0, test.getRules().size());
    assertEquals(0, test2.getRules().size());
    list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
    assertEquals(0, list.size());
  }