2011/8/22 Chris Richmond <span dir="ltr"><<a href="mailto:crichmond@referentia.com">crichmond@referentia.com</a>></span><br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div text="#000000" bgcolor="#FFFFFF">
Doesn't retracting all objects then inserting new objects cause the
rules to be evaluated for the objects currently in working memory?<br>
<br></div></blockquote><div><br>Mostly, yes, but rules using just not(<something>) may not fire again unless retracting changes it to true.<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div text="#000000" bgcolor="#FFFFFF">
Do I need to use Stateless instead of Stateful or something?<br></div></blockquote><div><br>Both use the same reasoning.<br><br>Disposing the old session and creating a new one might be one way of achieving what you have in mind. Also, adding a pattern with a "trigger" fact you can change judiciously might do the trick.<br>
<br>-W<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div text="#000000" bgcolor="#FFFFFF">
<br>
Thanks,<br><font color="#888888">
<br>
Chris</font><div><div></div><div class="h5"><br>
<br>
On 8/19/2011 7:47 PM, Wolfgang Laun wrote:
<blockquote type="cite">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.<br>
<br>
-W<br>
<br>
<br>
<div class="gmail_quote">2011/8/20 Chris Richmond <span dir="ltr"><<a href="mailto:crichmond@referentia.com" target="_blank">crichmond@referentia.com</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div text="#000000" bgcolor="#FFFFFF"> 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. <br>
<br>
Two clasess: TestMain and TestObject and rule file Test.drl
I have included below.<br>
<br>
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. <br>
So the rule fires on the first batch, but on no other
batches after that???? What gives???? <br>
<br>
Among the Inserttion and retraction events I only see:<br>
<br>
A proper object does not exist<br>
<br>
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.<br>
<br>
What is going on???<br>
<br>
<br>
<br>
<br>
<br>
TestMain.java *************************************<br>
<br>
package com.sample;<br>
<br>
import java.util.ArrayList;<br>
import java.util.List;<br>
<br>
import org.drools.KnowledgeBase;<br>
import org.drools.KnowledgeBaseConfiguration;<br>
import org.drools.KnowledgeBaseFactory;<br>
import org.drools.builder.KnowledgeBuilder;<br>
import org.drools.builder.KnowledgeBuilderError;<br>
import org.drools.builder.KnowledgeBuilderErrors;<br>
import org.drools.builder.KnowledgeBuilderFactory;<br>
import org.drools.builder.ResourceType;<br>
import org.drools.conf.EventProcessingOption;<br>
import org.drools.event.rule.ObjectInsertedEvent;<br>
import org.drools.event.rule.ObjectRetractedEvent;<br>
import org.drools.event.rule.ObjectUpdatedEvent;<br>
import org.drools.event.rule.WorkingMemoryEventListener;<br>
import org.drools.io.ResourceFactory;<br>
import org.drools.runtime.KnowledgeSessionConfiguration;<br>
import org.drools.runtime.StatefulKnowledgeSession;<br>
import org.drools.runtime.conf.ClockTypeOption;<br>
import org.drools.runtime.rule.FactHandle;<br>
import org.drools.runtime.rule.WorkingMemoryEntryPoint;<br>
<br>
public class TestMain {<br>
<br>
@SuppressWarnings("restriction")<br>
public static void main(String[] args) {<br>
<br>
<br>
try {<br>
<br>
KnowledgeSessionConfiguration config =
KnowledgeBaseFactory.newKnowledgeSessionConfiguration();<br>
config.setOption( ClockTypeOption.get("realtime") );<br>
<br>
<br>
KnowledgeBase kbase;<br>
kbase = readKnowledgeBase();<br>
final StatefulKnowledgeSession ksession =
kbase.newStatefulKnowledgeSession();<br>
<br>
WorkingMemoryEntryPoint myStream =
ksession.getWorkingMemoryEntryPoint("My Stream");<br>
<br>
ksession.addEventListener(new
WorkingMemoryEventListener(){<br>
<br>
@Override<br>
public void objectInserted(ObjectInsertedEvent oie)
{<br>
System.err.println("Inserted: " + oie.toString());<br>
<br>
}<br>
<br>
@Override<br>
public void objectRetracted(ObjectRetractedEvent
arg0) {<br>
System.err.println("Retracted: " +
arg0.toString());<br>
<br>
}<br>
<br>
@Override<br>
public void objectUpdated(ObjectUpdatedEvent arg0) {<br>
// TODO Auto-generated method stub<br>
<br>
}<br>
<br>
});<br>
<br>
<br>
for (int a = 0; a < 1000; a++){<br>
List<FactHandle> factHandles = new
ArrayList<FactHandle>();<br>
for (int x = 0; x < 6; x++){<br>
<br>
double reading = 11.3;<br>
float f = (float)reading;<br>
TestObject dr = new TestObject("Reading " + x, f);<br>
FactHandle fh = myStream.insert(dr);<br>
factHandles.add(fh);<br>
<br>
<br>
<br>
}<br>
ksession.fireAllRules();<br>
for(FactHandle fh : factHandles){<br>
myStream.retract(fh);<br>
}<br>
Thread.sleep(4000);<br>
}<br>
<br>
} catch (Exception e) {<br>
// TODO Auto-generated catch block<br>
e.printStackTrace();<br>
}<br>
<br>
}<br>
<br>
@SuppressWarnings("restriction")<br>
private static KnowledgeBase readKnowledgeBase() throws
Exception {<br>
KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();<br>
kbuilder.add(ResourceFactory.newClassPathResource("Test.drl"),
ResourceType.DRL);<br>
KnowledgeBuilderErrors errors = kbuilder.getErrors();<br>
if (errors.size() > 0) {<br>
for (KnowledgeBuilderError error: errors) {<br>
System.err.println(error);<br>
}<br>
throw new IllegalArgumentException("Could not parse
knowledge.");<br>
}<br>
<br>
KnowledgeBaseConfiguration kbConfig =
KnowledgeBaseFactory.newKnowledgeBaseConfiguration();<br>
kbConfig.setOption( EventProcessingOption.STREAM );<br>
KnowledgeBase kbase =
KnowledgeBaseFactory.newKnowledgeBase(kbConfig);<br>
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());<br>
<br>
<br>
<br>
<br>
return kbase;<br>
}<br>
<br>
<br>
<br>
<br>
}<br>
<br>
TestObject.java *************************************<br>
package com.sample;<br>
<br>
public class TestObject {<br>
<br>
<br>
<br>
private String name;<br>
private float reading;<br>
<br>
<br>
public TestObject(String name, float reading){<br>
<a href="http://this.name" target="_blank">this.name</a> = name;<br>
this.reading = reading;<br>
}<br>
<br>
<br>
public String getName() {<br>
return name;<br>
}<br>
<br>
<br>
public void setName(String name) {<br>
<a href="http://this.name" target="_blank">this.name</a> = name;<br>
}<br>
<br>
<br>
public float getReading() {<br>
return reading;<br>
}<br>
<br>
<br>
public void setReading(float reading) {<br>
this.reading = reading;<br>
}<br>
}<br>
<br>
<br>
Test.drl*************************************************<br>
#created on: Aug 19, 2011<br>
package com.sample<br>
<br>
<br>
<br>
rule "Test For Lack of Objects with criteria"<br>
<br>
when<br>
not(TestObject(name=="blah") from entry-point "My
Stream")<br>
then<br>
System.err.println("A proper object does not
exist");<br>
end<br>
<br>
<br>
<br>
<br>
On 8/18/2011 8:46 PM, Wolfgang Laun wrote:
<blockquote type="cite">On 19 August 2011 15:17, Chris
Richmond <span dir="ltr"><<a href="mailto:crichmond@referentia.com" target="_blank">crichmond@referentia.com</a>></span>
wrote:<br>
<div class="gmail_quote">
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> How do I fire a rule if an object
with certain characterstics does not<br>
exists.<br>
<br>
For example my class Foo, if I have a rule:<br>
<br>
rule "Identify Foos with values"<br>
<br>
when<br>
Foo(stringProp=="blah", intProp==5)<br>
then<br>
System.err.println("A Foo was found!");<br>
end<br>
<br>
<br>
So how do I check for lack of existence of an object
with certain<br>
characteristics<br>
<br>
I tried:<br>
rule "Flag missing Foos with values"<br>
<br>
when<br>
not(Foo(stringProp=="blah", intProp==5))<br>
then<br>
System.err.println("A proper foo does not
exist");<br>
end<br>
</blockquote>
<div><br>
That's the way to do it.<br>
</div>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> <br>
I also tried:<br>
rule "Flag missing Foos with values"<br>
<br>
when<br>
not(exists(Foo(stringProp=="blah",
intProp==5)))<br>
then<br>
System.err.println("A proper foo does not
exist");<br>
end<br>
</blockquote>
<div><br>
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".<br>
<br>
-W<br>
</div>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
</blockquote>
</div>
<br>
<br>
<fieldset></fieldset>
<br>
<pre>_______________________________________________
rules-users mailing list
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a>
</pre>
</blockquote>
<br>
</div>
<br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br>
</blockquote>
</div>
<br>
<br>
<fieldset></fieldset>
<br>
<pre>_______________________________________________
rules-users mailing list
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a>
</pre>
</blockquote>
<br>
</div></div></div>
<br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br>