Search in sources :

Example 6 with EntryPointId

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;
}
Also used : QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) MultiAccumulate(org.drools.core.rule.MultiAccumulate) EntryPoint(org.drools.model.EntryPoint) From(org.drools.model.From) RuleConditionElement(org.drools.core.rule.RuleConditionElement) WindowReference(org.drools.model.WindowReference) UnitData(org.drools.model.UnitData) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) LambdaDataProvider(org.drools.modelcompiler.constraints.LambdaDataProvider) DataProvider(org.drools.core.spi.DataProvider) EntryPointId(org.drools.core.rule.EntryPointId) LambdaDataProvider(org.drools.modelcompiler.constraints.LambdaDataProvider) Declaration(org.drools.core.rule.Declaration) WindowDeclaration(org.drools.core.rule.WindowDeclaration) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 7 with EntryPointId

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);
    }
}
Also used : MVELDataProvider(org.drools.core.base.dataproviders.MVELDataProvider) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) FromDescr(org.drools.compiler.lang.descr.FromDescr) From(org.drools.core.rule.From) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) EntryPointId(org.drools.core.rule.EntryPointId) Declaration(org.drools.core.rule.Declaration)

Example 8 with EntryPointId

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());
        }
    }
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) EntryPointId(org.drools.core.rule.EntryPointId) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint)

Example 9 with EntryPointId

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));
}
Also used : NodeFactory(org.drools.core.reteoo.builder.NodeFactory) EntryPointId(org.drools.core.rule.EntryPointId) FactHandle(org.kie.api.runtime.rule.FactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 10 with EntryPointId

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);
}
Also used : EntryPointId(org.drools.core.rule.EntryPointId)

Aggregations

EntryPointId (org.drools.core.rule.EntryPointId)11 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)3 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)3 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 EventFactHandle (org.drools.core.common.EventFactHandle)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 QueryElementFactHandle (org.drools.core.common.QueryElementFactHandle)2 EntryPointNode (org.drools.core.reteoo.EntryPointNode)2 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)2 Declaration (org.drools.core.rule.Declaration)2 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)2 EntryPoint (org.kie.api.runtime.rule.EntryPoint)2 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)1 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)1 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 FromDescr (org.drools.compiler.lang.descr.FromDescr)1 MVELDataProvider (org.drools.core.base.dataproviders.MVELDataProvider)1 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)1 InternalWorkingMemoryEntryPoint (org.drools.core.common.InternalWorkingMemoryEntryPoint)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1