View Javadoc

1   package cz.cuni.amis.pogamut.sposh.elements;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.awt.datatransfer.DataFlavor;
6   import java.util.Collections;
7   
8   /**
9    * Goal contains list of Sense. Goal is part of reactive plans that mark when is
10   * plan done its job (all senses of goal has to be fulfilled).
11   * 
12   * @author Honza Havlicek
13   */
14  public final class Goal extends PoshDummyElement {
15      // XXX: more TLC only for use in DC
16      static Goal createFail() {
17          return new Goal(new Sense("fail"));
18      }
19  
20      static Goal createEmpty() {
21          return new Goal(new ElementList<Sense>());
22      }
23      
24  	private ElementList<Sense> _sense_list;
25  
26  	/**
27  	 * Create a new goal with no senses.
28  	 */
29  	public Goal(Sense sense) {
30                  sense.setParent(this);
31  
32                  _sense_list = new ElementList<Sense>();
33                  _sense_list.add(sense);
34  	}
35  
36  	/**
37  	 * Create a new goal with passed senses.
38  	 *
39  	 * @param apList list of senses of goal, not null
40  	 */
41  	public Goal(ElementList<Sense> sense_list) {
42                  for (Sense sense : sense_list) {
43                      sense.setParent(this);
44                  }
45  		_sense_list = sense_list;
46          }
47  
48  	/**
49  	 * Add another sense to goal.
50  	 * @param poshAct sense that will be added.
51  	 */
52  	public void addSense(Sense sense) {
53  		sense.setParent(this);
54  		_sense_list.add(sense);
55  
56                  emitChildNode(sense);
57  	}
58  
59      /**
60       * If the only sense in the goal is either fail or succeed, replace it with
61       * passed sense.
62       * <p>
63       * Otherwise same as {@link Goal.addSense()}.
64       */
65      public void addUserSense(Sense sense) {
66          if (_sense_list.size() == 1 && ("succeed".equals(_sense_list.get(0).getSenseName()) || "fail".equals(_sense_list.get(0).getSenseName()))) {
67              _sense_list.get(0).changeTo(sense);
68          } else {
69              addSense(sense);
70          }
71      }
72  
73  	@Override
74  	public String toString() {
75  		return "(goal " + _sense_list.toString()+")";
76  	}
77  
78  	@Override
79  	public List<PoshElement> getChildDataNodes() {
80  		return new ArrayList<PoshElement>(_sense_list);
81  	}
82  
83  	@Override
84  	public boolean moveChild(PoshElement child, int relativePosition) {
85  		return moveNodeInList(this._sense_list, child, relativePosition);
86  	}
87  
88  	public static final DataFlavor dataFlavor = new DataFlavor(Goal.class, "goal"); 
89  	
90  	@Override
91  	public DataFlavor getDataFlavor() {
92  		return dataFlavor;
93  	}
94  
95  	@Override
96  	public void addChildDataNode(PoshElement newChild) {
97  		if (newChild instanceof Sense) {
98  			this.addSense((Sense) newChild);
99  		} else { 
100 			throw new RuntimeException("Class " + newChild.getClass().getSimpleName() + " not accepted.");
101 		}
102 	}
103 
104 	@Override
105 	public void neutralizeChild(PoshElement childNode) {
106 		if (this._sense_list.contains(childNode)) {
107 			if (this._sense_list.size() <= 1) {
108 				this.addSense(new Sense("fail"));
109 			}
110 			this._sense_list.remove(childNode);
111 			childNode.remove();
112 		}
113 	}
114 
115         /**
116          * Get unmodifiable list of all senses in the goal.
117          */
118         public synchronized List<Sense> getSenses() {
119             return Collections.unmodifiableList(this._sense_list);
120         }
121 }