1 17 18 19 package SOFA.SOFAnode.Util.DFSRChecker.node; 20 21 import java.util.LinkedList ; 22 import java.util.TreeMap ; 23 import java.util.NoSuchElementException ; 24 import java.util.TreeSet ; 25 import java.util.Iterator ; 26 27 import SOFA.SOFAnode.Util.DFSRChecker.parser.Debug; 28 import SOFA.SOFAnode.Util.SyntaxErrorException; 29 30 31 37 public class ActionRepository { 38 39 40 public ActionRepository() { 41 repository = new TreeMap (); 42 inverseRepository = new TreeMap (); 43 atomicRepository = new TreeMap (); 44 atomicInverseRepository = new TreeMap (); 45 usedItems = new TreeSet (); 46 } 47 48 53 public int addItem(String item) throws SyntaxErrorException { 54 Integer i; 55 56 Debug.println(3, "Adding item to repository: " + item); 57 58 try { 59 i = (Integer ) inverseRepository.get(item); 60 } catch (ClassCastException e) { 61 i = null; 62 } catch (NullPointerException e) { 63 i = null; 64 } 65 66 if (i != null) { touchItem(i.intValue()); 68 return i.intValue(); 69 } 70 71 else { int index; 73 try { 74 index = ((Integer ) repository.lastKey()).intValue() + 1; 75 } catch (NoSuchElementException e) { 76 index = 0; 77 } 78 79 String next; 80 81 String purename = getPureName(item); 82 repository.put(new Integer (index), purename); 84 85 inverseRepository.put(purename, new Integer (index)); 86 87 next = "!" + purename; 88 ++index; 89 repository.put(new Integer (index), next); 90 inverseRepository.put(next, new Integer (index)); 91 92 next = "?" + purename; 93 ++index; 94 repository.put(new Integer (index), next); 95 inverseRepository.put(next, new Integer (index)); 96 97 next = "!" + purename + "^"; 98 ++index; 99 repository.put(new Integer (index), next); 100 inverseRepository.put(next, new Integer (index)); 101 102 next = "?" + purename + "^"; 103 ++index; 104 repository.put(new Integer (index), next); 105 inverseRepository.put(next, new Integer (index)); 106 107 next = "!" + purename + "$"; 108 ++index; 109 repository.put(new Integer (index), next); 110 inverseRepository.put(next, new Integer (index)); 111 112 next = "?" + purename + "$"; 113 ++index; 114 repository.put(new Integer (index), next); 115 inverseRepository.put(next, new Integer (index)); 116 117 next = "#" + purename; 118 ++index; 119 repository.put(new Integer (index), next); 120 inverseRepository.put(next, new Integer (index)); 121 122 next = "#" + purename + "^"; 123 ++index; 124 repository.put(new Integer (index), next); 125 inverseRepository.put(next, new Integer (index)); 126 127 next = "#" + purename + "$"; 128 ++index; 129 repository.put(new Integer (index), next); 130 inverseRepository.put(next, new Integer (index)); 131 132 Debug.println(3, "Action added"); 133 134 Integer returnindex = (Integer ) inverseRepository.get(item); 135 if (returnindex == null) { 136 throw new SyntaxErrorException("This is not an event: " + item); 137 } 138 touchItem(returnindex.intValue()); 139 return (returnindex).intValue(); 140 141 } 142 } 143 144 150 public int addAtomicItem(TreeSet events) { 151 Integer i; 152 153 try { 154 i = (Integer ) atomicInverseRepository.get(events); 155 } catch (ClassCastException e) { 156 i = null; 157 } catch (NullPointerException e) { 158 i = null; 159 } 160 161 if (i != null) { touchItem(i.intValue()); 163 return i.intValue(); 164 } 165 else { 166 167 LinkedList copy = new LinkedList (); 168 copy.addAll(events); 169 LinkedList variants = this.createAtomicVariants(copy); 170 171 Iterator it = variants.iterator(); 172 while (it.hasNext()) { 173 int index = -1 - atomicRepository.size(); 174 175 AtomicAction item = new AtomicAction((LinkedList )it.next()); 176 atomicRepository.put(new Integer (index),item); 177 atomicInverseRepository.put(item, new Integer (index)); 178 } 179 180 return ((Integer )atomicInverseRepository.get(new AtomicAction(events))).intValue(); 181 } 182 } 183 184 187 public TreeSet getAllUsedEventIndices() { 188 return new TreeSet (usedItems); 189 190 198 } 199 200 206 public boolean getItemPresence(int index) { 207 return repository.containsKey(new Integer (index)); 208 } 209 210 215 public String getItemString(int index) { 216 try { 217 if (index >= 0) 218 return (String ) repository.get(new Integer (index)); 219 else { 220 String result = new String ("["); 221 TreeSet actions = (TreeSet )atomicRepository.get(new Integer (index)); 222 Iterator it = actions.iterator(); 223 while (it.hasNext()) { 224 result = result + getItemString(((Integer )it.next()).intValue()); 225 if (it.hasNext()) 226 result += ", "; 227 } 228 result += "]"; 229 230 return result; 231 } 232 } catch (NullPointerException e) { 233 return null; 234 } 235 } 236 237 240 public int getItemIndex(String item) { 241 try { 242 Integer result = (Integer ) inverseRepository.get(item); 243 touchItem(result.intValue()); 244 return result.intValue(); 245 } catch (NullPointerException e) { 246 if (!item.equals("")) 247 Debug.println(1, "Action repository: try to access action '" + item + "' - not present in repository.\n"); 248 return -1; 249 } 250 } 251 252 258 public int getItemIndex(AtomicAction action) { 259 try { 260 Integer result = (Integer )atomicInverseRepository.get(action); 261 return result.intValue(); 262 } catch (NullPointerException e) { 263 if (!action.equals(null)) 264 Debug.println(1, "Action repository: try to access atomic action '" + action + "' - not present in repository.\n"); 265 return -1; 266 } 267 268 } 269 270 276 static public int getPure(int event) { 277 return (event / VARIANTCNT) * VARIANTCNT; 278 } 279 280 286 public int getInverseAction(int event) { 287 if (((event % VARIANTCNT) < 7) && (event % VARIANTCNT > 0)) { 288 if ((event % 2) == 1) { 289 touchItem(event + 1); 290 return event + 1; 291 } else { 292 touchItem(event - 1); 293 return event - 1; 294 } 295 } 296 297 else 298 return event; 299 } 300 301 304 public int getTauAction(int event) { 305 int root = (event / VARIANTCNT) * VARIANTCNT; 306 int index = event % VARIANTCNT; 307 308 if ((index == 3) || (index == 4)) { 309 touchItem(root + 8); 310 return root + 8; 311 } else if ((index == 5) || (index == 6)) { 312 touchItem(root + 9); 313 return root + 9; 314 } else { 315 touchItem(root + 7); 316 return root + 7; 317 } 318 } 319 320 325 public boolean isResponse(int event) { 326 int subtype = event % VARIANTCNT; 327 return (subtype == 2) || (subtype == 4) || (subtype == 6); 328 } 329 330 335 public boolean isTau(int event) { 336 int subtype = event % VARIANTCNT; 337 return subtype >= 7; 338 } 339 340 348 public int getEventAction(int event) { 349 if ((event % VARIANTCNT == 3) || (event % VARIANTCNT == 5)) 350 return event / 10 * 10 + 1; 351 else if ((event % VARIANTCNT == 4) || (event % VARIANTCNT == 6)) 352 return event / 10 * 10 + 2; 353 else if ((event % VARIANTCNT == 8) || (event % VARIANTCNT == 9)) 354 return event / 10 * 10 + 7; 355 else 356 return event; 357 } 358 359 364 static private String getPureName(String action) { 365 String result = new String (action); 366 367 char first = action.charAt(0); 368 char last = action.charAt(action.length() - 1); 369 370 if ((first == '?') || (first == '!') || (first == '#')) 371 result = result.substring(1); 372 373 if ((last == '^') || (last == '$')) 374 result = result.substring(0, result.length() - 1); 375 376 return result; 377 } 378 379 383 private void touchItem(int index) { 384 if ((index % VARIANTCNT != 0) && (index % VARIANTCNT != 1) && (index % VARIANTCNT != 2) && (index % VARIANTCNT != 7)) 385 usedItems.add(new Integer (index)); 386 } 387 388 392 public AtomicAction getAtomicEvents(int atomicactionindex) { 393 return (AtomicAction)atomicRepository.get(new Integer (atomicactionindex)); 394 } 395 396 397 398 401 private LinkedList createAtomicVariants(LinkedList events) { 402 LinkedList result = new LinkedList (); 403 LinkedList subresult1; 404 LinkedList subresult2 = new LinkedList (); 405 406 if (events.size() == 0) 407 return result; 408 409 if (events.size() == 1) { 410 subresult2.add(events.getFirst()); 411 result.add(subresult2); 412 subresult2 = new LinkedList (); 413 subresult2.add(new Integer (getTauAction(((Integer )events.getFirst()).intValue()))); 414 result.add(subresult2); 415 return result; 416 } 417 418 Integer first = (Integer )events.removeFirst(); 419 420 subresult1 = createAtomicVariants(events); 421 subresult2 = duplicateList(subresult1); 422 423 424 Iterator it = subresult1.iterator(); 425 while (it.hasNext()) { 426 LinkedList item = (LinkedList )it.next(); 427 item.add(first); 428 result.add(item); 429 } 430 431 Integer tau = new Integer (getTauAction(first.intValue())); 432 it = subresult2.iterator(); 433 while (it.hasNext()) { 434 LinkedList item = (LinkedList )it.next(); 435 item.add(tau); 436 result.add(item); 437 } 438 439 return result; 440 } 441 442 446 private LinkedList duplicateList(LinkedList source) { 447 LinkedList newlist = new LinkedList (); 448 449 Iterator it = source.iterator(); 450 451 while(it.hasNext()) { 452 LinkedList item = (LinkedList )it.next(); 453 LinkedList newitem = new LinkedList (); 454 455 Iterator internal = item.iterator(); 456 457 while (internal.hasNext()) { 458 newitem.add(new Integer (((Integer )internal.next()).intValue())); 459 } 460 461 newlist.add(newitem); 462 } 463 464 return newlist; 465 } 466 467 470 public void flushCache() { 471 Iterator it = repository.keySet().iterator(); 472 473 while (it.hasNext()) { 474 System.out.println(repository.get((Integer ) it.next())); 475 } 476 } 477 478 480 483 private TreeMap repository; 484 485 489 private TreeMap inverseRepository; 490 491 494 private TreeMap atomicRepository; 495 496 500 private TreeMap atomicInverseRepository; 501 502 505 static final int VARIANTCNT = 10; 506 507 510 private TreeSet usedItems; 511 512 } | Popular Tags |