1
2
3
4
5
6
7
8 package net.kwfgrid.gwes;
9
10 import net.kwfgrid.gworkflowdl.structure.*;
11 import net.kwfgrid.gwes.util.StringUtils;
12
13 import java.util.ArrayList;
14
15
16
17
18
19
20 public class TokenUtils {
21
22 private static TokenUtils _instance;
23 public final static int DATA_GROUP_IGNORE_READ_WRITE = 1;
24 public final static int DATA_GROUP_IGNORE_CONTROL = 2;
25
26 private TokenUtils() {
27
28 }
29
30
31
32
33 public static TokenUtils getInstance() {
34 if (_instance == null) {
35 _instance = new TokenUtils();
36 }
37 return _instance;
38 }
39
40
41
42
43
44
45
46 public static Token getNextUnlockedToken(Edge edge, boolean isReadEdge, String dataGroup, int ignoreDataGroups) {
47 for (Token token : edge.getPlace().getTokens()) {
48 if (!token.isLocked() && checkDataGroup(token, isReadEdge, dataGroup, ignoreDataGroups)) {
49 return token;
50 }
51 }
52 return null;
53 }
54
55
56
57
58
59
60
61 public static int getNextUnlockedTokenIndex(Edge edge, boolean isReadEdge, String dataGroup, int ignoreDataGroups) {
62 Token[] tokens = edge.getPlace().getTokens();
63 for (int i = 0; i<tokens.length; i++) {
64 if (!tokens[i].isLocked() && checkDataGroup(tokens[i], isReadEdge, dataGroup, ignoreDataGroups)) {
65 return i;
66 }
67 }
68 return -1;
69 }
70
71
72
73
74
75
76
77
78
79 public static boolean checkDataGroup(Token token, boolean isReadWriteToken, String dataGroup, int ignoreDataGroup) {
80 if (dataGroup == null) return true;
81 if ( ((ignoreDataGroup& DATA_GROUP_IGNORE_READ_WRITE)!=0) && isReadWriteToken) return true;
82 if ( ((ignoreDataGroup&DATA_GROUP_IGNORE_CONTROL)!=0) && token.getData() == null) return true;
83 String tDataGroup = token.getProperties().get(Constants.PROP_TOKEN_DATA_GROUP);
84 return tDataGroup == null || tDataGroup.equals(dataGroup);
85 }
86
87 public static String updateDataGroup(TokenParameter tp, int ignoreDataGroups, String[] combineDataGroupsEdgeExpressions, ArrayList<String> dataGroups) {
88
89 if (((ignoreDataGroups& DATA_GROUP_IGNORE_READ_WRITE)!=0) && (tp.scope==TokenParameter.Scope.READ || tp.scope == TokenParameter.Scope.WRITE) ) return null;
90
91 if ( ((ignoreDataGroups&DATA_GROUP_IGNORE_CONTROL)!=0) && tp.token.getData() == null) return null;
92
93 String dataGroup = null;
94 String tokenDataGroup = tp.token.getProperties().get(Constants.PROP_TOKEN_DATA_GROUP);
95
96 if (combineDataGroupsEdgeExpressions != null) {
97
98 for (String edgeExpression : combineDataGroupsEdgeExpressions) {
99 if (edgeExpression.equals(tp.edge.getExpression())) {
100 dataGroups.add(tokenDataGroup);
101 return null;
102 }
103 }
104 } else {
105
106 dataGroup = tokenDataGroup;
107 }
108 return dataGroup;
109 }
110
111 public static int getIgnoreDataGroupsCode(Transition transition) {
112 String str = transition.getProperties().get(Constants.PROP_TRANSITION_IGNORE_DATA_GROUPS);
113 if (str == null) return 0;
114 int code = 0;
115 if (str.contains("read") || str.contains("write")) code += DATA_GROUP_IGNORE_READ_WRITE;
116 if (str.contains("control")) code +=DATA_GROUP_IGNORE_CONTROL;
117 return code;
118 }
119
120 public static String[] getCombineDataGroupsEdgeExpressions(Transition transition) {
121 String str = transition.getProperties().get(Constants.PROP_TRANSITION_COMBINE_DATA_GROUPS);
122 if (str == null) return null;
123 return StringUtils.splitSpacesAndMore(str);
124 }
125
126 }