View Javadoc

1   /*
2    * Copyright 2010 Fraunhofer Gesellschaft, Munich, Germany,
3    * for its Fraunhofer Institute for Computer Architecture and Software
4    * Technology (FIRST), Berlin, Germany. All rights reserved.
5    * http://www.first.fraunhofer.de/
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   * @author Andreas Hoheisel
17   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
18   * @version $Id: TokenUtils.java 1433 2010-11-29 18:06:07Z hoheisel $
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          // private constructor because of singleton pattern.
28      }
29  
30      /**
31       * Singleton pattern
32       */
33      public static TokenUtils getInstance() {
34          if (_instance == null) {
35              _instance = new TokenUtils();
36          }
37          return _instance;
38      }
39  
40      /**
41       * Search next token of a certain dataGroup which is not locked.
42       *
43       * @param edge
44       * @return The next unlocked token or <code>null</code> if there is no unlocked token.
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       * Get the index of the next token of a certain dataGroup which is not locked
57       * @param edge
58       * @param dataGroup
59       * @return The index of the next unlocked token or <code>-1</code> if there is no unlocked token.
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       * Check if the token belongs to a certain data group.
73       *
74       * @param token
75       * @param dataGroup
76       * @return "true" iff the dataGroup is null or if there is no data.group property or if the data.group property
77       *         equals the dataGroup string.
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          // check if read or write token which should be ignored
89          if (((ignoreDataGroups& DATA_GROUP_IGNORE_READ_WRITE)!=0) && (tp.scope==TokenParameter.Scope.READ || tp.scope == TokenParameter.Scope.WRITE) ) return null;
90          // check if control token which should be ignored
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              // combine data groups
98              for (String edgeExpression : combineDataGroupsEdgeExpressions) {
99                  if (edgeExpression.equals(tp.edge.getExpression())) {
100                     dataGroups.add(tokenDataGroup);
101                     return null;
102                 }
103             }
104         } else {
105             // single data group
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 }