Package org.drools.rule.builder.dialect.mvel

Examples of org.drools.rule.builder.dialect.mvel.MVELDialect$ModifyInterceptor


    private static Map<String, Class<?>> getInputs(final RuleBuildContext context,
                                                   String code,
                                                   BoundIdentifiers bindings,
                                                   Map<String, Class<?>> parentVars) {
        MVELDialect mvel = (MVELDialect) context.getDialect("mvel");

        MVELAnalysisResult mvelAnalysis = null;
        try {
            mvelAnalysis = (MVELAnalysisResult) mvel.analyzeBlock(context,
                    context.getRuleDescr(),
                    null,
                    code,
                    bindings,
                    parentVars,
View Full Code Here


                                          Pattern pattern,
                                          BaseDescr base,
                                          String expr,
                                          Map<String, OperatorDescr> aliases ) {
        Dialect dialect = context.getDialect();
        MVELDialect mvelDialect = (MVELDialect) context.getDialect( "mvel" );
        context.setDialect( mvelDialect );

        PredicateDescr pdescr = new PredicateDescr( context.getRuleDescr().getResource(), expr );
        pdescr.copyParameters( base );
        pdescr.copyLocation( base );
View Full Code Here

            }
        } else if ( fieldName.indexOf( '.' ) > -1 || fieldName.indexOf( '[' ) > -1 || fieldName.indexOf( '(' ) > -1 ) {
            // we need MVEL extractor for expressions
            Dialect dialect = context.getDialect();
            try {
                MVELDialect mvelDialect = (MVELDialect) context.getDialect( "mvel" );
                context.setDialect( mvelDialect );

                Map<String, Class< ? >> declarations = getDeclarationsMap( descr,
                                                                           context,
                                                                           false );
View Full Code Here

        String timestamp = ( annotationDescr != null ) ? annotationDescr.getSingleValue() : null;
        if (timestamp != null) {
            type.setTimestampAttribute( timestamp );
            Package pkg = pkgRegistry.getPackage();           

            MVELDialect dialect = ( MVELDialect ) pkgRegistry.getDialectCompiletimeRegistry().getDialect( "mvel" );           
            PackageBuildContext context = new PackageBuildContext();           
            context.init( this, pkg, typeDescr, pkgRegistry.getDialectCompiletimeRegistry(), dialect, null );
            if ( !type.isTypesafe() ) {
                context.setTypesafe( false );
            }
           
            MVELAnalysisResult results = ( MVELAnalysisResult )
                                context.getDialect().analyzeExpression( context,
                                                                        typeDescr,
                                                                        timestamp,
                                                                        new BoundIdentifiers( Collections.EMPTY_MAP,
                                                                                              Collections.EMPTY_MAP,
                                                                                              Collections.EMPTY_MAP,
                                                                                              type.getTypeClass() ) );
           
            InternalReadAccessor reader = pkg.getClassFieldAccessorStore().getMVELReader( ClassUtils.getPackage(type.getTypeClass()),
                                                                                          type.getTypeClass().getName(),
                                                                                          timestamp,
                                                                                          type.isTypesafe(),
                                                                                          results.getReturnType() );
           
            MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData( "mvel" );
            data.addCompileable( (MVELCompileable) reader );
            ( (MVELCompileable) reader ).compile( data );
            type.setTimestampExtractor( reader );
        }

        annotationDescr = typeDescr.getAnnotation( TypeDeclaration.ATTR_DURATION );
        String duration = ( annotationDescr != null ) ? annotationDescr.getSingleValue() : null;
        if (duration != null) {
            type.setDurationAttribute( duration );
            Package pkg = pkgRegistry.getPackage();
           
            MVELDialect dialect = ( MVELDialect ) pkgRegistry.getDialectCompiletimeRegistry().getDialect( "mvel" );           
            PackageBuildContext context = new PackageBuildContext();           
            context.init( this, pkg, typeDescr, pkgRegistry.getDialectCompiletimeRegistry(), dialect, null );
            if ( !type.isTypesafe() ) {
                context.setTypesafe( false );
            }
View Full Code Here

                continue;
            }

            if ( !simple || !context.isTypesafe() ) {
                Dialect dialect = context.getDialect();
                MVELDialect mvelDialect = (MVELDialect) context.getDialect( "mvel" );
                context.setDialect( mvelDialect );

                PredicateDescr pdescr = new PredicateDescr( expr );
                buildEval( context,
                           pattern,
                           pdescr,
                           null,
                           aliases );

                // fall back to original dialect
                context.setDialect( dialect );
                continue;
            }

            if ( !(d instanceof RelationalExprDescr) ) {
                throw new RuntimeException( "What caused this?: " + d );
            }

            RelationalExprDescr exprDescr = (RelationalExprDescr) d;

            AtomicExprDescr rdescr = ((AtomicExprDescr) exprDescr.getRight());
            String fieldName = ((AtomicExprDescr) exprDescr.getLeft()).getExpression();
            String value = rdescr.getExpression().trim();

            ExprBindings rightExpr = new ExprBindings();
            setInputs( context,
                       rightExpr,
                       (pattern.getObjectType() instanceof ClassObjectType) ? ((ClassObjectType) pattern.getObjectType()).getClassType() : FactTemplate.class,
                       value );

            String[] parts = fieldName.split( "\\." );
            if ( parts.length == 2 ) {
                if ( "this".equals( parts[0].trim() ) ) {
                    if ( parts[1].trim().startsWith( "[" ) ) {
                        // they are trying map accessors to rewrite to eval
                        Dialect dialect = context.getDialect();
                        MVELDialect mvelDialect = (MVELDialect) context.getDialect( "mvel" );
                        context.setDialect( mvelDialect );

                        PredicateDescr pdescr = new PredicateDescr( expr );
                        buildEval( context,
                                   pattern,
                                   pdescr,
                                   null,
                                   aliases );

                        // fall back to original dialect
                        context.setDialect( dialect );
                        continue;
                    } else {
                        // it's a redundant this so trim
                        fieldName = parts[1];
                    }
                } else if ( pattern.getDeclaration() != null && parts[0].trim().equals( pattern.getDeclaration().getIdentifier() ) ) {
                    // it's a redundant declaration so trim
                    fieldName = parts[1];
                }
            }

            if ( fieldName.indexOf( '.' ) >= 0 || fieldName.indexOf( '[' ) >= 0 || fieldName.indexOf( '(' ) >= 0 ) {
                // if left has any inputs then we need to rewrite to eval
                ExprBindings leftExpr = new ExprBindings();
                setInputs( context,
                           leftExpr,
                           ((ClassObjectType) ((Pattern) context.getBuildStack().peek()).getObjectType()).getClassType(),
                           fieldName );
                if ( !leftExpr.getRuleBindings().isEmpty() ) {
                    Dialect dialect = context.getDialect();
                    MVELDialect mvelDialect = (MVELDialect) context.getDialect( "mvel" );
                    context.setDialect( mvelDialect );

                    PredicateDescr pdescr = new PredicateDescr( expr );
                    buildEval( context,
                               pattern,
                               pdescr,
                               null,
                               aliases );

                    // fall back to original dialect
                    context.setDialect( dialect );
                    continue;
                }
            }

            final InternalReadAccessor extractor = getFieldReadAccessor( context,
                                                                         d,
                                                                         pattern.getObjectType(),
                                                                         fieldName,
                                                                         null,
                                                                         false );

            if ( extractor == null ) {
                context.getErrors().add( new DescrBuildError( context.getParentDescr(),
                                                              d,
                                                              null,
                                                              "Unable to build constraint as  '" + fieldName + "' is invalid" ) );
                continue;
            }

            String operator = relDescr.getOperator().trim();

            Restriction restriction = null;
            // is it a literal? Does not include enums
            if ( rdescr.isLiteral() ) {
                restriction = buildLiteralRestriction( context,
                                                       extractor,
                                                       new LiteralRestrictionDescr( operator,
                                                                                    relDescr.isNegated(),
                                                                                    relDescr.getParameters(),
                                                                                    value,
                                                                                    LiteralRestrictionDescr.TYPE_STRING ) ); // default type
            } else {
                // is it an enum?
                int dotPos = value.indexOf( '.' );
                if ( dotPos >= 0 ) {
                    final String className = value.substring( 0,
                                                              dotPos );
                    String enumName = value.substring( dotPos + 1 );
                    try {
                        final Class< ? > cls = context.getDialect().getTypeResolver().resolveType( className );
                        if ( enumName.indexOf( '(' ) < 0 && enumName.indexOf( '.' ) < 0 && enumName.indexOf( '[' ) < 0 ) {
                            restriction = buildLiteralRestriction( context,
                                                                   extractor,
                                                                   new LiteralRestrictionDescr( operator,
                                                                                                relDescr.isNegated(),
                                                                                                relDescr.getParameters(),
                                                                                                value,
                                                                                                LiteralRestrictionDescr.TYPE_STRING ) ); // default type
                        }
                    } catch ( ClassNotFoundException e ) {
                        // do nothing as this is just probing to see if it was a class, which we now know it isn't :)
                    }
                }
            }

            if ( restriction != null ) {
                pattern.addConstraint( new LiteralConstraint( extractor,
                                                              (LiteralRestriction) restriction ) );
                continue;
            }

            Declaration declr = null;
            if ( value.indexOf( '(' ) < 0 && value.indexOf( '.' ) < 0 && value.indexOf( '[' ) < 0 ) {
                declr = context.getDeclarationResolver().getDeclaration( context.getRule(),
                                                                               value );

                if ( declr == null ) {
                    // trying to create implicit declaration
                    final Pattern thisPattern = (Pattern) context.getBuildStack().peek();
                    declr = this.createDeclarationObject( context,
                                                          value,
                                                          thisPattern );
                    if ( declr == null ) {
                        context.getErrors().add( new DescrBuildError( context.getParentDescr(),
                                                                      d,
                                                                      null,
                                                                      "Unable to return Declaration for identifier '" + value + "'" ) );
                        continue;
                    }
                }
            }

            if ( declr == null ) {
                parts = value.split( "\\." );
                if ( parts.length == 2 ) {
                    if ( "this".equals( parts[0].trim() ) ) {
                        declr = this.createDeclarationObject( context,
                                                              parts[1].trim(),
                                                              (Pattern) context.getBuildStack().peek() );
                        value = parts[1].trim();
                    } else {
                        declr = context.getDeclarationResolver().getDeclaration( context.getRule(),
                                                                                 parts[0].trim() );
                        // if a declaration exists, then it may be a variable direct property access
                        if ( declr != null ) {
                            if ( declr.isPatternDeclaration() ) {
                                declr = this.createDeclarationObject( context,
                                                                      parts[1].trim(),
                                                                      declr.getPattern() );
                                value = parts[1].trim();

                            } else {
                                context.getErrors().add( new DescrBuildError( context.getParentDescr(),
                                                                              d,
                                                                              "",
                                                                              "Not possible to directly access the property '" + parts[1] + "' of declaration '" + parts[0] + "' since it is not a pattern" ) );
                                continue;
                            }
                        }
                    }
                }
            }

            if ( declr != null ) {
                Target right = getRightTarget( extractor );
                Target left = (declr.isPatternDeclaration() && !(Date.class.isAssignableFrom( declr.getExtractor().getExtractToClass() ) || Number.class.isAssignableFrom( declr.getExtractor().getExtractToClass() ))) ? Target.HANDLE : Target.FACT;
                final Evaluator evaluator = getEvaluator( context,
                                                          d,
                                                          extractor.getValueType(),
                                                          operator,
                                                          relDescr.isNegated(),
                                                          relDescr.getParametersText(),
                                                          left,
                                                          right );
                if ( evaluator == null ) {
                    continue;
                }

                restriction = new VariableRestriction( extractor,
                                                       declr,
                                                       evaluator );

                if ( declr.getPattern().getObjectType().equals( new ClassObjectType( DroolsQuery.class ) ) ) {
                    // declaration is query argument, so allow for unification.
                    restriction = new UnificationRestriction( (VariableRestriction) restriction );
                }
            }

            if ( restriction == null ) {
                Dialect dialect = context.getDialect();
                if ( !value.startsWith( "(" ) ) {
                    // it's not a traditional return value, so override the dialect
                    MVELDialect mvelDialect = (MVELDialect) context.getDialect( "mvel" );
                    context.setDialect( mvelDialect );
                }

                // execute it as a return value
                restriction = buildRestriction( context,
View Full Code Here

    private void setInputs( RuleBuildContext context,
                            ExprBindings descrBranch,
                            Class thisClass,
                            String expr ) {
        MVELDialect dialect = (MVELDialect) context.getDialect( "mvel" );

        ParserConfiguration conf = new ParserConfiguration();
        conf.setImports( dialect.getImports() );
        conf.setPackageImports( (HashSet) dialect.getPackgeImports() );

        conf.setClassLoader( context.getPackageBuilder().getRootClassLoader() );

        final ParserContext pctx = new ParserContext( conf );
        pctx.setStrictTypeEnforcement( false );
View Full Code Here

                                                        final LiteralRestrictionDescr literalRestrictionDescr ) {
        FieldValue field = null;
        try {
            String value = literalRestrictionDescr.getText().trim();

            MVELDialect dialect = (MVELDialect) context.getDialect( "mvel" );
            ParserConfiguration pconf = new ParserConfiguration();
            pconf.setImports( dialect.getImports() );
            pconf.setPackageImports( (HashSet) dialect.getPackgeImports() );
            ParserContext pctx = new ParserContext( pconf );

            field = FieldFactory.getFieldValue( MVEL.executeExpression( MVEL.compileExpression( value,
                                                                                                pctx ) ),
                                                extractor.getValueType(),
View Full Code Here

    protected String fixBlockDescr(final RuleBuildContext context,
                                   String originalCode,
                                   List<JavaBlockDescr> blocks,
                                   BoundIdentifiers bindings,
                                   Map<String, Declaration> decls) {
        MVELDialect mvel = (MVELDialect) context.getDialect( "mvel" );

        // sorting exit points for correct order iteration
        Collections.sort( blocks,
                          new Comparator<JavaBlockDescr>() {
                              public int compare(JavaBlockDescr o1,
View Full Code Here

   
    public Map<String, Class<?>> getInputs(final RuleBuildContext context,
                                           String code,
                                           BoundIdentifiers bindings,
                                           Map<String, Class<?>> parentVars) {       
        MVELDialect mvel = (MVELDialect) context.getDialect( "mvel" );
       
        MVELAnalysisResult mvelAnalysis = null;
        try {
            mvelAnalysis = ( MVELAnalysisResult ) mvel.analyzeBlock( context,
                                                                     context.getRuleDescr(),
                                                                     null,
                                                                     code,
                                                                     bindings,
                                                                     parentVars,
View Full Code Here

        String timestamp = ( annotationDescr != null ) ? annotationDescr.getSingleValue() : null;
        if (timestamp != null) {
            type.setTimestampAttribute( timestamp );
            Package pkg = pkgRegistry.getPackage();           

            MVELDialect dialect = ( MVELDialect ) pkgRegistry.getDialectCompiletimeRegistry().getDialect( "mvel" );           
            PackageBuildContext context = new PackageBuildContext();           
            context.init( this, pkg, typeDescr, pkgRegistry.getDialectCompiletimeRegistry(), dialect, null );
            if ( !type.isTypesafe() ) {
                context.setTypesafe( false );
            }
           
            MVELAnalysisResult results = ( MVELAnalysisResult )
                                context.getDialect().analyzeExpression( context,
                                                                        typeDescr,
                                                                        timestamp,
                                                                        new BoundIdentifiers( Collections.EMPTY_MAP,
                                                                                              Collections.EMPTY_MAP,
                                                                                              Collections.EMPTY_MAP,
                                                                                              type.getTypeClass() ) );
           
            InternalReadAccessor reader = pkg.getClassFieldAccessorStore().getMVELReader( ClassUtils.getPackage(type.getTypeClass()),
                                                                                          type.getTypeClass().getName(),
                                                                                          timestamp,
                                                                                          type.isTypesafe(),
                                                                                          results.getReturnType() );
           
            MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData( "mvel" );
            data.addCompileable( (MVELCompileable) reader );
            ( (MVELCompileable) reader ).compile( data );
            type.setTimestampExtractor( reader );
        }

        annotationDescr = typeDescr.getAnnotation( TypeDeclaration.ATTR_DURATION );
        String duration = ( annotationDescr != null ) ? annotationDescr.getSingleValue() : null;
        if (duration != null) {
            type.setDurationAttribute( duration );
            Package pkg = pkgRegistry.getPackage();
           
            MVELDialect dialect = ( MVELDialect ) pkgRegistry.getDialectCompiletimeRegistry().getDialect( "mvel" );           
            PackageBuildContext context = new PackageBuildContext();           
            context.init( this, pkg, typeDescr, pkgRegistry.getDialectCompiletimeRegistry(), dialect, null );
            if ( !type.isTypesafe() ) {
                context.setTypesafe( false );
            }
View Full Code Here

TOP

Related Classes of org.drools.rule.builder.dialect.mvel.MVELDialect$ModifyInterceptor

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.