View Javadoc

1   package cz.cuni.amis.pogamut.sposh.elements;
2   
3   import cz.cuni.amis.pogamut.sposh.elements.Arguments.Argument;
4   import cz.cuni.amis.pogamut.sposh.exceptions.CycleException;
5   import cz.cuni.amis.pogamut.sposh.exceptions.DuplicateNameException;
6   import cz.cuni.amis.pogamut.sposh.exceptions.InvalidNameException;
7   import cz.cuni.amis.pogamut.sposh.exceptions.MissingParameterException;
8   import java.awt.datatransfer.DataFlavor;
9   import java.beans.PropertyChangeEvent;
10  import java.util.ArrayList;
11  import java.util.List;
12  
13  /**
14   * Adopt element is a stopgap. When engine requires to clear the stack, stack
15   * will be cleared only up to first adopt with failed exit condition.
16   *
17   * @author Jimmy
18   * @author Honza
19   */
20  public class Adopt extends PoshDummyElement<Adopt, PoshPlan> implements IParametrizedElement, IConditionElement<Adopt> {
21  
22      /**
23       * Data flavor of adopt class, used for drag-and-drop
24       */
25      public static final DataFlavor dataFlavor = new DataFlavor(Adopt.class, "adopt-node");
26      /**
27       * Property string of competence name
28       */
29      public static final String adName = "adName";
30      /**
31       * Property string used for {@link PropertyChangeEvent} when {@link FormalParameters}
32       * are changed.
33       */
34      public static final String adParams = "adParams";
35      private String name;
36      private FormalParameters parameters;
37      private Trigger<Adopt> exitCondition;
38      private final TriggeredAction adoptedElement;
39  
40      public Adopt(String name, FormalParameters parameters, List<Sense> exitCondition, PrimitiveCall adoptedElement) {
41          this.name = name;
42          this.parameters = parameters;
43  
44          this.exitCondition = new Trigger<Adopt>(this, exitCondition);
45  
46          this.adoptedElement = LapElementsFactory.createAction(adoptedElement);
47          this.adoptedElement.setParent(this);
48      }
49  
50      @Override
51      public FormalParameters getParameters() {
52          return parameters;
53      }
54  
55      @Override
56      public void setParameters(FormalParameters newParams) {
57          FormalParameters oldParams = this.parameters;
58          this.parameters = newParams;
59          firePropertyChange(adParams, oldParams, newParams);
60      }
61  
62      @Override
63      public String getName() {
64          return name;
65      }
66  
67      public void setName(String name) throws InvalidNameException, DuplicateNameException, CycleException {
68          PoshPlan plan = getRootNode();
69  
70          name = name.trim();
71  
72          if (!name.matches(IDENT_PATTERN)) {
73              throw new InvalidNameException("Name " + name + " is not valid.");
74          }
75  
76          // Check for duplicity
77          if (!this.name.equals(name)) {
78              if (plan != null && !plan.isUniqueNodeName(name)) {
79                  throw new DuplicateNameException("New name for adopt '" + this.name + "'(" + name + ") is not unique for reaction plan.");
80              }
81          }
82  
83          String oldName = this.name;
84          this.name = name;
85  
86          if (plan != null && plan.isCycled()) {
87              this.name = oldName;
88              throw new CycleException("New name (" + name + ") for adopt '" + this.name + "' is causing cycle.");
89          }
90          firePropertyChange(adName, oldName, name);
91      }
92  
93  
94      @Override
95      public void rename(String newName) throws InvalidNameException, CycleException, DuplicateNameException {
96          throw new UnsupportedOperationException("Not supported yet.");
97      }
98      
99      public void addCondition(Sense sense) {
100         exitCondition.add(sense);
101     }
102 
103     public Trigger<Adopt> getExitCondition() {
104         return exitCondition;
105     }
106 
107     /**
108      * Get exit condition of the adopt.
109      *
110      * @see #getExitCondition()
111      * @return Exit condition
112      */
113     @Override
114     public Trigger<Adopt> getCondition() {
115         return getExitCondition();
116     }
117 
118     public TriggeredAction getAdoptedElement() {
119         return adoptedElement;
120     }
121 
122     @Override
123     public DataFlavor getDataFlavor() {
124         return dataFlavor;
125     }
126 
127     @Override
128     public LapType getType() {
129         return LapType.ADOPT;
130     }
131 
132     @Override
133     public List<? extends PoshElement> getChildDataNodes() {
134         List<PoshElement> children = new ArrayList<PoshElement>();
135         children.addAll(exitCondition);
136         children.add(this.adoptedElement);
137 
138         return children;
139     }
140 
141     @Override
142     public boolean moveChild(int newIndex, PoshElement child) {
143         throw new UnsupportedOperationException("Moving Adopt doesn't make sense.");
144     }
145 }