Search in sources :

Example 11 with PropagationContextFactory

use of org.drools.core.common.PropagationContextFactory in project drools by kiegroup.

the class StatefulKnowledgeSessionImpl method initInitialFact.

public InternalFactHandle initInitialFact(InternalKnowledgeBase kBase, InternalWorkingMemoryEntryPoint entryPoint, EntryPointId epId, MarshallerReaderContext context) {
    InitialFact initialFact = InitialFactImpl.getInstance();
    InternalFactHandle handle = new DefaultFactHandle(0, initialFact, 0, entryPoint);
    ClassObjectTypeConf otc = (ClassObjectTypeConf) entryPoint.getObjectTypeConfigurationRegistry().getObjectTypeConf(epId, initialFact);
    ObjectTypeNode otn = otc.getConcreteObjectTypeNode();
    if (otn != null) {
        PropagationContextFactory ctxFact = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
        PropagationContext pctx = ctxFact.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, handle, epId, context);
        otn.assertInitialFact(handle, pctx, this);
    }
    return handle;
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ClassObjectTypeConf(org.drools.core.reteoo.ClassObjectTypeConf) PropagationContext(org.drools.core.spi.PropagationContext) InitialFact(org.drools.core.InitialFact) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 12 with PropagationContextFactory

use of org.drools.core.common.PropagationContextFactory in project drools by kiegroup.

the class AddRemoveRule method insertFacts.

private static void insertFacts(PathEndNodes endNodes, InternalWorkingMemory[] wms) {
    Set<LeftTupleNode> visited = new HashSet<LeftTupleNode>();
    for (PathEndNode endNode : endNodes.subjectEndNodes) {
        LeftTupleNode[] nodes = endNode.getPathNodes();
        for (int i = 0; i < nodes.length; i++) {
            LeftTupleNode node = nodes[i];
            if (NodeTypeEnums.isBetaNode(node) && node.getAssociationsSize() == 1) {
                if (!visited.add(node)) {
                    // this is to avoid rentering a path, and processing nodes twice. This can happen for nested subnetworks.
                    continue;
                }
                BetaNode bn = (BetaNode) node;
                if (!bn.isRightInputIsRiaNode()) {
                    for (int j = 0; j < wms.length; j++) {
                        PropagationContextFactory pctxFactory = wms[j].getKnowledgeBase().getConfiguration().getComponentFactory().getPropagationContextFactory();
                        final PropagationContext pctx = pctxFactory.createPropagationContext(wms[j].getNextPropagationIdCounter(), PropagationContext.Type.RULE_ADDITION, null, null, null);
                        bn.getRightInput().updateSink(bn, pctx, wms[j]);
                    }
                }
            }
        }
    }
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) BetaNode(org.drools.core.reteoo.BetaNode) PropagationContext(org.drools.core.spi.PropagationContext) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) PathEndNode(org.drools.core.reteoo.PathEndNode) HashSet(java.util.HashSet)

Example 13 with PropagationContextFactory

use of org.drools.core.common.PropagationContextFactory in project drools by kiegroup.

the class AlphaNodeTest method testLiteralConstraintAssertObjectWithoutMemory.

@Test
public void testLiteralConstraintAssertObjectWithoutMemory() throws Exception {
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    BuildContext buildContext = new BuildContext(kBase);
    buildContext.setRule(new RuleImpl("test"));
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final RuleImpl rule = new RuleImpl("test-rule");
    PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
    final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
    final MockObjectSource source = new MockObjectSource(buildContext.getNextId());
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
    final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
    // With Memory
    final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, // no memory
    buildContext);
    final MockObjectSink sink = new MockObjectSink();
    alphaNode.addObjectSink(sink);
    final Cheese cheddar = new Cheese("cheddar", 5);
    final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert(cheddar);
    // check sink is empty
    assertLength(0, sink.getAsserted());
    // object should assert as it passes text
    alphaNode.assertObject(f0, context, ksession);
    assertEquals(1, sink.getAsserted().size());
    Object[] list = (Object[]) sink.getAsserted().get(0);
    assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
    final Cheese stilton = new Cheese("stilton", 6);
    final DefaultFactHandle f1 = new DefaultFactHandle(1, stilton);
    // object should NOT assert as it does not pass test
    alphaNode.assertObject(f1, context, ksession);
    assertLength(1, sink.getAsserted());
    list = (Object[]) sink.getAsserted().get(0);
    assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) PropagationContext(org.drools.core.spi.PropagationContext) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Cheese(org.drools.core.test.model.Cheese) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) BuildContext(org.drools.core.reteoo.builder.BuildContext) ClassFieldReader(org.drools.core.base.ClassFieldReader) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) FieldValue(org.drools.core.spi.FieldValue) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

PropagationContextFactory (org.drools.core.common.PropagationContextFactory)13 BuildContext (org.drools.core.reteoo.builder.BuildContext)8 PropagationContext (org.drools.core.spi.PropagationContext)8 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)7 ClassObjectType (org.drools.core.base.ClassObjectType)4 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)4 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)4 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)4 Test (org.junit.Test)4 MvelConstraintTestUtil (org.drools.core.rule.MvelConstraintTestUtil)3 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)3 FieldValue (org.drools.core.spi.FieldValue)3 Cheese (org.drools.core.test.model.Cheese)3 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Cheese (org.drools.compiler.Cheese)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1