1 package Analysis.Scheduling;
3 import java.util.Iterator;
4 import java.util.LinkedList;
5 import java.util.Queue;
6 import java.util.Vector;
8 import Analysis.TaskStateAnalysis.FEdge;
9 import Analysis.TaskStateAnalysis.FlagState;
10 import Analysis.TaskStateAnalysis.FEdge.NewObjInfo;
11 import IR.ClassDescriptor;
12 import IR.TaskDescriptor;
13 import IR.VarDescriptor;
14 import IR.Tree.FlagExpressionNode;
16 public class TaskSimulator {
18 Vector<Queue<ObjectSimulator>> paraQueues;
23 public class ExeResult {
25 Vector<ObjectSimulator> newObjs;
32 public int getFinishTime() {
36 public void setFinishTime(int finishTime) {
37 this.finishTime = finishTime;
40 public Vector<ObjectSimulator> getNewObjs() {
44 public void addNewObj(ObjectSimulator newObj) {
45 if(this.newObjs == null) {
46 this.newObjs = new Vector<ObjectSimulator>();
49 this.newObjs.add(newObj);
54 public TaskSimulator(TaskDescriptor td, CoreSimulator cs) {
57 this.paraQueues = null;
58 this.currentRun = null;
63 public CoreSimulator getCs() {
67 public TaskDescriptor getTd() {
71 public ExeResult getCurrentRun() {
75 public Vector<Queue<ObjectSimulator>> getParaQueues() {
79 public void enquePara(ObjectSimulator obj) {
80 ClassDescriptor cd = obj.getCd();
81 int paraNum = td.numParameters();
82 for(int i = 0; i < paraNum; i++) {
83 VarDescriptor para = td.getParameter(i);
84 if(cd.equals(para.getType().getClassDesc())) {
85 // check if the status is right
86 FlagExpressionNode fen = td.getFlag(para);
87 if(SchedulingUtil.isTaskTrigger_flag(fen, obj.getCurrentFS())) {
88 if(this.paraQueues == null) {
89 this.paraQueues = new Vector<Queue<ObjectSimulator>>();
90 for(int j = 0; j < paraNum; j++) {
91 this.paraQueues.add(null);
94 if(this.paraQueues.elementAt(i) == null) {
95 this.paraQueues.setElementAt(new LinkedList<ObjectSimulator>(), i);
97 this.paraQueues.elementAt(i).add(obj);
103 public void refreshPara(ObjectSimulator obj, boolean remove) {
104 ClassDescriptor cd = obj.getCd();
105 int paraNum = td.numParameters();
106 for(int i = 0; i < paraNum; i++) {
107 VarDescriptor para = td.getParameter(i);
108 if(cd.equals(para.getType().getClassDesc())) {
110 if((this.paraQueues != null) &&
111 (this.paraQueues.elementAt(i) != null) &&
112 (this.paraQueues.elementAt(i).contains(obj))){
113 this.paraQueues.elementAt(i).remove(obj);
116 // check if the status is right
117 FlagExpressionNode fen = td.getFlag(para);
118 if(SchedulingUtil.isTaskTrigger_flag(fen, obj.getCurrentFS())) {
119 if(this.paraQueues == null) {
120 this.paraQueues = new Vector<Queue<ObjectSimulator>>();
121 for(int j = 0; j < paraNum; j++) {
122 this.paraQueues.add(null);
125 if(this.paraQueues.elementAt(i) == null) {
126 this.paraQueues.setElementAt(new LinkedList<ObjectSimulator>(), i);
128 this.paraQueues.elementAt(i).add(obj);
130 if((this.paraQueues != null) &&
131 (this.paraQueues.elementAt(i) != null) &&
132 (this.paraQueues.elementAt(i).contains(obj))){
133 this.paraQueues.elementAt(i).remove(obj);
141 public void process() {
148 if(this.currentRun == null) {
149 this.currentRun = new ExeResult();
153 // According to runtime statistic information, decide the execution path of this task this time.
154 // Mainly following things:
155 // 1.the result, i.e. the result FlagState reached by each parameter.
158 for(int i = 0; i < paraQueues.size(); i++) {
159 ObjectSimulator tpara = paraQueues.elementAt(i).peek();
160 // remove this object from the remaining parameter queues
161 for(int j = i + 1; j < paraQueues.size(); j++) {
162 paraQueues.elementAt(j).remove(tpara);
164 FlagState tfstate = tpara.getCurrentFS();
165 FEdge toexecute = tfstate.process(td);
166 finishTime += toexecute.getExeTime();
167 if((toexecute.getNewObjInfoHashtable() != null) && (toexecute.getNewObjInfoHashtable().size() > 0)) {
169 Iterator it = toexecute.getNewObjInfoHashtable().keySet().iterator();
170 int invokeNum = toexecute.getInvokeNum();
171 while(it.hasNext()) {
172 ClassDescriptor cd = (ClassDescriptor)it.next();
173 NewObjInfo noi = toexecute.getNewObjInfo(cd);
174 if(noi.getInvokeNum() < ((int)Math.round(((noi.getProbability() / 100) * noi.getNewRate() * invokeNum)))) {
175 for(int j = 0; j < noi.getNewRate(); j++) {
176 ObjectSimulator tmpObj = new ObjectSimulator(cd, noi.getRoot());
177 this.currentRun.addNewObj(tmpObj);
183 //FlagState tFState = (FlagState)toexecute.getTarget();
184 //tpara.setCurrentFS(tFState);
185 tpara.applyEdge(toexecute);
187 finishTime /= paraQueues.size();
188 this.currentRun.setFinishTime(finishTime);
191 public void updateFinishTime(int time) {
192 this.currentRun.setFinishTime(this.currentRun.finishTime - time);
196 public void finish() {