use of org.drools.core.spi.PatternExtractor in project drools by kiegroup.
the class AccumulateTemplateTest method testInvokerGenerationMultiPattern.
@Test
public void testInvokerGenerationMultiPattern() {
final String className = "accumulate0";
final String[] declarationTypes = new String[] { "String", "int" };
final Declaration[] declarations = new Declaration[] { new Declaration("name", store.getReader(Person.class, "name"), null), new Declaration("age", store.getReader(Person.class, "age"), null) };
final Declaration[] inner = new Declaration[] { new Declaration("$cheese", new PatternExtractor(new ClassObjectType(Cheese.class)), null), new Declaration("$person", new PatternExtractor(new ClassObjectType(Person.class)), null) };
final String[] globals = new String[] { "aGlobal", "anotherGlobal" };
final List globalTypes = Arrays.asList(new String[] { "String", "String" });
final Map map = new HashMap();
map.put("className", StringUtils.ucFirst(className));
map.put("instanceName", className);
map.put("package", "org.drools");
map.put("ruleClassName", "Rule0");
map.put("invokerClassName", "Rule0" + StringUtils.ucFirst(className) + "Invoker");
map.put("declarations", declarations);
map.put("declarationTypes", declarationTypes);
map.put("globals", globals);
map.put("globalTypes", globalTypes);
map.put("innerDeclarations", inner);
map.put("attributes", new Attribute[] { new Attribute("int", "x") });
map.put("initCode", "x = 0;");
map.put("actionCode", "x += 1;");
map.put("reverseCode", "");
map.put("resultCode", "x + 10");
map.put("supportsReverse", "false");
map.put("resultType", Integer.class);
map.put("hashCode", new Integer(10));
map.put("isMultiPattern", Boolean.TRUE);
TemplateRegistry registry = getInvokerTemplateRegistry();
Object method = TemplateRuntime.execute(registry.getNamedTemplate("accumulateInvoker"), null, new MapVariableResolverFactory(map), registry);
// System.out.println( method );
}
use of org.drools.core.spi.PatternExtractor in project drools by kiegroup.
the class MVELSalienceBuilderTest method setUp.
@Before
public void setUp() throws Exception {
InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
ruleDescr.addAttribute(new AttributeDescr("salience", "(p.age + 20)/2"));
ruleDescr.setConsequence("");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final ObjectType personObjeectType = new ClassObjectType(Person.class);
final Pattern pattern = new Pattern(0, personObjeectType);
final PatternExtractor extractor = new PatternExtractor(personObjeectType);
final Declaration declaration = new Declaration("p", extractor, pattern);
final Map<String, Declaration> map = new HashMap<String, Declaration>();
map.put("p", declaration);
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
kBase = KnowledgeBaseFactory.newKnowledgeBase();
buildContext = new BuildContext(kBase, Collections.emptyList());
SalienceBuilder salienceBuilder = new MVELSalienceBuilder();
salienceBuilder.build(context);
((MVELSalienceExpression) context.getRule().getSalience()).compile((MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel"));
}
use of org.drools.core.spi.PatternExtractor in project drools by kiegroup.
the class AccumulateTemplateTest method testInvokerGenerationSinglePattern.
@Test
public void testInvokerGenerationSinglePattern() {
final String className = "accumulate0";
final String[] declarationTypes = new String[] { "String", "int" };
final Declaration[] declarations = new Declaration[] { new Declaration("name", store.getReader(Person.class, "name"), null), new Declaration("age", store.getReader(Person.class, "age"), null) };
final Declaration[] inner = new Declaration[] { new Declaration("cheese", new PatternExtractor(new ClassObjectType(Cheese.class)), null), new Declaration("price", store.getReader(Cheese.class, "price"), null) };
final String[] globals = new String[] { "aGlobal", "anotherGlobal" };
final List globalTypes = Arrays.asList(new String[] { "String", "String" });
final Map map = new HashMap();
map.put("className", StringUtils.ucFirst(className));
map.put("instanceName", className);
map.put("package", "org.drools");
map.put("ruleClassName", "Rule0");
map.put("invokerClassName", "Rule0" + StringUtils.ucFirst(className) + "Invoker");
map.put("declarations", declarations);
map.put("declarationTypes", declarationTypes);
map.put("globals", globals);
map.put("globalTypes", globalTypes);
map.put("innerDeclarations", inner);
map.put("attributes", new Attribute[] { new Attribute("int", "x") });
map.put("initCode", "x = 0;");
map.put("actionCode", "x += 1;");
map.put("reverseCode", "");
map.put("resultCode", "x + 10");
map.put("supportsReverse", "false");
map.put("resultType", Integer.class);
map.put("hashCode", new Integer(10));
map.put("isMultiPattern", Boolean.FALSE);
TemplateRegistry registry = getInvokerTemplateRegistry();
Object method = TemplateRuntime.execute(registry.getNamedTemplate("accumulateInvoker"), null, new MapVariableResolverFactory(map), registry);
// System.out.println( method );
}
use of org.drools.core.spi.PatternExtractor in project drools by kiegroup.
the class AccumulateTemplateTest method testMethodGeneration.
@Test
public void testMethodGeneration() {
final String className = "accumulate0";
final String[] declarationTypes = new String[] { "String", "int" };
final Declaration[] declarations = new Declaration[] { new Declaration("name", null, null), new Declaration("age", null, null) };
final Declaration[] inner = new Declaration[] { new Declaration("cheese", new PatternExtractor(new ClassObjectType(Cheese.class)), null), new Declaration("price", store.getReader(Cheese.class, "price"), null) };
final String[] globals = new String[] { "aGlobal", "anotherGlobal" };
final List globalTypes = Arrays.asList(new String[] { "String", "String" });
final Map map = new HashMap();
map.put("className", StringUtils.ucFirst(className));
map.put("instanceName", className);
map.put("package", "org.drools");
map.put("ruleClassName", "Rule0");
map.put("invokerClassName", "Rule0" + StringUtils.ucFirst(className) + "Invoker");
map.put("declarations", declarations);
map.put("declarationTypes", declarationTypes);
map.put("globals", globals);
map.put("globalTypes", globalTypes);
map.put("innerDeclarations", inner);
map.put("attributes", new String[] { "x" });
map.put("attributesTypes", new String[] { "int" });
map.put("initCode", "x = 0;");
map.put("actionCode", "x += 1;");
map.put("reverseCode", "x -= 1;");
map.put("resultCode", "x + 10");
map.put("supportsReverse", "true");
map.put("resultType", Integer.class);
map.put("hashCode", new Integer(10));
TemplateRegistry registry = getRuleTemplateRegistry();
Object method = TemplateRuntime.execute(registry.getNamedTemplate("accumulateInnerClass"), null, new MapVariableResolverFactory(map), registry);
// System.out.println( method );
}
use of org.drools.core.spi.PatternExtractor in project drools by kiegroup.
the class KiePackagesBuilder method processFunctions.
private Variable processFunctions(RuleContext ctx, AccumulatePattern accPattern, RuleConditionElement source, Pattern pattern, Set<String> usedVariableName, Collection<Binding> bindings, boolean isGroupBy, AccumulateFunction accFunction, InternalReadAccessor selfReader, Accumulator[] accumulators, List<Declaration> requiredDeclarationList, int arrayIndexOffset, int i) {
Binding binding = findBindingForAccumulate(bindings, accFunction);
if (binding != null) {
for (Variable var : binding.getInputVariables()) {
usedVariableName.add(var.getName());
}
}
final BindingEvaluator bindingEvaluator = createBindingEvaluator(ctx, binding);
final Accumulator accumulator = createAccumulator(usedVariableName, bindingEvaluator, accFunction);
Variable boundVar = accPattern.getBoundVariables()[i];
Declaration declaration;
if (!isGroupBy && accumulators.length == 1) {
declaration = new Declaration(boundVar.getName(), new PatternExtractor(new ClassObjectType(boundVar.getType())), pattern, true);
} else {
// GroupBy or multi-accumulate always return an array
// If GroupBy has no bound function, it uses an anonymous one.
// The result is still in element 0 so must be offset using arrayIndexOffset
declaration = new Declaration(boundVar.getName(), new ArrayElementReader(selfReader, i + arrayIndexOffset, boundVar.getType()), pattern, true);
}
pattern.addDeclaration(declaration);
ctx.addDeclaration(boundVar, declaration);
accumulators[i] = accumulator;
Declaration[] requiredDeclarations = getRequiredDeclarationsForAccumulate(ctx, source, accFunction, binding, bindingEvaluator);
requiredDeclarationList.addAll(Arrays.asList(requiredDeclarations));
return boundVar;
}
Aggregations