use of org.drools.core.rule.EntryPointId in project drools by kiegroup.
the class KiePackagesBuilder method addPatternForVariable.
private Pattern addPatternForVariable(RuleContext ctx, GroupElement group, Variable patternVariable) {
Pattern pattern = new Pattern(ctx.getNextPatternIndex(), // offset will be set by ReteooBuilder
0, getObjectType(patternVariable), patternVariable.getName(), true);
if (patternVariable instanceof org.drools.model.Declaration) {
org.drools.model.Declaration decl = (org.drools.model.Declaration) patternVariable;
if (decl.getSource() != null) {
if (decl.getSource() instanceof EntryPoint) {
pattern.setSource(new EntryPointId(((EntryPoint) decl.getSource()).getName()));
} else if (decl.getSource() instanceof WindowReference) {
WindowReference<?> window = (WindowReference) decl.getSource();
if (!ctx.getPkg().getWindowDeclarations().containsKey(window.getName())) {
createWindowReference(ctx, window);
}
pattern.setSource(new org.drools.core.rule.WindowReference(window.getName()));
} else if (decl.getSource() instanceof From) {
From<?> from = (From) decl.getSource();
DataProvider provider = new LambdaDataProvider(ctx.getDeclaration(from.getVariable()), from.getProvider(), from.isReactive());
org.drools.core.rule.From fromSource = new org.drools.core.rule.From(provider);
fromSource.setResultPattern(pattern);
pattern.setSource(fromSource);
} else if (decl.getSource() instanceof UnitData) {
UnitData unitData = (UnitData) decl.getSource();
pattern.setSource(new EntryPointId(ctx.getRule().getRuleUnitClassName() + "." + unitData.getName()));
} else {
throw new UnsupportedOperationException("Unknown source: " + decl.getSource());
}
} else {
Accumulate accSource = ctx.getAccumulateSource(patternVariable);
if (accSource != null) {
for (RuleConditionElement element : group.getChildren()) {
if (element instanceof Pattern && ((Pattern) element).getSource() == accSource) {
if (accSource instanceof MultiAccumulate) {
((Pattern) element).getConstraints().forEach(pattern::addConstraint);
((Pattern) element).getDeclarations().values().forEach(d -> {
pattern.addDeclaration(d);
d.setPattern(pattern);
});
}
group.getChildren().remove(element);
break;
}
}
pattern.setSource(accSource);
}
}
if (decl.getWindow() != null) {
pattern.addBehavior(createWindow(decl.getWindow()));
}
}
ctx.registerPattern(patternVariable, pattern);
return pattern;
}
use of org.drools.core.rule.EntryPointId in project drools by kiegroup.
the class MVELFromBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
String text = ((FromDescr) descr).getExpression();
Optional<EntryPointId> entryPointId = context.getEntryPointId(text);
if (entryPointId.isPresent()) {
return entryPointId.get();
}
// This builder is re-usable in other dialects, so specify by name
MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
boolean typeSafe = context.isTypesafe();
if (!dialect.isStrictMode()) {
context.setTypesafe(false);
}
try {
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
AnalysisResult analysis = dialect.analyzeExpression(context, descr, text, new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
if (analysis == null) {
// something bad happened
return null;
}
Class<?> returnType = ((MVELAnalysisResult) analysis).getReturnType();
if (prefixPattern != null && !prefixPattern.isCompatibleWithFromReturnType(returnType)) {
context.addError(new DescrBuildError(descr, context.getRuleDescr(), null, "Pattern of type: '" + prefixPattern.getObjectType() + "' on rule '" + context.getRuleDescr().getName() + "' is not compatible with type " + returnType.getCanonicalName() + " returned by source"));
return null;
}
final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
final Declaration[] declarations = new Declaration[usedIdentifiers.getDeclrClasses().size()];
int j = 0;
for (String str : usedIdentifiers.getDeclrClasses().keySet()) {
declarations[j++] = decls.get(str);
}
Arrays.sort(declarations, SortDeclarations.instance);
MVELCompilationUnit unit = dialect.getMVELCompilationUnit(text, analysis, declarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.CONSEQUENCE);
MVELDataProvider dataProvider = new MVELDataProvider(unit, context.getDialect().getId());
From from = new From(dataProvider);
from.setResultPattern(prefixPattern);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
data.addCompileable(from, dataProvider);
dataProvider.compile(data, context.getRule());
return from;
} catch (final Exception e) {
DialectUtil.copyErrorLocation(e, descr);
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to build expression for 'from' : " + e.getMessage() + " '" + text + "'"));
return null;
} finally {
context.setTypesafe(typeSafe);
}
}
use of org.drools.core.rule.EntryPointId in project drools by kiegroup.
the class StatefulKnowledgeSessionImpl method updateEntryPointsCache.
// ------------------------------------------------------------
// Instance methods
// ------------------------------------------------------------
public void updateEntryPointsCache() {
if (kBase.getAddedEntryNodeCache() != null) {
for (EntryPointNode addedNode : kBase.getAddedEntryNodeCache()) {
EntryPointId id = addedNode.getEntryPoint();
if (EntryPointId.DEFAULT.equals(id))
continue;
WorkingMemoryEntryPoint wmEntryPoint = new NamedEntryPoint(id, addedNode, this);
entryPoints.put(id.getEntryPointId(), wmEntryPoint);
}
}
if (kBase.getRemovedEntryNodeCache() != null) {
for (EntryPointNode removedNode : kBase.getRemovedEntryNodeCache()) {
entryPoints.remove(removedNode.getEntryPoint().getEntryPointId());
}
}
}
use of org.drools.core.rule.EntryPointId in project drools by kiegroup.
the class ReteooWorkingMemoryTest method testDifferentEntryPointsOnSameFact.
@Test
public void testDifferentEntryPointsOnSameFact() {
// JBRULES-2971
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
Rete rete = kBase.getRete();
NodeFactory nFacotry = kBase.getConfiguration().getComponentFactory().getNodeFactoryService();
EntryPointNode epn = nFacotry.buildEntryPointNode(kBase.getReteooBuilder().getIdGenerator().getNextId(), RuleBasePartitionId.MAIN_PARTITION, kBase.getConfiguration().isMultithreadEvaluation(), rete, new EntryPointId("xxx"));
kBase.getRete().addObjectSink(epn);
KieSession ksession = kBase.newKieSession();
FactHandle f1 = ksession.insert("f1");
EntryPoint ep = ksession.getEntryPoint("xxx");
try {
ep.update(f1, "s1");
fail("Should throw an exception");
} catch (IllegalArgumentException e) {
}
try {
ep.retract(f1);
fail("Should throw an exception");
} catch (IllegalArgumentException e) {
}
ksession.update(f1, "s1");
assertNotNull(ksession.getObject(f1));
ksession.retract(f1);
ksession.retract(f1);
assertNull(ksession.getObject(f1));
}
use of org.drools.core.rule.EntryPointId in project drools by kiegroup.
the class Rete method assertObject.
/**
* This is the entry point into the network for all asserted Facts. Iterates a cache
* of matching <code>ObjectTypdeNode</code>s asserting the Fact. If the cache does not
* exist it first iteraes and builds the cache.
*
* @param factHandle
* The FactHandle of the fact to assert
* @param context
* The <code>PropagationContext</code> of the <code>WorkingMemory</code> action
* @param workingMemory
* The working memory session.
*/
public void assertObject(final InternalFactHandle factHandle, final PropagationContext context, final InternalWorkingMemory workingMemory) {
EntryPointId entryPoint = context.getEntryPoint();
EntryPointNode node = this.entryPoints.get(entryPoint);
ObjectTypeConf typeConf = ((WorkingMemoryEntryPoint) workingMemory.getWorkingMemoryEntryPoint(entryPoint.getEntryPointId())).getObjectTypeConfigurationRegistry().getObjectTypeConf(entryPoint, factHandle.getObject());
node.assertObject(factHandle, context, typeConf, workingMemory);
}
Aggregations