Setting a global that is a subclass of the global reference works fine. See attached code.
--- On Mon, 5/4/09, Mere Huzoor <[email protected]> wrote: > From: Mere Huzoor <[email protected]> > Subject: Re: [rules-users] setGlobal issue in StatefulSession > To: [email protected], "Rules Users List" <[email protected]> > Date: Monday, May 4, 2009, 1:39 PM > Please ignore the syntax in this posting. Yes, it is suppose > to be like > this: > > public abstract class Base { > > } > > I would appreciate if you can explain how I can achieve the > polymorphism > behavior in drools (explained in my example). FYI I'm > using version 4.0.7. > > Thanks! > > On Mon, May 4, 2009 at 10:57 AM, Greg Barton > <[email protected]> wrote: > > > > > Well, we can start out with the fact that this is > improper java syntax: > > > > class abstract Base () {} > > > > The () is not in a class declaration. Does this code > compile for you? :) > > > > --- On Mon, 5/4/09, tellkb <[email protected]> > wrote: > > > > > From: tellkb <[email protected]> > > > Subject: [rules-users] setGlobal issue in > StatefulSession > > > To: [email protected] > > > Date: Monday, May 4, 2009, 12:35 PM > > > Hi there, > > > > > > I want to set setGlobal in working memory at run > time based > > > on derived > > > class, e.g. DerivedOne or DerivedTwo. But it > throws > > > following error: > > > > > > Illegal class for global. Expected > > > [org.comp.app.bean.Base], found > > > [org.comp.app.bean.DerivedOne]. > > > > > > > > > It should automatically resolve the derived > class. Please > > > let me know if I'm > > > doing something wrong. I'd appreciate your > help. > > > > > > > > > Thanks, > > > -KB > > > > > > Here are the classed I'm using in my test > project: > > > > > > > > > class abstract Base () { > > > > > > public void init( StatefulSession > workingMemory ) { > > > this.workingMemory = workingMemory; > > > > workingMemory.setGlobal("app", this); > > > } > > > public void logMe() { > > > System.out.println("From > Derived BASE..."); > > > } > > > } > > > > > > class DerivedOne() extends Base{ > > > public void runRules( Object obj) { > > > ObjOne obj1 = (ObjOne)obj > > > workingMemory.insert(obj1); > > > workingMemory.fireAllRules(); > > > workingMemory.dispose(); > > > } > > > public void logMe() { > > > System.out.println("From > Derived ONE..."); > > > } > > > } > > > > > > class DerivedTwo() extends Base{ > > > public void runRules( Object obj) { > > > ObjTwo obj2 = (ObjTwo)obj > > > workingMemory.insert(obj2); > > > workingMemory.fireAllRules(); > > > workingMemory.dispose(); > > > } > > > public void logMe() { > > > System.out.println("From > Derived ONE..."); > > > } > > > } > > > > > > > > > class Caller() { > > > > > > RuleBase ruleBase = > ruleBaseFactory.open(docRules); > > > workingMemory = > ruleBase.newStatefulSession(); > > > > > > if (sub==1) { > > > der1 = new DerivedOne(); > > > der1.init(workingMemory); > > > der1.runRules(); > > > } > > > else if (sub==1) { > > > der2 = new DerivedTwo(); > > > der2.init(workingMemory); > > > der2.runRules(); > > > } > > > } > > > > > > //********DRL FILE***** > > > package com.sample > > > > > > import com.sample.app.RuleCaller.Message; > > > > > > global com.sample.app.Base app; > > > > > > rule "using a static function" > > > when > > > eval( true ) > > > then > > > System.out.println( "Message from > Rule." ); > > > app.logMe(); > > > end > > > //********DRL FILE***** > > > -- > > > View this message in context: > > > > > > http://www.nabble.com/setGlobal-issue-in-StatefulSession-tp23373008p23373008.html > > > Sent from the drools - user mailing list archive > at > > > Nabble.com. > > > > > > _______________________________________________ > > > rules-users mailing list > > > [email protected] > > > > https://lists.jboss.org/mailman/listinfo/rules-users > > > > > > > > _______________________________________________ > > rules-users mailing list > > [email protected] > > 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 org.drools.RuleBase;
import org.drools.RuleBaseConfiguration;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.compiler.PackageBuilder;
import org.drools.compiler.PackageBuilderConfiguration;
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) {
try {
PackageBuilderConfiguration config = new PackageBuilderConfiguration();
PackageBuilder builder = new PackageBuilder( config );
builder.addPackageFromDrl( new InputStreamReader( DroolsTest.class.getResourceAsStream( "/Sample.drl" ) ) );
Package pkg = builder.getPackage();
RuleBaseConfiguration conf = new RuleBaseConfiguration();
//conf.setShadowProxy( false );
// add the package to a rulebase
final RuleBase ruleBase = RuleBaseFactory.newRuleBase( conf );
ruleBase.addPackage( pkg );
StatefulSession session = ruleBase.newStatefulSession();
new SubclassBar().init(session);
//go !
Message message = new Message();
message.setMessage( "Hello World" );
message.setStatus( Message.HELLO );
session.insert( message );
session.fireAllRules();
} catch (Throwable t) {
t.printStackTrace();
}
}
public static class Message {
public static final int HELLO = 0;
public static final int GOODBYE = 1;
private String message;
private int status;
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStatus() {
return this.status;
}
public void setStatus( int status ) {
this.status = status;
}
}
public abstract static class Superclass {
public abstract void log(String message);
public void init(StatefulSession session) {
session.setGlobal("loggingGlobal", this);
}
}
public static class SubclassFoo extends Superclass {
public void log(String message) {
System.out.println(getClass().getSimpleName() + " says " + message);
}
}
public static class SubclassBar extends Superclass {
public void log(String message) {
System.out.println(getClass().getSimpleName() + " says " + message);
}
}
}
_______________________________________________ rules-users mailing list [email protected] https://lists.jboss.org/mailman/listinfo/rules-users
