Parent: [0181a5] (diff)

Child: [094214] (diff)

Download this file

BPMNManager.java    236 lines (189 with data), 7.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package converter;
import bpcf.*;
import bpcf.impl.EndImpl;
import bpcf.impl.StartImpl;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.UUID;
/**
* Created by miguel on 06-05-2014.
*/
public class BPMNManager implements Manager {
private BpcfFactory factory;
private BPCtrlFlowModel bpCtrlFlowModel;
private HashMap<String, Object> elements;
private long numberOfElements = 1;
int n = 0;
public BPMNManager() {
factory = BpcfFactory.eINSTANCE;
bpCtrlFlowModel = factory.createBPCtrlFlowModel();
elements = new HashMap<String, Object>();
}
public void addActivity(String id, String name) {
System.out.println(".. Adding Activity " + name);
Activity activity = factory.createActivity();
activity.setId(numberOfElements);
numberOfElements++;
activity.setName(name);
bpCtrlFlowModel.getBpElements().add(activity);
elements.put(id, activity);
}
public void addFlow(BPElement one, BPElement two) {
one.getTarget().add(two);
two.getSource().add(one);
}
public void addFlow(String idFrom, String idTo) {
if (n < 4) {
n++;
}
if (elements.containsKey(idFrom) && elements.containsKey(idTo)) {
System.out.println(".. Adding flow from " + ((BPElement) elements.get(idFrom)).getName() + " to " + ((BPElement) elements.get(idTo)).getName());
addFlow((BPElement) elements.get(idFrom), (BPElement) elements.get(idTo));
}
}
public void addMerge(String id) {
addMerge(id, id);
}
public void addMerge(String id, String name) {
System.out.println(".. Adding Merge Element " + name);
Merge merge = factory.createMerge();
merge.setId(numberOfElements);
numberOfElements++;
merge.setName(name);
bpCtrlFlowModel.getBpElements().add(merge);
elements.put(id, merge);
}
public void addFork(String id) {
addFork(id, id);
}
public void addFork(String id, String name) {
System.out.println(".. Adding Fork Element " + name);
Fork fork = factory.createFork();
fork.setId(numberOfElements);
numberOfElements++;
fork.setName(name);
bpCtrlFlowModel.getBpElements().add(fork);
elements.put(id, fork);
}
public void addJoin(String id) {
addJoin(id, id);
}
public void addJoin(String id, String name) {
System.out.println(".. Adding Join Element " + name);
Join join = factory.createJoin();
join.setId(numberOfElements);
numberOfElements++;
join.setName(name);
bpCtrlFlowModel.getBpElements().add(join);
elements.put(id, join);
}
public void addDecision(String id) {
addDecision(id, id);
}
public void addDecision(String id, String name) {
System.out.println(".. Adding Decision Element " + name);
Decision decision = factory.createDecision();
decision.setId(numberOfElements);
numberOfElements++;
decision.setName(name);
bpCtrlFlowModel.getBpElements().add(decision);
elements.put(id, decision);
}
public void addStart(String id) {
addStart(id, "Start");
}
public void addStart(String id, String name) {
System.out.println(".. Adding Start Element " + name);
Start start = factory.createStart();
start.setName(name);
start.setId(numberOfElements);
numberOfElements++;
elements.put(id, start);
bpCtrlFlowModel.getBpElements().add(start);
bpCtrlFlowModel.getStartEvents().add(start);
// start.getTarget().add(elements.get(flowToId));
// elements.get(flowToId).getSource().add(start);
}
public void addArtificialStart(String flowToId) {
if (elements.containsKey(flowToId) && elements.get(flowToId).getClass() != StartImpl.class) {
System.out.println(".. Adding Infered Start Element flowing to " + ((BPElement) elements.get(flowToId)).getName());
String uuid = UUID.randomUUID().toString();
addStart(uuid, "START");
addFlow((BPElement) elements.get(uuid), (BPElement) elements.get(flowToId));
}
}
public void addFinish(String id) {
addFinish(id, "End");
}
public void addFinish(String id, String name) {
System.out.println(".. Adding Finish Element " + name);
// System.out.println("ADDING FINISH: " + id + " --> " + name);
End end = factory.createEnd();
end.setId(numberOfElements);
end.setName(name);
numberOfElements++;
elements.put(id, end);
bpCtrlFlowModel.getBpElements().add(end);
// elements.get(flowFrom).getTarget().add(end);
// end.getSource().add(elements.get(flowFrom));
}
public void addArtificialFinish(String flowFromId) {
if (elements.containsKey(flowFromId) && elements.get(flowFromId).getClass() != EndImpl.class) {
System.out.println(".. Adding Infered End Element flowing from " + ((BPElement) elements.get(flowFromId)).getName());
String uuid = UUID.randomUUID().toString();
addFinish(uuid, "END");
addFlow((BPElement) elements.get(flowFromId), (BPElement) elements.get(uuid));
}
}
public void saveModel(String path) {
System.out.println("Saving model in " + path + "...");
/*for (String a : elements.keySet())
System.out.println(a);*/
ResourceSet resSet = new ResourceSetImpl();
resSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
Resource.Factory.Registry.DEFAULT_EXTENSION, new org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl());
String newFileString = new File(path).getAbsolutePath();
// System.out.println(newFileString);
URI fileURI = URI.createFileURI(newFileString);
Resource resource = resSet.createResource(fileURI);
resource.getContents().add(bpCtrlFlowModel);
try {
resource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
e.printStackTrace();
}
}
public HashMap<String, Object> getElements() {
return elements;
}
public static void main(String[] args) {
BpcfFactory factory = BpcfFactory.eINSTANCE;
BPCtrlFlowModel bpCtrlFlowModel = factory.createBPCtrlFlowModel();
// bpCtrlFlowModel.getBpElements().add(start);
BPElement start = factory.createStart();
Start start1 = factory.createStart();
// System.out.println(StartImpl.class);
// bpCtrlFlowModel.getStartEvents().add(start);
}
private static void saveEMFModel(EObject model, String filepath) {
ResourceSet resSet = new ResourceSetImpl();
resSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
Resource.Factory.Registry.DEFAULT_EXTENSION, new org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl());
String newFileString = new File(filepath).getAbsolutePath();
URI fileURI = URI.createFileURI(newFileString);
Resource resource = resSet.createResource(fileURI);
resource.getContents().add(model);
try {
resource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
e.printStackTrace();
}
}
}