re[rules-users] move hardcoded values in DRL or have constants inside drl
Earnest Dyke
earniedyke at yahoo.com
Mon Jun 1 09:16:24 EDT 2009
Wolfgang makes a good point. However, given the requirement that the value changes and is not truly static, I would recommend something like the following which, if your SearchParm loads the data from a persisted source you could change it's value without any code change.
Earnie!
package com.sample
rule "Test Rule"
when
SearchParam($searchName : searchName)
$person : Person(name == $searchName)
then
System.out.println(drools.getRule().getName() + " [" + $searchName + "] [" + $person.getName() + "]");
$person.setActivation(true);
end
package com.sample;
public class Person {
private String name;
private Boolean activation;
public Person(String name, Boolean activation) {
this.name = name;
this.activation = activation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getActivation() {
return activation;
}
public void setActivation(Boolean activation) {
this.activation = activation;
}
}
package com.sample;
public class SearchParam {
private String searchName;
public SearchParam(String searchName) {
super();
this.searchName = searchName;
}
public String getSearchName() {
return searchName;
}
public void setSearchName(String searchName) {
this.searchName = searchName;
}
}
package com.sample;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.compiler.PackageBuilder;
import org.drools.compiler.PackageBuilderConfiguration;
import org.drools.rule.Package;
import org.drools.rule.builder.dialect.java.JavaDialectConfiguration;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class GlobalTest {
private RuleBase ruleBase;
@Test
public void testWithGlobal() {
StatefulSession ss = ruleBase.newStatefulSession();
Person p = new Person("John",false);
ss.insert(p);
SearchParam sp = new SearchParam("John");
ss.insert(sp);
ss.fireAllRules();
assertTrue(p.getActivation().equals(true));
}
@Before
public void buildRules() throws Exception {
Reader source = new InputStreamReader(this.getClass()
.getResourceAsStream("/test.drl"));
Properties props = new Properties();
props.setProperty("drools.dialect.java.compiler", "JANINO");
PackageBuilderConfiguration cfg = new PackageBuilderConfiguration(props);
JavaDialectConfiguration javaConf = (JavaDialectConfiguration) cfg
.getDialectConfiguration("java");
javaConf.setCompiler(JavaDialectConfiguration.JANINO);
PackageBuilder builder = new PackageBuilder(cfg);
builder.addPackageFromDrl(source);
Package pkg = builder.getPackage();
ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage(pkg);
}
}
________________________________
From: Wolfgang Laun <wolfgang.laun at gmail.com>
To: Rules Users List <rules-users at lists.jboss.org>
Sent: Monday, June 1, 2009 7:57:36 AM
Subject: Re: re[rules-users] move hardcoded values in DRL or have constants inside drl
But assigning a value to a global isn't straightforward - you can't do
it in the declaration. You'll have to do it from the application (or
use some dirty hack, in a start-up rule); either way it's separate
from the declaration.
IMHO, using a Java class with public static final is definitely cleaner.
-W
2009/6/1 Earnest Dyke <earniedyke at yahoo.com>:
> How about adding them as Globals and referencing them that way? You should
> be able to put them in a map as well in a Global and reference them.
>
> Earnie!
>
> ________________________________
> From: karthizap <karthizap at gmail.com>
> To: rules-users at lists.jboss.org
> Sent: Monday, June 1, 2009 7:14:56 AM
> Subject: re[rules-users] move hardcoded values in DRL or have constants
> inside drl
>
> Is there is anyway i can remove hardcoded values in the DRL file? Can I have
> the constants file(like data dictionary) inside drl/functions. I don't want
> to refer java constants class directly in drl file and would like to declare
> and refer those constants inside drl file.
> ________________________________
> View this message in context: remove hardcoded values in DRL or have
> constants inside drl
> Sent from the drools - user mailing list archive at Nabble.com.
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
_______________________________________________
rules-users mailing list
rules-users at lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20090601/7bb3073d/attachment.html
More information about the rules-users
mailing list