diff --git a/.classpath b/.classpath index f2c8d8b..eab132d 100644 --- a/.classpath +++ b/.classpath @@ -8,6 +8,15 @@ + + + + + + + + + diff --git a/lib/antlr-3.4-complete-no-antlrv2.jar b/lib/antlr-3.4-complete-no-antlrv2.jar new file mode 100644 index 0000000..bf0ad01 Binary files /dev/null and b/lib/antlr-3.4-complete-no-antlrv2.jar differ diff --git a/lib/drools-api-5.2.0.M1.jar b/lib/drools-api-5.2.0.M1.jar new file mode 100644 index 0000000..302c4d5 Binary files /dev/null and b/lib/drools-api-5.2.0.M1.jar differ diff --git a/lib/drools-compiler-5.3.0.Final.jar b/lib/drools-compiler-5.3.0.Final.jar new file mode 100644 index 0000000..d6a1a5c Binary files /dev/null and b/lib/drools-compiler-5.3.0.Final.jar differ diff --git a/lib/drools-core-5.3.0.Final.jar b/lib/drools-core-5.3.0.Final.jar new file mode 100644 index 0000000..65ddb7b Binary files /dev/null and b/lib/drools-core-5.3.0.Final.jar differ diff --git a/lib/drools-decisiontables-5.3.0.Final.jar b/lib/drools-decisiontables-5.3.0.Final.jar new file mode 100644 index 0000000..101d740 Binary files /dev/null and b/lib/drools-decisiontables-5.3.0.Final.jar differ diff --git a/lib/drools-jsr94-5.3.0.Final.jar b/lib/drools-jsr94-5.3.0.Final.jar new file mode 100644 index 0000000..2bab5e9 Binary files /dev/null and b/lib/drools-jsr94-5.3.0.Final.jar differ diff --git a/lib/ecj.jar b/lib/ecj.jar new file mode 100644 index 0000000..683475a Binary files /dev/null and b/lib/ecj.jar differ diff --git a/lib/knowledge-api-5.3.0.Final.jar b/lib/knowledge-api-5.3.0.Final.jar new file mode 100644 index 0000000..dd33d24 Binary files /dev/null and b/lib/knowledge-api-5.3.0.Final.jar differ diff --git a/lib/mvel2-2.1.Beta8.jar b/lib/mvel2-2.1.Beta8.jar new file mode 100644 index 0000000..1bbc05f Binary files /dev/null and b/lib/mvel2-2.1.Beta8.jar differ diff --git a/rules/BasicAccountRule.drl b/rules/BasicAccountRule.drl new file mode 100644 index 0000000..9ea1bc0 --- /dev/null +++ b/rules/BasicAccountRule.drl @@ -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 \ No newline at end of file diff --git a/src/org/drools/examples/simple/Account.java b/src/org/drools/examples/simple/Account.java new file mode 100644 index 0000000..210c5b5 --- /dev/null +++ b/src/org/drools/examples/simple/Account.java @@ -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; + } + +} diff --git a/src/org/drools/examples/simple/DroolsExample.java b/src/org/drools/examples/simple/DroolsExample.java new file mode 100644 index 0000000..8722524 --- /dev/null +++ b/src/org/drools/examples/simple/DroolsExample.java @@ -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; + } + +} \ No newline at end of file