Search in sources :

Example 11 with PatternExtractor

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 );
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) HashMap(java.util.HashMap) PatternExtractor(org.drools.core.spi.PatternExtractor) TemplateRegistry(org.mvel2.templates.TemplateRegistry) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) List(java.util.List) Declaration(org.drools.core.rule.Declaration) Person(org.drools.compiler.Person) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 12 with PatternExtractor

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"));
}
Also used : Pattern(org.drools.core.rule.Pattern) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) ClassObjectType(org.drools.core.base.ClassObjectType) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) HashMap(java.util.HashMap) SalienceBuilder(org.drools.compiler.rule.builder.SalienceBuilder) MVELSalienceBuilder(org.drools.mvel.builder.MVELSalienceBuilder) PatternExtractor(org.drools.core.spi.PatternExtractor) MVELDialect(org.drools.mvel.builder.MVELDialect) MVELSalienceBuilder(org.drools.mvel.builder.MVELSalienceBuilder) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) BuildContext(org.drools.core.reteoo.builder.BuildContext) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) MVELSalienceExpression(org.drools.mvel.expr.MVELSalienceExpression) RuleDescr(org.drools.drl.ast.descr.RuleDescr) Declaration(org.drools.core.rule.Declaration) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Before(org.junit.Before)

Example 13 with PatternExtractor

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 );
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) HashMap(java.util.HashMap) PatternExtractor(org.drools.core.spi.PatternExtractor) Cheese(org.drools.mvel.compiler.Cheese) TemplateRegistry(org.mvel2.templates.TemplateRegistry) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) List(java.util.List) Declaration(org.drools.core.rule.Declaration) Person(org.drools.mvel.compiler.Person) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 14 with PatternExtractor

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 );
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) HashMap(java.util.HashMap) PatternExtractor(org.drools.core.spi.PatternExtractor) Cheese(org.drools.mvel.compiler.Cheese) TemplateRegistry(org.mvel2.templates.TemplateRegistry) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) List(java.util.List) Declaration(org.drools.core.rule.Declaration) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 15 with PatternExtractor

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;
}
Also used : Binding(org.drools.model.Binding) LambdaAccumulator(org.drools.modelcompiler.constraints.LambdaAccumulator) Accumulator(org.drools.core.spi.Accumulator) PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) ClassObjectType(org.drools.core.base.ClassObjectType) PatternExtractor(org.drools.core.spi.PatternExtractor) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) Declaration(org.drools.core.rule.Declaration) WindowDeclaration(org.drools.core.rule.WindowDeclaration) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) BindingEvaluator(org.drools.modelcompiler.constraints.BindingEvaluator)

Aggregations

ClassObjectType (org.drools.core.base.ClassObjectType)15 PatternExtractor (org.drools.core.spi.PatternExtractor)15 Declaration (org.drools.core.rule.Declaration)13 HashMap (java.util.HashMap)10 Test (org.junit.Test)8 List (java.util.List)6 Map (java.util.Map)6 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)6 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)6 TemplateRegistry (org.mvel2.templates.TemplateRegistry)6 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)4 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)4 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 Pattern (org.drools.core.rule.Pattern)4 ObjectType (org.drools.core.spi.ObjectType)4 Cheese (org.drools.compiler.Cheese)3 Properties (java.util.Properties)2 Person (org.drools.compiler.Person)2 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)2