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

📄 conwayconsequencefactory.java

📁 drools 一个开放源码的规则引擎
💻 JAVA
字号:
package org.drools.examples.conway.rules.dsl;

import org.drools.examples.conway.Cell;
import org.drools.rule.Declaration;
import org.drools.rule.InvalidRuleException;
import org.drools.rule.Rule;
import org.drools.semantics.base.ClassObjectType;
import org.drools.smf.Configuration;
import org.drools.smf.ConsequenceFactory;
import org.drools.smf.FactoryException;
import org.drools.spi.Consequence;
import org.drools.spi.RuleBaseContext;

/**
 * @author <a href="mailto:brown_j@ociweb.com">Jeff Brown</a>
 * @version $Id: ConwayConsequenceFactory.java,v 1.2 2005/05/16 23:31:19 brownj Exp $
 */
public class ConwayConsequenceFactory
        implements
        ConsequenceFactory
{
    public Consequence newConsequence(Rule rule,
                                      RuleBaseContext context,
                                      Configuration config) throws FactoryException
    {

        Configuration childConfig = null;
        Configuration[] configurations = config.getChildren();
        Consequence consequence = null;
        final String cellName = config.getAttribute( "cellName" );
        final Declaration cellDeclaration = getDeclaration( rule,
                                                            Cell.class,
                                                            cellName );
        for ( int i = 0; i < configurations.length; i++ )
        {
            childConfig = configurations[i];

            if ( childConfig.getName().equals( "giveBirthToCell" ) )
            {
                consequence = new GiveBirthConsequence( cellDeclaration );
            } else if ( childConfig.getName().equals( "killCell" ) )
            {
                consequence = new KillCellConsequence( cellDeclaration );
            }
        }


        return consequence;
    }

    private Declaration getDeclaration(Rule rule,
                                       Class clazz,
                                       String identifier) throws FactoryException
    {
        Declaration declaration = rule.getParameterDeclaration( identifier );
        if ( declaration == null )
        {
            ClassObjectType type = new ClassObjectType( clazz );
            try
            {
                declaration = rule.addParameterDeclaration( identifier,
                                                            type );
            }
            catch ( InvalidRuleException e )
            {
                final FactoryException factoryException = new FactoryException( "Error occurred establishing parameter." );
                factoryException.initCause( e );
                throw factoryException;
            }
        }
        return declaration;
    }

}

⌨️ 快捷键说明

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