View Javadoc

1   package net.kwfgrid.gworkflowdl.analysis;
2   
3   import net.kwfgrid.gworkflowdl.structure.*;
4   
5   import java.util.Hashtable;
6   
7   /***
8    * Created by IntelliJ IDEA.
9    * User: hans
10   * Date: 21.10.2005
11   * Time: 11:24:57
12   * To change this template use File | Settings | File Templates.
13   */
14  public class ComplexityReducer {
15      public Workflow workflow;
16  
17      public Hashtable rememberPlaces;
18      public Hashtable rememberPlacesBack;
19      public Hashtable rememberTransitions;
20  
21  
22      public ComplexityReducer(Workflow wf) {
23          workflow = wf;
24          rememberPlaces = new Hashtable();
25          rememberPlacesBack = new Hashtable();
26          rememberTransitions = new Hashtable();
27      }
28  
29      public boolean reduceStep() {
30          boolean success = false;
31  
32          Place pin = null;
33          Place pout = null;
34          int tIndex = -1;
35          int pIndex = -1;
36  
37          boolean loop = false;
38  
39          Transition[] ts = workflow.getTransitions();
40          for (int i = 0; i < ts.length; i++) {
41              Transition t = ts[i];
42              Edge[] ins = t.getInEdges();
43              Edge[] outs = t.getOutEdges();
44              if (ins.length == 1 && outs.length == 1) {
45                  // found transition to reduce
46                  pin = ins[0].getPlace();
47                  pout = outs[0].getPlace();
48                  if (pin.getCapacity() != Place.INFINITY || pout.getCapacity() != Place.INFINITY) {
49                      continue;
50                  }
51                  if (pin == pout) {
52                      loop = true;
53                  }
54                  tIndex = i;
55  
56                  pIndex = workflow.getPlaceIndex(pin.getID());
57  
58                  // is pin input of other transitions
59                  boolean noOtherInput = true;
60                  for (int j = 0; j < ts.length; j++) {
61                      if (j == i) {
62                          continue;
63                      }
64                      Edge[] insj = ts[j].getInEdges();
65                      for (int k = 0; k < insj.length; k++) {
66                          if (insj[k].getPlace() == pin) {
67                              noOtherInput = false;
68                              break;
69                          }
70                      }
71                      if (!noOtherInput) {
72                          break;
73                      }
74                  }
75                  if (!noOtherInput) {
76                      continue;
77                  }
78  
79                  // is pout output of other transitions
80                  boolean noOtherOutput = true;
81                  for (int j = 0; j < ts.length; j++) {
82                      if (j == i) {
83                          continue;
84                      }
85                      Edge[] outsj = ts[j].getOutEdges();
86                      for (int k = 0; k < outsj.length; k++) {
87                          if (outsj[k].getPlace() == pout) {
88                              noOtherOutput = false;
89                              break;
90                          }
91                      }
92                      if (!noOtherOutput) {
93                          break;
94                      }
95                  }
96                  if (!noOtherOutput) {
97                      continue;
98                  }
99  
100                 success = true;
101                 break;
102             }
103         }
104         if (!success) {
105             return false;
106         }
107 
108         try {
109 
110             int tokenNumber = pin.getTokenNumber();
111             for (int n = 0; n < tokenNumber; n++) {
112                 pout.addToken(Factory.newToken());
113             }
114         } catch (CapacityException e) {
115             System.out.println("can not be");
116         }
117         // replace in workflow pin by pout
118         if (!loop) {
119             for (int i = 0; i < ts.length; i++) {
120                 Transition t = ts[i];
121                 Edge[] ins = t.getInEdges();
122                 Edge[] outs = t.getOutEdges();
123                 for (int j = 0; j < ins.length; j++) {
124                     if (ins[j].getPlace().getID().equals(pin.getID())) {
125                         ins[j].setPlace(pout);
126                     }
127                 }
128                 for (int j = 0; j < outs.length; j++) {
129                     if (outs[j].getPlace().getID().equals(pin.getID())) {
130                         outs[j].setPlace(pout);
131                     }
132                 }
133                 t.setInEdges(ins);
134                 t.setOutEdges(outs);
135             }
136         }
137         workflow.removeTransition(tIndex);
138         rememberTransitions.put(ts[tIndex].getID(), pout.getID());
139 
140         if (!loop) {
141             workflow.removePlace(pIndex);
142             rememberPlaces.put(pin.getID(), pout.getID());
143             rememberPlacesBack.put(pout.getID(), pin.getID());
144 
145         }
146 
147         return true;
148     }
149 
150     public void reduce() {
151         while (reduceStep()) {
152         }
153     }
154 }