Search in sources :

Example 76 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory 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.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 77 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory 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 78 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory in project drools by kiegroup.

the class AbstractModel method getMappedMiningPojo.

public Map.Entry<String, String> getMappedMiningPojo() {
    Map<String, String> result = new HashMap<>();
    if (!templateRegistry.contains(getMiningPojoTemplateName())) {
        this.addMiningTemplateToRegistry(templateRegistry);
    }
    List<PMMLMiningField> dataFields = this.getMiningFields();
    Map<String, Object> vars = new HashMap<>();
    String className = this.getMiningPojoClassName();
    vars.put("pmmlPackageName", PMML_JAVA_PACKAGE_NAME);
    vars.put("className", className);
    vars.put("imports", new ArrayList<>());
    vars.put("dataFields", dataFields);
    vars.put("modelName", this.getModelId());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        TemplateRuntime.execute(templateRegistry.getNamedTemplate(this.getMiningPojoTemplateName()), null, new MapVariableResolverFactory(vars), baos);
    } catch (TemplateRuntimeError tre) {
        // need to figure out logging here
        return null;
    }
    result.put(PMML_JAVA_PACKAGE_NAME + "." + className, new String(baos.toByteArray()));
    return result.entrySet().iterator().next();
}
Also used : TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 79 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory in project drools by kiegroup.

the class AbstractModel method getMappedRuleUnit.

@Override
public Map.Entry<String, String> getMappedRuleUnit() {
    Map<String, String> result = new HashMap<>();
    if (!templateRegistry.contains(this.getRuleUnitTemplateName())) {
        this.addRuleUnitTemplateToRegistry(templateRegistry);
    }
    Map<String, Object> vars = new HashMap<>();
    String className = this.getRuleUnitClassName();
    vars.put("pmmlPackageName", this.getModelPackageName());
    vars.put("className", className);
    vars.put("pojoInputClassName", PMMLRequestData.class.getName());
    if (this instanceof Miningmodel) {
        vars.put("miningPojoClassName", this.getMiningPojoClassName());
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        TemplateRuntime.execute(templateRegistry.getNamedTemplate(this.getRuleUnitTemplateName()), null, new MapVariableResolverFactory(vars), baos);
    } catch (TemplateError te) {
        return null;
    } catch (TemplateRuntimeError tre) {
        // need to figure out logging here
        return null;
    }
    result.put(this.getModelPackageName() + "." + className, new String(baos.toByteArray()));
    return result.isEmpty() ? null : result.entrySet().iterator().next();
}
Also used : TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) PMMLRequestData(org.kie.api.pmml.PMMLRequestData) HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TemplateError(org.mvel2.templates.TemplateError)

Example 80 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory in project drools by kiegroup.

the class MiningSegmentation method generateSegmentationRules.

public String generateSegmentationRules() {
    StringBuilder builder = new StringBuilder();
    loadTemplates(this.multipleModelMethod);
    Map<String, Object> templateVars = new HashMap<>();
    // "org.kie.pmml.pmml_4_2."+this.getSegmentationId();
    String pkgName = this.getOwner().getModelPackageName();
    CompiledTemplate ct = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    switch(this.multipleModelMethod) {
        case AVERAGE:
            break;
        case MAJORITY_VOTE:
            break;
        case MAX:
            break;
        case MEDIAN:
            break;
        case MODEL_CHAIN:
            List<MiningSegmentTransfer> segmentTransfers = new ArrayList<>();
            MiningSegmentTransfer mst = new MiningSegmentTransfer(this.getSegmentationId(), "1", "2");
            mst.addResultToRequestMapping("calculatedScore", "calculatedScore");
            segmentTransfers.add(mst);
            templateVars.put("segmentTransfers", segmentTransfers);
            templateVars.put("miningModel", this.getOwner());
            templateVars.put("childSegments", this.getMiningSegments());
            templateVars.put("packageName", pkgName);
            templateVars.put("resultMappings", segmentTransfers);
            templateVars.put("ruleUnitClassName", this.getOwner().getRuleUnitClassName());
            ct = templates.getNamedTemplate(this.multipleModelMethod.name());
            TemplateRuntime.execute(ct, null, new MapVariableResolverFactory(templateVars), baos);
            builder.append(new String(baos.toByteArray()));
            break;
        case SELECT_ALL:
            templateVars.put("ruleUnitClassName", this.getOwner().getRuleUnitClassName());
            templateVars.put("miningModel", this.getOwner());
            templateVars.put("childSegments", this.getMiningSegments());
            templateVars.put("packageName", pkgName);
            ct = templates.getNamedTemplate(this.multipleModelMethod.name());
            TemplateRuntime.execute(ct, null, new MapVariableResolverFactory(templateVars), baos);
            builder.append(new String(baos.toByteArray()));
            break;
        case SELECT_FIRST:
            templateVars.put("ruleUnitClassName", this.getOwner().getRuleUnitClassName());
            templateVars.put("miningModel", this.getOwner());
            templateVars.put("childSegments", this.getMiningSegments());
            templateVars.put("packageName", pkgName);
            ct = templates.getNamedTemplate(this.multipleModelMethod.name());
            TemplateRuntime.execute(ct, null, new MapVariableResolverFactory(templateVars), baos);
            builder.append(new String(baos.toByteArray()));
            break;
        case SUM:
            break;
        case WEIGHTED_AVERAGE:
            break;
        case WEIGHTED_MAJORITY_VOTE:
            break;
    }
    return builder.toString();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Aggregations

MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)79 HashMap (java.util.HashMap)43 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)29 ParserContext (org.mvel2.ParserContext)24 Serializable (java.io.Serializable)18 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)17 CompiledExpression (org.mvel2.compiler.CompiledExpression)16 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)16 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)14 TemplateRegistry (org.mvel2.templates.TemplateRegistry)13 Debugger (org.mvel2.debug.Debugger)12 Frame (org.mvel2.debug.Frame)12 HashSet (java.util.HashSet)10 MapObject (org.mvel2.tests.core.res.MapObject)10 IndexedVariableResolverFactory (org.mvel2.integration.impl.IndexedVariableResolverFactory)9 LinkedHashMap (java.util.LinkedHashMap)7 Map (java.util.Map)7 Test (org.junit.Test)7 TemplateRuntimeError (org.mvel2.templates.TemplateRuntimeError)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6