Now with Drools demo (Rete System to match which level building rules to fire for which PlayerProfiles/Archetypes).

This commit is contained in:
Woody Folsom
2012-03-17 16:46:01 -04:00
parent 28cc5715e9
commit 27d357f1fc
13 changed files with 100 additions and 0 deletions

View File

@@ -8,6 +8,15 @@
<classpathentry kind="lib" path="lib/slf4j-api-1.6.4.jar"/>
<classpathentry kind="lib" path="lib/xuggle-xuggler.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/drools-compiler-5.3.0.Final.jar"/>
<classpathentry kind="lib" path="lib/drools-core-5.3.0.Final.jar"/>
<classpathentry kind="lib" path="lib/drools-decisiontables-5.3.0.Final.jar"/>
<classpathentry kind="lib" path="lib/drools-jsr94-5.3.0.Final.jar"/>
<classpathentry kind="lib" path="lib/drools-api-5.2.0.M1.jar"/>
<classpathentry kind="lib" path="lib/xstream-1.4.2.jar"/>
<classpathentry kind="lib" path="lib/knowledge-api-5.3.0.Final.jar"/>
<classpathentry kind="lib" path="lib/ecj.jar"/>
<classpathentry kind="lib" path="lib/antlr-3.4-complete-no-antlrv2.jar"/>
<classpathentry kind="lib" path="lib/mvel2-2.1.Beta8.jar"/>
<classpathentry kind="output" path="classes"/>
</classpath>

Binary file not shown.

BIN
lib/drools-api-5.2.0.M1.jar Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
lib/ecj.jar Normal file

Binary file not shown.

Binary file not shown.

BIN
lib/mvel2-2.1.Beta8.jar Normal file

Binary file not shown.

View File

@@ -0,0 +1,7 @@
package org.drools.examples.simple;
rule "Account balance is less than 100"
when
$account : Account( balance < 100 ) // condition
then
System.out.println("Account number: " + $account.getId() + " is below 100"); // consequence
end

View File

@@ -0,0 +1,19 @@
package org.drools.examples.simple;
public class Account {
private int balance;
private String Id;
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
}

View File

@@ -0,0 +1,65 @@
package org.drools.examples.simple;
import java.io.File;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.StatelessKnowledgeSession;
public class DroolsExample {
/**
* @param args
*/
public static void main(String[] args) {
//Create KnowledgeBase...
KnowledgeBase knowledgeBase = createKnowledgeBase();
//Create a statefull session
StatefulKnowledgeSession session = knowledgeBase.newStatefulKnowledgeSession();
//Create Facts - two ban accounts
Account account = new Account();
account.setBalance(10);
account.setId("N1");
Account account2 = new Account();
account2.setBalance(120);
account2.setId("N2");
//Insert the bank account facts into a State full session
session.insert(account);
session.insert(account2);
//Note that at this point, afetr fact insertation the Agenda has one activation ready to be fired.
//Account1 is less than 100.
//Only now we will fire the rules which are already in the agenda
session.fireAllRules();
//A message of N1 is less than 100 will be printed to stdout.
}
/**
* Create new knowledge base
*/
private static KnowledgeBase createKnowledgeBase() {
KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
//Add drl file into builder
File accountRules = new File("rules/BasicAccountRule.drl"); //Where the account rule is.
builder.add(ResourceFactory.newFileResource(accountRules), ResourceType.DRL);
if (builder.hasErrors()) {
throw new RuntimeException(builder.getErrors().toString());
}
KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
//Add to Knowledge Base packages from the builder which are actually the rules from the drl file.
knowledgeBase.addKnowledgePackages(builder.getKnowledgePackages());
return knowledgeBase;
}
}