Package org.drools.reteoo

Examples of org.drools.reteoo.InstrumentedWorkingMemoryImpl


    
     * </pre>
     */
    public void testBoundConstraint()
    {
        InstrumentedWorkingMemoryImpl workingMemory = new InstrumentedWorkingMemoryImpl( );

        ObjectType stringObjectType = new ClassObjectType( String.class );

        /* Determines how the bound value is extracted from the column */
        Extractor typeOfCheeseExtractor = new Extractor( ) {
            public Object getValue(Object object)
            {
                return ((Cheese) object).getType( );
            }
        };

        /* Bind the extractor to a decleration */
        /* Declarations know the column they derive their value form */
        Declaration typeOfCheeseDeclaration = new Declaration( 0,
                                                               "typeOfCheese",
                                                               stringObjectType,
                                                               typeOfCheeseExtractor,
                                                               0 );


        /* Create some facts */
        Cheese cheddar = new Cheese( "cheddar",
                                     5 );

        FactHandle f0 = workingMemory.createFactHandle( 0 );
        workingMemory.putObject( f0,
                                 cheddar );
        InstrumentedReteTuple tuple = new InstrumentedReteTuple( 0,
                                                                 f0,
                                                                 workingMemory );

View Full Code Here


     * really is actually a constraint, ie making sure it has the same value as
     * derived from the first column's type field.
     */
    public void testDoubleBoundConstraint()
    {
        InstrumentedWorkingMemoryImpl workingMemory = new InstrumentedWorkingMemoryImpl( );

        ObjectType stringObjectType = new ClassObjectType( String.class );

        /* Determines how the bound value is extracted from the column */
        Extractor typeOfCheeseExtractor = new Extractor( ) {
            public Object getValue(Object object)
            {
                return ((Cheese) object).getType( );
            }
        };

        /* Bind the extractor to a decleration */
        /* Declarations know the column they derive their value form */
        Declaration typeOfCheeseDeclaration = new Declaration( 0,
                                                               "typeOfCheese",
                                                               stringObjectType,
                                                               typeOfCheeseExtractor,
                                                               0 );

        ReturnValueExpressionConstraint isCheddar = new ReturnValueExpressionConstraint( ) {
            public boolean isAllowed(Object object,
                                     FactHandle handle,
                                     Declaration[] declarations,
                                     Tuple tuple,
                                     ConstraintComparator comparator)
            {

                return comparator.compare( ((Cheese) object).getType( ),
                                           tuple.get( declarations[0] ) );
            }

        };

        /*
         * Creates a constraint with an expression and an array of required
         * declarations
         */
        ReturnValueConstraint constraint1 = new ReturnValueConstraint( isCheddar,
                                                                       new Declaration[]{typeOfCheeseDeclaration},
                                                                       new StringConstraintComparator( ConstraintComparator.EQUAL ) );

        Cheese cheddar0 = new Cheese( "cheddar",
                                      5 );
        FactHandle f0 = workingMemory.createFactHandle( 0 );
        workingMemory.putObject( f0,
                                 cheddar0 );
        InstrumentedReteTuple tuple = new InstrumentedReteTuple( 0,
                                                                 f0,
                                                                 workingMemory );

        Cheese cheddar1 = new Cheese( "cheddar",
                                      5 );
        FactHandle f1 = workingMemory.createFactHandle( 1 );
        workingMemory.putObject( f1,
                                 cheddar1 );
        tuple = new InstrumentedReteTuple( tuple,
                                           new InstrumentedReteTuple( 1,
                                                                      f1,
                                                                      workingMemory ) );

        assertTrue( constraint1.isAllowed( cheddar1,
                                           f1,
                                           tuple ) );

        cheddar1 = new Cheese( "stilton",
                               5 );
        workingMemory.putObject( f1,
                                 cheddar1 );

        /*
         * simulate a modify, so we can check for a false assertion.
         */
 
View Full Code Here

    
     * </pre>
     */
    public void testBooleanExpressionConstraint()
    {
        InstrumentedWorkingMemoryImpl workingMemory = new InstrumentedWorkingMemoryImpl( );

        ObjectType integerObjectType = new ClassObjectType( Integer.class );

        /* Determines how the bound value is extracted from the column */
        Extractor priceOfCheeseExtractor = new Extractor( ) {
            public Object getValue(Object object)
            {
                return new Integer( ((Cheese) object).getPrice( ) );
            }
        };

        /* Bind the extractor to a decleration */
        /* Declarations know the column they derive their value form */
        Declaration price1Declaration = new Declaration( 0,
                                                         "price1",
                                                         integerObjectType,
                                                         priceOfCheeseExtractor,
                                                         0 );

        /* Bind the extractor to a decleration */
        /* Declarations know the column they derive their value form */
        Declaration price2Declaration = new Declaration( 1,
                                                         "price2",
                                                         integerObjectType,
                                                         priceOfCheeseExtractor,
                                                         1 );

        BooleanExpressionConstraint isDoubleThePrice = new BooleanExpressionConstraint( ) {
            public boolean isAllowed(Object object,
                                     FactHandle handle,
                                     Declaration declaration, // ?price2
                                     Declaration[] declarations, // ?price1
                                     Tuple tuple)
            {
                int price1 = ((Integer) tuple.get( declarations[0] )).intValue( );
                int price2 = ((Integer) tuple.get( declaration )).intValue( );
                return (price2 == (price1 * 2));

            }
        };

        BooleanConstraint constraint1 = new BooleanConstraint( isDoubleThePrice,
                                                               price2Declaration,
                                                               new Declaration[]{price1Declaration} );

        Cheese cheddar0 = new Cheese( "cheddar",
                                      5 );
        FactHandle f0 = workingMemory.createFactHandle( 0 );
        workingMemory.putObject( f0,
                                 cheddar0 );
        InstrumentedReteTuple tuple = new InstrumentedReteTuple( 0,
                                                                 f0,
                                                                 workingMemory );

        Cheese cheddar1 = new Cheese( "cheddar",
                                      10 );
        FactHandle f1 = workingMemory.createFactHandle( 1 );
        workingMemory.putObject( f1,
                                 cheddar1 );
        tuple = new InstrumentedReteTuple( tuple,
                                           new InstrumentedReteTuple( 1,
                                                                      f1,
                                                                      workingMemory ) );
View Full Code Here

    
     * </pre>
     */
    public void testReturnValueConstraint()
    {
        InstrumentedWorkingMemoryImpl workingMemory = new InstrumentedWorkingMemoryImpl( );

        ObjectType integerObjectType = new ClassObjectType( Integer.class );

        /* Determines how the bound value is extracted from the column */
        Extractor priceOfCheeseExtractor = new Extractor( ) {
            public Object getValue(Object object)
            {
                return new Integer( ((Cheese) object).getPrice( ) );
            }
        };

        /* Bind the extractor to a decleration */
        /* Declarations know the column they derive their value form */
        Declaration priceDeclaration = new Declaration( 0,
                                                        "price",
                                                        integerObjectType,
                                                        priceOfCheeseExtractor,
                                                        0 );

        ReturnValueExpressionConstraint isDoubleThePrice = new ReturnValueExpressionConstraint( ) {
            public boolean isAllowed(Object object,
                                     FactHandle handle,
                                     Declaration[] declarations, // ?price
                                     Tuple tuple,
                                     ConstraintComparator comparator)
            {
                int price = ((Integer) tuple.get( declarations[0] )).intValue( );

                return comparator.compare( new Integer( ((Cheese) object).getPrice( ) ),
                                           new Integer( 2 * price ) );
            }
        };

        ReturnValueConstraint constraint1 = new ReturnValueConstraint( isDoubleThePrice,
                                                                       new Declaration[]{priceDeclaration},
                                                                       new NumericConstraintComparator( ConstraintComparator.EQUAL ) );

        ReturnValueConstraint constraint2 = new ReturnValueConstraint( isDoubleThePrice,
                                                                       new Declaration[]{priceDeclaration},
                                                                       new NumericConstraintComparator( ConstraintComparator.GREATER ) );

        Cheese cheddar0 = new Cheese( "cheddar",
                                      5 );
        FactHandle f0 = workingMemory.createFactHandle( 0 );
        workingMemory.putObject( f0,
                                 cheddar0 );
        InstrumentedReteTuple tuple = new InstrumentedReteTuple( 0,
                                                                 f0,
                                                                 workingMemory );

        Cheese cheddar1 = new Cheese( "cheddar",
                                      10 );
        FactHandle f1 = workingMemory.createFactHandle( 1 );
        workingMemory.putObject( f1,
                                 cheddar1 );
        tuple = new InstrumentedReteTuple( tuple,
                                           new InstrumentedReteTuple( 0,
                                                                      f1,
                                                                      workingMemory ) );
View Full Code Here

TOP

Related Classes of org.drools.reteoo.InstrumentedWorkingMemoryImpl

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.