org.mvel2.integration.impl.MapVariableResolverFactory Java Examples

The following examples show how to use org.mvel2.integration.impl.MapVariableResolverFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaRuleBuilderHelper.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private static void generateInvokerTemplate(final String invokerTemplate,
                                            final RuleBuildContext context,
                                            final String className,
                                            final Map vars,
                                            final Object invokerLookup,
                                            final BaseDescr descrLookup) {
    TemplateRegistry registry = getInvokerTemplateRegistry(context.getKnowledgeBuilder().getRootClassLoader());
    final String invokerClassName = context.getPkg().getName() + "." + context.getRuleDescr().getClassName() + StringUtils.ucFirst( className ) + "Invoker";

    context.addInvoker( invokerClassName,
                        (String) TemplateRuntime.execute( registry.getNamedTemplate( invokerTemplate ),
                                                          null,
                                                          new MapVariableResolverFactory( vars ),
                                                          registry ) );

    context.addInvokerLookup( invokerClassName, invokerLookup );
    context.addDescrLookups( invokerClassName, descrLookup );
}
 
Example #2
Source File: AbstractJavaProcessBuilder.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public void generateTemplates(final String ruleTemplate,
                             final String invokerTemplate,
                             final ProcessBuildContext context,
                             final String className,
                             final Map vars,
                             final Object invokerLookup,
                             final BaseDescr descrLookup) {
    TemplateRegistry registry = getRuleTemplateRegistry();

    context.getMethods().add((String)
            TemplateRuntime.execute(registry.getNamedTemplate(ruleTemplate), null, new MapVariableResolverFactory(vars), registry)
    );

    registry = getInvokerTemplateRegistry();
    final String invokerClassName = context.getPkg().getName() + "." + context.getProcessDescr().getClassName() + StringUtils.ucFirst(className) + "Invoker";

    context.getInvokers().put(invokerClassName,
            (String)TemplateRuntime.execute(registry.getNamedTemplate(invokerTemplate), null, new MapVariableResolverFactory(vars), registry)
    );

    context.addInvokerLookup(invokerClassName, invokerLookup);
    context.addDescrLookups(invokerClassName, descrLookup);
}
 
Example #3
Source File: IBatisNamespaceShardingRule.java    From cobarclient with Apache License 2.0 6 votes vote down vote up
public boolean isDefinedAt(IBatisRoutingFact routingFact) {
    Validate.notNull(routingFact);
    String namespace = StringUtils.substringBeforeLast(routingFact.getAction(), ".");
    boolean matches = StringUtils.equals(namespace, getTypePattern());
    if (matches) {
        try {
            Map<String, Object> vrs = new HashMap<String, Object>();
            vrs.putAll(getFunctionMap());
            vrs.put("$ROOT", routingFact.getArgument()); // add top object reference for expression
            VariableResolverFactory vrfactory = new MapVariableResolverFactory(vrs);
            if (MVEL.evalToBoolean(getAttributePattern(), routingFact.getArgument(), vrfactory)) {
                return true;
            }
        } catch (Throwable t) {
            logger
                    .info(
                            "failed to evaluate attribute expression:'{}' with context object:'{}'\n{}",
                            new Object[] { getAttributePattern(), routingFact.getArgument(), t });
        }
    }
    return false;
}
 
Example #4
Source File: IBatisSqlActionShardingRule.java    From cobarclient with Apache License 2.0 6 votes vote down vote up
public boolean isDefinedAt(IBatisRoutingFact routingFact) {
    Validate.notNull(routingFact);
    boolean matches = StringUtils.equals(getTypePattern(), routingFact.getAction());
    if (matches) {
        try {
            Map<String, Object> vrs = new HashMap<String, Object>();
            vrs.putAll(getFunctionMap());
            vrs.put("$ROOT", routingFact.getArgument()); // add top object reference for expression
            VariableResolverFactory vrfactory = new MapVariableResolverFactory(vrs);
            if (MVEL.evalToBoolean(getAttributePattern(), routingFact.getArgument(), vrfactory)) {
                return true;
            }
        } catch (Throwable t) {
            logger
                    .info(
                            "failed to evaluate attribute expression:'{}' with context object:'{}'\n{}",
                            new Object[] { getAttributePattern(), routingFact.getArgument(), t });
        }
    }

    return false;
}
 
Example #5
Source File: JavaRuleBuilderHelper.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public static void generateMethodTemplate(final String ruleTemplate, final RuleBuildContext context, final Map vars) {
    TemplateRegistry registry = getRuleTemplateRegistry(context.getKnowledgeBuilder().getRootClassLoader());

    context.addMethod((String) TemplateRuntime.execute( registry.getNamedTemplate(ruleTemplate),
                                                        null,
                                                        new MapVariableResolverFactory(vars),
                                                        registry) );
}
 
Example #6
Source File: DrlDumper.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public String dump( final PackageDescr pkg ) {
    Map<String, Object> context = new HashMap<String, Object>();
    context.put( "pkg",
                 pkg );
    context.put( "mvel",
                 mvel );

    return (String) TemplateRuntime.execute( REPORT_REGISTRY.getNamedTemplate( "drl" ),
                                             null,
                                             new MapVariableResolverFactory( context ),
                                             REPORT_REGISTRY );
}
 
Example #7
Source File: SessionReporter.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public static String generateReport(final String ruleTemplate,
                                    final StatefulKnowledgeSessionInfo session,
                                    final Map<String, Object> vars) {
    Map<String, Object> context = new HashMap<String, Object>();
    if ( vars != null ) {
        context.putAll( vars );
    }
    context.put( "session",
                 session );

    return (String) TemplateRuntime.execute( REPORT_REGISTRY.getNamedTemplate( ruleTemplate ),
                                             null,
                                             new MapVariableResolverFactory( context ),
                                             REPORT_REGISTRY );
}
 
Example #8
Source File: ELFunction.java    From baymax with Apache License 2.0 5 votes vote down vote up
public Integer execute(String columnValue, Map<String, Object> extension) {
    Map<String, Object> vrs = new HashMap<String, Object>();
    //, Map<String, ElFunction<?,?>> functionMap
    //vrs.putAll(functionMap);// 拓展函数
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", columnValue);
    vrs.put("$ROOT", params);
    VariableResolverFactory vrfactory = new MapVariableResolverFactory(vrs);
    return MVEL.eval(expression, params, vrfactory, Integer.class);
}
 
Example #9
Source File: AccumulateTemplateTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@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 );
}
 
Example #10
Source File: AccumulateTemplateTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@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 );
}
 
Example #11
Source File: AccumulateTemplateTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@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 );
}