use of org.drools.core.WorkingMemory in project drools by kiegroup.
the class BaseMannersTest method getPathDone.
/**
* <pre>
* rule pathDone() {
* Context context; Seating seating;
* when {
* context : Context( state == Context.MAKE_PATH )
* seating : Seating( pathDone == false )
* } then {
* seating.setPathDone( true );
* context.setName( Context.CHECK_DONE );
* }
* }
* </pre>
*
* @return
* @throws InvalidRuleException
*/
private RuleImpl getPathDone() throws InvalidRuleException {
final RuleImpl rule = new RuleImpl("pathDone");
// -----------
// context : Context( state == Context.MAKE_PATH )
// -----------
final Pattern contextPattern = new Pattern(0, this.contextType, "context");
contextPattern.addConstraint(getLiteralConstraint(contextPattern, "state", Context.MAKE_PATH));
rule.addPattern(contextPattern);
final Declaration contextDeclaration = rule.getDeclaration("context");
// ---------------
// seating : Seating( pathDone == false )
// ---------------
final Pattern seatingPattern = new Pattern(1, this.seatingType, "seating");
seatingPattern.addConstraint(getLiteralConstraint(seatingPattern, "pathDone", false));
rule.addPattern(seatingPattern);
final Declaration seatingDeclaration = rule.getDeclaration("seating");
// ------------
// context.setName( Context.CHECK_DONE );
// seating.setPathDone( true );
// ------------
final Consequence consequence = new Consequence() {
public void evaluate(KnowledgeHelper drools, WorkingMemory workingMemory) throws ConsequenceException {
try {
RuleImpl rule = drools.getRule();
LeftTuple tuple = (LeftTuple) drools.getTuple();
Context context = (Context) drools.get(contextDeclaration);
Seating seating = (Seating) drools.get(seatingDeclaration);
seating.setPathDone(true);
// if ( seating.getId() == 6 ) {
// System.err.println( "pause" );
// }
drools.update(tuple.get(seatingDeclaration));
context.setState(Context.CHECK_DONE);
drools.update(tuple.get(contextDeclaration), context);
// System.err.println( "path done" + seating );
} catch (Exception e) {
e.printStackTrace();
throw new ConsequenceException(e);
}
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
};
rule.setConsequence(consequence);
return rule;
}
use of org.drools.core.WorkingMemory in project drools by kiegroup.
the class BaseMannersTest method getFindSeating.
/**
* <pre>
* rule findSeating() {
* Context context;
* int seatingId, seatingPid;
* String seatingRightGuestName, leftGuestName;
* Sex rightGuestSex;
* Hobby rightGuestHobby;
* Count count;
*
* when {
* context : Context( state == Context.ASSIGN_SEATS )
* Seating( seatingId:id, seatingPid:pid, pathDone == true
* seatingRightSeat:rightSeat seatingRightGuestName:rightGuestName )
* Guest( name == seatingRightGuestName, rightGuestSex:sex, rightGuestHobby:hobby )
* Guest( leftGuestName:name , sex != rightGuestSex, hobby == rightGuestHobby )
*
* count : Count()
*
* not ( Path( id == seatingId, guestName == leftGuestName) )
* not ( Chosen( id == seatingId, guestName == leftGuestName, hobby == rightGuestHobby) )
* } then {
* int newSeat = rightSeat + 1;
* drools.assert( new Seating( coung.getValue(), rightSeat, rightSeatName, leftGuestName, newSeat, countValue, id, false );
* drools.assert( new Path( countValue, leftGuestName, newSeat );
* drools.assert( new Chosen( id, leftGuestName, rightGuestHobby ) );
*
* System.err.println( "seat " + rightSeat + " " + rightSeatName + " " + leftGuestName );
*
* count.setCount( countValue + 1 );
* context.setPath( Context.MAKE_PATH );
* }
* }
* </pre>
*
* @return
* @throws InvalidRuleException
*/
private RuleImpl getFindSeating() throws InvalidRuleException {
final RuleImpl rule = new RuleImpl("findSeating");
// ---------------
// context : Context( state == Context.ASSIGN_SEATS )
// ---------------
final Pattern contextPattern = new Pattern(0, this.contextType, "context");
contextPattern.addConstraint(getLiteralConstraint(contextPattern, "state", Context.ASSIGN_SEATS));
rule.addPattern(contextPattern);
final Declaration contextDeclaration = rule.getDeclaration("context");
// -------------------------------
// Seating( seatingId:id, seatingPid:pid, pathDone == true
// seatingRightSeat:rightSeat seatingRightGuestName:rightGuestName )
// -------------------------------
final Pattern seatingPattern = new Pattern(1, this.seatingType);
setFieldDeclaration(seatingPattern, "id", "seatingId");
setFieldDeclaration(seatingPattern, "pid", "seatingPid");
seatingPattern.addConstraint(getLiteralConstraint(seatingPattern, "pathDone", true));
setFieldDeclaration(seatingPattern, "rightSeat", "seatingRightSeat");
setFieldDeclaration(seatingPattern, "rightGuestName", "seatingRightGuestName");
rule.addPattern(seatingPattern);
final Declaration seatingIdDeclaration = rule.getDeclaration("seatingId");
final Declaration seatingPidDeclaration = rule.getDeclaration("seatingPid");
final Declaration seatingRightGuestNameDeclaration = rule.getDeclaration("seatingRightGuestName");
final Declaration seatingRightSeatDeclaration = rule.getDeclaration("seatingRightSeat");
// --------------
// Guest( name == seatingRightGuestName, rightGuestSex:sex,
// rightGuestHobby:hobby )
// ---------------
final Pattern rightGuestPattern = new Pattern(2, this.guestType);
rightGuestPattern.addConstraint(getBoundVariableConstraint(rightGuestPattern, "name", seatingRightGuestNameDeclaration, "=="));
setFieldDeclaration(rightGuestPattern, "sex", "rightGuestSex");
setFieldDeclaration(rightGuestPattern, "hobby", "rightGuestHobby");
rule.addPattern(rightGuestPattern);
final Declaration rightGuestSexDeclaration = rule.getDeclaration("rightGuestSex");
final Declaration rightGuestHobbyDeclaration = rule.getDeclaration("rightGuestHobby");
// ----------------
// Guest( leftGuestName:name , sex != rightGuestSex, hobby ==
// rightGuestHobby )
// ----------------
final Pattern leftGuestPattern = new Pattern(3, this.guestType);
setFieldDeclaration(leftGuestPattern, "name", "leftGuestName");
leftGuestPattern.addConstraint(getBoundVariableConstraint(rightGuestPattern, "hobby", rightGuestHobbyDeclaration, "=="));
leftGuestPattern.addConstraint(getBoundVariableConstraint(leftGuestPattern, "sex", rightGuestSexDeclaration, "!="));
rule.addPattern(leftGuestPattern);
final Declaration leftGuestNameDeclaration = rule.getDeclaration("leftGuestName");
// ---------------
// count : Count()
// ---------------
final Pattern count = new Pattern(4, this.countType, "count");
rule.addPattern(count);
final Declaration countDeclaration = rule.getDeclaration("count");
// --------------
// not ( Path( id == seatingId, guestName == leftGuestName) )
// --------------
final Pattern notPathPattern = new Pattern(5, this.pathType);
notPathPattern.addConstraint(getBoundVariableConstraint(notPathPattern, "id", seatingIdDeclaration, "=="));
notPathPattern.addConstraint(getBoundVariableConstraint(notPathPattern, "guestName", leftGuestNameDeclaration, "=="));
final GroupElement notPath = GroupElementFactory.newNotInstance();
notPath.addChild(notPathPattern);
rule.addPattern(notPath);
// ------------
// not ( Chosen( id == seatingId, guestName == leftGuestName, hobby ==
// rightGuestHobby ) )
// ------------
final Pattern notChosenPattern = new Pattern(6, this.chosenType);
notChosenPattern.addConstraint(getBoundVariableConstraint(notChosenPattern, "id", seatingIdDeclaration, "=="));
notChosenPattern.addConstraint(getBoundVariableConstraint(notChosenPattern, "guestName", leftGuestNameDeclaration, "=="));
notChosenPattern.addConstraint(getBoundVariableConstraint(notChosenPattern, "hobby", rightGuestHobbyDeclaration, "=="));
final GroupElement notChosen = GroupElementFactory.newNotInstance();
notChosen.addChild(notChosenPattern);
rule.addPattern(notChosen);
// ------------
// int newSeat = rightSeat + 1;
// drools.assert( new Seating( coung.getValue(), rightSeat,
// rightSeatName, leftGuestName, newSeat, countValue, id, false );
// drools.assert( new Path( countValue, leftGuestName, newSeat );
// drools.assert( new Chosen( id, leftGuestName, rightGuestHobby ) );
//
// System.err.println( "seat " + rightSeat + " " + rightSeatName + " " +
// leftGuestName );
//
// count.setCount( countValue + 1 );
// context.setPath( Context.MAKE_PATH );
// ------------
final Consequence consequence = new Consequence() {
public void evaluate(KnowledgeHelper drools, WorkingMemory workingMemory) throws ConsequenceException {
try {
// MemoryVisitor visitor = new MemoryVisitor( ( InternalWorkingMemory ) workingMemory );
// visitor.visit( workingMemory.getRuleBase() );
RuleImpl rule = drools.getRule();
LeftTuple tuple = (LeftTuple) drools.getTuple();
Context context = (Context) drools.get(contextDeclaration);
Count count = (Count) drools.get(countDeclaration);
int seatId = seatingIdDeclaration.getExtractor().getIntValue((InternalWorkingMemory) workingMemory, tuple.get(seatingIdDeclaration).getObject());
int seatingRightSeat = seatingRightSeatDeclaration.getExtractor().getIntValue((InternalWorkingMemory) workingMemory, tuple.get(seatingRightSeatDeclaration).getObject());
String leftGuestName = (String) drools.get(leftGuestNameDeclaration);
String rightGuestName = (String) drools.get(seatingRightGuestNameDeclaration);
Hobby rightGuestHobby = (Hobby) drools.get(rightGuestHobbyDeclaration);
Seating seating = new Seating(count.getValue(), seatId, false, seatingRightSeat, rightGuestName, seatingRightSeat + 1, leftGuestName);
drools.insert(seating);
Path path = new Path(count.getValue(), seatingRightSeat + 1, leftGuestName);
drools.insert(path);
Chosen chosen = new Chosen(seatId, leftGuestName, rightGuestHobby);
drools.insert(chosen);
count.setValue(count.getValue() + 1);
// if ( count.getValue() == 5 ) {
// drools.retractObject( tuple.getFactHandleForDeclaration( countDeclaration ) );
// } else {
// drools.update( tuple.getFactHandleForDeclaration( countDeclaration ),
// count );
// }
drools.update(tuple.get(countDeclaration), count);
context.setState(Context.MAKE_PATH);
drools.update(tuple.get(contextDeclaration), context);
System.err.println("find seating : " + seating + " : " + path + " : " + chosen);
} catch (Exception e) {
e.printStackTrace();
throw new ConsequenceException(e);
}
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
};
rule.setConsequence(consequence);
return rule;
}
use of org.drools.core.WorkingMemory in project drools by kiegroup.
the class ConsequenceGenerator method generate.
public static void generate(final ConsequenceStub stub, KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) {
RuleTerminalNode rtn = (RuleTerminalNode) knowledgeHelper.getMatch().getTuple().getTupleSink();
final Declaration[] declarations = rtn.getRequiredDeclarations();
final Tuple tuple = knowledgeHelper.getTuple();
// Sort declarations based on their offset, so it can ascend the tuple's parents stack only once
final List<DeclarationMatcher> declarationMatchers = matchDeclarationsToTuple(declarations);
final ClassGenerator generator = createInvokerClassGenerator(stub, workingMemory).setInterfaces(Consequence.class, CompiledInvoker.class);
generator.addMethod(ACC_PUBLIC, "getName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
push(stub.getGeneratedInvokerClassName());
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(null, KnowledgeHelper.class, WorkingMemory.class), new String[] { "java/lang/Exception" }, new GeneratorHelper.EvaluateMethod() {
public void body(MethodVisitor mv) {
// Tuple tuple = knowledgeHelper.getTuple();
mv.visitVarInsn(ALOAD, 1);
invokeInterface(KnowledgeHelper.class, "getTuple", Tuple.class);
cast(LeftTuple.class);
// LeftTuple
mv.visitVarInsn(ASTORE, 3);
// Declaration[] declarations = ((RuleTerminalNode)knowledgeHelper.getMatch().getTuple().getTupleSink()).getDeclarations();
mv.visitVarInsn(ALOAD, 1);
invokeInterface(KnowledgeHelper.class, "getMatch", Activation.class);
invokeInterface(Activation.class, "getTuple", Tuple.class);
invokeInterface(Tuple.class, "getTupleSink", Sink.class);
cast(RuleTerminalNode.class);
invokeVirtual(RuleTerminalNode.class, "getRequiredDeclarations", Declaration[].class);
mv.visitVarInsn(ASTORE, 4);
Tuple currentTuple = tuple;
// astore start position for objects to store in loop
objAstorePos = 6;
int[] paramsPos = new int[declarations.length];
// declarationMatchers is already sorted by offset with tip declarations now first
for (DeclarationMatcher matcher : declarationMatchers) {
// original index refers to the array position with RuleTerminalNode.getDeclarations()
int i = matcher.getOriginalIndex();
int handlePos = objAstorePos;
int objPos = ++objAstorePos;
paramsPos[i] = handlePos;
currentTuple = traverseTuplesUntilDeclaration(currentTuple, matcher.getRootDistance(), 3);
// handle = tuple.getFactHandle()
mv.visitVarInsn(ALOAD, 3);
invokeInterface(Tuple.class, "getOriginalFactHandle", InternalFactHandle.class);
mv.visitVarInsn(ASTORE, handlePos);
String declarationType = declarations[i].getTypeName();
if (stub.getNotPatterns()[i]) {
// notPattern indexes field declarations
// declarations[i].getValue((InternalWorkingMemory)workingMemory, fact[i].getObject());
// org.kie.rule.Declaration[]
mv.visitVarInsn(ALOAD, 4);
// i
push(i);
// declarations[i]
mv.visitInsn(AALOAD);
// WorkingMemory
mv.visitVarInsn(ALOAD, 2);
cast(InternalWorkingMemory.class);
// handle[i]
mv.visitVarInsn(ALOAD, handlePos);
invokeInterface(InternalFactHandle.class, "getObject", Object.class);
storeObjectFromDeclaration(declarations[i], declarationType);
// The facthandle should be set to that of the field, if it's an object, otherwise this will return null
// fact[i] = (InternalFactHandle)workingMemory.getFactHandle(obj);
mv.visitVarInsn(ALOAD, 2);
loadAsObject(objPos);
invokeInterface(WorkingMemory.class, "getFactHandle", FactHandle.class, Object.class);
cast(InternalFactHandle.class);
mv.visitVarInsn(ASTORE, handlePos);
} else {
// handle[i]
mv.visitVarInsn(ALOAD, handlePos);
invokeInterface(InternalFactHandle.class, "getObject", Object.class);
mv.visitTypeInsn(CHECKCAST, internalName(declarationType));
// obj[i]
objAstorePos += store(objPos, declarationType);
}
}
// @{ruleClassName}.@{methodName}(KnowledgeHelper, @foreach{declr : declarations} Object, FactHandle @end)
StringBuilder consequenceMethodDescr = new StringBuilder("(L" + KnowledgeHelper.class.getName().replace('.', '/') + ";");
// KnowledgeHelper
mv.visitVarInsn(ALOAD, 1);
for (int i = 0; i < declarations.length; i++) {
// obj[i]
load(paramsPos[i] + 1);
// handle[i]
mv.visitVarInsn(ALOAD, paramsPos[i]);
consequenceMethodDescr.append(typeDescr(declarations[i].getTypeName())).append("L" + FactHandle.class.getName().replace('.', '/') + ";");
}
// @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
parseGlobals(stub.getGlobals(), stub.getGlobalTypes(), 2, consequenceMethodDescr);
consequenceMethodDescr.append(")V");
mv.visitMethodInsn(INVOKESTATIC, stub.getInternalRuleClassName(), stub.getMethodName(), consequenceMethodDescr.toString());
mv.visitInsn(RETURN);
}
});
stub.setConsequence(generator.<Consequence>newInstance());
}
Aggregations