use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class InvokerGenerator method createStubGenerator.
public static ClassGenerator createStubGenerator(final InvokerDataProvider data, final ClassLoader classLoader, final TypeResolver typeResolver, final Set<String> imports) {
final ClassGenerator generator = new ClassGenerator(data.getPackageName() + "." + data.getInvokerClassName(), classLoader, typeResolver);
generator.addStaticField(ACC_PRIVATE + ACC_FINAL, "serialVersionUID", Long.TYPE, GeneratorHelper.INVOKER_SERIAL_UID).addDefaultConstructor();
generator.addMethod(ACC_PUBLIC, "hashCode", generator.methodDescr(Integer.TYPE), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
push(data.hashCode());
mv.visitInsn(IRETURN);
}
}).addMethod(ACC_PUBLIC, "getMethodBytecode", generator.methodDescr(List.class), new GeneratorHelper.GetMethodBytecodeMethod(data)).addMethod(ACC_PUBLIC, "equals", generator.methodDescr(Boolean.TYPE, Object.class), new GeneratorHelper.EqualsMethod()).addMethod(ACC_PUBLIC, "getPackageName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
push(data.getPackageName());
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "getGeneratedInvokerClassName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
push(data.getInvokerClassName() + "Generated");
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "getExpectedDeclarationTypes", generator.methodDescr(String[].class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
Declaration[] declarations = ((InvokerContext) data).getDeclarations();
List<String> declarationTypes = new ArrayList<String>(declarations.length);
for (Declaration decl : declarations) {
declarationTypes.add(decl.getTypeName());
}
returnAsArray(declarationTypes, String.class);
}
}).addMethod(ACC_PUBLIC, "getRuleClassName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
push(data.getRuleClassName());
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "getInternalRuleClassName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
push(data.getInternalRuleClassName());
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "getMethodName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
push(data.getMethodName());
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "getInvokerClassName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
push(data.getInvokerClassName());
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "getGlobals", generator.methodDescr(String[].class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
returnAsArray(data.getGlobals());
}
}).addMethod(ACC_PUBLIC, "getGlobalTypes", generator.methodDescr(String[].class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
returnAsArray(data.getGlobalTypes());
}
}).addMethod(ACC_PUBLIC, "getPackageImports", generator.methodDescr(String[].class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
returnAsArray(imports, String.class);
}
});
return generator;
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class JavaAccumulateBuilder method buildAccumulator.
private Accumulator buildAccumulator(RuleBuildContext context, AccumulateDescr accumDescr, Map<String, Declaration> declsInScope, Map<String, Class<?>> declCls, boolean readLocalsFromTuple, Declaration[] sourceDeclArr, Set<Declaration> requiredDecl, AccumulateFunctionCallDescr fc, AccumulateFunction function) {
// analyze the expression
final JavaAnalysisResult analysis = (JavaAnalysisResult) context.getDialect().analyzeBlock(context, accumDescr, fc.getParams().length > 0 ? fc.getParams()[0] : "\"\"", new BoundIdentifiers(declCls, context));
if (analysis == null) {
// not possible to get the analysis results - compilation error has been already logged
return null;
}
final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
// create the array of used declarations
final Declaration[] previousDeclarations = collectRequiredDeclarations(declsInScope, requiredDecl, usedIdentifiers);
// generate the code template
return generateFunctionCallCodeTemplate(context, accumDescr, sourceDeclArr, fc, function, usedIdentifiers, previousDeclarations, readLocalsFromTuple);
}
use of org.drools.core.rule.Declaration 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.rule.Declaration 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));
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class BuildUtilsTest method testCalculateTemporalDistance.
/**
* Test method for {@link org.drools.core.reteoo.builder.BuildUtils#calculateTemporalDistance(org.drools.core.rule.GroupElement)}.
*/
@Test
public void testCalculateTemporalDistance() {
// input is here just for "documentation" purposes
Interval[][] input = new Interval[][] { { new Interval(0, 0), new Interval(-2, 2), new Interval(-3, 4), new Interval(MIN, MAX), new Interval(MIN, MAX) }, { new Interval(-2, 2), new Interval(0, 0), new Interval(MIN, MAX), new Interval(1, 2), new Interval(MIN, MAX) }, { new Interval(-4, 3), new Interval(MIN, MAX), new Interval(0, 0), new Interval(2, 3), new Interval(MIN, MAX) }, { new Interval(MIN, MAX), new Interval(-2, -1), new Interval(-3, -2), new Interval(0, 0), new Interval(1, 10) }, { new Interval(MIN, MAX), new Interval(MIN, MAX), new Interval(MIN, MAX), new Interval(-10, -1), new Interval(0, 0) } };
Interval[][] expected = new Interval[][] { { new Interval(0, 0), new Interval(-2, 2), new Interval(-3, 2), new Interval(-1, 4), new Interval(0, 14) }, { new Interval(-2, 2), new Interval(0, 0), new Interval(-2, 0), new Interval(1, 2), new Interval(2, 12) }, { new Interval(-2, 3), new Interval(0, 2), new Interval(0, 0), new Interval(2, 3), new Interval(3, 13) }, { new Interval(-4, 1), new Interval(-2, -1), new Interval(-3, -2), new Interval(0, 0), new Interval(1, 10) }, { new Interval(-14, 0), new Interval(-12, -2), new Interval(-13, -3), new Interval(-10, -1), new Interval(0, 0) } };
AfterEvaluatorDefinition evals = new AfterEvaluatorDefinition();
ClassObjectType ot = new ClassObjectType(StockTick.class, true);
Pattern a = new Pattern(0, ot, "$a");
Pattern b = new Pattern(1, ot, "$b");
b.addConstraint(new EvaluatorConstraint(new Declaration[] { a.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "-2,2"), new SelfReferenceClassFieldReader(StockTick.class)));
Pattern c = new Pattern(2, ot, "$c");
c.addConstraint(new EvaluatorConstraint(new Declaration[] { a.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "-3,4"), new SelfReferenceClassFieldReader(StockTick.class)));
Pattern d = new Pattern(3, ot, "$d");
d.addConstraint(new EvaluatorConstraint(new Declaration[] { b.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "1,2"), new SelfReferenceClassFieldReader(StockTick.class)));
d.addConstraint(new EvaluatorConstraint(new Declaration[] { c.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "2,3"), new SelfReferenceClassFieldReader(StockTick.class)));
Pattern e = new Pattern(4, ot, "$e");
e.addConstraint(new EvaluatorConstraint(new Declaration[] { d.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "1,10"), new SelfReferenceClassFieldReader(StockTick.class)));
GroupElement not = new GroupElement(Type.NOT);
not.addChild(e);
GroupElement and = new GroupElement(Type.AND);
and.addChild(a);
and.addChild(b);
and.addChild(c);
and.addChild(d);
and.addChild(not);
TemporalDependencyMatrix matrix = utils.calculateTemporalDistance(and);
// printMatrix( matrix.getMatrix() );
assertEqualsMatrix(expected, matrix.getMatrix());
assertEquals(15, matrix.getExpirationOffset(a));
assertEquals(11, matrix.getExpirationOffset(d));
assertEquals(1, matrix.getExpirationOffset(e));
}
Aggregations