Package org.drools.rule

Examples of org.drools.rule.Rule


        cheeseConfiguration.setText( Cheese.class.getName( ) );
        ObjectTypeFactory objectTypeFactory = module.getObjectTypeFactory( "class" );

        final RuleSet ruleSet = new RuleSet( "test RuleSet",
                                             this.ruleBaseContext );
        final Rule rule = new Rule( "Test Rule 1",
                                    ruleSet );
        rule.setImporter( new DefaultImporter( ) );
        ObjectType cheeseType = objectTypeFactory.newObjectType( rule,
                                                                 this.ruleBaseContext,
                                                                 cheeseConfiguration );

        tuple = new MockTuple( );
        rule.setImporter( new DefaultImporter( ) );
        tuple.setRule( rule );
        tuple.setWorkingMemory( new MockWorkingMemory( ) );

        // simple condition checks
        assertTrue( testCondition( testNumber++,
                                   tuple,
                                   rule ) ); // 0
        assertFalse( testCondition( testNumber++,
                                    tuple,
                                    rule ) ); // 1
        assertTrue( testCondition( testNumber++,
                                   tuple,
                                   rule ) ); // 2
        assertTrue( testCondition( testNumber++,
                                   tuple,
                                   rule ) ); // 3
        assertTrue( testCondition( testNumber++,
                                   tuple,
                                   rule ) ); // 4

        Declaration camembertDecl = rule.addParameterDeclaration( "camembert",
                                                                  cheeseType );
        Declaration stiltonDecl = rule.addParameterDeclaration( "stilton",
                                                                cheeseType );
        rule.setImporter( this.importer );
        // condition check with a single declaration
        tuple.put( camembertDecl,
                   new Cheese( "camembert" ) );
        assertTrue( testCondition( testNumber++,
                                   tuple,
                                   rule ) ); // 5
        assertFalse( testCondition( testNumber++,
                                    tuple,
                                    rule ) ); // 6

        // condition check with a single declaration
        tuple = new MockTuple( );
        rule.setImporter( this.importer );
        tuple.setRule( rule );
        tuple.setWorkingMemory( new MockWorkingMemory( ) );
        tuple.put( stiltonDecl,
                   new Cheese( "stilton" ) );
        assertTrue( testCondition( testNumber++,
                                   tuple,
                                   rule ) ); // 7
        assertFalse( testCondition( testNumber++,
                                    tuple,
                                    rule ) ); // 8

        // condition check with two declarations
        tuple = new MockTuple( );
        rule.setImporter( this.importer );
        tuple.setRule( rule );
        tuple.setWorkingMemory( new MockWorkingMemory( ) );
        tuple.put( stiltonDecl,
                   new Cheese( "stilton" ) );
        tuple.put( camembertDecl,
                   new Cheese( "camembert" ) );
        assertFalse( testCondition( testNumber++,
                                    tuple,
                                    rule ) ); // 9
        assertTrue( testCondition( testNumber++,
                                   tuple,
                                   rule ) ); // 10
        assertTrue( testCondition( testNumber++,
                                   tuple,
                                   rule ) ); // 11
        assertFalse( testCondition( testNumber++,
                                    tuple,
                                    rule ) ); // 12

        // condition check with 2 declarations and application data
        WorkingMemory workingMemory = new MockWorkingMemory( );
        workingMemory.setApplicationData( "bites",
                                          new Integer( 3 ) );
        workingMemory.setApplicationData( "favouriteCheese",
                                          new Cheese( "camembert" ) );
        tuple.setWorkingMemory( workingMemory );

        HashMap applicationData = new HashMap( );
        applicationData.put( "bites",
                             Integer.class );
        applicationData.put( "favouriteCheese",
                             Cheese.class );

        rule.setApplicationData( applicationData );

        assertTrue( testCondition( testNumber++,
                                   tuple,
                                   rule ) ); // 13
        assertFalse( testCondition( testNumber++,
                                    tuple,
                                    rule ) ); // 14
        assertTrue( testCondition( testNumber++,
                                   tuple,
                                   rule ) ); // 15

        // test code works no matter what the order of decl are
        tuple = new MockTuple( );
        rule.setImporter( this.importer );
        rule.setApplicationData( new HashMap( ) );
        tuple.setRule( rule );
        workingMemory = new MockWorkingMemory( );
        tuple.setWorkingMemory( workingMemory );

        DefaultConfiguration stringConfiguration = new DefaultConfiguration( "test2" );
        stringConfiguration.setText( String.class.getName( ) );
        ObjectType stringType = objectTypeFactory.newObjectType( rule,
                                                                 this.ruleBaseContext,
                                                                 stringConfiguration );
        Declaration favouriteCheeseDecl = rule.addParameterDeclaration( "favouriteCheese",
                                                                        stringType );

        tuple.put( favouriteCheeseDecl,
                   "camembert" );
        tuple.put( camembertDecl,
                   new Cheese( "camembert" ) );
        assertTrue( testCondition( testNumber++,
                                   tuple,
                                   rule ) ); // 16
        assertTrue( testCondition( testNumber++,
                                   tuple,
                                   rule ) ); // 17

        // test condition syntax with commas - Drools Issue #77
        assertTrue( testCondition( testNumber++,
                                   tuple,
                                   rule ) ); // 18

        // test exceptions
        rule.setImporter( this.importer );
        tuple.setRule( rule );
        try
        {
            testCondition( testNumber++,
                           tuple,
                           rule );
            fail( "Condition should throw an exception" );
        }
        catch ( ConditionException e )
        {
            // expected
        }

        // need to add a test for declaration order

        // 20
        // test imports
        tuple = new MockTuple( );
        rule.setImporter( this.importer );
        tuple.setRule( rule );
        workingMemory = new MockWorkingMemory( );
        tuple.setWorkingMemory( workingMemory );

        assertTrue( testCondition( testNumber++,
View Full Code Here


        cheeseConfiguration.setText( Cheese.class.getName( ) );
        ObjectTypeFactory objectTypeFactory = module.getObjectTypeFactory( "class" );

        final RuleSet ruleSet = new RuleSet( "test RuleSet",
                                             this.ruleBaseContext );
        Rule rule = new Rule( "Test Rule 1",
                              ruleSet );

        rule.setImporter( new DefaultImporter( ) );
        ObjectType cheeseType = objectTypeFactory.newObjectType( rule,
                                                                 this.ruleBaseContext,
                                                                 cheeseConfiguration );

        tuple = new MockTuple( );
        rule.setImporter( this.importer );
        tuple.setRule( rule );
        tuple.setWorkingMemory( new MockWorkingMemory( ) );

        // simple condition, no declrations
        testConsequence( 0,
                         tuple,
                         rule );

        // need to declare so that the tests have SMFTestFrameWork.Cheese
        // imported
        Declaration camembertDecl = rule.addParameterDeclaration( "camembert",
                                                                  cheeseType );
        Declaration stiltonDecl = rule.addParameterDeclaration( "stilton",
                                                                cheeseType );

        Cheese camembert = new Cheese( "camembert" );
        Cheese stilton = new Cheese( "stilton" );
        tuple.put( camembertDecl,
                   camembert );
        tuple.put( stiltonDecl,
                   stilton );

        // tests nested classes, public static class SMFTestFrameWork.Cheese,
        // works
        testConsequence( 1,
                         tuple,
                         rule );

        // now start doing tests with declarations
        // first confirm that biteLeft is 3
        assertEquals( 3,
                      camembert.getBitesLeft( ) );
        assertEquals( 3,
                      stilton.getBitesLeft( ) );
        // execute consequence that calles eatCheese()
        testConsequence( 2,
                         tuple,
                         rule );
        // camembert should be eaten once, and stilton twice
        assertEquals( 2,
                      camembert.getBitesLeft( ) );
        assertEquals( 1,
                      stilton.getBitesLeft( ) );

        // test condition with declarations and application data
        WorkingMemory workingMemory = new MockWorkingMemory( );
        workingMemory.setApplicationData( "bites",
                                          new Integer( 3 ) );
        workingMemory.setApplicationData( "cheeses",
                                          new HashMap( ) );

        HashMap applicationData = new HashMap( );
        applicationData.put( "bites",
                             Integer.class );
        applicationData.put( "cheeses",
                             HashMap.class );

        rule.setApplicationData( applicationData );

        tuple.setWorkingMemory( workingMemory );
        testConsequence( 3,
                         tuple,
                         rule );
        assertEquals( 1,
                      camembert.getBitesLeft( ) );
        assertEquals( 0,
                      stilton.getBitesLeft( ) );
        Map map = (Map) workingMemory.getApplicationData( "cheeses" );
        assertEquals( camembert,
                      map.get( "favourite cheese" ) );
        assertEquals( 3,
                      ((Integer) map.get( "bites" )).intValue( ) );

        // 4
        // test exceptions
        rule = new Rule( "Test Rule 1",
                         ruleSet );
        rule.setImporter( this.importer );
        tuple.setRule( rule );
        try
        {
            testConsequence( 6,
                             tuple,
                             rule );
            fail( "Consequence should throw an exception" );
        }
        catch ( ConsequenceException e )
        {
            // expected
        }

        // 7
        // test imports
        tuple = new MockTuple( );
        rule = new Rule( "Test Rule 1",
                         ruleSet );
        rule.setImporter( this.importer );
        tuple.setRule( rule );
        workingMemory = new MockWorkingMemory( );
        tuple.setWorkingMemory( workingMemory );
        try
        {
View Full Code Here

        }
    }

    public Object start( String uri, String localName, Attributes attrs ) throws SAXException
    {
        Rule rule = (Rule) ruleSetReader.getParent( Rule.class );

        String identifier = attrs.getValue( "identifier" );

        if ( identifier == null || identifier.trim( ).equals( "" ) )
        {
            throw new SAXParseException(
                    "<parameter> requires an 'identifier' attribute",
                    ruleSetReader.getLocator( ) );
        }

        identifier = identifier.trim( );

        try
        {
            return rule.addParameterDeclaration( identifier, null );
        }
        catch ( InvalidRuleException e )
        {
            throw new SAXParseException( "'" + identifier + "' is already defined", ruleSetReader.getLocator() );
        }
View Full Code Here

    }

    public Object end( String uri, String localName ) throws SAXException
    {
        Declaration declaration = (Declaration) ruleSetReader.getCurrent( );
        Rule rule = (Rule) ruleSetReader.getParent( Rule.class );

        if ( rule == null || declaration == null )
        {
            throw new SAXParseException( "unable to construct <parameter>",
                    ruleSetReader.getLocator( ) );
View Full Code Here

        DurationFactory factory = module.getDurationFactory( localName );
        Duration duration;
        try
        {
            Rule rule = (Rule) this.ruleSetReader.getParent( Rule.class );

            duration = factory.newDuration( rule,
                                            this.ruleSetReader.getFactoryContext( ),
                                            config );

            rule.setDuration( duration );
        }
        catch ( FactoryException e )
        {
            throw new SAXParseException( "error constructing duration",
                    this.ruleSetReader.getLocator( ), e );
View Full Code Here

        ConditionFactory factory = module.getConditionFactory( localName );
        Condition[] conditions;
        try
        {
            Rule rule = (Rule) this.ruleSetReader.getParent( Rule.class );
            conditions = factory.newCondition( rule,
                                              this.ruleSetReader.getFactoryContext( ),
                                              config );

            for (int i = 0; i < conditions.length; i++)
            {
                rule.addCondition( conditions[i] );
            }
        }
        catch ( FactoryException e )
        {   
            throw new SAXParseException( "error constructing condition",
View Full Code Here

        ObjectTypeFactory factory = module.getObjectTypeFactory( localName );

        try
        {
            Rule rule = (Rule) this.ruleSetReader.getParent( Rule.class );
            Declaration declaration = (Declaration) this.ruleSetReader.getParent( Declaration.class );
            ((DefaultConfiguration) config).setAttribute( "identifier",
                                                          declaration.getIdentifier( ) );
     
            ObjectType objectType = factory.newObjectType( rule,
View Full Code Here

    {
        // <rule-set name="fibonacci" ...>
        RuleSet ruleSet = new RuleSet( "fibonacci" );

        // <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 fDeclaration = 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( fDeclaration );
                return f.getSequence( ) == 1;
            }

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

            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( fDeclaration );
                return f.getValue( ) == -1;
            }

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

            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( fDeclaration );
                f.setValue( 1 );
                System.err.println( f.getSequence( ) + " == " + f.getValue( ) );

                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( f ),
                                                f );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        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 Bootstrap2 Rule
        // <java:consequence>
        // f.setValue( 1 );
        // System.err.println( f.getSequence() + " == " + f.getValue() );
        // drools.modifyObject( f );
        // </java:consequence>
        final Consequence bootstrapConsequence2 = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );

                Fibonacci f = (Fibonacci) tuple.get( fDeclaration2 );
                f.setValue( 1 );
                System.err.println( f.getSequence( ) + " == " + f.getValue( ) );

                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( f ),
                                                f );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        bootstrap2Rule.setConsequence( bootstrapConsequence2 );
        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 );
                System.err.println( "recurse for " + f.getSequence( ) );
                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( ) );
                System.err.println( f3.getSequence( ) + " == " + f3.getValue( ) );
                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( f3 ),
                                                f3 );
                    workingMemory.retractObject( tuple.getFactHandleForObject( f1 ) );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        calculateRule.setConsequence( calculateConsequence );
        ruleSet.addRule( calculateRule );

        // Build the RuleSet.
        RuleBaseBuilder builder = new RuleBaseBuilder( );
        builder.addRuleSet( ruleSet );
View Full Code Here

        verbose( "Read " + numberOfRules + " primes" + stopwatch( 0 ) );

        verbose( "Creating " + numberOfRules + " rules..." );
        for ( int i = 0; i < numberOfRules; i++ )
        {
            Rule rule = new Rule( "Factor by " + primes[i] );

            Declaration numberDecl = rule.addParameterDeclaration( "number", numberType );
            rule.addCondition( new FactorCondition( numberDecl, primes[i] ) );
            rule.setConsequence( new FactorConsequence( numberDecl, primes[i] ) );
            ruleSet.addRule( rule );
        }
        verbose( "Created " + numberOfRules + " rules" + stopwatch( 0 ) );

        // Build the RuleSets.
View Full Code Here

                             String packageName,
                             Map ruleMap,
                             ClassLoader classLoader) throws IntegrationException
    {
        Rule[] rules = ruleSet.getRules();
        Rule rule = null;
        SemanticInvokeable component = null;
        String name = null;
        String semanticPackageName = null;
        try
        {
            for ( int i = 0; i < rules.length; i++ )
            {
                rule = rules[i];
                Condition[] conditions = (Condition[]) rule.getConditions().toArray( new Condition[rule.getConditions().size()] );
                for ( int j = 0; j < conditions.length; j++ )
                {
                    //only wire up this condition if it implements SemanticInvokeble
                    if ( !(conditions[j] instanceof SemanticInvokeable) )
                    {
                        continue;
                    }
                    component = (SemanticInvokeable) conditions[j];
                    name = component.getName();
                    semanticPackageName = packageName + "." + component.getSemanticType();
                    component.setInvoker( (Invoker) classLoader.loadClass( semanticPackageName + "." + ruleMap.get( rule ) + "Invoker$" + name.toUpperCase().charAt( 0 ) + name.substring( 1 ) + "Invoker" ).newInstance() );

                }

                //only wire up this consequenceif it implements SemanticInvokeble
                if ( rule.getConsequence() instanceof SemanticInvokeable )
                {
                    component = (SemanticInvokeable) rule.getConsequence();
                    name = component.getName();
                    semanticPackageName = packageName + "." + component.getSemanticType();
                    component.setInvoker( (Invoker) classLoader.loadClass( semanticPackageName + "." + ruleMap.get( rule ) + "Invoker$" + name.toUpperCase().charAt( 0 ) + name.substring( 1 ) + "Invoker" ).newInstance() );
                }
            }
View Full Code Here

TOP

Related Classes of org.drools.rule.Rule

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.