private KnowledgeBase buildKnowledgeBase(String... rules) {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";

    int i = 0;
    for (String lhs : rules) {
      str += "rule rule" + (i++) + "  when \n";
      str += lhs;
      str += "then \n";
      str += "end \n";
    }

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);

    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(LRUnlinkingOption.ENABLED);

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf);
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;
  }
 public void testProcessFactory() {
   RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.process");
   factory
       // header
       .name("My process")
       .packageName("org.jbpm")
       // nodes
       .startNode(1)
       .name("Start")
       .done()
       .actionNode(2)
       .name("Action")
       .action("java", "System.out.println(\"Action\");")
       .done()
       .endNode(3)
       .name("End")
       .done()
       // connections
       .connection(1, 2)
       .connection(2, 3);
   RuleFlowProcess process = factory.validate().getProcess();
   KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
   kbuilder.add(
       ResourceFactory.newByteArrayResource(
           XmlBPMNProcessDumper.INSTANCE.dump(process).getBytes()),
       ResourceType.BPMN2);
   KnowledgeBase kbase = kbuilder.newKnowledgeBase();
   StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
   ksession.startProcess("org.jbpm.process");
 }
Exemple #3
0
  @Test
  public void testUpdateWithNonEffectiveActivations() throws Exception {
    // JBRULES-3604
    String str =
        "package inheritance\n"
            + "\n"
            + "import org.drools.Address\n"
            + "\n"
            + "rule \"Parent\"\n"
            + "    enabled false\n"
            + "    when \n"
            + "        $a : Address(suburb == \"xyz\")\n"
            + "    then \n"
            + "        System.out.println( $a ); \n"
            + "end \n"
            + "rule \"Child\" extends \"Parent\" \n"
            + "    when \n"
            + "        $b : Address( this == $a, street == \"123\")\n"
            + "    then \n"
            + "        System.out.println( $b ); \n"
            + "end";

    KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    builder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);

    if (builder.hasErrors()) {
      throw new RuntimeException(builder.getErrors().toString());
    }
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(LRUnlinkingOption.ENABLED);
    KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(kconf);
    knowledgeBase.addKnowledgePackages(builder.getKnowledgePackages());

    StatefulKnowledgeSession ksession = knowledgeBase.newStatefulKnowledgeSession();

    Address address = new Address();

    address.setSuburb("xyz");
    org.kie.runtime.rule.FactHandle addressHandle = ksession.insert(address);

    int rulesFired = ksession.fireAllRules();

    assertEquals(0, rulesFired);

    address.setStreet("123");

    ksession.update(addressHandle, address);

    rulesFired = ksession.fireAllRules();

    System.out.println(rulesFired);
    assertEquals(1, rulesFired);

    ksession.dispose();
  }
  private void addRuleAssetToVerifier() {

    ContentHandler handler = ContentManager.getHandler(ruleAsset.getFormat());

    if (!(handler instanceof IRuleAsset)) {
      throw new IllegalStateException("IRuleAsset Expected");
    }

    RuleModel model = (RuleModel) ruleAsset.getContent();

    String brl = BRXMLPersistence.getInstance().marshal(model);

    verifier.addResourcesToVerify(
        ResourceFactory.newByteArrayResource(brl.getBytes()), ResourceType.BRL);
  }
Exemple #5
0
  @Test
  public void testCompilationMustFailComparingAClassLiteral() {
    // DROOLS-20
    String str =
        "import org.drools.integrationtests.MiscTest2.Answer\n"
            + "import org.drools.integrationtests.MiscTest2.AnswerGiver\n"
            + "rule \"Test Rule\"\n"
            + "when\n"
            + "   AnswerGiver(Answer == Answer.YES)\n"
            + "then\n"
            + "end";

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertTrue(kbuilder.hasErrors());
  }
Exemple #6
0
  @Test
  public void testClassNotFoundAfterDeserialization() throws Exception {
    // JBRULES-3670
    String drl =
        "package completely.other.deal;\n"
            + "\n"
            + "declare Person\n"
            + "   firstName : String\n"
            + "   lastName : String\n"
            + "end\n"
            + "\n"
            + "rule \"now use it B\"\n"
            + "   when\n"
            + "       Person( $christianName, $surname; )\n"
            + "   then\n"
            + "       insert( new Person( $christianName, null ) );\n"
            + "end";

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);

    if (kbuilder.hasErrors()) {
      throw new RuntimeException("" + kbuilder.getErrors());
    }

    FileManager fileManager = new FileManager().setUp();

    try {
      File root = fileManager.getRootDirectory();

      ObjectOutputStream out =
          new ObjectOutputStream(new FileOutputStream(new File(root, "test.drl.compiled")));
      out.writeObject(kbuilder.getKnowledgePackages());
      out.close();

      KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
      kconf.setOption(LRUnlinkingOption.ENABLED);
      KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf);

      ObjectInputStream in =
          new ObjectInputStream(new FileInputStream(new File(root, "test.drl.compiled")));
      kbase.addKnowledgePackages((Collection<KnowledgePackage>) in.readObject());
      in.close();
    } finally {
      fileManager.tearDown();
    }
  }
Exemple #7
0
  @Test
  public void testMVELForLoop() throws Exception {
    // JBRULES-3717
    String str =
        "rule demo\n"
            + "dialect \"mvel\"\n"
            + "when\n"
            + "then\n"
            + "   for ( int i = 1; i <= 3; i++ ) {\n"
            + "       insert( \"foo\" + i );\n"
            + "   }\n"
            + "end";

    KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    builder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);

    if (builder.hasErrors()) {
      throw new RuntimeException(builder.getErrors().toString());
    }
  }
Exemple #8
0
  @Test
  public void testDynamicAddRule() {
    // DROOLS-17
    String str =
        "import org.drools.integrationtests.MiscTest2.A\n"
            + "rule r1 when\n"
            + "    $a : A( f1 == 1 )\n"
            + "then\n"
            + "end\n"
            + "\n"
            + "rule r2 when\n"
            + "    $a : A( f2 == 1 )\n"
            + "then\n"
            + "end\n"
            + "\n"
            + "rule r3 when\n"
            + "    $a : A( f3 == 1 )"
            + "then\n"
            + "end";

    String str2 =
        "import org.drools.integrationtests.MiscTest2.A\n"
            + "rule r4 when\n"
            + "    $a : A( f2 == 1, f4 == 1 )"
            + "then\n"
            + "end";

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);

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

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    FactHandle fh = ksession.insert(new A(1, 1, 1, 1));

    ksession.fireAllRules();

    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL);

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

    ksession.fireAllRules();

    // this second insert forces the regeneration of the otnIds
    ksession.insert(new A(2, 2, 2, 2));

    LeftTuple leftTuple = ((DefaultFactHandle) fh).getFirstLeftTuple();
    ObjectTypeNode.Id letTupleOtnId = leftTuple.getLeftTupleSink().getLeftInputOtnId();
    leftTuple = leftTuple.getLeftParentNext();
    while (leftTuple != null) {
      assertTrue(letTupleOtnId.before(leftTuple.getLeftTupleSink().getLeftInputOtnId()));
      letTupleOtnId = leftTuple.getLeftTupleSink().getLeftInputOtnId();
      leftTuple = leftTuple.getLeftParentNext();
    }
  }