BusinessProcesses.java
001 /*
002  * Created on Aug 29, 2007
003  *
004  */
005 package com.x8ing.mc.bp;
006 
007 import java.io.Serializable;
008 
009 import com.x8ing.lsm4j.state.MaximumIterationsReachedException;
010 import com.x8ing.lsm4j.state.NoMatchingTransitionConditionFoundException;
011 import com.x8ing.lsm4j.state.ProcessableGraph;
012 import com.x8ing.lsm4j.state.ProcessableState;
013 import com.x8ing.lsm4j.std.TrueCondition;
014 import com.x8ing.mc.Configuration;
015 import com.x8ing.mc.bp.develop.AddBugToFixListAction;
016 import com.x8ing.mc.bp.develop.AnalyzeCostLossAction;
017 import com.x8ing.mc.bp.develop.BugFixedCondition;
018 import com.x8ing.mc.bp.develop.CostLossCheckCondition;
019 import com.x8ing.mc.bp.develop.DeployAndReleaseAction;
020 import com.x8ing.mc.bp.develop.DeployAndReleaseCondition;
021 import com.x8ing.mc.bp.develop.DocumentAndFinishProcessAction;
022 import com.x8ing.mc.bp.develop.EndProcessAction;
023 import com.x8ing.mc.bp.develop.FixAllKnownBugsAction;
024 import com.x8ing.mc.bp.develop.TestingAction;
025 import com.x8ing.mc.bp.develop.TestingPassedCondition;
026 import com.x8ing.mc.bp.operation.OperationDownAction;
027 import com.x8ing.mc.bp.operation.OperationRunningDependentCondition;
028 import com.x8ing.mc.bp.operation.OperationRunsAction;
029 
030 /**
031  @author Patrick Heusser
032  */
033 public class BusinessProcesses implements Serializable {
034 
035   private static final long serialVersionUID = 4384932097601999397L;
036 
037   private Configuration configuration = null;
038 
039   private ProcessableGraph bpOperation = null;
040 
041   private ProcessableGraph bpChange = null;
042 
043   static final int bpStartID = 1;
044 
045   public BusinessProcesses(Configuration configuration) {
046     this.configuration = configuration;
047     init();
048   }
049 
050   private void init() {
051     setUpProcessGraph();
052   }
053 
054   private void setUpProcessGraph() {
055 
056     int uidCounter = 100;
057 
058     // scope: set up operation process
059     {
060 
061       bpOperation = new ProcessableGraph();
062 
063       // //////////////////////////////////////////////////////////////////////////////
064       // create actions
065       OperationRunsAction operationRunsAction = new OperationRunsAction();
066       OperationDownAction operationDownAction = new OperationDownAction();
067 
068       // //////////////////////////////////////////////////////////////////////////////
069       // create states
070       ProcessableState operationRuns = new ProcessableState(bpStartID, "operationRuns", operationRunsAction, false);
071       ProcessableState operationDown = new ProcessableState(uidCounter++, "operationRuns", operationDownAction, false);
072 
073       // //////////////////////////////////////////////////////////////////////////////
074       // create transitions
075       bpOperation.addValidTransition(operationRuns, operationRuns, new OperationRunningDependentCondition(true));
076       bpOperation.addValidTransition(operationRuns, operationDown, new OperationRunningDependentCondition(false));
077       bpOperation.addValidTransition(operationDown, operationDown, new OperationRunningDependentCondition(false));
078       bpOperation.addValidTransition(operationDown, operationRuns, new OperationRunningDependentCondition(true));
079     }
080 
081     // scope: set up change process
082     {
083       bpChange = new ProcessableGraph();
084 
085       // //////////////////////////////////////////////////////////////////////////////
086       // create actions
087       AnalyzeCostLossAction analyzeCostLossAction = new AnalyzeCostLossAction();
088       AddBugToFixListAction addBugToFixListAction = new AddBugToFixListAction();
089       FixAllKnownBugsAction fixAllKnownBugsAction = new FixAllKnownBugsAction();
090       DeployAndReleaseAction deployAndReleaseAction = new DeployAndReleaseAction();
091       DocumentAndFinishProcessAction documentAndFinishProcessAction = new DocumentAndFinishProcessAction();
092       TestingAction testingAction = new TestingAction();
093       EndProcessAction endProcessAction = new EndProcessAction();
094 
095       // //////////////////////////////////////////////////////////////////////////////
096       // create states
097       ProcessableState analyzeCostLoss = new ProcessableState(bpStartID, "analyzeCostLoss", analyzeCostLossAction, false);
098       ProcessableState addBugToFixList = new ProcessableState(uidCounter++, "addBugToFixList", addBugToFixListAction, false);
099       ProcessableState fixAllKnownBugs = new ProcessableState(uidCounter++, "fixAllKnownBugs", fixAllKnownBugsAction, false);
100       ProcessableState testing = new ProcessableState(uidCounter++, "testing", testingAction, false);
101       ProcessableState deployAndRelease = new ProcessableState(uidCounter++, "deployAndRelease", deployAndReleaseAction,
102           false);
103       ProcessableState documentAndFinishProcess = new ProcessableState(uidCounter++, "documentAndFinishProcess",
104           documentAndFinishProcessAction, false);
105       ProcessableState endState = new ProcessableState(uidCounter++, "end", endProcessAction, false);
106 
107       // //////////////////////////////////////////////////////////////////////////////
108       // create transitions
109       bpChange.addValidTransition(analyzeCostLoss, addBugToFixList, new CostLossCheckCondition(true, configuration
110           .getProcessCostLossTriggerForBugFixing()));
111       bpChange.addValidTransition(analyzeCostLoss, fixAllKnownBugs, new CostLossCheckCondition(false, configuration
112           .getProcessCostLossTriggerForBugFixing()));
113 
114       bpChange.addValidTransition(fixAllKnownBugs, testing, new BugFixedCondition(false));
115       bpChange.addValidTransition(fixAllKnownBugs, fixAllKnownBugs, new BugFixedCondition(true));
116 
117       bpChange.addValidTransition(testing, deployAndRelease, new TestingPassedCondition(false));
118       bpChange.addValidTransition(testing, fixAllKnownBugs, new TestingPassedCondition(true));
119 
120       bpChange.addValidTransition(deployAndRelease, deployAndRelease, new DeployAndReleaseCondition(true));
121       bpChange.addValidTransition(deployAndRelease, documentAndFinishProcess, new DeployAndReleaseCondition(false));
122 
123       bpChange.addValidTransition(addBugToFixList, documentAndFinishProcess, new TrueCondition());
124 
125       bpChange.addValidTransition(documentAndFinishProcess, endState, new TrueCondition())// end
126 
127     }
128 
129   }
130 
131   public BusinessContext run() {
132 
133     BusinessContext businessContext = new BusinessContext(configuration);
134 
135     try {
136 
137       // init both processes
138       bpOperation.run(bpStartID, businessContext, 1);
139 
140       for (int i = 0; i < configuration.getNumberOfSimulationDays(); i++) {
141 
142         // let the operation run for one step
143         bpOperation.runContinue(1);
144 
145         // check for new bugs
146         int queuedBugs = businessContext.getQueuedBugs().size();
147         boolean isDevelopProcessBusy = businessContext.isDevelopProcessBusy();
148 
149         if (isDevelopProcessBusy) {
150 
151           // let developers do their work
152           businessContext.getLogBook().addLogEntry(businessContext.getCurrentDate(),
153               "developers are still busy, let them work.");
154 
155           bpChange.runContinue(1);
156 
157         else {
158 
159           // develop department has free capacity, check for any queued bugs
160           if (queuedBugs > 0) {
161 
162             businessContext.getLogBook().addLogEntry(
163                 businessContext.getCurrentDate(),
164                 "we have " + queuedBugs
165                     " bugs in queue. develop department has free capacity, we give analyzeCostLoss order.");
166 
167             // transfer bugs to develop process
168             businessContext.transferQueuedBugsToDevelopBugList();
169 
170             // start parallel change-process
171             bpChange.run(bpStartID, businessContext, 1);
172           }
173 
174         }
175 
176       }
177 
178     catch (NoMatchingTransitionConditionFoundException e) {
179       e.printStackTrace();
180     catch (MaximumIterationsReachedException e) {
181       e.printStackTrace();
182     }
183 
184     return businessContext;
185 
186   }
187 
188 }