use of org.drools.core.common.BaseNode in project drools by kiegroup.
the class StatefulKnowledgeSessionImpl method internalGetQueryResult.
protected QueryResultsImpl internalGetQueryResult(boolean calledFromRHS, String queryName, Object... arguments) {
try {
startOperation();
this.lock.lock();
this.kBase.executeQueuedActions();
// eventually enqueued by events that have been inserted when already expired
if (calledFromRHS) {
flushPropagations();
flushPropagations();
} else {
agenda.executeFlush();
agenda.executeFlush();
}
DroolsQuery queryObject = new DroolsQuery(queryName, arguments, getQueryListenerInstance(), false, null, null, null, null, null);
InternalFactHandle handle = this.handleFactory.newFactHandle(queryObject, null, this, this);
final PropagationContext pCtx = pctxFactory.createPropagationContext(getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, null, null, handle, getEntryPoint());
BaseNode[] tnodes = evalQuery(queryName, queryObject, handle, pCtx, calledFromRHS);
List<Map<String, Declaration>> decls = new ArrayList<Map<String, Declaration>>();
if (tnodes != null) {
for (BaseNode node : tnodes) {
decls.add(((QueryTerminalNode) node).getSubRule().getOuterDeclarations());
}
}
this.handleFactory.destroyFactHandle(handle);
return new QueryResultsImpl((List<QueryRowWithSubruleIndex>) queryObject.getQueryResultCollector().getResults(), decls.toArray(new Map[decls.size()]), this, (queryObject.getQuery() != null) ? queryObject.getQuery().getParameters() : new Declaration[0]);
} finally {
this.lock.unlock();
endOperation();
}
}
use of org.drools.core.common.BaseNode in project drools by kiegroup.
the class ReteooRuleBuilder method addSubRule.
private TerminalNode addSubRule(final BuildContext context, final GroupElement subrule, final int subruleIndex, final RuleImpl rule) throws InvalidPatternException {
context.setSubRule(subrule);
// gets the appropriate builder
ReteooComponentBuilder builder = this.utils.getBuilderFor(subrule);
// checks if an initial-fact is needed
if (builder.requiresLeftActivation(this.utils, subrule)) {
this.addInitialFactPattern(subrule);
}
// builds and attach
builder.build(context, this.utils, subrule);
if (context.isTerminated()) {
context.setTerminated(false);
return ((TerminalNode) context.getLastNode());
}
if (rule.getTimer() != null) {
builder = this.utils.getBuilderFor(Timer.class);
builder.build(context, this.utils, rule.getTimer());
}
ActivationListenerFactory factory = context.getKnowledgeBase().getConfiguration().getActivationListenerFactory(rule.getActivationListener());
TerminalNode terminal = factory.createActivationListener(context.getNextId(), context.getTupleSource(), rule, subrule, subruleIndex, context);
BaseNode baseTerminalNode = (BaseNode) terminal;
baseTerminalNode.networkUpdated(new UpdateContext());
baseTerminalNode.attach(context);
setPathEndNodes(context);
AddRemoveRule.addRule(terminal, context.getWorkingMemories(), context.getKnowledgeBase());
// adds the terminal node to the list of nodes created/added by this sub-rule
context.getNodes().add(baseTerminalNode);
return terminal;
}
use of org.drools.core.common.BaseNode in project drools by kiegroup.
the class ReteooBuilder method resetMasks.
private void resetMasks(Collection<BaseNode> nodes) {
NodeSet leafSet = new NodeSet();
for (BaseNode node : nodes) {
if (node.getType() == NodeTypeEnums.AlphaNode) {
ObjectSource source = (AlphaNode) node;
while (true) {
source.resetInferredMask();
BaseNode parent = source.getParentObjectSource();
if (parent.getType() != NodeTypeEnums.AlphaNode) {
break;
}
source = (ObjectSource) parent;
}
updateLeafSet(source, leafSet);
} else if (NodeTypeEnums.isBetaNode(node)) {
BetaNode betaNode = (BetaNode) node;
if (betaNode.isInUse()) {
leafSet.add(betaNode);
}
} else if (NodeTypeEnums.isTerminalNode(node)) {
RuleTerminalNode rtNode = (RuleTerminalNode) node;
if (rtNode.isInUse()) {
leafSet.add(rtNode);
}
}
}
for (BaseNode node : leafSet) {
if (NodeTypeEnums.isTerminalNode(node)) {
((TerminalNode) node).initInferredMask();
} else {
// else node instanceof BetaNode
((BetaNode) node).initInferredMask();
}
}
}
use of org.drools.core.common.BaseNode in project drools by kiegroup.
the class BuildUtils method attachNode.
/**
* Attaches a node into the network. If a node already exists that could
* substitute, it is used instead.
*
* @param context
* The current build context
* @param candidate
* The node to attach.
*
* @return the actual attached node that may be the one given as parameter
* or eventually one that was already in the cache if sharing is enabled
*/
public <T extends BaseNode> T attachNode(BuildContext context, T candidate) {
BaseNode node = null;
RuleBasePartitionId partition = null;
if (candidate.getType() == NodeTypeEnums.EntryPointNode) {
// entry point nodes are always shared
node = context.getKnowledgeBase().getRete().getEntryPointNode(((EntryPointNode) candidate).getEntryPoint());
// all EntryPointNodes belong to the main partition
partition = RuleBasePartitionId.MAIN_PARTITION;
} else if (candidate.getType() == NodeTypeEnums.ObjectTypeNode) {
// object type nodes are always shared
Map<ObjectType, ObjectTypeNode> map = context.getKnowledgeBase().getRete().getObjectTypeNodes(context.getCurrentEntryPoint());
if (map != null) {
ObjectTypeNode otn = map.get(((ObjectTypeNode) candidate).getObjectType());
if (otn != null) {
// adjusting expiration offset
otn.mergeExpirationOffset((ObjectTypeNode) candidate);
node = otn;
}
}
// all ObjectTypeNodes belong to the main partition
partition = RuleBasePartitionId.MAIN_PARTITION;
} else if (isSharingEnabledForNode(context, candidate)) {
if ((context.getTupleSource() != null) && NodeTypeEnums.isLeftTupleSink(candidate)) {
node = context.getTupleSource().getSinkPropagator().getMatchingNode(candidate);
} else if ((context.getObjectSource() != null) && NodeTypeEnums.isObjectSink(candidate)) {
node = context.getObjectSource().getObjectSinkPropagator().getMatchingNode(candidate);
} else {
throw new RuntimeException("This is a bug on node sharing verification. Please report to development team.");
}
}
if (node != null && !areNodesCompatibleForSharing(context, node, candidate)) {
node = null;
}
if (node == null) {
// only attach() if it is a new node
node = candidate;
// new node, so it must be labeled
if (partition == null) {
// if it does not has a predefined label
if (context.getPartitionId() == null) {
// if no label in current context, create one
context.setPartitionId(context.getKnowledgeBase().createNewPartitionId());
}
partition = context.getPartitionId();
}
// set node whit the actual partition label
node.setPartitionId(context, partition);
node.attach(context);
// adds the node to the context list to track all added nodes
context.getNodes().add(node);
} else {
// shared node found
mergeNodes(node, candidate);
// undo previous id assignment
context.releaseId(candidate);
if (partition == null && context.getPartitionId() == null) {
partition = node.getPartitionId();
// if no label in current context, create one
context.setPartitionId(partition);
}
}
node.addAssociation(context, context.getRule());
return (T) node;
}
Aggregations