package com.hp.drools.odm.demo;
import java.util.ArrayList;
import org.drools.RuleBase;
import org.drools.StatefulSession;
import com.hp.drools.odm.DrlContent;
import com.hp.drools.odm.GlobalStatements;
import com.hp.drools.odm.ImportStatements;
import com.hp.drools.odm.RawRightHeadSide;
import com.hp.drools.odm.RightHeadSideStatementBuilder;
import com.hp.drools.odm.Rule;
import com.hp.drools.odm.SimpleAndLeftHeadSide;
import com.hp.drools.util.CollectionUtil;
import com.hp.drools.util.DroolsGlobal;
import com.hp.drools.util.DroolsRuleBaseCache;
import com.hp.drools.util.DroolsUtil;
import com.hp.drools.util.ICollectionObjectJob;
public class Demo {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// initial fact object
Lot lot1 = new Lot();
lot1.setLotId("A001.1");
lot1.setStatus("running");
lot1.setWaferQty(new Integer(24));
Lot lot2 = new Lot();
lot2.setLotId("A002.1");
lot2.setStatus("Wait NP");
lot2.setWaferQty(new Integer(25));
Stage stage1 = new Stage();
stage1.setStageId("S001.1");
stage1.setName("mask");
Stage stage2 = new Stage();
stage2.setStageId("S002.1");
stage2.setName("wash");
// construct rule content
ImportStatements importStatements = new ImportStatements();
importStatements.addImportClass(Lot.class);
importStatements.addImportClass(Stage.class);
importStatements.addImportClass(ActionB.class);
GlobalStatements globalStatements = new GlobalStatements();
ArrayList actionExecuteRule1 = new ArrayList();
globalStatements.addGlobalVariable("actionExecuteRule1", actionExecuteRule1.getClass());
SimpleAndLeftHeadSide lhs = new SimpleAndLeftHeadSide();
ArrayList lotWaferQtys = new ArrayList();
lotWaferQtys.add(new Integer(24));
lotWaferQtys.add(new Integer(25));
ArrayList lotIds = new ArrayList();
lotIds.add("A001.1");
lotIds.add("A002.1");
lhs.addConditionElement(new Question("lot", "Lot", "status", "==", "running").convertToDrlConditionElement());
//lhs.addConditionElement(new Question("lot1", "Lot", "waferQty", ">=", new Integer(24)).convertToDrlConditionElement());
//lhs.addConditionElement(new Question(null, "Lot", "waferQty", "in", lotWaferQtys).convertToDrlConditionElement());
lhs.addConditionElement(new Question(null, "Lot", "lotId", "not in", lotIds).convertToDrlConditionElement());
lhs.addConditionElement(new Question("stage", "Stage", "name", "==", "wash").convertToDrlConditionElement());
RightHeadSideStatementBuilder rightHeadSideStatementBuilder
= new RightHeadSideStatementBuilder();
rightHeadSideStatementBuilder.addStatment("System.out.println(\"lot.id=\" + $lot.getLotId());");
rightHeadSideStatementBuilder.addStatment("System.out.println(\"lot.status=\" + $lot.getStatus());");
rightHeadSideStatementBuilder.addStatment("System.out.println(\"lot.waferQty=\" + $lot.getWaferQty());");
// rightHeadSideStatementBuilder.addStatment("System.out.println(\"lot1.id=\" + $lot.getLotId());");
// rightHeadSideStatementBuilder.addStatment("System.out.println(\"lot1.status=\" + $lot1.getStatus());");
// rightHeadSideStatementBuilder.addStatment("System.out.println(\"lot1.waferQty=\" + $lot1.getWaferQty());");
rightHeadSideStatementBuilder.addStatment("System.out.println(\"stage.id=\" + $stage.getStageId());");
rightHeadSideStatementBuilder.addStatment("System.out.println(\"stage.name=\" + $stage.getName());");
rightHeadSideStatementBuilder.addStatment("actionExecuteRule1.add(new ActionB());");
RawRightHeadSide rhs = new RawRightHeadSide(rightHeadSideStatementBuilder.toString());
Rule rule = new Rule("Auto Generate Rule", lhs, rhs);
DrlContent drlContent = new DrlContent("com.hp.drools.odm.demo");
drlContent.setImportStatements(importStatements);
drlContent.setGlobalStatements(globalStatements);
drlContent.addRule(rule);
System.out.println(drlContent.convertToDrlContent());
// fire rule to run
String ruleBaseKey = "demo";
RuleBase ruleBase = DroolsRuleBaseCache.addRuleBaseByContent(ruleBaseKey,
new String[] {drlContent.convertToDrlContent()});
StatefulSession statefulSession = DroolsUtil.getSession(ruleBase);
DroolsGlobal actionExecuteRule1Global = new DroolsGlobal();
actionExecuteRule1Global.setVariableName("actionExecuteRule1");
actionExecuteRule1Global.setVariableObject(actionExecuteRule1);
DroolsUtil.setGlobal(statefulSession, actionExecuteRule1Global);
DroolsUtil.insertFact(statefulSession, lot1);
DroolsUtil.insertFact(statefulSession, lot2);
DroolsUtil.insertFact(statefulSession, stage1);
DroolsUtil.insertFact(statefulSession, stage2);
DroolsUtil.fireAllRules(statefulSession);
CollectionUtil.doJobForEachObject(actionExecuteRule1, new ICollectionObjectJob() {
public void job(int objectIndex, Object objectInCollection) {
AbstractAction action = (AbstractAction) objectInCollection;
action.action();
}
});
DroolsUtil.dispose(statefulSession);
// lhs = new SimpleAndLeftHeadSide();
// lhs.addConditionElement(new Question("lot", "Lot", "status", "==", "Wait NP").convertToDrlConditionElement());
// lhs.addConditionElement(new Question(null, "Lot", "waferQty", ">=", new Integer(24)).convertToDrlConditionElement());
// lhs.addConditionElement(new Question("stage", "Stage", "name", "==", "mask").convertToDrlConditionElement());
//
// rule.setLhs(lhs);
//
// System.out.println(drlContent.convertToDrlContent());
//
//
// actionExecuteRule1.clear();
//
// // fire rule to run
// ruleBaseKey = "demo";
// ruleBase = DroolsRuleBaseCache.refreshRuleBaseByContent(ruleBaseKey,
// new String[] {drlContent.convertToDrlContent()});
//
// statefulSession = DroolsUtil.getSession(ruleBase);
//
// actionExecuteRule1Global = new DroolsGlobal();
// actionExecuteRule1Global.setVariableName("actionExecuteRule1");
// actionExecuteRule1Global.setVariableObject(actionExecuteRule1);
// DroolsUtil.setGlobal(statefulSession, actionExecuteRule1Global);
//
// DroolsUtil.insertFact(statefulSession, lot1);
// DroolsUtil.insertFact(statefulSession, lot2);
// DroolsUtil.insertFact(statefulSession, stage1);
// DroolsUtil.insertFact(statefulSession, stage2);
//
// DroolsUtil.fireAllRules(statefulSession);
//
// CollectionUtil.doJobForEachObject(actionExecuteRule1, new ICollectionObjectJob() {
//
// public void job(int objectIndex, Object objectInCollection) {
// AbstractAction action = (AbstractAction) objectInCollection;
// action.action();
// }
// });
//
// DroolsUtil.dispose(statefulSession);
}
}
评论0