Java Code Examples for org.mvel2.templates.TemplateRuntime#execute()

The following examples show how to use org.mvel2.templates.TemplateRuntime#execute() . 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 7 votes vote down vote up
public static synchronized TemplateRegistry getInvokerTemplateRegistry(ClassLoader cl) {
    if ( !INVOKER_REGISTRY.contains( "invokers" ) ) {
        InputStream javaInvokersMvelStream = JavaRuleBuilderHelper.class.getResourceAsStream( JAVA_INVOKERS_MVEL );
        INVOKER_REGISTRY.addNamedTemplate( "invokers",
                                           TemplateCompiler.compileTemplate( javaInvokersMvelStream ) );
        try {
            javaInvokersMvelStream.close();
        } catch ( IOException ex ) {
            logger.debug( "Failed to close stream!", ex );
        }
        TemplateRuntime.execute( INVOKER_REGISTRY.getNamedTemplate( "invokers" ),
                                 null,
                                 INVOKER_REGISTRY );            
    }        
    return INVOKER_REGISTRY;
}
 
Example 2
Source File: JavaRuleBuilderHelper.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public static synchronized TemplateRegistry getRuleTemplateRegistry(ClassLoader cl) {
    if ( !RULE_REGISTRY.contains( "rules" ) ) {
        InputStream javaRuleMvelStream = JavaRuleBuilderHelper.class.getResourceAsStream( JAVA_RULE_MVEL );
        RULE_REGISTRY.addNamedTemplate( "rules",
                                        TemplateCompiler.compileTemplate( javaRuleMvelStream ) );
        try {
            javaRuleMvelStream.close();
        } catch ( IOException ex ) {
            logger.debug( "Failed to close stream!", ex );
        }
        TemplateRuntime.execute( RULE_REGISTRY.getNamedTemplate( "rules" ),
                                 null,
                                 RULE_REGISTRY );            
    }
    
    return RULE_REGISTRY;
}
 
Example 3
Source File: DrlDumper.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public DrlDumper() {
    REPORT_REGISTRY.addNamedTemplate( "drl",
                                      TemplateCompiler.compileTemplate( DrlDumper.class.getResourceAsStream( "drl.mvel" ),
                                                                        (Map<String, Class< ? extends Node>>) null ) );

    /**
     * Process these templates
     */
    TemplateRuntime.execute( REPORT_REGISTRY.getNamedTemplate( "drl" ),
                             null,
                             REPORT_REGISTRY );
}
 
Example 4
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 5
Source File: AccumulateTemplateTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private TemplateRegistry getRuleTemplateRegistry() {
    TemplateRegistry ruleRegistry = new SimpleTemplateRegistry();
    CompiledTemplate compiled = TemplateCompiler.compileTemplate( JavaRuleBuilderHelper.class.getResourceAsStream( "javaRule.mvel" ),
                                                                  (Map<String, Class<? extends Node>>) null );
    TemplateRuntime.execute( compiled,
                             null,
                             ruleRegistry );

    return ruleRegistry;
}
 
Example 6
Source File: AccumulateTemplateTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private TemplateRegistry getInvokerTemplateRegistry() {
    TemplateRegistry invokerRegistry = new SimpleTemplateRegistry();
    CompiledTemplate compiled = TemplateCompiler.compileTemplate( JavaRuleBuilderHelper.class.getResourceAsStream( "javaInvokers.mvel" ),
                                                                  (Map<String, Class<? extends Node>>) null );
    TemplateRuntime.execute( compiled,
                             null,
                             invokerRegistry );

    return invokerRegistry;
}
 
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: SessionReporter.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public static void addNamedTemplate(String name,
                                    InputStream template) {
    REPORT_REGISTRY.addNamedTemplate( name,
                                      TemplateCompiler.compileTemplate( template,
                                                                        (Map<String, Class<? extends Node>>) null ) );
    /*
     * Process these templates
     */
    TemplateRuntime.execute( REPORT_REGISTRY.getNamedTemplate( name ),
                             null,
                             REPORT_REGISTRY );
}
 
Example 9
Source File: PolicyTemplateUtil.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a dynamic description for the given policy and stores the
 * result on the policy bean instance.  This should be done prior
 * to returning the policybean back to the user for a REST call to the
 * management API.
 * @param policy the policy
 * @throws Exception any exception
 */
public static void generatePolicyDescription(PolicyBean policy) throws Exception {
    PolicyDefinitionBean def = policy.getDefinition();
    PolicyDefinitionTemplateBean templateBean = getTemplateBean(def);
    if (templateBean == null) {
        return;
    }
    String cacheKey = def.getId() + "::" + templateBean.getLanguage(); //$NON-NLS-1$
    CompiledTemplate template = templateCache.get(cacheKey);
    if (template == null) {
        template = TemplateCompiler.compileTemplate(templateBean.getTemplate());
        templateCache.put(cacheKey, template);
    }
    try {
        // TODO hack to fix broken descriptions - this util should probably not know about encrypted data
        String jsonConfig = policy.getConfiguration();
        if (CurrentDataEncrypter.instance != null) {
            EntityType entityType = EntityType.Api;
            if (policy.getType() == PolicyType.Client) {
                entityType = EntityType.ClientApp;
            } else if (policy.getType() == PolicyType.Plan) {
                entityType = EntityType.Plan;
            }
            DataEncryptionContext ctx = new DataEncryptionContext(policy.getOrganizationId(),
                    policy.getEntityId(), policy.getEntityVersion(), entityType);
            jsonConfig = CurrentDataEncrypter.instance.decrypt(jsonConfig, ctx);
        }
        Map<String, Object> configMap = mapper.readValue(jsonConfig, Map.class);
        configMap = new PolicyConfigMap(configMap);
        String desc = (String) TemplateRuntime.execute(template, configMap);
        policy.setDescription(desc);
    } catch (Exception e) {
        e.printStackTrace();
        // TODO properly log the error
        policy.setDescription(templateBean.getTemplate());
    }
}
 
Example 10
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 11
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 12
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 );
}
 
Example 13
Source File: MvelTemplateEngine.java    From spark-template-engines with Apache License 2.0 4 votes vote down vote up
@Override
public String render(final ModelAndView modelAndView) {
    final CompiledTemplate compiledTemplate = loadTemplate(modelAndView.getViewName());
    return (String) TemplateRuntime.execute(compiledTemplate, modelAndView.getModel());
}
 
Example 14
Source File: PayloadExpressionEvaluator.java    From chancery with Apache License 2.0 4 votes vote down vote up
public final String evaluateForPayload(@Nonnull CallbackPayload payload) {
    return (String) TemplateRuntime.execute(compiledTemplate, payload);
}