Using Wolfgang's inline eval epression, I get the following error: "Cannot
use this in a static context" when compiling the rule. I changed
"this" by "$c" and it works. I don't know why we are in a static context.
The rule that worked for me is:
Rule "Rule1"
when
$c:Cheese(type == "chedar", eval($c.existsInShop ("shopName"))
then
System.out.println("Rule1 is fired");
end
I attached a working test case. Change $c to "*this" *in rule's condition
and you will see the error I mentioned.
Best,
2010/1/4 Wolfgang Laun <[email protected]>
> What you need is called "inline eval expression" (see the Drools Expert
> manual),
> or an eval conditional element. The first one might be written as:
>
> $c:Cheese(type == "chedar", eval( this.existsInShop("shopName") ) )
>
> -W
>
>
>
> On Mon, Jan 4, 2010 at 12:49 PM, orchid <[email protected]> wrote:
>
>>
>> Hi All,
>>
>> I'm trying to call a class method from the LHS of a rule in the following
>> way:
>>
>> public class Cheese{
>> private string type;
>> //setters and getters
>> public boolean existsInShop(String shop){
>> ...
>> return true;
>> }
>> }
>>
>> I would like to call the method "existsInShop" from a rule:
>>
>> Rule "Rule1"
>> when
>> $c:Cheese(type == "chedar", existsInShop "shopName")
>> then
>> System.out.println("Rule1 is fired");
>> end
>>
>> I get the compilation error: "no viable alternative at input "shopName" in
>> rule "Rule1" in pattern document". How can I call the method from the
>> rule?
>>
>> Thanks in advance.
>>
>>
>>
>> --
>> View this message in context:
>> http://n3.nabble.com/Call-class-method-from-LHS-of-a-rule-tp108383p108383.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
>
>
--
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Esteban Aliverti
package org.drools.user.test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.util.FileManager;
public class MethodInvocationTest extends TestCase {
FileManager fileManager;
@Override
protected void setUp() throws Exception {
fileManager = new FileManager();
fileManager.setUp();
}
@Override
protected void tearDown() throws Exception {
fileManager.tearDown();
}
public class Cheese {
private String type;
private boolean inShop;
public boolean existsInShop(String shop) {
System.out.println("existsInShop method invoked!");
return this.isInShop();
}
public boolean isInShop() {
return inShop;
}
public void setInShop(boolean inShop) {
this.inShop = inShop;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
public void testMethodInvocation() throws Exception {
String header1 = "";
header1 += "package org.drools.test\n";
header1 += "import org.drools.user.test.MethodInvocationTest.Cheese\n";
header1 += "global java.util.List list\n\n";
String rule1 = "";
rule1 += "rule rule1\n";
rule1 += "when\n";
rule1 += "\t$c:Cheese(type == \"chedar\", eval($c.existsInShop(\"a\")))\n";
rule1 += "then\n";
rule1 += "list.add( drools.getRule().getName());\n";
rule1 += "end\n";
File f1 = fileManager.newFile("rule1.drl");
Writer output = new BufferedWriter(new FileWriter(f1));
output.write(header1);
output.write(rule1);
output.close();
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newFileResource(f1), ResourceType.DRL);
if (kbuilder.hasErrors()) {
for (KnowledgeBuilderError error : kbuilder.getErrors()) {
System.err.println(error);
}
fail("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
//insert two Cheese instances
Cheese c1 = new Cheese();
c1.setType("chedar");
c1.setInShop(true);
Cheese c2 = new Cheese();
c2.setType("chedar");
c2.setInShop(false);
ksession.insert(c1);
ksession.insert(c2);
ksession.fireAllRules();
assertEquals(1, list.size());
assertTrue(list.contains("rule1"));
list.clear();
ksession.dispose();
}
}
_______________________________________________
rules-users mailing list
[email protected]
https://lists.jboss.org/mailman/listinfo/rules-users