[rules-users] Rule Fires with Null Bindings

mark.nowicki mark.nowicki at cubrc.org
Fri Aug 3 15:43:24 EDT 2012


Hi, I'm getting duplicate rule invocations when a rule is added to the
KnowledgeBase after the facts have been inserted in the
StatefulKnowledgeSession.  Very simple example, seems what I'm doing is
consistent with 5.3.0.Final Drools expert documentation.  Thanks for the
assistance,

--Mark

*Running code below outputs the following (firing twice for Person A,
Relationship A-C):*
RelationshipRule Fired!
person: Person( id=A, type=parent )
relationship: Relationship( parentId=A, childId=C )

RelationshipRule Fired!
person: Person( id=A, type=parent )
relationship: Relationship( parentId=A, childId=B )

RelationshipRule Fired!
person: Person( id=A, type=parent )
relationship: Relationship( parentId=A, childId=C )


*resources/factTypes.drl:*
package testing.facttypes;
declare Person
  id : String
  type : String
end
declare Relationship
  parentId : String
  childId : String
end


*resources/rules.drl:*
package testing.rules;

import java.util.ArrayList;
import testing.facttypes.Person;
import testing.facttypes.Relationship;

rule "RelationshipRule"
no-loop true
when
  person : Person(type == "parent")
  relationship : Relationship(parentId == person.id)
then
  System.out.println("RelationshipRule Fired!");
  System.out.println("person: " + person);
  System.out.println("relationship: " + relationship + "\n");
end


*public static void main snippet:*
            KnowledgeBase knowledgeBase =
KnowledgeBaseFactory.newKnowledgeBase();
            StatefulKnowledgeSession statefulKnowledgeSession =
knowledgeBase.newStatefulKnowledgeSession();

            KnowledgeBuilder knowledgeBuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder(knowledgeBase);
            Resource factTypes =
ResourceFactory.newClassPathResource("resources/factTypes.drl");
            knowledgeBuilder.add(factTypes, ResourceType.DRL);
           
knowledgeBase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());

            FactType factType =
knowledgeBase.getFactType("testing.facttypes", "Person");
            Object personA = factType.newInstance();
            factType.set(personA, "id", "A");
            factType.set(personA, "type", "parent");
            statefulKnowledgeSession.insert(personA);

            factType = knowledgeBase.getFactType("testing.facttypes",
"Person");
            Object personB = factType.newInstance();
            factType.set(personB, "id", "B");
            factType.set(personB, "type", "child");
            statefulKnowledgeSession.insert(personB);

            factType = knowledgeBase.getFactType("testing.facttypes",
"Relationship");
            Object relationshipAB = factType.newInstance();
            factType.set(relationshipAB, "parentId", "A");
            factType.set(relationshipAB, "childId", "B");
            statefulKnowledgeSession.insert(relationshipAB);

            factType = knowledgeBase.getFactType("testing.facttypes",
"Person");
            Object personC = factType.newInstance();
            factType.set(personC, "id", "C");
            factType.set(personC, "type", "child");
            statefulKnowledgeSession.insert(personC);

            factType = knowledgeBase.getFactType("testing.facttypes",
"Relationship");
            Object relationshipAC = factType.newInstance();
            factType.set(relationshipAC, "parentId", "A");
            factType.set(relationshipAC, "childId", "C");
            statefulKnowledgeSession.insert(relationshipAC);

            knowledgeBuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder(knowledgeBase);
            Resource rules =
ResourceFactory.newClassPathResource("resources/rules.drl");
            knowledgeBuilder.add(rules, ResourceType.DRL);
           
knowledgeBase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());

            statefulKnowledgeSession.fireAllRules(); 



--
View this message in context: http://drools.46999.n3.nabble.com/Rule-Fires-with-Null-Bindings-tp4019021.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list