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

📄 rulesheetlistener.java

📁 drools 一个开放源码的规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    private void finishRuleTable()
    {
        if ( _isInRuleTable )
        {
            _currentSequentialFlag = false;
            _currentParameters = new LinkedList( );
            _isInRuleTable = false;
        }
    }

    private void processNonRuleCell(int row,
                                    int column,
                                    String value)
    {
        if ( value.startsWith( RULE_TABLE_TAG ) )
        {
            initRuleTable( row,
                           column,
                           value );
        }
        else
        {
            _propertiesListner.newCell( row,
                                        column,
                                        value );
        }
    }

    private void processRuleCell(int row,
                                 int column,
                                 String value)
    {
        if ( value.startsWith( RULE_TABLE_TAG ) )
        {
            finishRuleTable( );
            initRuleTable( row,
                           column,
                           value );
            return;
        }

        // Ignore any comments cells preceeding the first rule table column
        if ( column < _ruleStartColumn )
        {
            return;
        }
        
        // Ignore any further cells from the rule def row
        if ( row == _ruleStartRow) 
        {
            return;
        }
        
        switch ( row - _ruleStartRow )
        {
        case ACTION_ROW :
            ActionType.addNewActionType(_actions, value, column, row);            
            break;
        case CODE_ROW :
            codeRow( row,
                     column,
                     value );
            break;
        case LABEL_ROW :
        	labelRow( row,
                    column,
                    value );
            break;
        default :
            nextRule( row,
                      column,
                      value );
            break;
        }
    }

    private void codeRow(int row,
                         int column,
                         String value)
    {
        ActionType actionType = getActionForColumn( row,
                column );

        if ( value.trim( ).equals( "" ) && (actionType.type == ActionType.ACTION || actionType.type == ActionType.CONDITION))
        {
            throw new DecisionTableParseException( "Code description - row:" + (row + 1) + 
                                                   " cell number:" + (column + 1) + " - does not contain any code specification. It should !" );
        }

        actionType.value = value;
    }

    private void labelRow(int row,
            int column,
            String value)
    {
        ActionType actionType = getActionForColumn( row, column );

        if ( !value.trim( ).equals( "" ) && (actionType.type == ActionType.ACTION || actionType.type == ActionType.CONDITION))
        {
        	_cellComments.put(new Integer(column),value);
        }
        else {
        	_cellComments.put(new Integer(column),"From column: " + Rule.convertColNumToColName(column));
        }
    }

    private ActionType getActionForColumn(int row,
                                          int column)
    {
        ActionType actionType = (ActionType) _actions.get( new Integer( column ) );

        if ( actionType == null )
        {
            throw new DecisionTableParseException( "Code description - row number:" + (row + 1) 
                                                   + " cell number:" + (column + 1) + 
                                                   " - does not have an 'ACTION' or 'CONDITION' column header." );
        }

        return actionType;
    }

    private void nextRule(int row,
                          int column,
                          String value)
    {     
        ActionType actionType = getActionForColumn( row,
                                                    column );

        if ( row - _ruleRow > 1 )
        {
            // Encountered a row gap from the last rule.
            // This is not part of the ruleset.
            finishRuleTable( );
            processNonRuleCell( row,
                                column,
                                value );
            return;
        }

        if ( row > _ruleRow )
        {
            // In a new row/rule
            _currentRule = createNewRuleForRow( row,
                                                _currentParameters );

            _ruleList.add( _currentRule );
            _ruleRow++;
        }
        

        
        if (actionType.type == ActionType.PRIORITY && !_currentSequentialFlag) // if the rule set is not sequential and the actionType type is PRIORITY then set the current Rule's salience paramenter with the value got from the cell
        {
        	_currentRule.setSalience( new Integer(value) );
        }
        else if (actionType.type == ActionType.NAME) // if the actionType type is PRIORITY then set the current Rule's name paramenter with the value got from the cell
        {
        	_currentRule.setName( value );
        }
        else if ( actionType.type == ActionType.DESCRIPTION) // if the actionType type is DESCRIPTION then set the current Rule's description paramenter with the value got from the cell
        {
        	_currentRule.setDescription( value );
        }
        else if ( actionType.type == ActionType.XORGROUP) // if the actionType type is NOLOOP then set the current Rule's no-loop paramenter with the value got from the cell
        {
        	_currentRule.setXorGroup( value );
        }
        else if ( actionType.type == ActionType.NOLOOP) // if the actionType type is NOLOOP then set the current Rule's no-loop paramenter with the value got from the cell
        {
        	_currentRule.setNoLoop( value );
        }
        else if ( actionType.type == ActionType.DURATION) // if the actionType type is DURATION then creates a new duration tag with the value got from the cell
        {
        	createDuration( column, 
        					value, 
        					actionType );
        }        
        else if ( actionType.type == ActionType.CONDITION )
        {
            createCondition( column,
                             value,
                             actionType );
        }
        else if ( actionType.type == ActionType.ACTION )
        {
            createConsequence( column,
                               value,
                               actionType );
        }


    }

    private Rule createNewRuleForRow(int row,
                                     List parameters)
    {
        String name = _currentRulePrefix + "_" + row;
        Integer salience = null;
        if ( _currentSequentialFlag )
        {
            salience = new Integer( Rule.calcSalience( row ) );
        }
        Rule rule = new Rule( name,
                              salience );
        rule.setComment( "From row number: " + (row + 1) );
        rule.setParameters( parameters );
        return rule;
    }

    private void createCondition(int column,
                                 String value,
                                 ActionType actionType)
    {

        Condition cond = new Condition( );
        cond.setSnippet( actionType.getSnippet( value ) );
        cond.setComment( cellComment( column ) );
        _currentRule.addCondition( cond );
    }

    
    // 08 - 16 - 2005 RIK: This function creates a new DURATION TAG if apply.
    // The value in the cell must be made with the first character of the parameter and the value next to it, separated by ":" character
    // Examples: w1:d3:h4 mean weeks="1" days="3" hours="4", m=1:s=45 means minutes="1" seconds="45"
    
    private void createDuration(int column,
            String value,
            ActionType actionType)
	{

		Duration dur = new Duration( );
		dur.setSnippet( value );
		dur.setComment( cellComment( column ) );
		_currentRule.setDuration( dur );
	}

    private void createConsequence(int column,
                                   String value,
                                   ActionType actionType)
    {

        Consequence cons = new Consequence( );
        cons.setSnippet( actionType.getSnippet( value ) );
        cons.setComment( cellComment( column ) );
        _currentRule.addConsequence( cons );
    }

    private boolean isCellValueEmpty(String value)
    {
        return value == null || "".equals( value.trim( ) );
    }

    private String cellComment(int column)
    {
        return "From column: " + Rule.convertColNumToColName( column );
    }

}

⌨️ 快捷键说明

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