use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class BaseMannersTest method getMakePath.
/**
* <pre>
* rule makePath() {
* Context context;
* int seatingId, seatingPid, pathSeat;
* String pathGuestName;
*
* when {
* Context( state == Context.MAKE_PATH )
* Seating( seatingId:id, seatingPid:pid, pathDone == false )
* Path( id == seatingPid, pathGuestName:guest, pathSeat:seat )
* (not Path( id == seatingId, guestName == pathGuestName )
* } else {
* drools.assert( new Path( seatingId, pathSeat, pathGuestName ) );
*
* }
* }
* </pre>
*
* @return
* @throws InvalidRuleException
*/
private RuleImpl getMakePath() throws InvalidRuleException {
final RuleImpl rule = new RuleImpl("makePath");
// -----------
// context : Context( state == Context.MAKE_PATH )
// -----------
final Pattern contextPattern = new Pattern(0, this.contextType);
contextPattern.addConstraint(getLiteralConstraint(contextPattern, "state", Context.MAKE_PATH));
rule.addPattern(contextPattern);
// ---------------
// Seating( seatingId:id, seatingPid:pid, pathDone == false )
// ---------------
final Pattern seatingPattern = new Pattern(1, this.seatingType);
setFieldDeclaration(seatingPattern, "id", "seatingId");
setFieldDeclaration(seatingPattern, "pid", "seatingPid");
seatingPattern.addConstraint(getLiteralConstraint(seatingPattern, "pathDone", false));
rule.addPattern(seatingPattern);
final Declaration seatingIdDeclaration = rule.getDeclaration("seatingId");
final Declaration seatingPidDeclaration = rule.getDeclaration("seatingPid");
// -----------
// Path( id == seatingPid, pathGuestName:guestName, pathSeat:seat )
// -----------
final Pattern pathPattern = new Pattern(2, this.pathType);
pathPattern.addConstraint(getBoundVariableConstraint(pathPattern, "id", seatingPidDeclaration, "=="));
setFieldDeclaration(pathPattern, "guestName", "pathGuestName");
setFieldDeclaration(pathPattern, "seat", "pathSeat");
rule.addPattern(pathPattern);
final Declaration pathGuestNameDeclaration = rule.getDeclaration("pathGuestName");
final Declaration pathSeatDeclaration = rule.getDeclaration("pathSeat");
// -------------
// (not Path( id == seatingId, guestName == pathGuestName )
// -------------
final Pattern notPathPattern = new Pattern(3, this.pathType);
notPathPattern.addConstraint(getBoundVariableConstraint(notPathPattern, "id", seatingIdDeclaration, "=="));
notPathPattern.addConstraint(getBoundVariableConstraint(notPathPattern, "guestName", pathGuestNameDeclaration, "=="));
final GroupElement not = GroupElementFactory.newNotInstance();
not.addChild(notPathPattern);
rule.addPattern(not);
// ------------
// drools.assert( new Path( id, pathName, pathSeat ) );
// ------------
final Consequence consequence = new Consequence() {
public void evaluate(KnowledgeHelper drools, WorkingMemory workingMemory) throws ConsequenceException {
try {
RuleImpl rule = drools.getRule();
LeftTuple tuple = (LeftTuple) drools.getTuple();
int id = seatingIdDeclaration.getExtractor().getIntValue((InternalWorkingMemory) workingMemory, tuple.get(seatingIdDeclaration).getObject());
int seat = pathSeatDeclaration.getExtractor().getIntValue((InternalWorkingMemory) workingMemory, tuple.get(pathSeatDeclaration).getObject());
String guestName = (String) drools.get(pathGuestNameDeclaration);
Path path = new Path(id, seat, guestName);
drools.insert(path);
// System.err.println( "make path : " + path );
} 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.common.InternalWorkingMemory in project drools by kiegroup.
the class ReteObjectTypeNode method attach.
public void attach(BuildContext context) {
super.attach(context);
if (context == null) {
return;
}
// to working memories
for (InternalWorkingMemory workingMemory : context.getWorkingMemories()) {
PropagationContextFactory pctxFactory = workingMemory.getKnowledgeBase().getConfiguration().getComponentFactory().getPropagationContextFactory();
final PropagationContext propagationContext = pctxFactory.createPropagationContext(workingMemory.getNextPropagationIdCounter(), PropagationContext.Type.RULE_ADDITION, null, null, null);
propagationContext.setEntryPoint(((EntryPointNode) this.source).getEntryPoint());
this.source.updateSink(this, propagationContext, workingMemory);
}
}
use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class ReteooBuilder method removeRules.
public synchronized void removeRules(Collection<RuleImpl> rulesToBeRemoved) {
// reset working memories for potential propagation
InternalWorkingMemory[] workingMemories = this.kBase.getWorkingMemories();
for (RuleImpl rule : rulesToBeRemoved) {
if (rule.hasChildren() && !rulesToBeRemoved.containsAll(rule.getChildren())) {
throw new RuntimeException("Cannot remove parent rule " + rule + " without having removed all its chikdren");
}
final RuleRemovalContext context = new RuleRemovalContext(rule);
context.setKnowledgeBase(kBase);
BaseNode[] rulesTerminalNodes = rules.remove(rule.getFullyQualifiedName());
if (rulesTerminalNodes == null) {
// there couldn't be any rule to be removed if it comes from a broken drl
continue;
}
for (BaseNode node : rulesTerminalNodes) {
removeTerminalNode(context, (TerminalNode) node, workingMemories);
}
if (rule.isQuery()) {
this.queries.remove(rule.getName());
}
if (rule.getParent() != null && !rulesToBeRemoved.contains(rule.getParent())) {
rule.getParent().removeChild(rule);
}
}
}
use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class ReteooBuilder method removeLeftTupleNode.
private boolean removeLeftTupleNode(InternalWorkingMemory[] wms, RuleRemovalContext context, Map<Integer, BaseNode> stillInUse, BaseNode node) {
boolean removed;
removed = node.remove(context, this, wms);
if (removed) {
stillInUse.remove(node.getId());
// phreak must clear node memories, although this should ideally be pushed into AddRemoveRule
for (InternalWorkingMemory workingMemory : wms) {
workingMemory.clearNodeMemory((MemoryFactory) node);
}
} else {
stillInUse.put(node.getId(), node);
}
return removed;
}
use of org.drools.core.common.InternalWorkingMemory in project jbpm by kiegroup.
the class MVELAction method execute.
public void execute(ProcessContext context) throws Exception {
int length = unit.getOtherIdentifiers().length;
Object[] vars = new Object[length];
if (unit.getOtherIdentifiers() != null) {
for (int i = 0; i < length; i++) {
vars[i] = context.getVariable(unit.getOtherIdentifiers()[i]);
}
}
InternalWorkingMemory internalWorkingMemory = null;
if (context.getKieRuntime() instanceof StatefulKnowledgeSessionImpl) {
internalWorkingMemory = ((StatefulKnowledgeSessionImpl) context.getKieRuntime()).getInternalWorkingMemory();
} else if (context.getKieRuntime() instanceof StatelessKnowledgeSessionImpl) {
StatefulKnowledgeSession statefulKnowledgeSession = ((StatelessKnowledgeSessionImpl) context.getKieRuntime()).newWorkingMemory();
internalWorkingMemory = ((StatefulKnowledgeSessionImpl) statefulKnowledgeSession).getInternalWorkingMemory();
}
VariableResolverFactory factory = unit.getFactory(context, // No previous declarations
null, // No rule
null, // No "right object"
null, // No (left) tuples
null, vars, internalWorkingMemory, (GlobalResolver) context.getKieRuntime().getGlobals());
// KnowledgePackage pkg = context.getKnowledgeRuntime().getKnowledgeBase().getKnowledgePackage( "MAIN" );
// if ( pkg != null && pkg instanceof KnowledgePackageImp) {
// MVELDialectRuntimeData data = ( MVELDialectRuntimeData ) ((KnowledgePackageImp) pkg).pkg.getDialectRuntimeRegistry().getDialectData( id );
// factory.setNextFactory( data.getFunctionFactory() );
// }
//
MVELSafeHelper.getEvaluator().executeExpression(this.expr, null, factory);
}
Aggregations