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