Package org.drools

Examples of org.drools.WorkingMemory


    public void testAddFromDdjUrl() throws IntegrationException,
                                   SAXException,
                                   IOException,
                                   FactException
    {
        WorkingMemory workingMemory;
        List list;
        RuleBase ruleBase;
        RuleBaseLoader ruleBaseLoader;
        RuleSetLoader ruleSetLoader = new RuleSetLoader( );

        // single URL parameter
        ruleSetLoader.addFromUrl( RuleBaseLoaderTest.class.getResource( "simple.java.drl" ) );

        // Try with addFromRuleSetLoader
        ruleBaseLoader = new RuleBaseLoader( );
        ruleBaseLoader.addFromRuleSetLoader( ruleSetLoader );
        RuleSetCompiler[] compilers = (RuleSetCompiler[]) ruleSetLoader.getRuleSets( ).values( ).toArray( new RuleSetCompiler[]{} );

        File root = null;
        try
        {
            root = createTempDirectory( );
            File file = new File( root,
                                  "drools.ddj" );
            FileOutputStream fos = new FileOutputStream( file );
            fos.write( compilers[0].getBinaryDeploymentJar( ) );
            fos.close( );

            // Try with URL to Drools Distribution Jar, ddj
            ruleBaseLoader = new RuleBaseLoader( );
            ruleBaseLoader.addFromUrl( file.toURL( ) );
            ruleBase = ruleBaseLoader.buildRuleBase( );
            workingMemory = ruleBase.newWorkingMemory( );
            list = new ArrayList( );
            workingMemory.setApplicationData( "list",
                                              list );
            workingMemory.assertObject( "help" );
            workingMemory.fireAllRules( );
            assertEquals( "help",
                          list.get( 0 ) );

            // Try with URL to Drools Distribution Jar, ddj []
            ruleBaseLoader = new RuleBaseLoader( );
            ruleBaseLoader.addFromUrl( new URL[]{file.toURL( )} );
            ruleBase = ruleBaseLoader.buildRuleBase( );
            workingMemory = ruleBase.newWorkingMemory( );
            list = new ArrayList( );
            workingMemory.setApplicationData( "list",
                                              list );
            workingMemory.assertObject( "help" );
            workingMemory.fireAllRules( );
            assertEquals( "help",
                          list.get( 0 ) );
        }
        catch ( Exception e )
        {
View Full Code Here


    public void testAddFromDdjByteArray() throws IntegrationException,
                                   SAXException,
                                   IOException,
                                   FactException
    {
        WorkingMemory workingMemory;
        List list;
        RuleBase ruleBase;
        RuleBaseLoader ruleBaseLoader;
        RuleSetLoader ruleSetLoader = new RuleSetLoader( );

        // single URL parameter
        ruleSetLoader.addFromUrl( RuleBaseLoaderTest.class.getResource( "simple.java.drl" ) );

        // Try with addFromRuleSetLoader
        ruleBaseLoader = new RuleBaseLoader( );
        ruleBaseLoader.addFromRuleSetLoader( ruleSetLoader );
        RuleSetCompiler[] compilers = (RuleSetCompiler[]) ruleSetLoader.getRuleSets( ).values( ).toArray( new RuleSetCompiler[]{} );

        File root = null;
        try
        {           
            // Try with URL to Drools Distribution Jar, ddj
            ruleBaseLoader = new RuleBaseLoader( );
            ruleBaseLoader.addFromByteArray( compilers[0].getBinaryDeploymentJar( ) );
            ruleBase = ruleBaseLoader.buildRuleBase( );
            workingMemory = ruleBase.newWorkingMemory( );
            list = new ArrayList( );
            workingMemory.setApplicationData( "list",
                                              list );
            workingMemory.assertObject( "help" );
            workingMemory.fireAllRules( );
            assertEquals( "help",
                          list.get( 0 ) );

            // Try with URL to Drools Distribution Jar, ddj []
            ruleBaseLoader = new RuleBaseLoader( );
            ruleBaseLoader.addFromByteArray( new byte[][] { compilers[0].getBinaryDeploymentJar( ) } );
            ruleBase = ruleBaseLoader.buildRuleBase( );
            workingMemory = ruleBase.newWorkingMemory( );
            list = new ArrayList( );
            workingMemory.setApplicationData( "list",
                                              list );
            workingMemory.assertObject( "help" );
            workingMemory.fireAllRules( );
            assertEquals( "help",
                          list.get( 0 ) );
        }
        catch ( Exception e )
        {
View Full Code Here

    public void testAddFromConfUrl() throws IntegrationException,
                                    SAXException,
                                    IOException,
                                    FactException
    {
        WorkingMemory workingMemory;
        List list;
        RuleBase ruleBase;
        RuleBaseLoader ruleBaseLoader;
        RuleSetLoader ruleSetLoader = new RuleSetLoader( );

        // single URL parameter
        ruleSetLoader.addFromUrl( RuleBaseLoaderTest.class.getResource( "simple.java.drl" ) );

        // Try with addFromRuleSetLoader
        ruleBaseLoader = new RuleBaseLoader( );
        ruleBaseLoader.addFromRuleSetLoader( ruleSetLoader );

        RuleSetCompiler[] compilers = (RuleSetCompiler[]) ruleSetLoader.getRuleSets( ).values( ).toArray( new RuleSetCompiler[]{} );

        File root = null;
        try
        {
            root = createTempDirectory( );
            File file = new File( root,
                                  "drools.ddj" );
            FileOutputStream fos = new FileOutputStream( file );
            fos.write( compilers[0].getBinaryDeploymentJar( ) );
            fos.close( );

            // make a conf file to the URL
            File conf = new File( root,
                                  "rule-base.conf" );
            fos = new FileOutputStream( conf );
            fos.write( file.toURL( ).toExternalForm( ).getBytes( ) );
            fos.close( );

            // Try with URL to Distribution conf file
            ruleBaseLoader = new RuleBaseLoader( );
            ruleBaseLoader.addFromUrl( conf.toURL( ) );
            ruleBase = ruleBaseLoader.buildRuleBase( );
            workingMemory = ruleBase.newWorkingMemory( );
            list = new ArrayList( );
            workingMemory.setApplicationData( "list",
                                              list );
            workingMemory.assertObject( "help" );
            workingMemory.fireAllRules( );
            assertEquals( "help",
                          list.get( 0 ) );

            // Try with URL to Distribution conf file
            ruleBaseLoader = new RuleBaseLoader( );
            ruleBaseLoader.addFromUrl( new URL[]{conf.toURL( )} );
            ruleBase = ruleBaseLoader.buildRuleBase( );
            workingMemory = ruleBase.newWorkingMemory( );
            list = new ArrayList( );
            workingMemory.setApplicationData( "list",
                                              list );
            workingMemory.assertObject( "help" );
            workingMemory.fireAllRules( );
            assertEquals( "help",
                          list.get( 0 ) );

        }
        catch ( Exception e )
View Full Code Here

            listeners.add(listener);
        }
    }
   
    private WorkingMemory createObject() {
        final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
        addApplicationData(workingMemory);
        addListeners(workingMemory);
        return workingMemory;
    }
View Full Code Here

//            assertTrue(expectedFacts.contains(fact));
//        }
//    }   
   
    public void FIXME_testApplicationData() throws Exception {
        WorkingMemory workingMemory = (WorkingMemory) contextHolder.context.getBean("workingMemory");
        assertNotNull(workingMemory);

        assertEquals("this is a child comment", workingMemory.getApplicationData("childComment"));
        assertEquals("this is an adult comment", workingMemory.getApplicationData("adultComment"));
    }
View Full Code Here

        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",
View Full Code Here

                      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( ) );
View Full Code Here

    public static void main(String args[]) throws Exception {

        final ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "org/drools/spring/examples/jiahvac/control/hvac.appctx.xml");

        final WorkingMemory workingMemory =
                (WorkingMemory) applicationContext.getBean("workingMemory");
       
        final Simulator simulator =
                (Simulator) applicationContext.getBean("simulator");
       
        final TempuratureControl control =
                (TempuratureControl) applicationContext.getBean("tempuratureControl");

        workingMemory.assertObject(control);

        for (Floor floor : simulator.getFloors()) {
            workingMemory.assertObject(floor.getThermometer(), true);
            workingMemory.assertObject(floor.getVent(), true);
        }
        for (HeatPump pump : simulator.getHeatPumps()) {
            workingMemory.assertObject(pump, true);
        }
        System.out.println("---- begin initial fireAllRules");
        workingMemory.fireAllRules();
        System.out.println("---- end initial fireAllRules");

        // The 'executor' bean is lazy-init so we can control when the simulator starts
        // (which is now). Alternatively, we could have defined a start method to begin
        // the simulation.
View Full Code Here

public class HelloWorldRunner {
   
    public static void run(RuleBase ruleBase) throws FactException {
        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;
            Fibonacci fibonacci;
            long start;
            long stop;

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

            fibonacci = new Fibonacci( 50 );
            start = System.currentTimeMillis( );
            workingMemory.assertObject( fibonacci );

            workingMemory.fireAllRules( );
            stop = System.currentTimeMillis( );

            System.err.println( "fibonacci(" + fibonacci.getSequence( )
                                + ") == " + fibonacci.getValue( ) + " took "
                                + ( stop - start ) + "ms" );
            System.err.println( "\nSecond run - code already compiled" );

            workingMemory = ruleBase.newWorkingMemory( );

            fibonacci = new Fibonacci( 50 );
            start = System.currentTimeMillis( );
            workingMemory.assertObject( fibonacci );

            workingMemory.fireAllRules( );
            stop = System.currentTimeMillis( );
            System.err.println( "fibonacci(" + fibonacci.getSequence( )
                                + ") == " + fibonacci.getValue( ) + " took "
                                + ( stop - start ) + "ms" );
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.