⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rulebuilder.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        final Declaration[] declarations = new Declaration[usedIdentifiers[0].size()];
        for ( int i = 0, size = usedIdentifiers[0].size(); i < size; i++ ) {
            declarations[i] = (Declaration) this.declarations.get( (String) usedIdentifiers[0].get( i ) );
        }

        final PredicateConstraint predicateConstraint = new PredicateConstraint( declaration,
                                                                                 declarations );
        column.addConstraint( predicateConstraint );

        StringTemplate st = RuleBuilder.ruleGroup.getInstanceOf( "predicateMethod" );

        st.setAttribute( "declaration",
                         declaration );
        st.setAttribute( "declarationType",
                         ((ClassObjectType) declaration.getObjectType()).getClassType().getName().replace( '$',
                                                                                                           '.' ) );

        setStringTemplateAttributes( st,
                                     declarations,
                                     (String[]) usedIdentifiers[1].toArray( new String[usedIdentifiers[1].size()] ),
                                     predicateDescr.getText() );

        st.setAttribute( "methodName",
                         classMethodName );

        final String predicateText = RuleBuilder.functionFixer.fix( predicateDescr.getText() );
        st.setAttribute( "text",
                         predicateText );

        this.methods.add( st.toString() );

        st = RuleBuilder.invokerGroup.getInstanceOf( "predicateInvoker" );

        st.setAttribute( "package",
                         this.pkg.getName() );
        st.setAttribute( "ruleClassName",
                         ucFirst( this.ruleDescr.getClassName() ) );
        st.setAttribute( "invokerClassName",
                         this.ruleDescr.getClassName() + ucFirst( classMethodName ) + "Invoker" );
        st.setAttribute( "methodName",
                         classMethodName );

        st.setAttribute( "declaration",
                         declaration );
        st.setAttribute( "declarationType",
                         ((ClassObjectType) declaration.getObjectType()).getClassType().getName().replace( '$',
                                                                                                           '.' ) );

        setStringTemplateAttributes( st,
                                     declarations,
                                     (String[]) usedIdentifiers[1].toArray( new String[usedIdentifiers[1].size()] ),
                                     predicateDescr.getText() );

        st.setAttribute( "hashCode",
                         predicateText.hashCode() );

        final String invokerClassName = this.pkg.getName() + "." + this.ruleDescr.getClassName() + ucFirst( classMethodName ) + "Invoker";
        this.invokers.put( invokerClassName,
                           st.toString() );
        this.invokerLookups.put( invokerClassName,
                                 predicateConstraint );
        this.descrLookups.put( invokerClassName,
                               predicateDescr );
    }

    private EvalCondition build(final EvalDescr evalDescr) {

        final String classMethodName = "eval" + this.counter++;
        evalDescr.setClassMethodName( classMethodName );

        final List[] usedIdentifiers = getUsedIdentifiers( evalDescr,
                                                           evalDescr.getText() );

        final Declaration[] declarations = new Declaration[usedIdentifiers[0].size()];
        for ( int i = 0, size = usedIdentifiers[0].size(); i < size; i++ ) {
            declarations[i] = (Declaration) this.declarations.get( (String) usedIdentifiers[0].get( i ) );
        }

        final EvalCondition eval = new EvalCondition( declarations );

        StringTemplate st = RuleBuilder.ruleGroup.getInstanceOf( "evalMethod" );

        setStringTemplateAttributes( st,
                                     declarations,
                                     (String[]) usedIdentifiers[1].toArray( new String[usedIdentifiers[1].size()] ),
                                     evalDescr.getText() );

        st.setAttribute( "methodName",
                         classMethodName );

        final String evalText = RuleBuilder.functionFixer.fix( evalDescr.getText() );
        st.setAttribute( "text",
                         evalText );

        this.methods.add( st.toString() );

        st = RuleBuilder.invokerGroup.getInstanceOf( "evalInvoker" );

        st.setAttribute( "package",
                         this.pkg.getName() );
        st.setAttribute( "ruleClassName",
                         ucFirst( this.ruleDescr.getClassName() ) );
        st.setAttribute( "invokerClassName",
                         this.ruleDescr.getClassName() + ucFirst( classMethodName ) + "Invoker" );
        st.setAttribute( "methodName",
                         classMethodName );

        setStringTemplateAttributes( st,
                                     declarations,
                                     (String[]) usedIdentifiers[1].toArray( new String[usedIdentifiers[1].size()] ),
                                     evalDescr.getText() );

        st.setAttribute( "hashCode",
                         evalText.hashCode() );

        final String invokerClassName = this.pkg.getName() + "." + this.ruleDescr.getClassName() + ucFirst( classMethodName ) + "Invoker";
        this.invokers.put( invokerClassName,
                           st.toString() );
        this.invokerLookups.put( invokerClassName,
                                 eval );
        this.descrLookups.put( invokerClassName,
                               evalDescr );
        return eval;
    }

    private void buildConsequence(final RuleDescr ruleDescr) {
        // generate method
        // generate Invoker
        final String classMethodName = "consequence";

        StringTemplate st = RuleBuilder.ruleGroup.getInstanceOf( "consequenceMethod" );

        st.setAttribute( "methodName",
                         classMethodName );

        final List[] usedIdentifiers = getUsedCIdentifiers( ruleDescr,
                                                            ruleDescr.getConsequence() );

        final Declaration[] declarations = new Declaration[usedIdentifiers[0].size()];
        for ( int i = 0, size = usedIdentifiers[0].size(); i < size; i++ ) {
            declarations[i] = (Declaration) this.declarations.get( (String) usedIdentifiers[0].get( i ) );
        }

        setStringTemplateAttributes( st,
                                     declarations,
                                     (String[]) usedIdentifiers[1].toArray( new String[usedIdentifiers[1].size()] ),
                                     ruleDescr.getConsequence() );
        st.setAttribute( "text",
                         RuleBuilder.functionFixer.fix( RuleBuilder.knowledgeHelperFixer.fix( ruleDescr.getConsequence() ) ) );

        this.methods.add( st.toString() );

        st = RuleBuilder.invokerGroup.getInstanceOf( "consequenceInvoker" );

        st.setAttribute( "package",
                         this.pkg.getName() );
        st.setAttribute( "ruleClassName",
                         ucFirst( this.ruleDescr.getClassName() ) );
        st.setAttribute( "invokerClassName",
                         ruleDescr.getClassName() + ucFirst( classMethodName ) + "Invoker" );
        st.setAttribute( "methodName",
                         classMethodName );

        setStringTemplateAttributes( st,
                                     declarations,
                                     (String[]) usedIdentifiers[1].toArray( new String[usedIdentifiers[1].size()] ),
                                     ruleDescr.getConsequence() );

        final List list = Arrays.asList( this.rule.getDeclarations() );

        final int[] indexes = new int[declarations.length];
        for ( int i = 0, length = declarations.length; i < length; i++ ) {
            indexes[i] = list.indexOf( declarations[i] );
        }

        st.setAttribute( "indexes",
                         indexes );

        st.setAttribute( "text",
                         ruleDescr.getConsequence() );

        final String invokerClassName = this.pkg.getName() + "." + ruleDescr.getClassName() + ucFirst( classMethodName ) + "Invoker";
        this.invokers.put( invokerClassName,
                           st.toString() );
        this.invokerLookups.put( invokerClassName,
                                 this.rule );
        this.descrLookups.put( invokerClassName,
                               ruleDescr );
    }

    private void buildRule(final RuleDescr ruleDescr) {
        // If there is no compiled code, return
        if ( this.methods.isEmpty() ) {
            this.ruleClass = null;
            return;
        }
        final String lineSeparator = System.getProperty( "line.separator" );

        final StringBuffer buffer = new StringBuffer();
        buffer.append( "package " + this.pkg.getName() + ";" + lineSeparator );

        for ( final Iterator it = this.pkg.getImports().iterator(); it.hasNext(); ) {
            buffer.append( "import " + it.next() + ";" + lineSeparator );
        }

        buffer.append( "public class " + ucFirst( this.ruleDescr.getClassName() ) + " {" + lineSeparator );
        buffer.append( "    private static final long serialVersionUID  = 7952983928232702826L;" + lineSeparator );

        for ( int i = 0, size = this.methods.size() - 1; i < size; i++ ) {
            buffer.append( this.methods.get( i ) + lineSeparator );
        }

        final String[] lines = buffer.toString().split( lineSeparator );

        this.ruleDescr.setConsequenceOffset( lines.length + 2 );
        //To get the error position in the DRL
        //error.getLine() - this.ruleDescr.getConsequenceOffset() + this.ruleDescr.getConsequenceLine()

        buffer.append( this.methods.get( this.methods.size() - 1 ) + lineSeparator );
        buffer.append( "}" );

        this.ruleClass = buffer.toString();
    }

    private void setStringTemplateAttributes(final StringTemplate st,
                                             final Declaration[] declarations,
                                             final String[] globals,
                                             final String text) {
        final String[] declarationTypes = new String[declarations.length];
        for ( int i = 0, size = declarations.length; i < size; i++ ) {
            declarationTypes[i] = ((ClassObjectType) declarations[i].getObjectType()).getClassType().getName().replace( '$',
                                                                                                                        '.' );
        }

        final List globalTypes = new ArrayList( globals.length );
        for ( int i = 0, length = globals.length; i < length; i++ ) {
            globalTypes.add( ((Class) this.pkg.getGlobals().get( globals[i] )).getName().replace( '$',
                                                                                                  '.' ) );
        }

        st.setAttribute( "declarations",
                         declarations );
        st.setAttribute( "declarationTypes",
                         declarationTypes );

        st.setAttribute( "globals",
                         globals );
        st.setAttribute( "globalTypes",
                         globalTypes );
    }

    private String ucFirst(final String name) {
        return name.toUpperCase().charAt( 0 ) + name.substring( 1 );
    }

    private FieldExtractor getFieldExtractor(final PatternDescr descr,
                                             final Class clazz,
                                             final String fieldName) {
        FieldExtractor extractor = null;
        try {
            extractor = classFieldExtractorCache.getExtractor( clazz, fieldName );
        } catch ( final RuntimeDroolsException e ) {
            this.errors.add( new RuleError( this.rule,
                                            descr,
                                            e,
                                            "Unable to create Field Extractor for '" + fieldName + "'" ) );
        }

        return extractor;
    }

    private Evaluator getEvaluator(final PatternDescr descr,
                                   final int valueType,
                                   final String evaluatorString) {
        final Evaluator evaluator = EvaluatorFactory.getEvaluator( valueType,
                                                                   evaluatorString );

        if ( evaluator == null ) {
            this.errors.add( new RuleError( this.rule,
                                            descr,
                                            null,
                                            "Unable to determine the Evaluator for  '" + valueType + "' and '" + evaluatorString + "'" ) );
        }

        return evaluator;
    }

    private List[] getUsedIdentifiers(final PatternDescr descr,
                                      final String text) {
        List[] usedIdentifiers = null;
        try {
            usedIdentifiers = this.analyzer.analyzeExpression( text,
                                                               new Set[]{this.declarations.keySet(), this.pkg.getGlobals().keySet()} );
        } catch ( final Exception e ) {
            this.errors.add( new RuleError( this.rule,
                                            descr,
                                            null,
                                            "Unable to determine the used declarations" ) );
        }
        return usedIdentifiers;
    }

    private List[] getUsedCIdentifiers(final PatternDescr descr,
                                       final String text) {
        List[] usedIdentifiers = null;
        try {
            usedIdentifiers = this.analyzer.analyzeBlock( text,
                                                          new Set[]{this.declarations.keySet(), this.pkg.getGlobals().keySet()} );
        } catch ( final Exception e ) {
            this.errors.add( new RuleError( this.rule,
                                            descr,
                                            null,
                                            "Unable to determine the used declarations" ) );
        }
        return usedIdentifiers;
    }

    static class ColumnCounter {
        // we start with -1 so that we can ++this.value - otherwise the first element has a lower value than the second in an 'or'
        private int          value = -1;

        private GroupElement ge;

        public void setParent(final GroupElement ge) {
            this.ge = ge;
        }

        public int getNext() {
            return ++this.value;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -