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());
}
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();
}
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();
}
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);
}
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);
}
}
Aggregations