Main Class as follows
package com.sample;
import java.io.InputStreamReader;
import java.io.Reader;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
/**
* This is a sample file to launch a rule package from a rule source
file.
*/
public class DroolsTest1 {
public static final void main(String[] args) {
try {
//load up the rulebase
RuleBase ruleBase = readRule();
WorkingMemory workingMemory = ruleBase.newWorkingMemory();
//go !
Message message = new Message();
message.setOp1(2);
message.setOp2(2);
workingMemory.assertObject( message );
workingMemory.fireAllRules();
} 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(
DroolsTest1.class.getResourceAsStream( "/Sample1.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 );
builder.addPackageFromXml( 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;
}
public static class Message {
int op1 ;
int op2;
/**
* @return the op1
*/
public int getOp1() {
return op1;
}
/**
* @param op1 the op1 to set
*/
public void setOp1(int op1) {
this.op1 = op1;
}
/**
* @return the op2
*/
public int getOp2() {
return op2;
}
/**
* @param op2 the op2 to set
*/
public void setOp2(int op2) {
this.op2 = op2;
}
}
}
It's in src/ java /com/ com.sample package
The sample1.drl file in src/ rules/Sample.drl is as follows
<?xml version="1.0"?>
<rule-set name="BusinessRulesSample"
xmlns="http://drools.org/rules"
xmlns:java="http://drools.org/semantics/java"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://drools.org/rules rules.xsd
http://drools.org/semantics/java java.xsd">
<!-- Code for Drools Article (
c)www.firstpartners.net 2005 -->
<!-- Import the Java Objects that we refer to in our rules -->
<java:import>java.lang.Object</java:import>
<java:import>java.lang.String</java:import>
<java:import>com.sample.DroolsTest1.*</java:import>
<java:functions>
public void printValues(int i, int j)
{
System.out.println("hi"+i+j);
}
</java:functions>
<!-- Check for XYZ Corp-->
<rule name="XYZCorp" >
<!-- Parameters we can pass into the business rule -->
<parameter identifier="message">
<class>com.sample.DroolsTest1.Message</class>
</parameter>
<!-- Conditions or 'Left Hand Side' (LHS) that must be met for
business rule to fire -->
<java:condition>message.getOp1()==2</java:condition>
!-- What happens when the business rule is activated -->
<java:consequence>
printValues(message.getOp1(),message.getOp2());
</java:consequence>
</rule>
</rule-set>
When I run the program I will get the following error. If I change
addPackageFromXml to addPackageFromDrl the error is same . So please
help me in this
(null: 7, 74): cvc-elt.1: Cannot find the declaration of element
'rule-set'.
org.drools.compiler.DroolsParserException
at org.drools.compiler.PackageBuilder.addPackageFromXml(Unknown
Source)
at com.sample.DroolsTest1.readRule(DroolsTest1.java:55)
at com.sample.DroolsTest1.main(DroolsTest1.java:21)
Thanks & Regards
Shilpa
The information contained in this electronic message and any attachments to this message
are intended for the exclusive use of the addressee(s) and may contain proprietary,
confidential or privileged information. If you are not the intended recipient, you should
not disseminate, distribute or copy this e-mail. Please notify the sender immediately and
destroy all copies of this message and any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient should check this
email and any attachments for the presence of viruses. The company accepts no liability
for any damage caused by any virus transmitted by this email.
www.wipro.com