My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Example  
An example to explain the big picture.
Featured, Phase-Requirements, Phase-Deploy, Example
Updated Feb 4, 2010 by jens.dietrich

Example

The code for this example is in the repository. The script contains only one rule:

rule1: if equals(category(customer),goldCustomer) then discount(customer,goldCustomerDiscount) 

The utility GenerateInterface is used to generate a public interface for this script. In particular, the class DiscountPolicy with the method getDiscount(Customer) is generated.

The Example script then does the following:

  1. reads the script from the rule
  2. compiles the (rule) into Java sources
  3. compiles the sources using the Java compiler API
  4. binds the object references in the script
  5. loads the generated class
  6. queries the class for the discount for a given customer
  7. prints the results and lists the rules used

Note that this mechanism works very much like JavaServerPages (JSPs)! The working directory containing the generated sources and compiled classes is in /takeWorkingDir.

	// prepare
	DiscountPolicy KB = null; // this is the generated interface
	BasicConfigurator.configure();

	// compile and bind constants referenced in rules
	KnowledgeBaseManager<DiscountPolicy> kbm = new KnowledgeBaseManager<DiscountPolicy>();
	Bindings bindings = new SimpleBindings();		
	bindings.put("goldCustomer",new CustomerCategory("gold"));
	bindings.put("goldCustomerDiscount",new Discount(20,true));
	KB = kbm.getKnowledgeBase(
	   DiscountPolicy.class, 
	   new ScriptKnowledgeSource("exampledata/example1/crm-example.take"),
	   bindings);
		
	// now use the generated classes to query the kb
	Customer john = new Customer("John");
	john.setCategory(new CustomerCategory("gold"));
	ResultSet<CustomerDiscount> result =  KB.getDiscount(john);
	System.out.println("The discount for John is: " + result.next().discount);
	    
	// print rules used
	System.out.println("The following rules have been used to calculate the discount: ");
	for (DerivationLogEntry e:result.getDerivationLog()) {
	        System.out.print(e.getCategory());
	    	System.out.print(" : ");
	    	System.out.println(e.getName());
	}
	    

Sign in to add a comment
Powered by Google Project Hosting