Package org.mvel2

Examples of org.mvel2.ParserContext


    public void testDebugSymbolCount() {
        String expr = "System.out.println( \"a1\" );\n" + "System.out.println( \"a2\" );\n" + "System.out.println( \"a3\" );\n" + "System.out.println( \"a4\" );\n";

        ExpressionCompiler compiler = new ExpressionCompiler( expr );

        ParserContext context = new ParserContext();
        context.setDebugSymbols( true );
        context.addImport( "System",
                           System.class );
        context.setStrictTypeEnforcement( true );
        //context.setDebugSymbols( true );
        context.setSourceFile( "mysource" );


        Serializable compiledExpression = compiler.compile( context );

        String s = DebugTools.decompile( compiledExpression );
View Full Code Here


    Action,
    Receiver {
    private Serializable expr;

    public MvelAction(String text) {
        final ParserContext parserContext = new ParserContext();
        parserContext.setStrictTypeEnforcement( false );

        ExpressionCompiler compiler = new ExpressionCompiler( text );
        this.expr = compiler.compile( );
    }
View Full Code Here

        } catch ( NoSuchFieldException e ) {
            e.printStackTrace();
            throw new RuntimeDroolsException( "Unable to resolve import '" + lastName + "'" );
        }

        final ParserContext parserContext = new ParserContext( resolvedImports,
                                                               null,
                                                               name );
        parserContext.getParserConfiguration().setClassLoader( classLoader );

        for ( String pkgImport : this.pkgImports ) {
            parserContext.addPackageImport( pkgImport );
        }

        parserContext.setInterceptors( interceptors );
        parserContext.setStrongTyping( strictMode );
        //parserContext.setStrictTypeEnforcement( strictMode );

        resolvedInputs = new HashMap<String, Class>( inputIdentifiers.length );

        parserContext.addInput( "drools",
                                KnowledgeHelper.class );

    resolvedInputs.put( "drools",
                        KnowledgeHelper.class );
   
    String lastIdentifier = null;
        String lastType = null;
        try {
            for ( int i = 0, length = inputIdentifiers.length; i < length; i++ ) {
                lastIdentifier = inputIdentifiers[i];
                lastType = inputTypes[i];
                Class cls = loadClass( classLoader,
                                       inputTypes[i] );
                resolvedInputs.put( inputIdentifiers[i],
                                    cls );
                parserContext.addInput( inputIdentifiers[i],
                                        cls );
            }
        } catch ( ClassNotFoundException e ) {
            e.printStackTrace();
            throw new RuntimeDroolsException( "Unable to resolve class '" + lastType + "' for identifier '" + lastIdentifier );
        }

        if ( parserContext.getInputs().get( "kcontext" ) == null)  {

          parserContext.addInput( "kcontext",
                                  RuleContext.class );
 
          resolvedInputs.put( "kcontext",
                              RuleContext.class );
View Full Code Here

                                "Action node '" + node.getName() + "' [" + node.getId() + "] has empty action."));
                        } else if( "mvel".equals( droolsAction.getDialect() ) ) {
                            try {
                                ExpressionCompiler compiler = new ExpressionCompiler(actionString);
                                compiler.setVerifying(true);
                                ParserContext parserContext = new ParserContext();
                                //parserContext.setStrictTypeEnforcement(true);
                                compiler.compile(parserContext);
                                List<ErrorDetail> mvelErrors = parserContext.getErrorList();
                                if (mvelErrors != null) {
                                    for (Iterator<ErrorDetail> iterator = mvelErrors.iterator(); iterator.hasNext(); ) {
                                        ErrorDetail error = iterator.next();
                                        errors.add(new ProcessValidationErrorImpl(process,
                                            "Action node '" + node.getName() + "' [" + node.getId() + "] has invalid action: " + error.getMessage() + "."));
View Full Code Here

    Expression,
    Receiver {
    private Serializable expr;

    public MvelExpression(String text) {
        final ParserContext parserContext = new ParserContext();
        parserContext.setStrictTypeEnforcement( false );

        ExpressionCompiler compiler = new ExpressionCompiler( text );
        this.expr = compiler.compile( parserContext );
    }
View Full Code Here

    public void lispFormHandler(LispForm lispForm) {
        StringBuilderAppendable appendable = new StringBuilderAppendable();
        FunctionHandlers.dump( lispForm,
                               appendable );

        ParserContext context = new ParserContext();
       

        String namespace = this.session.getAgenda().getFocusName();

        Package pkg = this.ruleBase.getPackage( namespace );
        if ( pkg == null ) {
            this.packageBuilder.addPackage( createPackageDescr( namespace ) );
            pkg = this.ruleBase.getPackage( namespace );
           
        }

        if ( pkg != null ) {
            // only time this will be null is if we have yet to do any packagedescr work

            try {
                for ( Iterator it = pkg.getImports().entrySet().iterator(); it.hasNext(); ) {
                    Entry entry = (Entry) it.next();
                    String importName = ((ImportDeclaration) entry.getValue()).getTarget();
                    if ( importName.endsWith( "*" )) {
                        context.addPackageImport( importName.substring( 0,
                                                                        importName.length() - 2 ) );
                    } else {
                     
                        Class cls = ((InternalRuleBase)ruleBase).getRootClassLoader().loadClass( importName );
                        context.addImport( cls.getSimpleName(),
                                           (Class) cls );
                    }
                }

            } catch ( Exception e ) {
View Full Code Here

        String name = context.getRuleName();
        RuleInfo currentRule = getCurrentRule( drlInfo,
                                               name );
        String qName = drlInfo.getPackageName() + "." + name;
        MVELDialect dialect = (MVELDialect) drlInfo.getDialectRegistry().getDialect("mvel");
        ParserContext initialContext = createInitialContext( params,
                                                             qName,
                                                             dialect );
        MvelContext mCon = new MvelContext();
        mCon.setContext( initialContext );
View Full Code Here

        //Lookup ParserConfiguration to retrieve imports and package imports
        PackageRegistry packageRegistry = dialect.getPackageRegistry();
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) packageRegistry.getDialectRuntimeRegistry().getDialectData( dialect.getId() );
        ParserConfiguration pconf = data.getParserConfiguration();

        final ParserContext context = new ParserContext( pconf.getImports(),
                                                         null,
                                                         qualifiedName );

        if ( pconf.getPackageImports() != null ) {
            for (String packageImport : pconf.getPackageImports()) {
                context.addPackageImport( packageImport );
            }
        }
        context.setStrictTypeEnforcement( false );

        context.setInterceptors( dialect.getInterceptors() );
        context.setInputs( params );
        context.addInput("drools", KnowledgeHelper.class);
        context.addInput("kcontext", RuleContext.class);
        context.setCompiled( true );
        return context;
    }
View Full Code Here

            vars.putAll( this.context );
            vars.put( "tuple",
                      ((LeftTuple)tuple).toFactHandles() );

            // compile MVEL expression
            ParserContext mvelctx = new ParserContext();
            Serializable compiled = MVEL.compileExpression( this.expr,
                                                            mvelctx );
            // execute the expression
            Boolean result = (Boolean) MVEL.executeExpression( compiled,
                                                               vars );
View Full Code Here

                Class<?> variableType = getVariableType(variableName);
                if (variableType != null) {
                    return new VariableExpression(variableName, analyzeExpressionNode(accessorNode, node, variableType), variableType);
                } else {
                    if (node.getLiteralValue() instanceof ParserContext) {
                        ParserContext pCtx = (ParserContext)node.getLiteralValue();
                        // it's not a variable but a method invocation on this
                        Class<?> thisClass = pCtx.getInputs().get("this");
                        try {
                            return new EvaluatedExpression(new MethodInvocation(thisClass.getMethod(variableName)));
                        } catch (NoSuchMethodException e) {
                            if (node.getEgressType() == Class.class) {
                                // there's no method on this with the given name, check if it is a class literal
                                Class<?> classLiteral = pCtx.getParserConfiguration().getImport(variableName);
                                if (classLiteral != null) {
                                    return new FixedExpression(Class.class, classLiteral);
                                }
                            }
                            throw new RuntimeException(e);
View Full Code Here

TOP

Related Classes of org.mvel2.ParserContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.