[rules-users] Ensure all values in a list are unique?

2009-04-16 Thread campbeln

I have an ArrayList of strings that I need to ensure are all internally
unique (ie: {"1","2","3"} = OK, {"1","2","1"} = Error). I've managed to
cobble together the code below but I'm running into the "forall w/ an
internal from" issue. I don't believe an upgrade is feasible at the moment,
so are there any alternative solutions?

NOTE: The collection of both $factGroups and $checkGroups is almost
definitely redundant, so please keep any laughing to a minimum =)


rule "BR033"
   no-loop true
 when
$fact : MyFact()

//# Get 2 copies of all the Groups associated with the $fact
$factGroups : ArrayList() from collect (
Group() from $fact.Groups
)
$checkGroups : ArrayList() from collect (
Group() from $fact.Groups
)

//# Traverse the $factGroups comparing each $currentGroup to 
the list
//# within $checkGroups, erroring out if something other then 1 
is found
forall (
$currentGroup : Group(this memberOf $factGroups)
$test : ArrayList(size != 1) from collect(
Group(this == $currentGroup) from $checkGroups
)
)
then
System.out.println("Failed: BR033");
end

I have found a number of postings that seem to be related, namely
http://www.nabble.com/Best-practice-for-rules-against-multiple-objects-td5621720.html#a5621720
but I've been unable to cobble together a solution thus far. All help is
appreciated!

Thanks!
-- 
View this message in context: 
http://www.nabble.com/Ensure-all-values-in-a-list-are-unique--tp23071079p23071079.html
Sent from the drools - user mailing list archive at Nabble.com.

___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


Re: [rules-users] Negation semantics in Drools

2009-04-16 Thread Edson Tirelli
   Ha, thanks a lot Greg. I need new glasses... he is actually comparing
with the parameter "second", but when creating the win fact, using the
parameter "first".

not Win(first == m.second)
  insert(new Win(m.first));

   Yes, in this case the engine is working exactly as it should.

   Anyway, I added the (fixed) test case to the codebase, just in case. :)

   Thanks,
   Edson

2009/4/16 Greg Barton 

> You don't have to worry.  The engine is acting as it should.
>
> The rule Paul had was this, a bit simplified for clarity:
>
> rule "direct"
> when
>m : Move()
>not Win(first == m.second)
> then
>insert(new Win(m.first));
> end
>
> If the insertion order is [Move(1,2), Move(2,3)] then the rule matches
> first on Move(2,3) and Win(2) is inserted.  No other rule fires because now
> Move(1,2) and Win(2) match up, removing the instantiation with Move(1,2)
> from the agenda.
>
> If the insertion order is [Move(2,3), Move(1,2)] then the order is this:
>
> matched Move(1,2) insert Win(1)
> matched Move(2,3) insert Win(2)
>
> The insertion of Win(1) in the first firing does NOT prevent the
> instantiation with Move(2,3) from then firing.
>
> So it's all good. :)  Sample code and output attached.
>
> --- On Thu, 4/16/09, Greg Barton  wrote:
>
> > From: Greg Barton 
> > Subject: Re: [rules-users] Negation semantics in Drools
> > To: "Rules Users List" 
> > Date: Thursday, April 16, 2009, 8:50 PM
> > It is on the latest snapshot release,
> > 5.0.0.20090417.005612-483
> >
> > --- On Thu, 4/16/09, Edson Tirelli 
> > wrote:
> >
> > > We need to investigate if that is still happening
> > in
> > > latest trunk.
> >
> >
> >
> > ___
> > rules-users mailing list
> > rules-users@lists.jboss.org
> > https://lists.jboss.org/mailman/listinfo/rules-users
>
>
>
>
> ___
> rules-users mailing list
> rules-users@lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>


-- 
 Edson Tirelli
 JBoss Drools Core Development
 JBoss, a division of Red Hat @ www.jboss.com
___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


Re: [rules-users] Negation semantics in Drools

2009-04-16 Thread Greg Barton
You don't have to worry.  The engine is acting as it should.

The rule Paul had was this, a bit simplified for clarity:

rule "direct"
when
m : Move()
not Win(first == m.second)
then
insert(new Win(m.first));
end

If the insertion order is [Move(1,2), Move(2,3)] then the rule matches first on 
Move(2,3) and Win(2) is inserted.  No other rule fires because now Move(1,2) 
and Win(2) match up, removing the instantiation with Move(1,2) from the agenda.

If the insertion order is [Move(2,3), Move(1,2)] then the order is this:

matched Move(1,2) insert Win(1)
matched Move(2,3) insert Win(2)

The insertion of Win(1) in the first firing does NOT prevent the instantiation 
with Move(2,3) from then firing.

So it's all good. :)  Sample code and output attached.

--- On Thu, 4/16/09, Greg Barton  wrote:

> From: Greg Barton 
> Subject: Re: [rules-users] Negation semantics in Drools
> To: "Rules Users List" 
> Date: Thursday, April 16, 2009, 8:50 PM
> It is on the latest snapshot release,
> 5.0.0.20090417.005612-483
> 
> --- On Thu, 4/16/09, Edson Tirelli 
> wrote:
> 
> > We need to investigate if that is still happening
> in
> > latest trunk.
> 
> 
>   
> ___
> rules-users mailing list
> rules-users@lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users


  

sample.drl
Description: Binary data
package com.sample;

import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Iterator;

import org.drools.FactHandle;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;

/**
 * This is a sample file to launch a rule package from a rule source file.
 */
public class DroolsTest {

public static final void main(String[] args) throws Exception {
	
	boolean reversed = false;
		
	if(args.length > 0) {
		reversed = Boolean.parseBoolean(args[0]);
	}
	
	System.out.println("*");
	System.out.println(reversed ? "Reversed" : "Regular");
	System.out.println();
	
	//load up the rulebase
RuleBase ruleBase = readRule();
WorkingMemory workingMemory = ruleBase.newStatefulSession();

//go !

Move[] moves = new Move[] { new Move(1,2), new Move(2,3) };

if(reversed) {
workingMemory.insert( moves[1] );
workingMemory.insert( moves[0] );
} else {
workingMemory.insert( moves[0] );
workingMemory.insert( moves[1] );
}

System.out.println("Before");

for(Iterator i = workingMemory.iterateFactHandles(); i.hasNext(); ) {
	System.out.println(workingMemory.getObject((FactHandle)i.next()));
}
System.out.println();

workingMemory.fireAllRules(); 

System.out.println();

System.out.println("After");

for(Iterator i = workingMemory.iterateFactHandles(); i.hasNext(); ) {
	System.out.println(workingMemory.getObject((FactHandle)i.next()));
}  

	System.out.println("*");
}

/**
 * Please note that this is the "low level" rule assembly API.
 */
	private static RuleBase readRule() throws Exception {
		//read in the source
		Reader source = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/sample.drl" ) );
		
		//optionally read in the DSL (if you are using it).
		//Reader dsl = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/mylang.dsl" ) );

		//Use package builder to build up a rule package.
		//An alternative lower level class called "DrlParser" can also be used...
		
		PackageBuilder builder = new PackageBuilder();

		//this will parse and compile in one step
		//NOTE: There are 2 methods here, the one argument one is for normal DRL.
		builder.addPackageFromDrl( source );

		//Use the following instead of above if you are using a DSL:
		//builder.addPackageFromDrl( source, dsl );
		
		//get the compiled package (which is serializable)
		Package pkg = builder.getPackage();
		
		//add the package to a rulebase (deploy the rule package).
		RuleBase ruleBase = RuleBaseFactory.newRuleBase();
		ruleBase.addPackage( pkg );
		return ruleBase;
	}
	
	public static class Move {
		
		public final int first;
		public final int second;
		
		public Move(int first, int second) {
			this.first = first;
			this.second = second;
		}
		
		public int getFirst() {
			return this.first;
		}
		
		public int getSecond() {
			return this.second;
		}
		
		public String toString() {
			return "Move " + first + "->" + second;
		}
	}
	
	public static class Win {
		
		private final int first;
		
		public Win(int first) {
			this.first = first;
		}
		
		public int getFirst() {
			return this.first;
		}
		
		publ

Re: [rules-users] Negation semantics in Drools

2009-04-16 Thread Greg Barton

It is on the latest snapshot release, 5.0.0.20090417.005612-483

--- On Thu, 4/16/09, Edson Tirelli  wrote:

> We need to investigate if that is still happening in
> latest trunk.


  
___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


Re: [rules-users] Negation semantics in Drools

2009-04-16 Thread Edson Tirelli
In this case, I think Paul's initial analysis is pertinent. Unless I am
missing something obvious, the order of the facts may change the order in
which the activations are executed, but not the number of executions. The
end result should be the same with the same number of Win facts in the
working memory.

We need to investigate if that is still happening in latest trunk.

[]s
Edson

2009/4/16 Paul Fodor 

> On Thu, Apr 16, 2009 at 1:18 PM, Greg Barton wrote:
>
>>
>> This is not a problem with negation.  I don't think you understand rule
>> conflict resolution.
>>
>> Here's your rule:
>>
>> rule "direct"
>> when
>>  m : Move(x : first, y : second)
>>  not Win(first == y)
>> then
>>  insert(new Win(m.getFirst()));
>> end
>>
>> This rule, regardles of whether it uses negation, will be affected by fact
>> insertion order.  This is because the first condition will match on any Move
>> in working memory.  When all of the potential firings of this rule are put
>> on the agenda, which will initially be one for each Move in working memory,
>> one must be selected to fire.  The order of fact insertion (recency) is one
>> of the ways Drools resolves this by default.
>
>
> Thank you. I see.
>
>
>> If you don't want that behavior you can change it by using a different
>> conflict resolver instance.  See classes in the package org.drools.conflict:
>>
>>
>> https://hudson.jboss.org/hudson/job/drools/lastSuccessfulBuild/artifact/trunk/target/javadocs/unstable/drools-core/org/drools/conflict/package-summary.html
>
> Or define your own.
>
>
> That is very dificult task. The win-nowin test was just an example of use
> of the default negation. My requirement covers stratified or non-stratified
> logic programs, and not one example in particular.
> I don't really think that it is possible to implement a general "rule
> conflict resolution" resolver for default negation. This is hard
> in production rule systems.
>
> Paul.
>
>
>> You can install a new one by calling either of these methods on your
>> RuleBaseConfiguration:
>>
>> setProperty("drools.conflictResolver", )
>>
>> or
>>
>> setConflictResolver()
>>
>> --- On Thu, 4/16/09, Paul Fodor  wrote:
>>
>> > From: Paul Fodor 
>> > Subject: [rules-users] Negation semantics in Drools
>> > To: "Rules Users List" 
>> > Date: Thursday, April 16, 2009, 10:43 AM
>>  > Dear Sir,
>> >
>> > I found a problem with negation in Drools when changing the
>> > order of the
>> > facts in the database also changes the final model.
>> > Consider the classic win-nowin problem encoded in Drools as
>> > follows:
>> >
>> > win.drl:
>> >
>> > package tests;
>> > import tests.Test.Win;
>> > import tests.Test.Move;
>> > rule "direct"
>> > when
>> > m : Move(x : first, y : second)
>> > not Win(first == y)
>> > then
>> >  insert(new Win(m.getFirst()));
>> > end
>> >
>> > With two moves in the database: move(1,2) and move(2,3),
>> > for one order of the facts in the input file we get one
>> > result: win(2),
>> > while for the other order (i.e., move(2,3) and move(1,2))
>> > we get 2 results:
>> > win(1) and win(2).
>> >
>> > For win_upper1_drools.drools:
>> > > java tests.Test win_upper1_drools.drools win.drl
>> > result
>> > reading rulefile: win.drl ...
>> > reading datafile: win_upper1_drools.drools ...
>> > computing cputime: 0.0
>> > computing walltime: 0.0030
>> > Derived facts in memory:move(1, 2).
>> > win(2).
>> > move(2, 3).
>> > 3
>> >
>> > For win_upper2_drools.drools:
>> > > java tests.Test win_upper2_drools.drools win.drl
>> > result
>> > reading rulefile: win.drl ...
>> > reading datafile: win_upper2_drools.drools ...
>> > computing cputime: 0.0
>> > computing walltime: 0.0040
>> > Derived facts in memory:win(1).
>> > win(2).
>> > move(1, 2).
>> > move(2, 3).
>> > 4
>> >
>> > I attached all the sources used in these tests in the
>> > email. This example is
>> > locally stratified. I am using the latest released version
>> > of Drools.
>> >
>> > Regards,
>> >  Paul Fodor
>> >
>> > win.drl:
>> >
>> > package tests;
>> > import tests.Test.Win;
>> > import tests.Test.Move;
>> > rule "direct"
>> > when
>> > m : Move(x : first, y : second)
>> >  not Win(first == y)
>> > then
>> >  insert(new Win(m.getFirst()));
>> > end
>> >
>> >
>> > win_upper1_drools.drools:
>> >
>> > move
>> > 1
>> > 2
>> > move
>> > 2
>> > 3
>> >
>> > win_upper2_drools.drools:
>> >
>> > move
>> > 2
>> > 3
>> > move
>> > 1
>> > 2
>> >
>> > Test.java:
>> >
>> > package tests;
>> > import java.io.*;
>> > import java.io.InputStreamReader;
>> > import org.drools.RuleBase;
>> > import org.drools.RuleBaseFactory;
>> > import org.drools.StatefulSession;
>> > import org.drools.compiler.PackageBuilder;
>> > import org.drools.FactHandle;
>> > import java.util.Iterator;
>> > import java.lang.management.ManagementFactory;
>> > import java.lang.management.ThreadMXBean;
>> > public class Test {
>> > public static void main (String args[]) {
>> >  if (args.length < 3) {
>> >  u

Re: [rules-users] Negation semantics in Drools

2009-04-16 Thread Paul Fodor
On Thu, Apr 16, 2009 at 1:18 PM, Greg Barton  wrote:

>
> This is not a problem with negation.  I don't think you understand rule
> conflict resolution.
>
> Here's your rule:
>
> rule "direct"
> when
>  m : Move(x : first, y : second)
>  not Win(first == y)
> then
>  insert(new Win(m.getFirst()));
> end
>
> This rule, regardles of whether it uses negation, will be affected by fact
> insertion order.  This is because the first condition will match on any Move
> in working memory.  When all of the potential firings of this rule are put
> on the agenda, which will initially be one for each Move in working memory,
> one must be selected to fire.  The order of fact insertion (recency) is one
> of the ways Drools resolves this by default.


Thank you. I see.


> If you don't want that behavior you can change it by using a different
> conflict resolver instance.  See classes in the package org.drools.conflict:
>
>
> https://hudson.jboss.org/hudson/job/drools/lastSuccessfulBuild/artifact/trunk/target/javadocs/unstable/drools-core/org/drools/conflict/package-summary.html

Or define your own.


That is very dificult task. The win-nowin test was just an example of use of
the default negation. My requirement covers stratified or non-stratified
logic programs, and not one example in particular.
I don't really think that it is possible to implement a general "rule
conflict resolution" resolver for default negation. This is hard
in production rule systems.

Paul.


> You can install a new one by calling either of these methods on your
> RuleBaseConfiguration:
>
> setProperty("drools.conflictResolver", )
>
> or
>
> setConflictResolver()
>
> --- On Thu, 4/16/09, Paul Fodor  wrote:
>
> > From: Paul Fodor 
> > Subject: [rules-users] Negation semantics in Drools
> > To: "Rules Users List" 
> > Date: Thursday, April 16, 2009, 10:43 AM
>  > Dear Sir,
> >
> > I found a problem with negation in Drools when changing the
> > order of the
> > facts in the database also changes the final model.
> > Consider the classic win-nowin problem encoded in Drools as
> > follows:
> >
> > win.drl:
> >
> > package tests;
> > import tests.Test.Win;
> > import tests.Test.Move;
> > rule "direct"
> > when
> > m : Move(x : first, y : second)
> > not Win(first == y)
> > then
> >  insert(new Win(m.getFirst()));
> > end
> >
> > With two moves in the database: move(1,2) and move(2,3),
> > for one order of the facts in the input file we get one
> > result: win(2),
> > while for the other order (i.e., move(2,3) and move(1,2))
> > we get 2 results:
> > win(1) and win(2).
> >
> > For win_upper1_drools.drools:
> > > java tests.Test win_upper1_drools.drools win.drl
> > result
> > reading rulefile: win.drl ...
> > reading datafile: win_upper1_drools.drools ...
> > computing cputime: 0.0
> > computing walltime: 0.0030
> > Derived facts in memory:move(1, 2).
> > win(2).
> > move(2, 3).
> > 3
> >
> > For win_upper2_drools.drools:
> > > java tests.Test win_upper2_drools.drools win.drl
> > result
> > reading rulefile: win.drl ...
> > reading datafile: win_upper2_drools.drools ...
> > computing cputime: 0.0
> > computing walltime: 0.0040
> > Derived facts in memory:win(1).
> > win(2).
> > move(1, 2).
> > move(2, 3).
> > 4
> >
> > I attached all the sources used in these tests in the
> > email. This example is
> > locally stratified. I am using the latest released version
> > of Drools.
> >
> > Regards,
> >  Paul Fodor
> >
> > win.drl:
> >
> > package tests;
> > import tests.Test.Win;
> > import tests.Test.Move;
> > rule "direct"
> > when
> > m : Move(x : first, y : second)
> >  not Win(first == y)
> > then
> >  insert(new Win(m.getFirst()));
> > end
> >
> >
> > win_upper1_drools.drools:
> >
> > move
> > 1
> > 2
> > move
> > 2
> > 3
> >
> > win_upper2_drools.drools:
> >
> > move
> > 2
> > 3
> > move
> > 1
> > 2
> >
> > Test.java:
> >
> > package tests;
> > import java.io.*;
> > import java.io.InputStreamReader;
> > import org.drools.RuleBase;
> > import org.drools.RuleBaseFactory;
> > import org.drools.StatefulSession;
> > import org.drools.compiler.PackageBuilder;
> > import org.drools.FactHandle;
> > import java.util.Iterator;
> > import java.lang.management.ManagementFactory;
> > import java.lang.management.ThreadMXBean;
> > public class Test {
> > public static void main (String args[]) {
> >  if (args.length < 3) {
> >  usage();
> >  }
> >  long starttime_cpu, endtime_cpu, starttime_sys,
> > endtime_sys;
> >  double cputime, walltime;
> >  ThreadMXBean tb_cpu = ManagementFactory.getThreadMXBean();
> >  // create rulebase
> >  try {
> >  FileWriter output = new FileWriter(args[2]);
> >  BufferedWriter bufWrite = new BufferedWriter(output);
> >  bufWrite.write(args[0] + "\n");
> >  bufWrite.flush();
> >  System.out.println("reading rulefile: " +
> > args[1] + " ...");
> >  Reader source = new
> > InputStreamReader(Test.class.getResourceAsStream(args[1]));
> >  final PackageBuilder builder =

Re: [rules-users] Negation semantics in Drools

2009-04-16 Thread Greg Barton

This is not a problem with negation.  I don't think you understand rule 
conflict resolution.

Here's your rule:

rule "direct"
when
  m : Move(x : first, y : second)
  not Win(first == y)
then
  insert(new Win(m.getFirst()));
end

This rule, regardles of whether it uses negation, will be affected by fact 
insertion order.  This is because the first condition will match on any Move in 
working memory.  When all of the potential firings of this rule are put on the 
agenda, which will initially be one for each Move in working memory, one must 
be selected to fire.  The order of fact insertion (recency) is one of the ways 
Drools resolves this by default.  

If you don't want that behavior you can change it by using a different conflict 
resolver instance.  See classes in the package org.drools.conflict:

https://hudson.jboss.org/hudson/job/drools/lastSuccessfulBuild/artifact/trunk/target/javadocs/unstable/drools-core/org/drools/conflict/package-summary.html

Or define your own.

You can install a new one by calling either of these methods on your 
RuleBaseConfiguration:

setProperty("drools.conflictResolver", )

or

setConflictResolver()

--- On Thu, 4/16/09, Paul Fodor  wrote:

> From: Paul Fodor 
> Subject: [rules-users] Negation semantics in Drools
> To: "Rules Users List" 
> Date: Thursday, April 16, 2009, 10:43 AM
> Dear Sir,
> 
> I found a problem with negation in Drools when changing the
> order of the
> facts in the database also changes the final model.
> Consider the classic win-nowin problem encoded in Drools as
> follows:
> 
> win.drl:
> 
> package tests;
> import tests.Test.Win;
> import tests.Test.Move;
> rule "direct"
> when
> m : Move(x : first, y : second)
> not Win(first == y)
> then
>  insert(new Win(m.getFirst()));
> end
> 
> With two moves in the database: move(1,2) and move(2,3),
> for one order of the facts in the input file we get one
> result: win(2),
> while for the other order (i.e., move(2,3) and move(1,2))
> we get 2 results:
> win(1) and win(2).
> 
> For win_upper1_drools.drools:
> > java tests.Test win_upper1_drools.drools win.drl
> result
> reading rulefile: win.drl ...
> reading datafile: win_upper1_drools.drools ...
> computing cputime: 0.0
> computing walltime: 0.0030
> Derived facts in memory:move(1, 2).
> win(2).
> move(2, 3).
> 3
> 
> For win_upper2_drools.drools:
> > java tests.Test win_upper2_drools.drools win.drl
> result
> reading rulefile: win.drl ...
> reading datafile: win_upper2_drools.drools ...
> computing cputime: 0.0
> computing walltime: 0.0040
> Derived facts in memory:win(1).
> win(2).
> move(1, 2).
> move(2, 3).
> 4
> 
> I attached all the sources used in these tests in the
> email. This example is
> locally stratified. I am using the latest released version
> of Drools.
> 
> Regards,
>  Paul Fodor
> 
> win.drl:
> 
> package tests;
> import tests.Test.Win;
> import tests.Test.Move;
> rule "direct"
> when
> m : Move(x : first, y : second)
>  not Win(first == y)
> then
>  insert(new Win(m.getFirst()));
> end
> 
> 
> win_upper1_drools.drools:
> 
> move
> 1
> 2
> move
> 2
> 3
> 
> win_upper2_drools.drools:
> 
> move
> 2
> 3
> move
> 1
> 2
> 
> Test.java:
> 
> package tests;
> import java.io.*;
> import java.io.InputStreamReader;
> import org.drools.RuleBase;
> import org.drools.RuleBaseFactory;
> import org.drools.StatefulSession;
> import org.drools.compiler.PackageBuilder;
> import org.drools.FactHandle;
> import java.util.Iterator;
> import java.lang.management.ManagementFactory;
> import java.lang.management.ThreadMXBean;
> public class Test {
> public static void main (String args[]) {
>  if (args.length < 3) {
>  usage();
>  }
>  long starttime_cpu, endtime_cpu, starttime_sys,
> endtime_sys;
>  double cputime, walltime;
>  ThreadMXBean tb_cpu = ManagementFactory.getThreadMXBean();
>  // create rulebase
>  try {
>  FileWriter output = new FileWriter(args[2]);
>  BufferedWriter bufWrite = new BufferedWriter(output);
>  bufWrite.write(args[0] + "\n");
>  bufWrite.flush();
>  System.out.println("reading rulefile: " +
> args[1] + " ...");
>  Reader source = new
> InputStreamReader(Test.class.getResourceAsStream(args[1]));
>  final PackageBuilder builder = new PackageBuilder();
>  builder.addPackageFromDrl(source);
>  if (builder.hasErrors()) {
>   System.out.println(builder.getErrors().toString());
>   System.exit(0);
>  }
>  final RuleBase ruleBase =
> RuleBaseFactory.newRuleBase();
>  ruleBase.addPackage(builder.getPackage());
>  final StatefulSession session =
> ruleBase.newStatefulSession();
>  // loading datafile
>  System.out.println("reading datafile: " +
> args[0] + " ...");
>  FileReader input = new FileReader(args[0]);
>  starttime_sys = System.nanoTime();
>  starttime_cpu = tb_cpu.getCurrentThreadCpuTime();
>  BufferedReader bufRead = new BufferedReader(input);
>  String first, second, line = bufRead.readLine();
>  w

[rules-users] Drools 5.0 imminent release

2009-04-16 Thread Mark Proctor
We are going to try and do 5.0 tomorrow. We are now just cleaning up the 
manual and any bugs we can find. If there is anything urgent with 
regards to a serious bug please let us know immediately. Likewise if 
anyone would care to proof read the docs and contribute some more, I've 
just revamped drools-expert and javadocs probably could do with more 
feedback. So this is your last opportunity to help make drools 5.0 great.


Mark


___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


[rules-users] Negation semantics in Drools

2009-04-16 Thread Paul Fodor
Dear Sir,

I found a problem with negation in Drools when changing the order of the
facts in the database also changes the final model.
Consider the classic win-nowin problem encoded in Drools as follows:

win.drl:

package tests;
import tests.Test.Win;
import tests.Test.Move;
rule "direct"
when
m : Move(x : first, y : second)
not Win(first == y)
then
 insert(new Win(m.getFirst()));
end

With two moves in the database: move(1,2) and move(2,3),
for one order of the facts in the input file we get one result: win(2),
while for the other order (i.e., move(2,3) and move(1,2)) we get 2 results:
win(1) and win(2).

For win_upper1_drools.drools:
> java tests.Test win_upper1_drools.drools win.drl result
reading rulefile: win.drl ...
reading datafile: win_upper1_drools.drools ...
computing cputime: 0.0
computing walltime: 0.0030
Derived facts in memory:move(1, 2).
win(2).
move(2, 3).
3

For win_upper2_drools.drools:
> java tests.Test win_upper2_drools.drools win.drl result
reading rulefile: win.drl ...
reading datafile: win_upper2_drools.drools ...
computing cputime: 0.0
computing walltime: 0.0040
Derived facts in memory:win(1).
win(2).
move(1, 2).
move(2, 3).
4

I attached all the sources used in these tests in the email. This example is
locally stratified. I am using the latest released version of Drools.

Regards,
 Paul Fodor

win.drl:

package tests;
import tests.Test.Win;
import tests.Test.Move;
rule "direct"
when
m : Move(x : first, y : second)
 not Win(first == y)
then
 insert(new Win(m.getFirst()));
end


win_upper1_drools.drools:

move
1
2
move
2
3

win_upper2_drools.drools:

move
2
3
move
1
2

Test.java:

package tests;
import java.io.*;
import java.io.InputStreamReader;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.compiler.PackageBuilder;
import org.drools.FactHandle;
import java.util.Iterator;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
public class Test {
public static void main (String args[]) {
 if (args.length < 3) {
 usage();
 }
 long starttime_cpu, endtime_cpu, starttime_sys, endtime_sys;
 double cputime, walltime;
 ThreadMXBean tb_cpu = ManagementFactory.getThreadMXBean();
 // create rulebase
 try {
 FileWriter output = new FileWriter(args[2]);
 BufferedWriter bufWrite = new BufferedWriter(output);
 bufWrite.write(args[0] + "\n");
 bufWrite.flush();
 System.out.println("reading rulefile: " + args[1] + " ...");
 Reader source = new
InputStreamReader(Test.class.getResourceAsStream(args[1]));
 final PackageBuilder builder = new PackageBuilder();
 builder.addPackageFromDrl(source);
 if (builder.hasErrors()) {
  System.out.println(builder.getErrors().toString());
  System.exit(0);
 }
 final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
 ruleBase.addPackage(builder.getPackage());
 final StatefulSession session = ruleBase.newStatefulSession();
 // loading datafile
 System.out.println("reading datafile: " + args[0] + " ...");
 FileReader input = new FileReader(args[0]);
 starttime_sys = System.nanoTime();
 starttime_cpu = tb_cpu.getCurrentThreadCpuTime();
 BufferedReader bufRead = new BufferedReader(input);
 String first, second, line = bufRead.readLine();
 while (line != null) {
  if (line.compareTo("move") == 0) {
  first = bufRead.readLine();
  second = bufRead.readLine();
  session.insert(new Move(first, second));
  }
  else if (line.compareTo("par") == 0) {
  first = bufRead.readLine();
  second = bufRead.readLine();
  session.insert(new ClassPar(first, second));
  }
  else if (line.compareTo("sib") == 0) {
  first = bufRead.readLine();
  second = bufRead.readLine();
  session.insert(new ClassSib(first, second));
  }
  line = bufRead.readLine();
 }
 endtime_cpu = tb_cpu.getCurrentThreadCpuTime();
 endtime_sys = System.nanoTime();
 cputime = (endtime_cpu - starttime_cpu) * 1e-9;
 cputime = Math.round(cputime * 1000) * 1e-3;
 walltime = (endtime_sys - starttime_sys) * 1e-9;
 walltime = Math.round(walltime * 1000) * 1e-3;
 bufWrite.write("loading cputime: " + cputime + "\n");
 bufWrite.write("loading walltime: " + walltime + "\n");
 bufWrite.flush();
 System.out.println("loading cputime: " + cputime);
 System.out.println("loading walltime: " + walltime);
 /*
 System.out.print("Facts in memory:");
 long count = 0;
 for (Iterator it = session.iterateFactHandles(); it.hasNext(); ) {
  FactHandle factHandle = (FactHandle) it.next();
  count ++;
  //System.out.println(session.getObject(factHandle));
 }
 System.out.println(count);
 */
 // computing
 System.out.println("calculating ...");
 starttime_sys = System.nanoTime();
 starttime_cpu = tb_cpu.getCurrentThreadCpuTime();
 session.fireAllRules();
 endtime_cpu = tb_cpu.getCurre

Re: [rules-users] How to make a rule only executed once?

2009-04-16 Thread David Sinclair
That is correct. So u want a rule to only fire one time for the life time of
the system? I have never heard of a requirement like that before :)

On Thu, Apr 16, 2009 at 3:31 AM, Brody bach  wrote:

>
> Hi David,
> thanks for reply...
> does this mean, the inserted object will stay only within the session?
> and the rule will be checked against again if I use a new session?
> Thanks
>
> Brody
>
>
> dave sinclair wrote:
> >
> > Check for the non-existence of a fact specific to the rule on the LHS.
> > When
> > the rule fires, assert the fact to ensure it never fires again. For
> > example
> >
> > rule "Foo"
> >when
> >   not Fired(rule == "foo")
> > 
> >then
> >   insert(new Fired("foo"));
> > ...
> > end
> >
> > On Wed, Apr 15, 2009 at 11:53 AM, Brody bach 
> wrote:
> >
> >>
> >> Hi all,
> >>
> >> Is there any way how I make a rule only executed once( i.e. if it has
> >> fired
> >> once, no other facts should be checked against it)
> >>
> >> Thanks for any hints
> >> Regards
> >> Brody
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/How-to-make-a-rule-only-executed-once--tp23061727p23061727.html
> >> Sent from the drools - user mailing list archive at Nabble.com.
> >>
> >> ___
> >> rules-users mailing list
> >> rules-users@lists.jboss.org
> >> https://lists.jboss.org/mailman/listinfo/rules-users
> >>
> >
> > ___
> > rules-users mailing list
> > rules-users@lists.jboss.org
> > https://lists.jboss.org/mailman/listinfo/rules-users
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/How-to-make-a-rule-only-executed-once--tp23061727p23072887.html
> Sent from the drools - user mailing list archive at Nabble.com.
>
> ___
> rules-users mailing list
> rules-users@lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


[rules-users] What do this output from Guvnor means?

2009-04-16 Thread Brody bach

Hi all, 
while using Guvnor, I noticed several outputs on the console.
And I'm curious enough what does these lines mean, and how can I eventually
make them faster..(questions are below this code block)

..
14:47:55,562 INFO  [STDOUT] [] [2009:04:106 14:04:562:debug] KnowledgeAgent
ChangeSet requires KnowledgeBuilder
14:47:55,562 INFO  [STDOUT] [] [2009:04:106 14:04:562:debug] KnowledgeAgent
rebuilding KnowledgeBase using ChangeSet
14:47:55,578 INFO  [STDOUT] [] [2009:04:106 14:04:578:debug] KnowledgeAgent
building resource=[UrlResource
path='http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/testRegulation/testRegulation']
14:47:55,578 INFO  [STDOUT] [] PackageName: testRegulation
14:47:55,578 INFO  [STDOUT] [] PackageVersion: testRegulation
14:47:55,578 INFO  [STDOUT] [] PackageIsLatest: false
14:47:55,578 INFO  [STDOUT] [] PackageIsSource: false
14:47:56,671 INFO  [STDOUT] [] PackageName: nationalRegulation
14:47:56,671 INFO  [STDOUT] [] PackageVersion: nationalRegulation
14:47:56,671 INFO  [STDOUT] [] PackageIsLatest: false
14:47:56,671 INFO  [STDOUT] [] PackageIsSource: false
14:47:57,609 INFO  [STDOUT] [] [2009:04:106 14:04:609:debug] KnowledgeAgent
adding KnowledgeDefinitionsPackage testRegulation
14:47:57,656 INFO  [STDOUT] [] [2009:04:106 14:04:656:info] KnowledgeAgent
new KnowledgeBase now built and in use

Several questions:

1. The lines in bold (PackageIsLatest) appears to take the most time. What
is actually happened there?
2. In the application, i usually have several of this code block and they
make the application slower.
Which instances (KnowledgeBase, (Stateless)KnowledgeSession,
KnowledgeAgentConfig, KnowledgeAgent) can I actually reuse (no
reinstantiating) without having the possibility that old rules package is
being used when I have a new one?

Thanks in advance!

Regards
BB
-- 
View this message in context: 
http://www.nabble.com/What-do-this-output-from-Guvnor-means--tp23077920p23077920.html
Sent from the drools - user mailing list archive at Nabble.com.

___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


Re: [rules-users] How to make a rule only executed once?

2009-04-16 Thread Brody bach

Hi David,
thanks for reply...
does this mean, the inserted object will stay only within the session?
and the rule will be checked against again if I use a new session?
Thanks

Brody


dave sinclair wrote:
> 
> Check for the non-existence of a fact specific to the rule on the LHS.
> When
> the rule fires, assert the fact to ensure it never fires again. For
> example
> 
> rule "Foo"
>when
>   not Fired(rule == "foo")
> 
>then
>   insert(new Fired("foo"));
> ...
> end
> 
> On Wed, Apr 15, 2009 at 11:53 AM, Brody bach  wrote:
> 
>>
>> Hi all,
>>
>> Is there any way how I make a rule only executed once( i.e. if it has
>> fired
>> once, no other facts should be checked against it)
>>
>> Thanks for any hints
>> Regards
>> Brody
>> --
>> View this message in context:
>> http://www.nabble.com/How-to-make-a-rule-only-executed-once--tp23061727p23061727.html
>> Sent from the drools - user mailing list archive at Nabble.com.
>>
>> ___
>> rules-users mailing list
>> rules-users@lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
> 
> ___
> rules-users mailing list
> rules-users@lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
> 
> 

-- 
View this message in context: 
http://www.nabble.com/How-to-make-a-rule-only-executed-once--tp23061727p23072887.html
Sent from the drools - user mailing list archive at Nabble.com.

___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users