Uploaded image for project: 'JBRULES'
  2. JBRULES-532

Query feature doesnt seem to work if there is more than one query.



    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Cannot Reproduce
    • Affects Version/s: None
    • Fix Version/s: 3.0.5
    • Component/s: None
    • Labels:


      Consider the following:-
      1) Foo.java
      package com.foo;

      public class Foo {
      private int a;
      private int b;
      public Foo(int a, int b)

      { super(); this.a = a; this.b = b; }

      public boolean test()

      { return (a > b); }

      public int getA()

      { return a; }

      public int getB()

      { return b; }

      2) FooTest.java

      package com.foo;
      import java.io.InputStreamReader;
      import java.io.*;
      import java.util.*;

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

      public class FooTest {


      • This is a sample file to launch a rule package from a rule source file.
        public static final void main(String[] args) {
        try {

      //load up the rulebase
      RuleBase ruleBase = readRule();
      WorkingMemory workingMemory = ruleBase.newWorkingMemory();

      //go !

      QueryResults results = workingMemory.getQueryResults( "Foo query" );
      if(results == null)

      { System.out.println("Result is null"); return; }

      System.out.println( "we have " + results.size() );

      for ( Iterator it = results.iterator(); it.hasNext(); )

      { QueryResult result = ( QueryResult ) it.next(); Foo foo = ( Foo ) result.get( "foo" ); System.out.println( "A="+foo.getA() + " B=" + foo.getB()); }

      } catch (Throwable t)

      { t.printStackTrace(); }



      • 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( FooTest.class.getResourceAsStream( "/Foo.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 wil 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; }

      3) Foo.drl
      #created on: Oct 9, 2006
      package com.foo

      rule "Initialization rule"
      assert(new Foo(1,1));
      assert(new Foo(2,1));
      assert(new Foo(2,3));
      assert(new Foo(3,2));
      assert(new Foo(3,4));
      assert(new Foo(4,3));

      query "Foo query"
      foo : Foo()

      The output is as follows:-
      we have 6
      A=1 B=1
      A=2 B=1
      A=2 B=3
      A=3 B=2
      A=3 B=4
      A=4 B=3

      Now add another query to the Foo.drl file

      query "Bar query"
      foo : Foo()

      and the output is

      Result is null

      Adding a second query caused the first query to stop working properly.

        Gliffy Diagrams




              • Assignee:
                tirelli Edson Tirelli
                schandra Sridhar Chandrasekharan
              • Votes:
                0 Vote for this issue
                0 Start watching this issue


                • Created: