capture
[rrq/gorite.git] / examples / planchoice / Main.java
1 package examples.planchoice;
2
3 import com.intendico.gorite.*;
4 import com.intendico.data.*;
5 import com.intendico.data.addon.Language;
6 import java.util.Vector;
7 import java.util.Map;
8 import java.util.Hashtable;
9
10 /**
11  * This is an example of using a plan choice plan for choosing which
12  * plan instance to pursue first / next for a goal.
13  */
14 public class Main {
15
16     /**
17      * Debug: dump the plan choice options
18      */
19     static private void debugOptions(Data data) {
20         Vector<ContextualGoal> options = (Vector<ContextualGoal>)
21             data.getValue( "options" );
22         Vector<ContextualGoal> failed = (Vector<ContextualGoal>)
23             data.getValue( "failed" );
24         int n = 0;
25         System.err.println( "=============" );
26         for ( ContextualGoal cg : failed ) {
27             Plan p = (Plan) cg.getGoalSubgoals()[0];
28             if ( p instanceof TransferGoal ) {
29                 p = (Plan) ((TransferGoal) p).goal;
30             }
31             System.err.printf(
32                 "Failed %d = %s (%s)\n", n++, cg.context, p.getType() );
33         }
34         n = 0;
35         for ( ContextualGoal cg : options ) {
36             Plan p = (Plan) cg.getGoalSubgoals()[0];
37             if ( p instanceof TransferGoal ) {
38                 p = (Plan) ((TransferGoal) p).goal;
39             }
40             System.err.printf(
41                 "Option %d = %s (%s)\n", n++, cg.context, p.getType() );
42         }
43     }
44
45     /**
46      * Application entry point.
47      */
48     static public void main(String [] args) throws Exception {
49         //Goal.tracing = true;
50         Performer ralph = new Performer( "ralph" ) {{
51             setPlanChoice( "go for it", "go for it choice" );
52             putInquirable( new Relation( "unary", 1 ) );
53             getInquirable( "unary" ).get( new Object [] { "AAA" } ).add();
54             getInquirable( "unary" ).get( new Object [] { "BBB" } ).add();
55             getInquirable( "unary" ).get( new Object [] { "CCC" } ).add();
56
57             addGoal( new Plan( "go for it", new Goal [] {
58                 new Goal( "print $x" ) {
59                     public States execute(Data data) {
60                         System.err.println(
61                             "First: " + data.getValue( "$x" ) );
62                         return States.FAILED;
63                     }
64                 }
65                     } ) {
66                 public Query context(Data d) {
67                     return Language.textToQuery(
68                         "unary( $x )", getInquirables().values(), null, d );
69                 }
70             } );
71
72             addGoal( new Plan( "go for it", new Goal [] {
73                 new Goal( "print $y" ) {
74                     public States execute(Data data) {
75                         System.err.println(
76                             "Second: " + data.getValue( "$y" ) );
77                         return States.FAILED;
78                     }
79                 }
80                     }) {
81                 public Query context(Data d) {
82                     return Language.textToQuery(
83                         "unary( $y )", getInquirables().values(), null, d );
84                 }
85             } );
86
87             /**
88              * Add a plan choice plan that inspects the bindings for
89              * the applicable plan instance options, then selects the
90              * first of highest character code of the first binding.
91              *
92              * Note that this particular choice logic could also be
93              * implemented as precedence methods of the object level
94              * plans.
95              */
96             addGoal( new Plan( "go for it choice", new Goal [] {
97                 new Goal( "select by planCode" ) {
98                     @Override
99                     public States execute(Data data,Goal.Instance i) {
100                         /**
101                            Input data elements:
102                            "options" = Vector of ContextualGoal
103                            "failed" = Vector of ContextualGoal
104                            Output data elements:
105                            "choice" = ContextualGoal (or null)
106                         **/
107                         debugOptions( data );
108                         int code = -1;
109                         Vector<ContextualGoal> options =
110                             (Vector<ContextualGoal>)
111                             data.getValue( "options" );
112                         if ( options.size() >= 1 ) {
113                             System.out.println(
114                                 "Cause: " +
115                                 options.get( 0 ).goal_data.thread_name );
116                         }
117                         ContextualGoal choice = null;
118                         for ( ContextualGoal g : options ) {
119                             int c = planCode( g );
120                             System.out.printf(
121                                 "code for %s is %d\n", g.context, c );
122                             if ( c <= code )
123                                 continue;
124                             choice = g;
125                             code = c;
126                         }
127                         if ( choice != null ) {
128                             data.setValue( "choice", choice );
129                         }
130                         return code > (int)'A'? States.PASSED : States.FAILED;
131                     }
132                 }
133                     } ) );
134         }};
135         Team team = new Team( "everyone" ) {{
136             setTaskTeam( "group", new TaskTeam() {{
137                 addRole( new Role( "sub", null ) {{
138                     putInquirable( new Relation( "unary", 1 ) );
139                     getInquirable( "unary" ).get(
140                         new Object [] { "ABC" } ).add();
141                     getInquirable( "unary" ).get(
142                         new Object [] { "DEF" } ).add();
143                     addGoal( new Plan( "go for it", new Goal [] {
144                         new Goal( "print $y" ) {
145                             public States execute(Data data) {
146                                 System.err.println(
147                                     "Third: " +
148                                     data.getValue( "$y" ) );
149                                 return States.FAILED;
150                             }
151                         }
152                             }) {
153                         public Query context(Data d) {
154                             return Language.textToQuery(
155                                 "unary( $y )",
156                                 getInquirables().values(), null, d );
157                         }
158                     } );
159                 }} );
160             }} );
161             addGoal( new Plan( "go for it", new Goal [] {
162                         deploy( "group" ),
163                         new TeamGoal( "sub", "go for it" )
164                     } ) );
165         }};
166         team.addPerformer( ralph );
167         team.performGoal( new BDIGoal( "go for it" ), "TOP", new Data() );
168     }
169
170     /**
171      * Utility method that gives a given plan option a code, which
172      * generally is 0, except when the goal represents a contextual
173      * plan instance with an instantiation context that offers a
174      * non-empty String for the first context variable, and in that
175      * case, the character code of the first character of that string
176      * is used.
177      */
178     static public int planCode(ContextualGoal g) {
179         if ( g.context == null ) {
180             return 0;
181         }
182         Vector<Ref> cg = (Vector<Ref>) g.context;
183         if ( cg == null || cg.size() == 0 )
184             return 0;
185         Object x = Ref.deref( cg.get( 0 ) );
186         if ( ! ( x instanceof String ) )
187             return 0;
188         String s = (String) x;
189         if ( s.length() == 0 )
190             return 0;
191         return (int) s.charAt( 0 );
192     }
193 }