Package org.drools.rule

Examples of org.drools.rule.RuleSet


            throw new SAXParseException(
                    "<rule-set> requires a 'name' attribute", ruleSetReader
                            .getLocator( ) );
        }

        RuleSet ruleSet = new RuleSet( ruleSetName.trim( ), this.ruleSetReader.getFactoryContext( ) );

        if ( ruleSetDesc == null || ruleSetDesc.trim( ).equals( "" ) )
        {
            ruleSet.setDocumentation( "" );
        }
        else
        {
            ruleSet.setDocumentation( ruleSetDesc );
        }
       
        ruleSet.setImporter( new DefaultImporter( ) );

        ruleSetReader.setRuleSet( ruleSet );
        return ruleSet;
    }
View Full Code Here


    {
        RuleBaseContext ruleBaseContext = new RuleBaseContext( );

        DefaultConfiguration configuration = new DefaultConfiguration( "test1" );

        final RuleSet ruleSet = new RuleSet( "test RuleSet",
                                             ruleBaseContext );

        final Rule rule = new Rule( "Test Rule 1",
                                    ruleSet );
View Full Code Here

        configuration.setAttribute( "type",
                                    typeName );
        configuration.setAttribute( "identifier",
                                    "state" );

        final RuleSet ruleSet = new RuleSet( "test RuleSet",
                                             ruleBaseContext );

        final Rule rule = new Rule( "Test Rule 1",
                                    ruleSet );
View Full Code Here

        {
            throw new RuleExecutionSetCreateException(
                "could not create RuleExecutionSet: " + e );
        }

        RuleSet ruleSet = reader.getRuleSet( );
        LocalRuleExecutionSetProviderImpl localRuleExecutionSetProvider =
            new LocalRuleExecutionSetProviderImpl( );
        return localRuleExecutionSetProvider.createRuleExecutionSet(
            ruleSet, properties );
    }
View Full Code Here

        //add conditions
        rule2.addCondition( new org.drools.spi.InstrumentedCondition( ) );
        rule2.addCondition( new org.drools.spi.InstrumentedCondition( ) );
        rule2.setSalience( 12 );

        RuleSet ruleSet = new RuleSet( "rule_set" );
        ruleSet.addRule( rule1 );
        ruleSet.addRule( rule2 );

        Builder builder = new Builder( );
        builder.addRuleSet( ruleSet );
        builder.setConflictResolver( DefaultConflictResolver.getInstance( ) );
        RuleBase ruleBase = builder.buildRuleBase( );
View Full Code Here

        throws RuleExecutionSetCreateException
    {
        try
        {
            RuleSetReader setReader = new RuleSetReader( );
            RuleSet ruleSet = setReader.read( ruleExecutionSetStream );
            return this.createRuleExecutionSet( ruleSet, properties );
        }
        catch ( SAXException e )
        {
            throw new RuleExecutionSetCreateException(
View Full Code Here

        throws RuleExecutionSetCreateException
    {
        try
        {
            RuleSetReader setReader = new RuleSetReader( );
            RuleSet ruleSet = setReader.read( ruleExecutionSetReader );
            return this.createRuleExecutionSet( ruleSet, properties );
        }
        catch ( SAXException e )
        {
            throw new RuleExecutionSetCreateException(
View Full Code Here

            Object ruleExecutionSetAst, Map properties )
        throws RuleExecutionSetCreateException
    {
        if ( ruleExecutionSetAst instanceof RuleSet )
        {
            RuleSet ruleSet = ( RuleSet ) ruleExecutionSetAst;
            return this.createRuleExecutionSet( ruleSet, properties );
        }
        throw new RuleExecutionSetCreateException(
            " Incoming AST object must be an org.drools.rule.RuleSet.  Was "
            + ruleExecutionSetAst.getClass( ) );
View Full Code Here

public class EventTest extends DroolsTestCase
{
    public void testAddToAgenda() throws Exception
    {
        RuleSet ruleSet = new RuleSet( "test rule-set" );
        Rule rule = new Rule( "test-rule" );

        // rule.addParameterDeclaration( intDecl );
        Declaration stringDecl1 = rule.addParameterDeclaration( "stringVar1",
                                                                new MockObjectType( String.class ) );
        Declaration stringDecl2 = rule.addParameterDeclaration( "stringVar2",
                                                                new MockObjectType( String.class ) );
        Declaration intDecl = rule.addParameterDeclaration( "intVar",
                                                            new MockObjectType( Integer.class ) );

        // add consequence
        rule.setConsequence( new org.drools.spi.Consequence( )
        {
            public void invoke(org.drools.spi.Tuple tuple)
            {
                // Agenda agenda =
                // ((WorkingMemoryImpl)workingMemory).getAgenda();
                // agenda.addToAgenda( ( ReteTuple ) tuple, tuple.getRule( ) );
                // System.err.println("fire");
            }
        } );
        // add condition
        InstrumentedCondition c1 = new InstrumentedCondition( );
        c1.addDeclaration( stringDecl1 );
        c1.isAllowed( true );
        InstrumentedCondition c2 = new InstrumentedCondition( );
        c2.addDeclaration( stringDecl2 );
        c2.isAllowed( true );
        InstrumentedCondition c3 = new InstrumentedCondition( );
        c3.addDeclaration( intDecl );
        c3.isAllowed( true );
        rule.addCondition( c1 );
        rule.addCondition( c2 );
        rule.addCondition( c3 );

        // rule.addCondition( new org.drools.spi.InstrumentedCondition( ) );
        ruleSet.addRule( rule );
        Builder builder = new Builder( );
        builder.addRuleSet( ruleSet );
        RuleBase ruleBase = builder.buildRuleBase( );

        WorkingMemoryImpl workingMemory = (WorkingMemoryImpl) ruleBase.newWorkingMemory( );
View Full Code Here

        URLClassLoader urlClassLoader = new URLClassLoader( new URL[]{} );
        ruleBaseContext.put( "java-classLoader",
                             urlClassLoader );
        urlClassLoader = (URLClassLoader) ruleBaseContext.get( "java-classLoader" );

        final RuleSet ruleSet = new RuleSet( "fibonacci" );
        ruleSet.addApplicationData( new ApplicationData( ruleSet,
                                                         "fibtotal",
                                                         FibTotal.class ) );

        // <rule name="Bootstrap 1" salience="20">
        final Rule bootstrap1Rule = new Rule( "Bootstrap 1" );
        bootstrap1Rule.setSalience( 20 );

        // Reuse the Java semantics ObjectType
        // so Drools can identify the Fibonacci class
        final ClassObjectType fibonacciType = new ClassObjectType( Fibonacci.class );

        // Build the declaration and specify it as a parameter of the Bootstrap1
        // Rule
        // <parameter identifier="f">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration fDeclaration1 = bootstrap1Rule.addParameterDeclaration( "f",
                                                                                  fibonacciType );

        // Build and Add the Condition to the Bootstrap1 Rule
        // <java:condition>f.getSequence() == 1</java:condition>
        final Condition conditionBootstrap1A = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f = (Fibonacci) tuple.get( fDeclaration1 );
                return f.getSequence( ) == 1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{fDeclaration1};
            }

            public String toString()
            {
                return "f.getSequence() == 1";
            }
        };
        bootstrap1Rule.addCondition( conditionBootstrap1A );

        // <java:condition>f.getValue() == -1</java:condition>
        final Condition conditionBootstrap1B = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f = (Fibonacci) tuple.get( fDeclaration1 );
                return f.getValue( ) == -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{fDeclaration1};
            }

            public String toString()
            {
                return "f.getValue() == -1";
            }
        };
        bootstrap1Rule.addCondition( conditionBootstrap1B );

        // Build and Add the Consequence to the Bootstrap1 Rule
        // <java:consequence>
        // f.setValue( 1 );
        // System.err.println( f.getSequence() + " == " + f.getValue() );
        // drools.modifyObject( f );
        // </java:consequence>
        final Consequence bootstrapConsequence = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );
                Fibonacci f = (Fibonacci) tuple.get( fDeclaration1 );
                f.setValue( 1 );

                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( f ),
                                                f );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }

                FibTotal total = (FibTotal) workingMemory.getApplicationData( "fibtotal" );
                total.setTotal( total.getTotal( ) + 1 );
            }
        };
        bootstrap1Rule.setConsequence( bootstrapConsequence );
        ruleSet.addRule( bootstrap1Rule );

        // <rule name="Bootstrap 2">
        final Rule bootstrap2Rule = new Rule( "Bootstrap 2" );

        // Specify the declaration as a parameter of the Bootstrap2 Rule
        // <parameter identifier="f">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration fDeclaration2 = bootstrap2Rule.addParameterDeclaration( "f",
                                                                                  fibonacciType );

        // Build and Add the Conditions to the Bootstrap1 Rule
        // <java:condition>f.getSequence() == 2</java:condition>
        final Condition conditionBootstrap2A = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f = (Fibonacci) tuple.get( fDeclaration2 );
                return f.getSequence( ) == 2;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{fDeclaration2};
            }

            public String toString()
            {
                return "f.getSequence() == 2";
            }
        };
        bootstrap2Rule.addCondition( conditionBootstrap2A );

        // <java:condition>f.getValue() == -1</java:condition>
        final Condition conditionBootstrap2B = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f = (Fibonacci) tuple.get( fDeclaration2 );
                return f.getValue( ) == -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{fDeclaration2};
            }

            public String toString()
            {
                return "f.getValue() == -1";
            }
        };
        bootstrap2Rule.addCondition( conditionBootstrap2B );

        // Build and Add the Consequence to the Bootstrap1 Rule
        // <java:consequence>
        // f.setValue( 1 );
        // System.err.println( f.getSequence() + " == " + f.getValue() );
        // drools.modifyObject( f );
        // </java:consequence>
        final Consequence bootstrap2Consequence = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );
                Fibonacci f = (Fibonacci) tuple.get( fDeclaration2 );
                f.setValue( 1 );

                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( f ),
                                                f );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }

                FibTotal total = (FibTotal) workingMemory.getApplicationData( "fibtotal" );
                total.setTotal( total.getTotal( ) + 1 );
            }
        };
        bootstrap2Rule.setConsequence( bootstrap2Consequence );
        ruleSet.addRule( bootstrap2Rule );

        // <rule name="Recurse" salience="10">
        final Rule recurseRule = new Rule( "Recurse" );
        recurseRule.setSalience( 10 );

        // <parameter identifier="f">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration fDeclarationRecurse = recurseRule.addParameterDeclaration( "f",
                                                                                     fibonacciType );

        // <java:condition>f.getValue() == -1</java:condition>
        final Condition conditionRecurse = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f = (Fibonacci) tuple.get( fDeclarationRecurse );
                return f.getValue( ) == -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{fDeclarationRecurse};
            }

            public String toString()
            {
                return "f.getValue() == -1";
            }
        };
        recurseRule.addCondition( conditionRecurse );

        // <java:consequence>
        // System.err.println( "recurse for " + f.getSequence() );
        // drools.assertObject( new Fibonacci( f.getSequence() - 1 ) );
        // </java:consequence>
        final Consequence recurseConsequence = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );

                Fibonacci f = (Fibonacci) tuple.get( fDeclarationRecurse );
                try
                {
                    workingMemory.assertObject( new Fibonacci( f.getSequence( ) - 1 ) );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        recurseRule.setConsequence( recurseConsequence );
        ruleSet.addRule( recurseRule );

        // <rule name="Calculate">
        final Rule calculateRule = new Rule( "Calculate" );

        // <parameter identifier="f1">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration f1Declaration = calculateRule.addParameterDeclaration( "f1",
                                                                                 fibonacciType );

        // <parameter identifier="f2">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration f2Declaration = calculateRule.addParameterDeclaration( "f2",
                                                                                 fibonacciType );

        // <parameter identifier="f3">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration f3Declaration = calculateRule.addParameterDeclaration( "f3",
                                                                                 fibonacciType );

        // <java:condition>f2.getSequence() ==
        // (f1.getSequence()+1)</java:condition>
        final Condition conditionCalculateA = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f1 = (Fibonacci) tuple.get( f1Declaration );
                Fibonacci f2 = (Fibonacci) tuple.get( f2Declaration );
                return f2.getSequence( ) == f1.getSequence( ) + 1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f1Declaration, f2Declaration};
            }

            public String toString()
            {
                return "f2.getSequence() == (f1.getSequence()+1)";
            }
        };
        calculateRule.addCondition( conditionCalculateA );

        // <java:condition>f3.getSequence() ==
        // (f2.getSequence()+1)</java:condition>
        final Condition conditionCalculateB = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f2 = (Fibonacci) tuple.get( f2Declaration );
                Fibonacci f3 = (Fibonacci) tuple.get( f3Declaration );
                return f3.getSequence( ) == f2.getSequence( ) + 1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f2Declaration, f3Declaration};
            }

            public String toString()
            {
                return "f3.getSequence() == (f2.getSequence()+1)";
            }
        };
        calculateRule.addCondition( conditionCalculateB );

        // <java:condition>f1.getValue() != -1</java:condition>
        final Condition conditionCalculateC = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f1 = (Fibonacci) tuple.get( f1Declaration );
                return f1.getValue( ) != -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f1Declaration};
            }

            public String toString()
            {
                return "f1.getValue() != -1";
            }
        };
        calculateRule.addCondition( conditionCalculateC );

        // <java:condition>f2.getValue() != -1</java:condition>
        final Condition conditionCalculateD = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f2 = (Fibonacci) tuple.get( f2Declaration );
                return f2.getValue( ) != -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f2Declaration};
            }

            public String toString()
            {
                return "f2.getValue() != -1";
            }
        };
        calculateRule.addCondition( conditionCalculateD );

        // <java:condition>f3.getValue() == -1</java:condition>
        final Condition conditionCalculateE = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f3 = (Fibonacci) tuple.get( f3Declaration );
                return f3.getValue( ) == -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f3Declaration};
            }

            public String toString()
            {
                return "f3.getValue() == -1";
            }
        };
        calculateRule.addCondition( conditionCalculateE );

        // <java:consequence>
        // f3.setValue( f1.getValue() + f2.getValue() );
        // System.err.println( f3.getSequence() + " == " + f3.getValue() );
        // drools.modifyObject( f3 );
        // drools.retractObject( f1 );
        // </java:consequence>
        final Consequence calculateConsequence = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );

                Fibonacci f1 = (Fibonacci) tuple.get( f1Declaration );
                Fibonacci f2 = (Fibonacci) tuple.get( f2Declaration );
                Fibonacci f3 = (Fibonacci) tuple.get( f3Declaration );

                f3.setValue( f1.getValue( ) + f2.getValue( ) );
                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( f3 ),
                                                f3 );
                    workingMemory.retractObject( tuple.getFactHandleForObject( f1 ) );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }

                FibTotal total = (FibTotal) workingMemory.getApplicationData( "fibtotal" );
                total.setTotal( total.getTotal( ) + 1 );
            }
        };
        calculateRule.setConsequence( calculateConsequence );
        ruleSet.addRule( calculateRule );

        // Build the RuleSet.

        RuleBaseBuilder builder = new RuleBaseBuilder( ruleBaseContext );
        builder.addRuleSet( ruleSet );
View Full Code Here

TOP

Related Classes of org.drools.rule.RuleSet

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.