1 /*********************************************************************
2 Copyright 2012, Ralph Ronnquist.
4 This file is part of GORITE.
6 GORITE is free software: you can redistribute it and/or modify it
7 under the terms of the Lesser GNU General Public License as published
8 by the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GORITE is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the Lesser GNU General Public
17 License along with GORITE. If not, see <http://www.gnu.org/licenses/>.
18 **********************************************************************/
20 package com.intendico.gorite;
22 import com.intendico.data.Query;
25 * The TransferGoal class is utility class for wrapping a goal that is
26 * transferred into a different target performer context.
28 public class TransferGoal extends Plan {
31 * Holds the target performer.
33 public Performer target;
36 * Holds the transferred goal.
43 public TransferGoal(Performer to,Goal g) {
44 super( g.getGoalName() );
50 * Constructor without Performer, which instead is looked up
51 * at instantiation using the control string.
53 public TransferGoal(Goal g) {
54 super( g.getGoalName() );
60 * Overrides the base class method {@link Goal#instantiate} to
61 * provide a wrapper instance for the transferred goal.
63 public Instance instantiate(String h,Data d) {
64 return new TransferInstance( h, d );
68 * Transfer goals are equals by the goals they transfer.
70 public boolean equals(Object x) {
71 return x instanceof TransferGoal && equals( (TransferGoal) x );
75 * Transfer goals are equals by the goals they transfer.
77 public boolean equals(TransferGoal x) {
78 return goal.equals( x.goal );
82 * Wrapper class for transfer goal instances.
84 public class TransferInstance extends Instance {
87 * Holds the instance of the transferred goal.
89 public Instance instance;
91 public Performer into;
93 * Constructor. Constructs an instance of the transferred
94 * goal while temporarily shifting into the target
97 public TransferInstance(String h,Data d) {
99 instance = goal.instantiate( h + "|", d );
103 * Control callback whereby the intention gets notified that
104 * it is cancelled. This will forward the cancellation to the
105 * currently progressing instance, if any.
107 public void cancel() {
108 if ( instance != null )
113 * Implements the instance action, which sets the target
114 * performer as {@link Goal#PERFORMER}, then invokes the
115 * target instance, and thereafter restores the performer.
117 public States action(String h,Data d)
118 throws LoopEndException, ParallelEndException {
119 Performer from = (Performer) d.getValue( PERFORMER );
121 if ( target == null ) {
122 into = (Performer) d.getValue( getGoalControl() );
126 // Note the use of setValue() rather than
127 // replaceValue() below. This is done to ensure
128 // that PERFORMER is set in the local data
129 // context, without replacing PERFORMER in the
132 // Also, the new PERFORMER is pushed on top of
133 // any existing value, showing the chain of
134 // performers involved in the computation. Unless
135 // the data context is in "goldfish" mode of
137 d.setValue( PERFORMER, into );
139 System.err.println( ">> " + into );
140 return instance.perform( d );
142 // Note that restoreValue only affects the local
144 d.restoreValue( PERFORMER, from );
146 System.err.println( "<< " + into );
152 * Implements the {@link Context#context} method by forwarding to
155 public Query context(Data d) throws Exception {
156 if ( goal instanceof Context ) {
157 Capability from = (Capability) d.getValue( PERFORMER );
160 System.err.println( "~(context)> (" + target + ")" );
162 d.setValue( PERFORMER, target );
163 return ((Context) goal).context( d ) ;
166 System.err.println( "<(context)~ (" + target + ")" );
168 d.restoreValue( PERFORMER, from );
176 * Implements the {@link Precedence#precedence} method by
177 * forwarding to the wrapped goal.
179 public int precedence(Data d) {
180 if ( goal instanceof Precedence ) {
181 Capability from = (Capability) d.getValue( PERFORMER );
184 System.err.println( "~(precedence)> (" + target + ")" );
186 d.setValue( PERFORMER, target );
187 return ((Precedence) goal).precedence( d );
190 System.err.println( "<(precedence)~ (" + target + ")" );
192 d.replaceValue( PERFORMER, from );
195 return DEFAULT_PRECEDENCE ;