use of org.drools.core.spi.InternalReadAccessor in project drools by kiegroup.
the class PatternTest method testDeclarationsFactTemplate.
@Test
public void testDeclarationsFactTemplate() throws Exception {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.store");
final FieldTemplate cheeseName = new FieldTemplateImpl("name", 0, String.class);
final FieldTemplate cheesePrice = new FieldTemplateImpl("price", 1, Integer.class);
final FieldTemplate[] fields = new FieldTemplate[] { cheeseName, cheesePrice };
final FactTemplate cheese = new FactTemplateImpl(pkg, "Cheese", fields);
final ObjectType type = new FactTemplateObjectType(cheese);
final Pattern col = new Pattern(0, type, "foo");
final Declaration dec = col.getDeclaration();
final InternalReadAccessor ext = dec.getExtractor();
assertEquals(Fact.class, ext.getExtractToClass());
final Fact stilton = cheese.createFact(10);
stilton.setFieldValue("name", "stilton");
stilton.setFieldValue("price", new Integer(200));
assertEquals(stilton, dec.getValue(null, stilton));
}
use of org.drools.core.spi.InternalReadAccessor in project drools by kiegroup.
the class PatternTest method testDeclarationsObjectType.
@Test
public void testDeclarationsObjectType() throws Exception {
final ObjectType type = new ClassObjectType(Cheese.class);
final Pattern col = new Pattern(0, type, "foo");
final Declaration dec = col.getDeclaration();
final InternalReadAccessor ext = dec.getExtractor();
assertEquals(Cheese.class, ext.getExtractToClass());
final Cheese stilton = new Cheese("stilton", 42);
assertEquals(stilton, dec.getValue(null, stilton));
}
use of org.drools.core.spi.InternalReadAccessor in project drools by kiegroup.
the class LeftLeftTupleIndexHashTableIteratorTest method getConstraint.
protected BetaNodeFieldConstraint getConstraint(String identifier, Operator operator, String fieldName, Class clazz) {
ClassFieldAccessorStore store = new ClassFieldAccessorStore();
store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
store.setEagerWire(true);
InternalReadAccessor extractor = store.getReader(clazz, fieldName);
Declaration declaration = new Declaration(identifier, extractor, new Pattern(0, new ClassObjectType(clazz)));
String expression = fieldName + " " + operator.getOperatorString() + " " + declaration.getIdentifier();
return new MvelConstraintTestUtil(expression, declaration, extractor);
}
use of org.drools.core.spi.InternalReadAccessor in project drools by kiegroup.
the class JavaAccumulateBuilder method buildExternalFunctionCall.
private Accumulate buildExternalFunctionCall(RuleBuildContext context, AccumulateDescr accumDescr, RuleConditionElement source, Map<String, Declaration> declsInScope, Map<String, Class<?>> declCls, boolean readLocalsFromTuple) {
// list of functions to build
final List<AccumulateFunctionCallDescr> funcCalls = accumDescr.getFunctions();
// list of available source declarations
final Declaration[] sourceDeclArr = source.getOuterDeclarations().values().toArray(new Declaration[source.getOuterDeclarations().size()]);
Arrays.sort(sourceDeclArr, RuleTerminalNode.SortDeclarations.instance);
// set of required previous declarations
Set<Declaration> requiredDecl = new HashSet<Declaration>();
Pattern pattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
if (accumDescr.isMultiFunction()) {
// the accumulator array
Accumulator[] accumulators = new Accumulator[funcCalls.size()];
// creating the custom array reader
InternalReadAccessor reader = new SelfReferenceClassFieldReader(Object[].class);
int index = 0;
for (AccumulateFunctionCallDescr fc : funcCalls) {
AccumulateFunction function = getAccumulateFunction(context, accumDescr, fc, source, declCls);
if (function == null) {
return null;
}
bindReaderToDeclaration(context, accumDescr, pattern, fc, new ArrayElementReader(reader, index, function.getResultType()), function.getResultType(), index);
accumulators[index++] = buildAccumulator(context, accumDescr, declsInScope, declCls, readLocalsFromTuple, sourceDeclArr, requiredDecl, fc, function);
}
return new MultiAccumulate(source, requiredDecl.toArray(new Declaration[requiredDecl.size()]), accumulators);
} else {
AccumulateFunctionCallDescr fc = accumDescr.getFunctions().get(0);
AccumulateFunction function = getAccumulateFunction(context, accumDescr, fc, source, declCls);
if (function == null) {
return null;
}
Class<?> returnType = function.getResultType();
if (!pattern.isCompatibleWithAccumulateReturnType(returnType)) {
context.addError(new DescrBuildError(accumDescr, context.getRuleDescr(), null, "Pattern of type: '" + pattern.getObjectType() + "' on rule '" + context.getRuleDescr().getName() + "' is not compatible with type " + returnType.getCanonicalName() + " returned by accumulate function."));
return null;
}
bindReaderToDeclaration(context, accumDescr, pattern, fc, new SelfReferenceClassFieldReader(function.getResultType()), function.getResultType(), -1);
Accumulator accumulator = buildAccumulator(context, accumDescr, declsInScope, declCls, readLocalsFromTuple, sourceDeclArr, requiredDecl, fc, function);
return new SingleAccumulate(source, requiredDecl.toArray(new Declaration[requiredDecl.size()]), accumulator);
}
}
use of org.drools.core.spi.InternalReadAccessor in project drools by kiegroup.
the class FactTemplateFieldExtractorTest method testDeclaration.
@Test
public void testDeclaration() {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.store");
final FieldTemplate cheeseName = new FieldTemplateImpl("name", 0, String.class);
final FieldTemplate cheesePrice = new FieldTemplateImpl("price", 1, Integer.class);
final FieldTemplate[] fields = new FieldTemplate[] { cheeseName, cheesePrice };
final FactTemplate cheese = new FactTemplateImpl(pkg, "Cheese", fields);
final InternalReadAccessor extractName = new FactTemplateFieldExtractor(cheese, 0);
final Pattern pattern = new Pattern(0, new FactTemplateObjectType(cheese));
final Declaration declaration = new Declaration("typeOfCheese", extractName, pattern);
final Fact brie = cheese.createFact(12);
brie.setFieldValue("name", "brie");
brie.setFieldValue("price", new Integer(55));
// Check we can extract Declarations correctly
assertEquals("brie", declaration.getValue(null, brie));
}
Aggregations