[jboss-jira] [JBoss JIRA] Resolved: (JBRULES-532) Query feature doesnt seem to work if there is more than one query.

Edson Tirelli (JIRA) jira-events at jboss.com
Wed Nov 1 15:26:41 EST 2006


     [ http://jira.jboss.com/jira/browse/JBRULES-532?page=all ]

Edson Tirelli resolved JBRULES-532.
-----------------------------------

    Fix Version/s: 3.0.5
       Resolution: Cannot Reproduce Bug
         Assignee: Edson Tirelli  (was: Mark Proctor)

I was not able to reproduce the problem in trunk.
I added test case to the integration tests anyway, but could not find any problem. Besides testing the exact same files above, I also tried these one:

---------- TEST CASE:
    public void testMultipleQueries() throws Exception {
        final PackageBuilder builder = new PackageBuilder();
        builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_MultipleQueries.drl" ) ) );
        final Package pkg = builder.getPackage();

        final RuleBase ruleBase = getRuleBase();
        ruleBase.addPackage( pkg );
        final WorkingMemory workingMemory = ruleBase.newWorkingMemory();

        workingMemory.fireAllRules();
        QueryResults results = workingMemory.getQueryResults( "StiltonCheese" );
        assertEquals( 2,
                      results.size() );
        for ( Iterator it = results.iterator(); it.hasNext(); ) {
            Assert.assertEquals( "stilton",
                                 ((Cheese) ((QueryResult) it.next()).get( "$cheese" )).getType() );
        }

        results = workingMemory.getQueryResults( "MuzzarelaCheese" );
        assertEquals( 3,
                      results.size() );
        for ( Iterator it = results.iterator(); it.hasNext(); ) {
            Assert.assertEquals( "muzzarela",
                                 ((Cheese) ((QueryResult) it.next()).get( "$cheese" )).getType() );
        }
    }


------------ DRL FILE:

package org.drools;

rule "Assert cheesery" 
when
then
    assert( new Cheese( "stilton", 5 ) );
    assert( new Cheese( "stilton", 10 ) );
    assert( new Cheese( "muzzarela", 5 ) );
    assert( new Cheese( "muzzarela", 6 ) );
    assert( new Cheese( "muzzarela", 7 ) );
end

query "StiltonCheese"
    $cheese : Cheese( type == "stilton" );
end

query "MuzzarelaCheese"
    $cheese : Cheese( type == "muzzarela" );
end

------------- CHEESE:

public class Cheese
    implements
    Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -1187540653710115339L;
    private String type;
    private int    price;

    public Cheese(final String type,
                  final int price) {
        super();
        this.type = type;
        this.price = price;
    }

    public int getPrice() {
        return this.price;
    }

    public String getType() {
        return this.type;
    }

    public void setType( String type ) {
        this.type = type;
    }

    public void setPrice(final int price) {
        this.price = price;
    }

    public String toString() {
        return "Cheese( type='" + this.type + "', price=" + this.price + " )";
    }

}




> Query feature doesnt seem to work if there is more than one query.
> ------------------------------------------------------------------
>
>                 Key: JBRULES-532
>                 URL: http://jira.jboss.com/jira/browse/JBRULES-532
>             Project: JBoss Rules
>          Issue Type: Bug
>      Security Level: Public(Everyone can see) 
>            Reporter: Sridhar Chandrasekharan
>         Assigned To: Edson Tirelli
>             Fix For: 3.0.5
>
>
> 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 !
> 	            
> 	            workingMemory.fireAllRules();   
> 	            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"
> 	when
> 		#conditions
> 	then 
> 		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));
> end
> 		
> query "Foo query"
> 	foo : Foo()
> end
> ****************************************
> 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()
> end
> and the output is 
> Result is null
> Adding a second query caused the first query to stop working properly.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://jira.jboss.com/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the jboss-jira mailing list