start of new file
[IRC.git] / Robust / src / Benchmarks / PERT / Java / PERT.java
1 import java.io.FileInputStream;
2
3 public class PERT {
4
5     int stageNum;
6     Stage[] stages;
7     Estimator estimator;
8
9
10     public PERT() {
11         this.stageNum = -1;
12         //this.stages = null;
13         //this.estimator = null;
14     }
15
16     public Estimator getEstimator() {
17         return estimator;
18     }
19
20     public void setEstimator(Estimator estimator) {
21         this.estimator = estimator;
22     }
23
24     public void setStageNum(int stageNum) {
25         this.stageNum = stageNum;
26     }
27
28     public void createStages() {
29         this.stages = new Stage[this.stageNum];
30         for(int i = 0; i < stageNum; ++i) {
31             this.stages[i] = new Stage(i);
32         }
33     }
34
35     public void sampling() {
36         for(int i = 0; i < this.stageNum; ++i) {
37             this.stages[i].sampling();
38         }
39     }
40
41     public void estimate() {
42         for(int i = 0; i < this.stageNum; ++i) {
43             this.stages[i].estimate();
44         }
45     }
46
47     public void merge() {
48         for(int i = 0; i < this.stageNum; ++i) {
49             Stage tmp = this.stages[i];
50             this.estimator.estimate(tmp.getAntTime(), tmp.getAntVariance2(), false);
51         }
52     }
53
54     public static void main(String args[]) {
55 //      try{
56             PERT pert = new PERT();
57
58             String path = new String("/home/jzhou/pert/conf.txt");
59             FileInputStream iStream = new FileInputStream(path);
60             byte[] b = new byte[1024];
61             int length = iStream.read(b);
62             if(length < 0) {
63                 System./*out.println*/printString("Error! Can not read from configure file: " + path + "\n");
64                 System.exit(-1);
65             }
66             iStream.close();
67             String content = new String(b, 0, length);
68             int index = content.indexOf('\n');
69             int stage = Integer.parseInt(content.substring(0, index));
70             Estimator estimator = new Estimator(stage);
71             pert.setStageNum(stage);
72             pert.setEstimator(estimator);
73             pert.createStages();
74             pert.sampling();
75             pert.estimate();
76             pert.merge();
77             path = new String("/home/jzhou/pert/prob.txt");
78             iStream = new FileInputStream(path);
79             byte c[] = new byte[1024];
80             length = iStream.read(c);
81             if(length < 0) {
82                 System./*out.println*/printString("Error! Can not read from input file: " + path + "\n");
83                 System.exit(-1);
84             }
85             iStream.close();
86             content = new String(c, 0, length);
87             index = content.indexOf('\n');
88             int x = Integer.parseInt(content.substring(0, index));
89             content = content.substring(index + 1);
90             index = content.indexOf('\n');
91             int y = Integer.parseInt(content.substring(0, index));
92             //System.out.println("x: " + x + "; y: " + y);
93             System./*out.println*/printString("The anticipate days need to finish this project is: " + pert.getEstimator().getTime() + "\n");
94             System./*out.println*/printString("And the anticipate variance is: " + (int)(pert.getEstimator().getVariance()*100) + "(/100)\n");
95             double prob = pert.getEstimator().getProbability(x, y);
96
97             System./*out.println*/printString("The probability of this project to be finished in " + x + " to " + y + " days is: " + (int)(prob*100) + "(/100)\n");
98 /*      } catch(Exception e) {
99             e.printStackTrace();
100             System.exit(-1);
101         }*/
102     }
103
104 }