Package org.drools

Examples of org.drools.WorkingMemory


        // </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 );
        RuleBase ruleBase = builder.build( );
        WorkingMemory workingMemory = ruleBase.newWorkingMemory( );

        System.out.println( "DUMP RETE" );
        System.out.println( "---------" );
        ReteDumper dumper = new ReteDumper( ruleBase );
        dumper.dumpRete( System.err );

        System.out.println( "DUMP RETE DOT" );
        System.out.println( "---------" );
        dumper.dumpReteToDot( System.err );

        // Assert the facts, and fire the rules.
        Fibonacci fibonacci = new Fibonacci( 50 );
        long start = System.currentTimeMillis( );
        workingMemory.assertObject( fibonacci );
        workingMemory.fireAllRules( );
        long stop = System.currentTimeMillis( );
        System.err.println( "fibonacci(" + fibonacci.getSequence( ) + ") == " + fibonacci.getValue( ) + " took " + (stop - start) + "ms" );
    }
View Full Code Here


  }
 
 

  private void executeRules(Claim claim, Team team) throws SAXException, IOException, IntegrationException, FactException {
    WorkingMemory engine = _ruleBase.newWorkingMemory();
    engine.assertObject(claim);
    engine.assertObject(team);
    engine.fireAllRules();
  }
View Full Code Here

            RuleBase ruleBase = ruleBaseLoader.buildRuleBase();           
           
            // Dumper dumper = new Dumper(ruleBase);
            // dumper.dumpRete(System.err);

            WorkingMemory workingMemory;

            System.err.println( "\nFirst run - code compiled on the fly" );
            workingMemory = ruleBase.newWorkingMemory( );                       
           
            lounge.setTemperature(292);
            bedroom.setTemperature(292);

            FactHandle loungeHandle = workingMemory.assertObject( lounge );
            FactHandle bedroomHandle = workingMemory.assertObject( bedroom );
            FactHandle heatingHandle = workingMemory.assertObject( heating );           
            workingMemory.fireAllRules( );

            lounge.setTemperature(295);
            bedroom.setTemperature(295);

            workingMemory.modifyObject( loungeHandle, lounge );
            workingMemory.modifyObject( bedroomHandle, bedroom );           
            workingMemory.fireAllRules( );           
        }
        catch ( Exception e )
        {
            e.printStackTrace( );
        }
View Full Code Here

        {
            cart.addItem( ( CartItem ) items.get( i ) );
        }

        //add the JFrame to the ApplicationData to allow for user interaction
        WorkingMemory workingMemory = ruleBase.newWorkingMemory( );
        workingMemory.setApplicationData( "frame", frame );
        workingMemory.assertObject( cart );
        workingMemory.fireAllRules( );

        //returns the state of the cart
        return cart.toString( );
    }
View Full Code Here

        List results = new LinkedList( );
        stopwatch( 0 );
        for ( int i = 0; i < numberOfFacts; i++ )
        {
            stopwatch( 1 );
            WorkingMemory workingMemory = ruleBase.newWorkingMemory( );

            Number fact = new Number( factValues[i] );
            workingMemory.assertObject( fact );
            workingMemory.fireAllRules( );
            results.addAll( workingMemory.getObjects( ) );

            verbose( fact + ":" + stopwatch( 1 ) );
        }

        System.out.println( "Total time:" + stopwatch( 0 ) );
        validate( results );

        // Example 2
        System.out.println( );
        System.out
                  .println( "== Example #2 ========================================" );
        System.out.println( "new WorkingMemory();" );
        System.out.println( "foreach Number {" );
        System.out.println( "    assertObject();" );
        System.out.println( "    fireAllRules();" );
        System.out.println( "}" );
        System.out
                  .println( "======================================================" );
        WorkingMemory workingMemory = ruleBase.newWorkingMemory( );

        stopwatch( 0 );
        for ( int i = 0; i < numberOfFacts; i++ )
        {
            stopwatch( 1 );

            Number fact = new Number( factValues[i] );
            workingMemory.assertObject( fact );
            workingMemory.fireAllRules( );

            verbose( fact + ":" + stopwatch( 1 ) );
        }

        System.out.println( "Total time:" + stopwatch( 0 ) );
        validate( workingMemory.getObjects( ) );

        // Example 3
        System.out.println( );
        System.out
                  .println( "== Example #3 ========================================" );
        System.out.println( "new WorkingMemory();" );
        System.out.println( "foreach Number {" );
        System.out.println( "    assertObject();" );
        System.out.println( "}" );
        System.out.println( "fireAllRules()" );
        System.out
                  .println( "======================================================" );
        workingMemory = ruleBase.newWorkingMemory( );

        verbose( "Asserting " + numberOfFacts + " facts..." );
        stopwatch( 0 );

        for ( int i = 0; i < numberOfFacts; i++ )
        {
            workingMemory.assertObject( new Number( factValues[i] ) );
        }
        verbose( "Firing all rules..." );
        workingMemory.fireAllRules( );

        if ( verbose )
        {
            for ( Iterator i = workingMemory.getObjects( ).iterator( ); i.hasNext(); )
            {
                System.out.println( i.next( ) );
            }
        }

        System.out.println( "Total time:" + stopwatch( 0 ) );
        validate( workingMemory.getObjects( ) );
    }
View Full Code Here

        System.out.println( "DUMP TO DOT" );
        System.out.println( "---------" );
        dumper.dumpReteToDot( System.out );

        WorkingMemory workingMemory = ruleBase.newWorkingMemory( );

        for ( int i = 0; i < 10; i++ )
        {
            workingMemory.assertObject( new Integer( i ) );
        }

        workingMemory.fireAllRules( );
    }
View Full Code Here

       
        //dumpReteNetwork( ruleBase );
       
        System.out.println( "FIRE RULES(Hello)" );
        System.out.println( "----------" );
        WorkingMemory workingMemory = ruleBase.newWorkingMemory( );
        workingMemory.addEventListener(
            new DebugWorkingMemoryEventListener( ) );
        workingMemory.assertObject( "Hello" );
        workingMemory.fireAllRules( );

        System.out.println( "\n" );

        System.out.println( "FIRE RULES(GoodBye)" );
        System.out.println( "----------" );
        workingMemory = ruleBase.newWorkingMemory( );
        workingMemory.addEventListener(
            new DebugWorkingMemoryEventListener( ) );
        workingMemory.assertObject( "Goodbye" );
        workingMemory.fireAllRules( );
    }
View Full Code Here

        ReteDumper dumper = new ReteDumper( ruleBase );
        dumper.dumpRete( System.out );

        System.out.println( "FIRE RULES(Hello)" );
        System.out.println( "----------" );
        WorkingMemory workingMemory = ruleBase.newWorkingMemory( );
        workingMemory.addEventListener( new DebugWorkingMemoryEventListener( ) );
        workingMemory.assertObject( "Hello" );
        workingMemory.fireAllRules( );

        System.out.println( "\n" );

        System.out.println( "FIRE RULES(GoodBye)" );
        System.out.println( "----------" );
        workingMemory = ruleBase.newWorkingMemory( );
        workingMemory.addEventListener( new DebugWorkingMemoryEventListener( ) );
        workingMemory.assertObject( "Goodbye" );
        workingMemory.fireAllRules( );
    }
View Full Code Here

           
            RuleBaseLoader ruleBaseLoader = new RuleBaseLoader();
            ruleBaseLoader.addFromRuleSetLoader(ruleSetLoader);
            RuleBase ruleBase = ruleBaseLoader.buildRuleBase();             
           
            WorkingMemory workingMemory = ruleBase.newWorkingMemory( );

            try
            {
                TroubleTicket bobTicket = new TroubleTicket( "bob" );
                TroubleTicket daveTicket = new TroubleTicket( "dave" );

                System.err.println( "----------------------------------------" );
                System.err.println( "    PRE" );
                System.err.println( "----------------------------------------" );

                System.err.println( bobTicket );
                System.err.println( daveTicket );

                System.err.println( "----------------------------------------" );

                // Now, simply assert them into the [org.drools.WorkingMemory]
                // and let the logic engine do the rest.

                workingMemory.assertObject( daveTicket );
                workingMemory.assertObject( bobTicket );

                System.err.println( "----------------------------------------" );
                System.err.println( "    POST ASSERT" );
                System.err.println( "----------------------------------------" );

                System.err.println( bobTicket );
                System.err.println( daveTicket );
               
                workingMemory.fireAllRules();

                System.err.println( "----------------------------------------" );

                try
                {
View Full Code Here

    {
        boolean didStateChange = false;
        try
        {
            RuleBase ruleBase = RuleBaseFactory.getRuleBase( );
            WorkingMemory workingMemory = ruleBase.newWorkingMemory( );
            // for (Cell[] rowOfCells : cells) {
            Cell[] rowOfCells = null;
            Cell cell = null;
            for ( int i = 0; i < cells.length; i++ )
            {
                rowOfCells = cells[i];
                for ( int j = 0; j < rowOfCells.length; j++ )
                {
                    cell = rowOfCells[j];
                    workingMemory.assertObject( cell );
                }
            }
            workingMemory.fireAllRules( );
            didStateChange = transitionState( );
        }
        catch ( Exception e )
        {
            e.printStackTrace( );
View Full Code Here

TOP

Related Classes of org.drools.WorkingMemory

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.