Details

      Description

      FACT: Person
      package test;

      public class Person {

      private String firstname;
      private String secondname;
      private String address;

      public Person() {
      }

      public String getFirstname()

      { return firstname; }

      public void setFirstname(String firstname)

      { this.firstname = firstname; }

      public String getSecondname()

      { return secondname; }

      public void setSecondname(String secondname)

      { this.secondname = secondname; }

      public String getAddress()

      { return address; }

      public void setAddress(String address)

      { this.address = address; }

      }

      r1.drl:
      package test1
      import test.Person
      import java.util.HashSet

      query find(String $first, String $second)
      p: Person(firstname == "111", secondname==$second)
      end

      rule "test"
      no-loop true
      when
      $list: HashSet( size >= 0 )
      from accumulate (
      Person($addr: address, firstname=="Tom", secondname=="Cat"),
      init ( HashSet set = new HashSet(); ),
      action ( set.add($addr); ),
      reverse ( set.remove($addr); ),
      result(set) )
      then
      System.out.println("It' OK~!");

      end

      Test.java
      package test;

      import java.util.Collection;

      import org.drools.KnowledgeBase;
      import org.drools.KnowledgeBaseFactory;
      import org.drools.builder.KnowledgeBuilder;
      import org.drools.builder.KnowledgeBuilderError;
      import org.drools.builder.KnowledgeBuilderErrors;
      import org.drools.builder.KnowledgeBuilderFactory;
      import org.drools.builder.ResourceType;
      import org.drools.definition.KnowledgePackage;
      import org.drools.io.ResourceFactory;
      import org.drools.runtime.StatefulKnowledgeSession;
      import org.drools.runtime.rule.FactHandle;

      public class Test {
      public static void main(String[] args) throws Exception

      { new Test().test(); }

      public void test() throws Exception {
      KnowledgeBase base = KnowledgeBaseFactory.newKnowledgeBase();
      StatefulKnowledgeSession s = base.newStatefulKnowledgeSession();
      new Th(s).start();
      Thread.sleep(2000);

      KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
      builder.add(ResourceFactory.newClassPathResource("r1.drl", getClass()), ResourceType.DRL);
      KnowledgeBuilderErrors errs = builder.getErrors();
      if (!errs.isEmpty()) {
      for (KnowledgeBuilderError e: errs)

      { System.out.println(e.toString()); }

      }
      Collection<KnowledgePackage> col = builder.getKnowledgePackages();
      base.addKnowledgePackages(col);
      }

      class Th extends Thread {
      StatefulKnowledgeSession s;
      public Th(StatefulKnowledgeSession s)

      { this.s = s; }

      public void run() {
      Person p = new Person();
      p.setFirstname("Tom");
      p.setSecondname("Cat");
      p.setAddress("Mars");
      FactHandle fact = s.insert(p);
      int i=0;
      while (true) {
      s.update(fact, p);
      s.fireAllRules();
      try

      { Thread.sleep(1000); }

      catch (InterruptedException e) {}
      i++;
      }
      }
      }
      }

      Run Test.java, will throws Exception:

      Exception in thread "Thread-0" org.drools.RuntimeDroolsException: java.lang.NullPointerException
      at org.drools.rule.Accumulate.reverse(Accumulate.java:212)
      at org.drools.reteoo.AccumulateNode.removeMatch(AccumulateNode.java:902)
      at org.drools.reteoo.AccumulateNode.modifyRightTuple(AccumulateNode.java:537)
      at org.drools.reteoo.BetaNode.modifyObject(BetaNode.java:531)
      at org.drools.reteoo.SingleObjectSinkAdapter.propagateModifyObject(SingleObjectSinkAdapter.java:68)
      at org.drools.reteoo.AlphaNode.modifyObject(AlphaNode.java:157)
      at org.drools.reteoo.SingleObjectSinkAdapter.propagateModifyObject(SingleObjectSinkAdapter.java:68)
      at org.drools.reteoo.AlphaNode.modifyObject(AlphaNode.java:157)
      at org.drools.reteoo.CompositeObjectSinkAdapter.doPropagateModifyObject(CompositeObjectSinkAdapter.java:507)
      at org.drools.reteoo.CompositeObjectSinkAdapter.propagateModifyObject(CompositeObjectSinkAdapter.java:421)
      at org.drools.reteoo.ObjectTypeNode.modifyObject(ObjectTypeNode.java:314)
      at org.drools.reteoo.EntryPointNode.modifyObject(EntryPointNode.java:265)
      at org.drools.common.NamedEntryPoint.update(NamedEntryPoint.java:470)
      at org.drools.common.AbstractWorkingMemory.update(AbstractWorkingMemory.java:960)
      at org.drools.common.AbstractWorkingMemory.update(AbstractWorkingMemory.java:933)
      at org.drools.impl.StatefulKnowledgeSessionImpl.update(StatefulKnowledgeSessionImpl.java:284)
      at test.Test$Th.run(Test.java:53)
      Caused by: java.lang.NullPointerException
      at test1.Rule_test_b59970efe0844a8d8797758b863454b1$Accumulate0.reverse(Rule_test_b59970efe0844a8d8797758b863454b1.java:52)
      at test1.Rule_test_b59970efe0844a8d8797758b863454b1Accumulate0Invoker.reverse(Rule_test_b59970efe0844a8d8797758b863454b1Accumulate0Invoker.java:69)
      at org.drools.rule.Accumulate.reverse(Accumulate.java:203)
      ... 16 more

      But, if I change the query to like this:
      query find(String $first, String $second)
      p: Person(firstname == "111", secondname==$second)
      end
      It will be ok.

      However if I change the query to like this:
      query find(String $first, String $second)
      p: Person(firstname == "Tom", secondname==$second)
      end
      It will throws Exception again.
      firstname=="Tom" is a condition of rule "test"

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Assignee:
                mark.proctor Mark Proctor
                Reporter:
                wangyingzhi Yingzhi Wang
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated: