Guest User

Untitled

a guest
Oct 17th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.25 KB | None | 0 0
  1. import de.unisiegen.informatik.bs.alvis.graph.datatypes.*;
  2. import de.unisiegen.informatik.bs.alvis.primitive.datatypes.*;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.HashMap;
  8. import java.util.concurrent.locks.Lock;
  9. import de.unisiegen.informatik.bs.alvis.vm.*;
  10.  
  11. public class Algorithm implements AbstractAlgo {
  12.     private int localbp;
  13.     private Lock lock;
  14.     private boolean onBreak;
  15.     private boolean stopThread = false;
  16.     private boolean onDecision;
  17.     private BPListener bplisten;
  18.     private DPListener dplisten;
  19.  
  20.     private PCGraph G;
  21.     private PCVertex s;
  22.  
  23.     public Algorithm() {}
  24.  
  25.     public void setParameters(Map<String, PCObject> params) {
  26.         this.G = (PCGraph)params.get("G");
  27.         this.s = (PCVertex)params.get("s");
  28.  
  29.     }
  30.  
  31.     public Map<String, PCObject> getParameterTypes() {
  32.         Map<String, PCObject> result = new HashMap<String, PCObject>();
  33.  
  34.         result.put("G", PCGraph.getNull());
  35.         result.put("s", PCVertex.getNull());
  36.  
  37.         return result;
  38.     }
  39.  
  40.     public void run() {
  41.         System.out.println("start succ");
  42.         try {
  43.             reachedBreakPoint(0);
  44.             stop();
  45.             {
  46.                 for (PCVertex v : (PCList<PCVertex>)reachedDecisionPoint(2, G, G.getVertices())) {
  47.                     stop();
  48.                     {
  49.                         v.setColor(new PCString("white"));
  50.                         v.setDistance(PCInteger.getInfty());
  51.                         v.setParentId(PCVertex.getNull());
  52.                     }
  53.                 }
  54.                 s.setColor(new PCString("grey"));
  55.                 reachedBreakPoint(7);
  56.                 stop();
  57.                 s.setDistance(new PCInteger(0));
  58.                 PCQueue<PCVertex> Q = new PCQueue<PCVertex>();
  59.                 Q.enqueue(s);
  60.                 while ((Q.isEmpty().not()).getLiteralValue()) {
  61.                     stop();
  62.                     {
  63.                         PCVertex u = new PCVertex();
  64.                         u = Q.dequeue();
  65.                         for (PCVertex v : (PCList<PCVertex>)reachedDecisionPoint(14, u, u.getAdjacents())) {
  66.                             stop();
  67.                             if ((v.getColor().equal(new PCString("white"))).getLiteralValue())
  68.                                 {
  69.                                     v.setColor(new PCString("grey"));
  70.                                     reachedBreakPoint(16);
  71.                                     stop();
  72.                                     v.setDistance(u.getDistance().add(new PCInteger(1)));
  73.                                     v.setParentId(u);
  74.                                     Q.enqueue(v);
  75.                                 }
  76.                         }
  77.                     }
  78.                 }
  79.             }
  80.         } catch (KillThreadException e) {
  81.             System.out.println("killed now");
  82.             return;
  83.         }
  84.     }
  85.  
  86.     public static void print(Object o) {
  87.         System.out.println(o);
  88.     }
  89.  
  90.     /**
  91.      * static code
  92.      *
  93.      * @param BPNr
  94.      *            set breakpoint field, inform breakpoint listener, sleeps
  95.      *            current thread
  96.      */
  97.     private SortableCollection reachedDecisionPoint(int DPNr,PCObject from ,SortableCollection toSort) {
  98.         if(toSort.size() <= 1) {
  99.             return toSort;
  100.         }
  101.         dplisten.onDecisionPoint(DPNr, from, toSort);
  102.         return toSort;
  103.     }
  104.  
  105.     /**
  106.      * static code
  107.      *
  108.      * @param BPNr
  109.      *            set breakpoint field, inform breakpoint listener, sleeps
  110.      *            current thread
  111.      */
  112.     private void reachedBreakPoint(int BPNr) {
  113.         System.out.println("reached bp");
  114.         localbp = BPNr;
  115.         Thread thr = new Thread(new Runnable() {
  116.             public void run() {
  117.                 if (bplisten != null) {
  118.                     bplisten.onBreakPoint(localbp);
  119.                 }
  120.             }
  121.         });
  122.         System.out.println("reached messenger");
  123.         thr.start();
  124.  
  125.         synchronized (this) {
  126.             onBreak = true;
  127.             do {
  128.                 try {
  129.                     System.out.println("reached wait in bp");
  130.                     wait();
  131.                 } catch (InterruptedException e) {
  132.                     e.printStackTrace();
  133.                 }
  134.             } while(onBreak);
  135.             System.out.println("leave wait in bp");
  136.         }
  137.     }
  138.  
  139.     public List<PCObject> getVariableReferences() {
  140.         return null;
  141.     }
  142.  
  143.     @Override
  144.     public void addBPListener(BPListener wantToListen) {
  145.         bplisten = wantToListen;
  146.     }
  147.  
  148.     public void addDPListener(DPListener wantToListen) {
  149.         dplisten = wantToListen;
  150.     }
  151.  
  152.     public void kill() {
  153.         System.out.println("to kill");
  154.         stopThread = true;
  155.         bplisten = null;
  156.         stopBreak();
  157.         System.out.println("to kill done");
  158.     }
  159.  
  160.     @Override
  161.     public void stopBreak() {
  162.         System.out.println("notify stop bp");
  163.         synchronized (this) {
  164.             onBreak = false;
  165.             this.notify();
  166.         }
  167.         System.out.println("done notify stop bp");
  168.     }
  169.  
  170.     @Override
  171.     public void setLock(Lock toLockOn) {
  172.         lock = toLockOn;
  173.     }
  174.  
  175.     private void stop() throws KillThreadException {
  176.         if (stopThread)
  177.             throw new KillThreadException();
  178.     }
  179. }
Add Comment
Please, Sign In to add comment