1 20 36 package gov.nasa.jpf.mc; 37 38 import gov.nasa.jpf.jvm.Verify; 39 40 41 class Constants { 42 public static final boolean east = true; 43 public static final boolean west = false; 44 } 45 46 class Torch { 47 public static boolean side = Constants.east; 48 49 public String toString () { 50 if (side == Constants.east) { 51 return "east"; 52 } else { 53 return "west"; 54 } 55 } 56 } 57 58 class Bridge { 59 static Person[] onBridge = new Person[2]; 60 static int numOnBridge = 0; 61 62 public static boolean isFull () { 63 return numOnBridge != 0; 64 } 65 66 public static int Cross () { 67 int time = 0; 68 Torch.side = !Torch.side; 69 70 if (numOnBridge == 1) { 71 onBridge[0].side = Torch.side; 72 time = onBridge[0].time; 73 74 } else { 78 onBridge[0].side = Torch.side; 79 onBridge[1].side = Torch.side; 80 81 if (onBridge[0].time > onBridge[1].time) { 82 time = onBridge[0].time; 83 } else { 84 time = onBridge[1].time; 85 } 86 87 } 92 93 return time; 94 } 95 96 public static void clearBridge () { 97 if (numOnBridge == 0) { 98 return; 99 } else if (numOnBridge == 1) { 100 onBridge[0] = null; 101 numOnBridge = 0; 102 } else { 103 onBridge[0] = null; 104 onBridge[1] = null; 105 numOnBridge = 0; 106 } 107 } 108 109 public static void initBridge () { 110 onBridge[0] = null; 111 onBridge[1] = null; 112 numOnBridge = 0; 113 } 114 115 public static boolean tryToCross (Person th) { 116 if ((numOnBridge < 2) && (onBridge[0] != th) && (onBridge[1] != th)) { 117 onBridge[numOnBridge++] = th; 118 119 return true; 120 } else { 121 return false; 122 } 123 } 124 } 125 126 class Person { 127 int id; 129 public int time; 130 public boolean side; 131 132 public Person (int i, int t) { 133 time = t; 134 side = Constants.east; 135 id = i; 136 } 137 138 public void move () { 139 if (side == Torch.side) { 140 if (!Verify.randomBool()) { 141 Bridge.tryToCross(this); 142 } 143 } 144 } 145 146 public String toString () { 147 return "" + id; 148 } 149 } 150 151 public class Crossing { 152 public static native void setTotal (int time); 153 154 public static native int getTotal (); 156 157 public static void main (String [] args) { 158 160 boolean isNative = false; 165 166 if (isNative) { 167 setTotal(30); } 169 170 int total = 0; 171 boolean finished = false; 172 Bridge.initBridge(); 173 174 Person p1 = new Person(1, 1); 175 Person p2 = new Person(2, 2); 176 Person p3 = new Person(3, 5); 177 Person p4 = new Person(4, 10); 178 180 while (!finished) { 181 p1.move(); 183 p2.move(); 184 p3.move(); 185 p4.move(); 186 187 if (Bridge.isFull()) { 188 total += Bridge.Cross(); 189 190 if (isNative) { 191 Verify.ignoreIf(total > getTotal()); 192 } else { 193 Verify.ignoreIf(total > 17); } 195 196 Bridge.clearBridge(); 197 198 finished = !(p1.side || p2.side || p3.side || p4.side); 200 } 201 202 } 204 205 207 if (isNative) { 208 if (total < getTotal()) { 209 System.out.println("new total " + total); 210 setTotal(total); 211 assert (total > getTotal()); 212 } 213 } else { 214 System.out.println("total time = " + total); 215 assert (total > 17); 216 } 217 218 } 220 221 static void printConfig (Person p1, Person p2, Person p3, Person p4, 222 int total) { 223 if (p1.side == Constants.east) { 224 System.out.print("p1(" + p1.time + ")"); 225 } 226 227 if (p2.side == Constants.east) { 228 System.out.print("p2(" + p2.time + ")"); 229 } 230 231 if (p3.side == Constants.east) { 232 System.out.print("p3(" + p3.time + ")"); 233 } 234 235 if (p4.side == Constants.east) { 236 System.out.print("p4(" + p4.time + ")"); 237 } 238 239 System.out.print(" - " + total + " -> "); 240 241 if (p1.side == Constants.west) { 242 System.out.print("p1(" + p1.time + ")"); 243 } 244 245 if (p2.side == Constants.west) { 246 System.out.print("p2(" + p2.time + ")"); 247 } 248 249 if (p3.side == Constants.west) { 250 System.out.print("p3(" + p3.time + ")"); 251 } 252 253 if (p4.side == Constants.west) { 254 System.out.print("p4(" + p4.time + ")"); 255 } 256 257 System.out.println(); 258 } 259 } 260 | Popular Tags |