Package org.drools.rule

Examples of org.drools.rule.Declaration


        // Build the declaration and specify it as a parameter of the Rule
        // <parameter identifier="context">
        // <class>org.drools.examples.manners.model.Context</class>
        // </parameter>
        final Declaration contextDeclA = findFirstSeatRule.addParameterDeclaration( "context",
                                                                                    contextType );

        // <parameter identifier="guest">
        // <class>org.drools.examples.manners.model.Guest</class>
        // </parameter>
        final Declaration guestDeclA = findFirstSeatRule.addParameterDeclaration( "guest",
                                                                                  guestType );

        // Build and Add the Condition to the Rule
        // <java:condition>context.isState("start")</java:condition>
        final Condition conditionA1 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Context context = (Context) tuple.get( contextDeclA );
                return context.isState( "start" );
            }

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

            public String toString()
            {
                return "context.isState(\"start\")";
            }
        };
        findFirstSeatRule.addCondition( conditionA1 );

        // Build and Add the Consequence to the Rule
        // <java:consequence>
        // System.out.println("FIRE: find first seat: " + guest);
        // import org.drools.examples.manners.model.Seating;
        // drools.assertObject(new Seating(1, guest, null));
        // context.setState("find_seating");
        // drools.modifyObject(context);
        // </java:consequence>
        final Consequence consequenceA = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );

                Context context = (Context) tuple.get( contextDeclA );
                Guest guest = (Guest) tuple.get( guestDeclA );

                try
                {
                    workingMemory.assertObject( new Seating( 1,
                                                             guest,
                                                             null ) );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }

                context.setState( "find_seating" );

                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( context ),
                                                context );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        findFirstSeatRule.setConsequence( consequenceA );
        ruleSet.addRule( findFirstSeatRule );

        // ========================================
        // <rule name="find seating" salience="30">
        // ========================================
        final Rule findSeatingRule = new Rule( "find seating" );
        findSeatingRule.setSalience( 30 );

        // Build the declaration and specify it as a parameter of the Rule
        // <parameter identifier="context">
        // <class>org.drools.examples.manners.model.Context</class>
        // </parameter>
        final Declaration contextDeclB = findSeatingRule.addParameterDeclaration( "context",
                                                                                  contextType );

        // <parameter identifier="guest">
        // <class>org.drools.examples.manners.model.Guest</class>
        // </parameter>
        final Declaration guestDeclB = findSeatingRule.addParameterDeclaration( "guest",
                                                                                guestType );

        // <parameter identifier="seating">
        // <class>org.drools.examples.manners.model.Seating</class>
        // </parameter>
        final Declaration seatingDeclB = findSeatingRule.addParameterDeclaration( "seating",
                                                                                  seatingType );

        // Build and Add the Condition to the Rule
        // <java:condition>context.isState("find_seating")</java:condition>
        final Condition conditionB1 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Context context = (Context) tuple.get( contextDeclB );
                return context.isState( "find_seating" );
            }

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

            public String toString()
            {
                return "context.isState(\"find_seating\")";
            }
        };
        findSeatingRule.addCondition( conditionB1 );

        // <java:condition>seating.getGuest2() == null</java:condition>
        final Condition conditionB2 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Seating seating = (Seating) tuple.get( seatingDeclB );
                return seating.getGuest2( ) == null;
            }

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

            public String toString()
            {
                return "seating.getGuest2() == null";
            }
        };
        findSeatingRule.addCondition( conditionB2 );

        // <java:condition>!seating.getTabooList().contains(guest)</java:condition>
        final Condition conditionB3 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Seating seating = (Seating) tuple.get( seatingDeclB );
                Guest guest = (Guest) tuple.get( guestDeclB );
                return !seating.getTabooList( ).contains( guest );
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{seatingDeclB, guestDeclB};
            }

            public String toString()
            {
                return "!seating.getTabooList().contains(guest)";
            }
        };
        findSeatingRule.addCondition( conditionB3 );

        // <java:condition>seating.getGuest1().hasOppositeSex(guest)</java:condition>
        final Condition conditionB4 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Seating seating = (Seating) tuple.get( seatingDeclB );
                Guest guest = (Guest) tuple.get( guestDeclB );
                return seating.getGuest1( ).hasOppositeSex( guest );
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{seatingDeclB, guestDeclB};
            }

            public String toString()
            {
                return "seating.getGuest1().hasOppositeSex(guest)";
            }
        };
        findSeatingRule.addCondition( conditionB4 );

        // <java:condition>seating.getGuest1().hasSameHobby(guest)</java:condition>
        final Condition conditionB5 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Seating seating = (Seating) tuple.get( seatingDeclB );
                Guest guest = (Guest) tuple.get( guestDeclB );
                return seating.getGuest1( ).hasSameHobby( guest );
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{seatingDeclB, guestDeclB};
            }

            public String toString()
            {
                return "seating.getGuest1().hasSameHobby(guest)";
            }
        };
        findSeatingRule.addCondition( conditionB5 );

        // Build and Add the Consequence to the Rule
        // <java:consequence>
        // System.out.println("FIRE: find seating: " + seating + " " + guest);
        //
        // Seating nextSeat = new Seating(seating.getSeat2(), guest, seating);
        // drools.assertObject(nextSeat);
        //
        // seating.setGuest2(guest);
        // seating.getTabooList().add(guest);
        // drools.modifyObject(seating);
        // </java:consequence>
        final Consequence consequenceB = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );

                Guest guest = (Guest) tuple.get( guestDeclB );
                Seating seating = (Seating) tuple.get( seatingDeclB );

                Seating nextSeat = new Seating( seating.getSeat2( ),
                                                guest,
                                                seating );
                try
                {
                    workingMemory.assertObject( nextSeat );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }

                seating.setGuest2( guest );
                seating.getTabooList( ).add( guest );

                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( seating ),
                                                seating );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        findSeatingRule.setConsequence( consequenceB );
        ruleSet.addRule( findSeatingRule );

        // ===========================================
        // <rule name="try another path" salience="20">
        // ===========================================
        final Rule tryAnotherPathRule = new Rule( "try another path" );
        tryAnotherPathRule.setSalience( 20 );

        // Build the declaration and specify it as a parameter of the Rule
        // <parameter identifier="context">
        // <class>org.drools.examples.manners.model.Context</class>
        // </parameter>
        final Declaration contextDeclC = tryAnotherPathRule.addParameterDeclaration( "context",
                                                                                     contextType );

        // <parameter identifier="lastSeat">
        // <class>org.drools.examples.manners.model.LastSeat</class>
        // </parameter>
        final Declaration lastSeatDeclC = tryAnotherPathRule.addParameterDeclaration( "lastSeat",
                                                                                      lastSeatType );

        // <parameter identifier="seating">
        // <class>org.drools.examples.manners.model.Seating</class>
        // </parameter>
        final Declaration seatingDeclC = tryAnotherPathRule.addParameterDeclaration( "seating",
                                                                                     seatingType );

        // <java:condition>context.isState("find_seating")</java:condition>
        final Condition conditionC1 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Context context = (Context) tuple.get( contextDeclC );
                return context.isState( "find_seating" );
            }

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

            public String toString()
            {
                return "context.isState(\"find_seating\")";
            }
        };
        tryAnotherPathRule.addCondition( conditionC1 );

        // <java:condition>lastSeat.getSeat() >
        // seating.getSeat1()</java:condition>
        final Condition conditionC2 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                LastSeat lastSeat = (LastSeat) tuple.get( lastSeatDeclC );
                Seating seating = (Seating) tuple.get( seatingDeclC );
                return lastSeat.getSeat( ) > seating.getSeat1( );
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{lastSeatDeclC, seatingDeclC};
            }

            public String toString()
            {
                return "lastSeat.getSeat() > seating.getSeat1()";
            }
        };
        tryAnotherPathRule.addCondition( conditionC2 );

        // <java:condition>seating.getGuest2() == null</java:condition>
        final Condition conditionC3 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Seating seating = (Seating) tuple.get( seatingDeclC );
                return seating.getGuest2( ) == null;
            }

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

            public String toString()
            {
                return "seating.getGuest2() == null";
            }
        };
        tryAnotherPathRule.addCondition( conditionC3 );

        // <java:consequence>
        // System.out.println("FIRE: try another path: " + seating);
        //
        // Seating prevSeat = seating.getPrevSeat();
        // prevSeat.setGuest2(null);
        // drools.modifyObject(prevSeat);
        //
        // drools.retractObject(seating);
        // </java:consequence>
        final Consequence consequenceC = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );

                Seating seating = (Seating) tuple.get( seatingDeclC );

                Seating prevSeat = seating.getPrevSeat( );
                prevSeat.setGuest2( null );

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

                try
                {
                    workingMemory.retractObject( tuple.getFactHandleForObject( seating ) );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        tryAnotherPathRule.setConsequence( consequenceC );
        ruleSet.addRule( tryAnotherPathRule );

        // =======================================
        // <rule name="we are done" salience="10">
        // =======================================
        final Rule weAreDoneRule = new Rule( "we are done" );
        weAreDoneRule.setSalience( 10 );

        // Build the declaration and specify it as a parameter of the Rule
        // <parameter identifier="context">
        // <class>org.drools.examples.manners.model.Context</class>
        // </parameter>
        final Declaration contextDeclD = weAreDoneRule.addParameterDeclaration( "context",
                                                                                contextType );

        // <parameter identifier="lastSeat">
        // <class>org.drools.examples.manners.model.LastSeat</class>
        // </parameter>
        final Declaration lastSeatDeclD = weAreDoneRule.addParameterDeclaration( "lastSeat",
                                                                                 lastSeatType );

        // <parameter identifier="seating">
        // <class>org.drools.examples.manners.model.Seating</class>
        // </parameter>
        final Declaration seatingDeclD = weAreDoneRule.addParameterDeclaration( "seating",
                                                                                seatingType );

        // <java:condition>context.isState("find_seating")</java:condition>
        final Condition conditionD1 = new Condition( )
        {
View Full Code Here


                    // used for rules, salience and timers so they work with 'or' CEs
                    prevDecl =  this.previousDeclarations;
                }               

                for ( int j = 0, length = prevDecl.length; j < length; j++ ) {
                    Declaration decl = prevDecl[j];
                    InternalFactHandle handle = getFactHandle( decl,
                                                               handles );

                    Object o = decl.getValue( (InternalWorkingMemory) workingMemory,
                                              handle.getObject() );
                    if ( knowledgeHelper != null && decl.isPatternDeclaration() ) {
                        identityMap.put( o,
                                         handle );
                    }
                    factory.getIndexedVariableResolver( i++ ).setValueo );
                }
            }
        }

        if ( this.localDeclarations != null && this.localDeclarations.length > 0 ) {
            for ( int j = 0, length = this.localDeclarations.length; j < length; j++ ) {
                Declaration decl = this.localDeclarations[j];
                Object o = decl.getValue( (InternalWorkingMemory) workingMemory,
                                          rightObject );
                factory.getIndexedVariableResolver( i++ ).setValue( o );
            }
        }
View Full Code Here

                                              new ClassObjectType( Cheese.class ) );

        final Pattern patternB = new Pattern( 1,
                                              new ClassObjectType( Cheese.class ) );

        final Declaration a = new Declaration( "a",
                                               extractor,
                                               patternA );
        final Declaration b = new Declaration( "b",
                                               extractor,
                                               patternB );

        final Map map = new HashMap();
        map.put( "a",
View Full Code Here

                                                             "price",
                                                             getClass().getClassLoader() );

        final Pattern pattern = new Pattern( 0,
                                             new ClassObjectType( int.class ) );
        final Declaration declaration = new Declaration( "a",
                                                         extractor,
                                                         pattern );
        final Map map = new HashMap();
        map.put( "a",
                 declaration );
View Full Code Here

                                              new ClassObjectType( int.class ) );

        final Pattern patternB = new Pattern( 1,
                                              new ClassObjectType( int.class ) );

        final Declaration a = new Declaration( "a",
                                               extractor,
                                               patternA );
        final Declaration b = new Declaration( "b",
                                               extractor,
                                               patternB );

        final Map map = new HashMap();
        map.put( "a",
View Full Code Here

            QueryTerminalNode node = (QueryTerminalNode) resultLeftTuple.getLeftTupleSink();
            Declaration[] decls = node.getDeclarations();
            DroolsQuery query = (DroolsQuery) this.factHandle.getObject();
            Object[] objects = new Object[query.getElements().length];

            Declaration decl;
            for ( int i = 0, length = this.variables.length; i < length; i++ ) {
                decl = decls[this.variables[i]];
                objects[this.variables[i]] = decl.getValue( workingMemory,
                                                            resultLeftTuple.get( decl ).getObject() );
            }

            QueryElementFactHandle resultHandle = createQueryResultHandle( context,
                                                                           workingMemory,
View Full Code Here

            InternalFactHandle rootHandle = resultLeftTuple.get( 0 );
            DroolsQuery query = (DroolsQuery) rootHandle.getObject();

            Object[] objects = new Object[query.getElements().length];

            Declaration decl;
            for ( int i = 0, length = this.variables.length; i < length; i++ ) {
                decl = decls[this.variables[i]];
                objects[this.variables[i]] = decl.getValue( workingMemory,
                                                            resultLeftTuple.get( decl ).getObject() );
            }

            QueryElementFactHandle handle = (QueryElementFactHandle) rightTuple.getFactHandle();
View Full Code Here

                    return true;
                }
            }
        }

        Declaration declr = null;
        if ( value2.indexOf( '(' ) < 0 && value2.indexOf( '.' ) < 0 && value2.indexOf( '[' ) < 0 ) {
            declr = context.getDeclarationResolver().getDeclaration( context.getRule(), value2 );

            if ( declr == null ) {
                // trying to create implicit declaration
                final Pattern thisPattern = (Pattern) context.getBuildStack().peek();
                declr = createDeclarationObject( context, value2, thisPattern );
            }
        }

        Declaration[] declarations = null;
        if ( declr == null ) {
            String[] parts = value2.split( "\\." );
            if ( parts.length == 2 ) {
                if ( "this".equals( parts[0].trim() ) ) {
                    declr = createDeclarationObject(context, parts[1].trim(), (Pattern) context.getBuildStack().peek());
                    value2 = parts[1].trim();
                } else {
                    declr = context.getDeclarationResolver().getDeclaration( context.getRule(), parts[0].trim() );
                    // if a declaration exists, then it may be a variable direct property access
                    if ( declr != null ) {
                        if ( declr.isPatternDeclaration() ) {
                            // TODO: no need to extract inner declaration when using mvel constraint
                            declarations = new Declaration[] { declr };
                            declr = createDeclarationObject(context, parts[1].trim(), declr.getPattern());
                            value2 = parts[1].trim();

                        } else {
                            // we will later fallback to regular predicates, so don't raise error
                            return false;
View Full Code Here

        final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();

        final List<Declaration> tupleDeclarations = new ArrayList<Declaration>();
        final List<Declaration> factDeclarations = new ArrayList<Declaration>();
        for ( String id : usedIdentifiers.getDeclrClasses().keySet() ) {
            final Declaration decl = context.getDeclarationResolver().getDeclaration( context.getRule(), id );
            if ( decl.getPattern() == pattern ) {
                factDeclarations.add( decl );
            } else {
                tupleDeclarations.add( decl );
            }
        }
View Full Code Here

            if ( fieldBindingDescr.isUnification() ) {
                return;
            }
        }

        Declaration declr = pattern.addDeclaration( fieldBindingDescr.getVariable() );

        final InternalReadAccessor extractor = getFieldReadAccessor( context,
                                                                     fieldBindingDescr,
                                                                     pattern.getObjectType(),
                                                                     fieldBindingDescr.getBindingField(),
                                                                     declr,
                                                                     true );
        declr.setReadAccessor( extractor );

        if ( extractor instanceof ClassFieldReader ) {
            List<String> watchlist = pattern.getListenedProperties();
            if ( watchlist == null ) {
                watchlist = new ArrayList<String>( 5 );
View Full Code Here

TOP

Related Classes of org.drools.rule.Declaration

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.