Browse Source

Add code to search in graph

Fabio Battaglia 5 years ago
parent
commit
91dcbebf26

+ 0 - 2
src/main/java/info/hkzlab/dupal/analyzer/palanalisys/graph/GraphState.java

@@ -2,8 +2,6 @@ package info.hkzlab.dupal.analyzer.palanalisys.graph;
 
 public interface GraphState {
     public int[] getInternalState();
-
     public boolean isStateFull();
     public GraphLink[] getLinks();
-    public int getMaxLinks();
 }

+ 11 - 13
src/main/java/info/hkzlab/dupal/analyzer/palanalisys/graph/PathFinder.java

@@ -2,11 +2,9 @@ package info.hkzlab.dupal.analyzer.palanalisys.graph;
 
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.Map;
 import java.util.Queue;
-import java.util.Set;
 
 public class PathFinder {
    private PathFinder() {};
@@ -18,27 +16,27 @@ public class PathFinder {
        ArrayList<GraphLink> linkStack = null;
        GraphState currentState = start;
 
-       pathMap.put(start.hashCode(), new ArrayList<>());
+       pathMap.put(start.hashCode(), new ArrayList<>()); // Bootstrap the pathMap by adding an empty path to the start state
 
        while(currentState != null) {
-           linkStack = pathMap.get(currentState.hashCode());
+           linkStack = pathMap.get(currentState.hashCode()); // Get the map to the current state
            if(!currentState.isStateFull()) return linkStack; // Ok, we found a state where we need to map other links
        
-           GraphLink[] stateLinks = currentState.getLinks();
+           GraphLink[] stateLinks = currentState.getLinks(); // Get links present in the current state
 
-            for(GraphLink l : stateLinks) {
-                if(!pathMap.containsKey(l.getDestinationState().hashCode())) {
-                    ArrayList<GraphLink> statePath = (ArrayList<GraphLink>)linkStack.clone();
-                    statePath.add(l);
+            for(GraphLink l : stateLinks) { // For evey link...
+                if(!pathMap.containsKey(l.getDestinationState().hashCode())) { // If it's not leading somewhere we've already visited or we've already put in our path map
+                    ArrayList<GraphLink> statePath = (ArrayList<GraphLink>)linkStack.clone(); // Copy the map to the current state
+                    statePath.add(l); // And append this link to it
 
-                    pathMap.put(l.getDestinationState().hashCode(), statePath);
-                    statesQueue.add(l.getDestinationState());
+                    pathMap.put(l.getDestinationState().hashCode(), statePath); // Then put this new path into the map
+                    statesQueue.add(l.getDestinationState()); // And add the state to the queue, to examine later
                 }
             }
 
-            currentState = statesQueue.poll();
+            currentState = statesQueue.poll(); // Pick the next state from the queue
        }
 
-       return null;
+       return null; // Found nothing...
    }
 }