Search in sources :

Example 36 with Pattern

use of org.drools.core.rule.Pattern in project drools by kiegroup.

the class RightTupleIndexHashTableTest method testTwoDifferentEntries.

@Test
public void testTwoDifferentEntries() throws Exception {
    final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    final Declaration declaration = new Declaration("typeOfCheese", extractor, pattern);
    final FieldIndex fieldIndex = new FieldIndex(extractor, declaration, MvelConstraint.INDEX_EVALUATOR);
    final TupleIndexHashTable map = new TupleIndexHashTable(new FieldIndex[] { fieldIndex }, false);
    assertEquals(0, map.size());
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle stiltonHandle1 = new DefaultFactHandle(1, stilton1);
    map.add(new RightTupleImpl(stiltonHandle1, null));
    final Cheese cheddar1 = new Cheese("cheddar", 35);
    final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(2, cheddar1);
    map.add(new RightTupleImpl(cheddarHandle1, null));
    assertEquals(2, map.size());
    assertEquals(2, tablePopulationSize(map));
    final Cheese stilton2 = new Cheese("stilton", 77);
    final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(2, stilton2);
    Tuple tuple = map.getFirst(new LeftTupleImpl(stiltonHandle2, null, true));
    assertSame(stiltonHandle1, tuple.getFactHandle());
    assertNull(tuple.getNext());
    final Cheese cheddar2 = new Cheese("cheddar", 5);
    final InternalFactHandle cheddarHandle2 = new DefaultFactHandle(2, cheddar2);
    tuple = map.getFirst(new LeftTupleImpl(cheddarHandle2, null, true));
    assertSame(cheddarHandle1, tuple.getFactHandle());
    assertNull(tuple.getNext());
}
Also used : Pattern(org.drools.core.rule.Pattern) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Cheese(org.drools.core.test.model.Cheese) Declaration(org.drools.core.rule.Declaration) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 37 with Pattern

use of org.drools.core.rule.Pattern in project drools by kiegroup.

the class CollectBuilder method build.

/**
 * @inheritDoc
 */
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    boolean existSubNetwort = false;
    final Collect collect = (Collect) rce;
    context.pushRuleComponent(collect);
    final List<BetaNodeFieldConstraint> resultBetaConstraints = context.getBetaconstraints();
    final List<AlphaNodeFieldConstraint> resultAlphaConstraints = context.getAlphaConstraints();
    final Pattern sourcePattern = collect.getSourcePattern();
    // get builder for the pattern
    final ReteooComponentBuilder builder = utils.getBuilderFor(sourcePattern);
    // save tuple source and pattern offset for later if needed
    final LeftTupleSource tupleSource = context.getTupleSource();
    final int currentPatternIndex = context.getCurrentPatternOffset();
    // builds the source pattern
    builder.build(context, utils, sourcePattern);
    // if object source is null, then we need to adapt tuple source into a subnetwork
    if (context.getObjectSource() == null) {
        RightInputAdapterNode riaNode = context.getComponentFactory().getNodeFactoryService().buildRightInputNode(context.getNextId(), context.getTupleSource(), tupleSource, context);
        // attach right input adapter node to convert tuple source into an object source
        context.setObjectSource(utils.attachNode(context, riaNode));
        // restore tuple source from before the start of the sub network
        context.setTupleSource(tupleSource);
        // create a tuple start equals constraint and set it in the context
        final TupleStartEqualsConstraint constraint = TupleStartEqualsConstraint.getInstance();
        final List<BetaNodeFieldConstraint> betaConstraints = new ArrayList<BetaNodeFieldConstraint>();
        betaConstraints.add(constraint);
        context.setBetaconstraints(betaConstraints);
        existSubNetwort = true;
    }
    BetaConstraints binder = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), false);
    // indexing for the results should be always disabled
    BetaConstraints resultBinder = utils.createBetaNodeConstraint(context, resultBetaConstraints, true);
    CollectAccumulator accumulator = new CollectAccumulator(collect, existSubNetwort);
    Accumulate accumulate = new SingleAccumulate(sourcePattern, sourcePattern.getRequiredDeclarations(), accumulator);
    AccumulateNode accNode = context.getComponentFactory().getNodeFactoryService().buildAccumulateNode(context.getNextId(), context.getTupleSource(), context.getObjectSource(), resultAlphaConstraints.toArray(new AlphaNodeFieldConstraint[resultAlphaConstraints.size()]), // source binder
    binder, resultBinder, accumulate, existSubNetwort, context);
    context.setTupleSource(utils.attachNode(context, accNode));
    // source pattern was bound, so nulling context
    context.setObjectSource(null);
    context.setCurrentPatternOffset(currentPatternIndex);
    context.popRuleComponent();
}
Also used : Pattern(org.drools.core.rule.Pattern) BetaConstraints(org.drools.core.common.BetaConstraints) Collect(org.drools.core.rule.Collect) AccumulateNode(org.drools.core.reteoo.AccumulateNode) ArrayList(java.util.ArrayList) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) SingleAccumulate(org.drools.core.rule.SingleAccumulate) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) CollectAccumulator(org.drools.core.base.accumulators.CollectAccumulator) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 38 with Pattern

use of org.drools.core.rule.Pattern in project drools by kiegroup.

the class PatternBuilder method build.

/**
 * @inheritDoc
 */
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    final Pattern pattern = (Pattern) rce;
    context.setLastBuiltPattern(pattern);
    context.pushRuleComponent(pattern);
    this.attachPattern(context, utils, pattern);
    context.popRuleComponent();
}
Also used : Pattern(org.drools.core.rule.Pattern)

Example 39 with Pattern

use of org.drools.core.rule.Pattern in project drools by kiegroup.

the class ReteooRuleBuilder method addInitialFactPattern.

/**
 * Adds a query pattern to the given subrule
 */
private void addInitialFactPattern(final GroupElement subrule) {
    // creates a pattern for initial fact
    final Pattern pattern = new Pattern(0, ClassObjectType.InitialFact_ObjectType);
    // adds the pattern as the first child of the given AND group element
    subrule.addChild(0, pattern);
}
Also used : Pattern(org.drools.core.rule.Pattern)

Example 40 with Pattern

use of org.drools.core.rule.Pattern in project drools by kiegroup.

the class ObjectSource method initDeclaredMask.

public void initDeclaredMask(BuildContext context) {
    if (context == null || context.getLastBuiltPatterns() == null) {
        // only happens during unit tests
        declaredMask = AllSetBitMask.get();
        return;
    }
    Pattern pattern = context.getLastBuiltPatterns()[0];
    ObjectType objectType = pattern.getObjectType();
    if (!(objectType instanceof ClassObjectType)) {
        // Only ClassObjectType can use property specific
        declaredMask = AllSetBitMask.get();
        return;
    }
    Class objectClass = ((ClassObjectType) objectType).getClassType();
    TypeDeclaration typeDeclaration = context.getKnowledgeBase().getTypeDeclaration(objectClass);
    if (typeDeclaration == null || !typeDeclaration.isPropertyReactive()) {
        // if property specific is not on, then accept all modification propagations
        declaredMask = AllSetBitMask.get();
    } else {
        List<String> settableProperties = getAccessibleProperties(context.getKnowledgeBase(), objectClass);
        declaredMask = calculateDeclaredMask(objectClass, settableProperties);
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

Pattern (org.drools.core.rule.Pattern)86 Declaration (org.drools.core.rule.Declaration)43 ClassObjectType (org.drools.core.base.ClassObjectType)39 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)30 Test (org.junit.Test)30 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)23 GroupElement (org.drools.core.rule.GroupElement)16 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)13 ObjectType (org.drools.core.spi.ObjectType)13 ObjectInput (java.io.ObjectInput)12 ObjectOutput (java.io.ObjectOutput)12 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)12 WorkingMemory (org.drools.core.WorkingMemory)12 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)12 Consequence (org.drools.core.spi.Consequence)12 IOException (java.io.IOException)11 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)11 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)11 TypeDeclaration (org.drools.core.rule.TypeDeclaration)11 InternalFactHandle (org.drools.core.common.InternalFactHandle)10