[rules-users] Checking for a lack of an object

Wolfgang Laun wolfgang.laun at gmail.com
Sat Aug 20 01:47:39 EDT 2011


A condition based on the negated existence quantifier is true when no such
object is in the WM. Once recognized as true, the rule fires, and that's it
until it isn't true any more, which is a sort of "rewind" for the condition
after which the begin of another period of absence is celebrated with
another firing. Repeated stop-and-go of the rule engine does not influence
this monitoring of truth.

-W


2011/8/20 Chris Richmond <crichmond at referentia.com>

>  Well..I insert some objects, fire the rules and this rule will trigger the
> first time (when it finds no object with those characterstis) but every time
> after than when I insert more objects and fire the rules, the rule never
> fires again. I have no idea why.  Here is my simple test case.
>
> Two clasess: TestMain and TestObject and rule file Test.drl I have included
> below.
>
> It insterts a group of facts at one time, fires the rules, and retracts all
> those facts from the stream.  I have an event listener on the session, as
> you see to verify injections and retractions are occuring.
> So the rule fires on the first batch, but on no other batches after
> that???? What gives????
>
> Among the Inserttion and retraction events I only see:
>
> A proper object does not exist
>
> One time, during firing rules on the first batch.  Why does this rule never
> fire again, even though every single batch of objects I insert/retract does
> not contain the proper rule values, and so should fire the rule.
>
> What is going on???
>
>
>
>
>
> TestMain.java *************************************
>
> package com.sample;
>
> import java.util.ArrayList;
> import java.util.List;
>
> import org.drools.KnowledgeBase;
> import org.drools.KnowledgeBaseConfiguration;
> 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.conf.EventProcessingOption;
> import org.drools.event.rule.ObjectInsertedEvent;
> import org.drools.event.rule.ObjectRetractedEvent;
> import org.drools.event.rule.ObjectUpdatedEvent;
> import org.drools.event.rule.WorkingMemoryEventListener;
> import org.drools.io.ResourceFactory;
> import org.drools.runtime.KnowledgeSessionConfiguration;
> import org.drools.runtime.StatefulKnowledgeSession;
> import org.drools.runtime.conf.ClockTypeOption;
> import org.drools.runtime.rule.FactHandle;
> import org.drools.runtime.rule.WorkingMemoryEntryPoint;
>
> public class TestMain {
>
>   @SuppressWarnings("restriction")
>   public static void main(String[] args) {
>
>
>     try {
>
>       KnowledgeSessionConfiguration config =
> KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
>       config.setOption( ClockTypeOption.get("realtime") );
>
>
>       KnowledgeBase kbase;
>       kbase = readKnowledgeBase();
>       final StatefulKnowledgeSession ksession =
> kbase.newStatefulKnowledgeSession();
>
>       WorkingMemoryEntryPoint myStream =
> ksession.getWorkingMemoryEntryPoint("My Stream");
>
>       ksession.addEventListener(new WorkingMemoryEventListener(){
>
>         @Override
>         public void objectInserted(ObjectInsertedEvent oie) {
>           System.err.println("Inserted: " + oie.toString());
>
>         }
>
>         @Override
>         public void objectRetracted(ObjectRetractedEvent arg0) {
>           System.err.println("Retracted: " + arg0.toString());
>
>         }
>
>         @Override
>         public void objectUpdated(ObjectUpdatedEvent arg0) {
>           // TODO Auto-generated method stub
>
>         }
>
>       });
>
>
>       for (int a = 0; a < 1000; a++){
>         List<FactHandle> factHandles = new ArrayList<FactHandle>();
>         for (int x = 0; x < 6; x++){
>
>           double reading = 11.3;
>           float f = (float)reading;
>           TestObject dr = new TestObject("Reading " + x, f);
>           FactHandle fh = myStream.insert(dr);
>           factHandles.add(fh);
>
>
>
>         }
>         ksession.fireAllRules();
>         for(FactHandle fh : factHandles){
>           myStream.retract(fh);
>         }
>         Thread.sleep(4000);
>       }
>
>     } catch (Exception e) {
>       // TODO Auto-generated catch block
>       e.printStackTrace();
>     }
>
>   }
>
>   @SuppressWarnings("restriction")
>   private static KnowledgeBase readKnowledgeBase() throws Exception {
>     KnowledgeBuilder kbuilder =
> KnowledgeBuilderFactory.newKnowledgeBuilder();
>     kbuilder.add(ResourceFactory.newClassPathResource("Test.drl"),
> ResourceType.DRL);
>     KnowledgeBuilderErrors errors = kbuilder.getErrors();
>     if (errors.size() > 0) {
>       for (KnowledgeBuilderError error: errors) {
>         System.err.println(error);
>       }
>       throw new IllegalArgumentException("Could not parse knowledge.");
>     }
>
>     KnowledgeBaseConfiguration kbConfig =
> KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
>     kbConfig.setOption( EventProcessingOption.STREAM );
>     KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbConfig);
>     kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
>
>
>
>
>     return kbase;
>   }
>
>
>
>
> }
>
> TestObject.java *************************************
> package com.sample;
>
> public class TestObject {
>
>
>
>   private String name;
>   private float reading;
>
>
>   public TestObject(String name, float reading){
>     this.name = name;
>     this.reading = reading;
>   }
>
>
>   public String getName() {
>     return name;
>   }
>
>
>   public void setName(String name) {
>     this.name = name;
>   }
>
>
>   public float getReading() {
>     return reading;
>   }
>
>
>   public void setReading(float reading) {
>     this.reading = reading;
>   }
> }
>
>
> Test.drl*************************************************
> #created on: Aug 19, 2011
> package com.sample
>
>
>
> rule "Test For Lack of Objects with criteria"
>
> when
>         not(TestObject(name=="blah")  from entry-point "My Stream")
> then
>         System.err.println("A proper object does not exist");
> end
>
>
>
>
> On 8/18/2011 8:46 PM, Wolfgang Laun wrote:
>
> On 19 August 2011 15:17, Chris Richmond <crichmond at referentia.com> wrote:
>
>> How do I fire a rule if an object with certain characterstics does not
>> exists.
>>
>> For example my class Foo, if I have a rule:
>>
>> rule "Identify Foos with values"
>>
>> when
>>         Foo(stringProp=="blah", intProp==5)
>> then
>>         System.err.println("A Foo was found!");
>> end
>>
>>
>> So how do I check for lack of existence of an object with certain
>> characteristics
>>
>> I tried:
>> rule "Flag missing Foos with values"
>>
>> when
>>         not(Foo(stringProp=="blah", intProp==5))
>> then
>>         System.err.println("A proper foo does not exist");
>> end
>>
>
> That's the way to do it.
>
>
>>
>> I also tried:
>> rule "Flag missing Foos with values"
>>
>> when
>>         not(exists(Foo(stringProp=="blah", intProp==5)))
>> then
>>         System.err.println("A proper foo does not exist");
>> end
>>
>
> Since not( Foo(...)) means "if no Foo(...) exists", the addition of
> "exists" is superfluous and generally considered bad style (even though some
> systems accept it to mean just "not".
>
> -W
>
>
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>
>
>
> _______________________________________________
> rules-users mailing listrules-users at lists.jboss.orghttps://lists.jboss.org/mailman/listinfo/rules-users
>
>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20110820/cf3a7475/attachment.html 


More information about the rules-users mailing list