use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class RightTupleIndexHashTableTest method testTwoEqualEntries.
@Test
public void testTwoEqualEntries() 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));
final Cheese stilton2 = new Cheese("stilton", 81);
final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(3, stilton2);
map.add(new RightTupleImpl(stiltonHandle2, null));
assertEquals(3, map.size());
assertEquals(2, tablePopulationSize(map));
// Check they are correctly chained to the same FieldIndexEntry
final Cheese stilton3 = new Cheese("stilton", 89);
final InternalFactHandle stiltonHandle3 = new DefaultFactHandle(4, stilton2);
final Tuple tuple = map.getFirst(new LeftTupleImpl(stiltonHandle3, null, true));
assertSame(stiltonHandle1, tuple.getFactHandle());
assertSame(stiltonHandle2, ((RightTuple) tuple.getNext()).getFactHandle());
}
use of org.drools.core.base.ClassObjectType 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.base.ClassObjectType in project drools by kiegroup.
the class PatternBuilder method attachObjectTypeNode.
private void attachObjectTypeNode(final BuildContext context, final BuildUtils utils, final Pattern pattern) {
boolean objectMemory = context.isObjectTypeNodeMemoryEnabled();
ObjectType objectType = pattern.getObjectType();
if (pattern.getObjectType() instanceof ClassObjectType) {
// Is this the query node, if so we don't want any memory
if (DroolsQuery.class == ((ClassObjectType) pattern.getObjectType()).getClassType()) {
context.setTupleMemoryEnabled(false);
context.setObjectTypeNodeMemoryEnabled(false);
}
}
ObjectTypeNode otn = context.getComponentFactory().getNodeFactoryService().buildObjectTypeNode(context.getNextId(), (EntryPointNode) context.getObjectSource(), objectType, context);
if (objectType.isEvent() && EventProcessingOption.STREAM.equals(context.getKnowledgeBase().getConfiguration().getEventProcessingMode())) {
ExpirationSpec expirationSpec = getExpirationForType(context, objectType);
if (expirationSpec.offset != NEVER_EXPIRES && expirationSpec.hard) {
// hard expiration is set, so use it
otn.setExpirationOffset(expirationSpec.offset);
} else {
// otherwise calculate it based on behaviours and temporal constraints
long offset = NEVER_EXPIRES;
for (Behavior behavior : pattern.getBehaviors()) {
if (behavior.getExpirationOffset() != NEVER_EXPIRES) {
offset = Math.max(behavior.getExpirationOffset(), offset);
}
}
// if there's no implicit expiration uses the (eventually set) soft one
if (offset == NEVER_EXPIRES && !expirationSpec.hard) {
offset = expirationSpec.offset;
}
long distance = context.getExpirationOffset(pattern);
if (distance == NEVER_EXPIRES) {
// it means the rules have no temporal constraints, or
// the constraints require events to be hold forever. In this
// case, we allow type declarations to override the implicit
// expiration offset by defining an expiration policy with the
// @expires tag
otn.setExpirationOffset(offset);
} else {
otn.setExpirationOffset(Math.max(distance, offset));
}
}
}
context.setObjectSource(utils.attachNode(context, otn));
context.setObjectTypeNodeMemoryEnabled(objectMemory);
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class XpathBackReference method getDeclaration.
public Declaration getDeclaration(Pattern pattern, String id) {
if (!id.startsWith(BACK_REFERENCE_HEAD)) {
return null;
}
Declaration declaration = declarations.get(id);
if (declaration != null) {
return declaration;
}
int backRefPos = Integer.parseInt(id.substring(XpathBackReference.BACK_REFERENCE_HEAD.length()));
int relativeOffset = backReferenceClasses.size() - 1 - backRefPos;
declaration = new Declaration(id, new PatternExtractor(new ClassObjectType(backReferenceClasses.get(backRefPos))), new RelativePattern(pattern, relativeOffset), true);
if (declarations == Collections.EMPTY_MAP) {
declarations = new HashMap<String, Declaration>();
}
declarations.put(id, declaration);
return declaration;
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class DeclarationTest method testDeclaration.
@Test
public void testDeclaration() {
final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
final Pattern pattern = new Pattern(5, new ClassObjectType(Cheese.class));
// Bind the extractor to a decleration
// Declarations know the pattern they derive their value from
final Declaration declaration = new Declaration("typeOfCheese", extractor, pattern);
assertEquals("typeOfCheese", declaration.getIdentifier());
assertSame(String.class, declaration.getDeclarationClass());
assertSame(extractor, declaration.getExtractor());
assertEquals(5, declaration.getPattern().getOffset());
}
Aggregations