1 package cz.cuni.amis.pogamut.sposh.elements;
2
3 import cz.cuni.amis.pogamut.sposh.engine.VariableContext;
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.FubarException;
7 import cz.cuni.amis.pogamut.sposh.exceptions.InvalidNameException;
8 import cz.cuni.amis.pogamut.sposh.executor.IWorkExecutor;
9 import java.util.List;
10 import java.awt.datatransfer.DataFlavor;
11 import java.util.Collections;
12 import java.util.LinkedList;
13 import java.util.logging.Level;
14 import java.util.logging.Logger;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 public class TriggeredAction extends NamedLapElement {
34
35 private PrimitiveCall actionCall;
36 public static final DataFlavor dataFlavor = new DataFlavor(TriggeredAction.class, "triggered_action");
37 public static final String taName = "taName";
38 public static final String taArgs = "taAruments";
39
40 TriggeredAction(String actionName) {
41 this.actionCall = new PrimitiveCall(actionName);
42 }
43
44 TriggeredAction(PrimitiveCall actionCall) {
45 this.actionCall = actionCall;
46 }
47
48 @Override
49 public List<PoshElement> getChildDataNodes() {
50 return Collections.<PoshElement>emptyList();
51 }
52
53 @Override
54 public String toString() {
55 return actionCall.toString();
56 }
57
58 @Override
59 public String getName() {
60 return actionCall.getName();
61 }
62
63 public PrimitiveCall getActionCall() {
64 return actionCall;
65 }
66
67
68
69
70
71
72
73
74 public void setActionName(String newName) throws InvalidNameException, CycleException {
75 newName = newName.trim();
76
77 if (!newName.matches(IDENT_PATTERN)) {
78 throw InvalidNameException.create(newName);
79 }
80
81 String oldName = this.getName();
82 this.actionCall = new PrimitiveCall(newName, actionCall.getParameters());
83
84 if (getRootNode() != null && getRootNode().isCycled()) {
85 this.actionCall = new PrimitiveCall(oldName, actionCall.getParameters());
86 throw CycleException.createFromName(newName);
87 }
88 firePropertyChange(taName, oldName, newName);
89 }
90
91 @Override
92 public boolean moveChild(int newIndex, PoshElement child) {
93 throw new UnsupportedOperationException();
94 }
95
96 @Override
97 public DataFlavor getDataFlavor() {
98 return dataFlavor;
99 }
100
101 @Override
102 public LapType getType() {
103 return LapType.ACTION;
104 }
105
106
107
108
109
110
111
112 public void synchronize(TriggeredAction source) throws CycleException {
113 try {
114 this.setActionName(source.getName());
115 Arguments sourceArguments = source.getActionCall().getParameters();
116 setArguments(new Arguments(sourceArguments));
117 } catch (InvalidNameException ex) {
118 throw new FubarException("Name of the source action \"" + source.getName() + "\" is not valid for this action.", ex);
119 }
120 }
121
122
123
124
125
126
127
128 private FormalParameters getElementParameters(PoshElement element) {
129 assert element != null;
130
131 if (element instanceof DriveElement) {
132 return new FormalParameters();
133 }
134 if (element instanceof CompetenceElement) {
135 CompetenceElement cel = (CompetenceElement) element;
136 Competence competence = cel.getParent();
137 return competence.getParameters();
138 }
139 if (element instanceof ActionPattern) {
140 ActionPattern ap = (ActionPattern) element;
141 return ap.getParameters();
142 }
143 return null;
144 }
145
146
147
148
149 public void setArguments(Arguments newArguments) {
150 String actionName = actionCall.getName();
151 Arguments oldArguments = actionCall.getParameters();
152 this.actionCall = new PrimitiveCall(actionName, newArguments);
153
154
155
156 firePropertyChange(taArgs, oldArguments, newArguments);
157 }
158
159
160
161
162
163
164 public Arguments getArguments() {
165 return this.actionCall.getParameters();
166 }
167 }