1 package examples.planchoice;
3 import com.intendico.gorite.*;
4 import com.intendico.data.*;
5 import com.intendico.data.addon.Language;
6 import java.util.Vector;
8 import java.util.Hashtable;
11 * This is an example of using a plan choice plan for choosing which
12 * plan instance to pursue first / next for a goal.
17 * Debug: dump the plan choice options
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" );
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;
32 "Failed %d = %s (%s)\n", n++, cg.context, p.getType() );
35 for ( ContextualGoal cg : options ) {
36 Plan p = (Plan) cg.getGoalSubgoals()[0];
37 if ( p instanceof TransferGoal ) {
38 p = (Plan) ((TransferGoal) p).goal;
41 "Option %d = %s (%s)\n", n++, cg.context, p.getType() );
46 * Application entry point.
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();
57 addGoal( new Plan( "go for it", new Goal [] {
58 new Goal( "print $x" ) {
59 public States execute(Data data) {
61 "First: " + data.getValue( "$x" ) );
66 public Query context(Data d) {
67 return Language.textToQuery(
68 "unary( $x )", getInquirables().values(), null, d );
72 addGoal( new Plan( "go for it", new Goal [] {
73 new Goal( "print $y" ) {
74 public States execute(Data data) {
76 "Second: " + data.getValue( "$y" ) );
81 public Query context(Data d) {
82 return Language.textToQuery(
83 "unary( $y )", getInquirables().values(), null, d );
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.
92 * Note that this particular choice logic could also be
93 * implemented as precedence methods of the object level
96 addGoal( new Plan( "go for it choice", new Goal [] {
97 new Goal( "select by planCode" ) {
99 public States execute(Data data,Goal.Instance i) {
102 "options" = Vector of ContextualGoal
103 "failed" = Vector of ContextualGoal
104 Output data elements:
105 "choice" = ContextualGoal (or null)
107 debugOptions( data );
109 Vector<ContextualGoal> options =
110 (Vector<ContextualGoal>)
111 data.getValue( "options" );
112 if ( options.size() >= 1 ) {
115 options.get( 0 ).goal_data.thread_name );
117 ContextualGoal choice = null;
118 for ( ContextualGoal g : options ) {
119 int c = planCode( g );
121 "code for %s is %d\n", g.context, c );
127 if ( choice != null ) {
128 data.setValue( "choice", choice );
130 return code > (int)'A'? States.PASSED : States.FAILED;
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) {
148 data.getValue( "$y" ) );
149 return States.FAILED;
153 public Query context(Data d) {
154 return Language.textToQuery(
156 getInquirables().values(), null, d );
161 addGoal( new Plan( "go for it", new Goal [] {
163 new TeamGoal( "sub", "go for it" )
166 team.addPerformer( ralph );
167 team.performGoal( new BDIGoal( "go for it" ), "TOP", new Data() );
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
178 static public int planCode(ContextualGoal g) {
179 if ( g.context == null ) {
182 Vector<Ref> cg = (Vector<Ref>) g.context;
183 if ( cg == null || cg.size() == 0 )
185 Object x = Ref.deref( cg.get( 0 ) );
186 if ( ! ( x instanceof String ) )
188 String s = (String) x;
189 if ( s.length() == 0 )
191 return (int) s.charAt( 0 );