Beispiel #1
0
  public DirectedGraph<Node, Edge> convert() {
    DirectedGraph<Node, Edge> graph = new SimpleDirectedGraph<Node, Edge>(Edge.factory);

    // Add vertices

    for (Type t : hierarchy) graph.addVertex(Node.get(t));

    // Add edges

    for (Type t : hierarchy) {
      for (Type i : hierarchy.getInterfaces(t)) {
        graph.addVertex(Node.get(i));
        graph.addEdge(Node.get(t), Node.get(i));
      }

      Type sc = hierarchy.getSuperclass(t);

      if (sc == null) {
        assert t.equals(OBJECT) : t;
        continue;
      }

      graph.addVertex(Node.get(sc));
      graph.addEdge(Node.get(t), Node.get(sc));
    }

    // Check for cycles

    if (new CycleDetector<Node, Edge>(graph).detectCycles())
      throw new CyclicHierarchyException(graph.toString());

    return graph;
  }
Beispiel #2
0
  public void testDirectedMatrixNoNodeIdZeroMissingEdges() {
    DirectedGraph<Integer, DefaultEdge> g = new DirectedPseudograph<>(DefaultEdge.class);
    g.addVertex(1);
    g.addVertex(2);
    g.addVertex(3);
    g.addVertex(4);
    g.addVertex(5);
    g.addEdge(1, 2);
    g.addEdge(1, 3);
    g.addEdge(3, 1);
    g.addEdge(3, 4);
    g.addEdge(4, 5);
    g.addEdge(5, 1);
    g.addEdge(5, 2);
    g.addEdge(5, 3);
    g.addEdge(5, 4);
    g.addEdge(5, 5);

    CSVExporter<Integer, DefaultEdge> exporter =
        new CSVExporter<>(nameProvider, CSVFormat.MATRIX, ';');
    exporter.setParameter(CSVFormat.Parameter.MATRIX_FORMAT_ZERO_WHEN_NO_EDGE, true);
    StringWriter w = new StringWriter();
    exporter.exportGraph(g, w);
    assertEquals(DIRECTED_MATRIX_NO_NODEID_ZERO_NO_EDGE, w.toString());
  }
Beispiel #3
0
  public void testAdjacencyDirected() {
    DirectedGraph<String, DefaultEdge> g =
        new DirectedMultigraph<String, DefaultEdge>(DefaultEdge.class);
    g.addVertex(V1);
    g.addVertex(V2);
    g.addEdge(V1, V2);
    g.addVertex(V3);
    g.addEdge(V3, V1);
    g.addEdge(V3, V1);

    Writer w = new StringWriter();
    exporter.exportAdjacencyMatrix(w, g);
    assertEquals(DIRECTED_ADJACENCY, w.toString());
  }
  /** . */
  public void testStronglyConnected3() {
    DirectedGraph<String, DefaultEdge> g =
        new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
    g.addVertex(V1);
    g.addVertex(V2);
    g.addVertex(V3);
    g.addVertex(V4);

    g.addEdge(V1, V2);
    g.addEdge(V2, V3);
    g.addEdge(V3, V1); // strongly connected

    g.addEdge(V1, V4);
    g.addEdge(V2, V4);
    g.addEdge(V3, V4); // weakly connected

    StrongConnectivityInspector<String, DefaultEdge> inspector =
        new StrongConnectivityInspector<String, DefaultEdge>(g);

    // convert from List to Set because we need to ignore order
    // during comparison
    Set<Set<String>> actualSets = new HashSet<Set<String>>(inspector.stronglyConnectedSets());

    // construct the expected answer
    Set<Set<String>> expectedSets = new HashSet<Set<String>>();
    Set<String> set = new HashSet<String>();
    set.add(V1);
    set.add(V2);
    set.add(V3);
    expectedSets.add(set);
    set = new HashSet<String>();
    set.add(V4);
    expectedSets.add(set);

    assertEquals(expectedSets, actualSets);

    actualSets.clear();

    List<DirectedSubgraph<String, DefaultEdge>> subgraphs = inspector.stronglyConnectedSubgraphs();

    for (DirectedSubgraph<String, DefaultEdge> sg : subgraphs) {
      actualSets.add(sg.vertexSet());

      StrongConnectivityInspector<String, DefaultEdge> ci =
          new StrongConnectivityInspector<String, DefaultEdge>(sg);
      assertTrue(ci.isStronglyConnected());
    }

    assertEquals(expectedSets, actualSets);
  }
  private void buildFullyConnectedGraph(NodeLists listPairs) {

    for (int i = 0; i < listPairs.getList1().size(); i++) {

      // falls Knoten noch nicht erzeugt wurde
      if (!graph.containsVertex(listPairs.getList1().get(i))) {
        graph.addVertex(listPairs.getList1().get(i));
      }
      if (!parents.contains(listPairs.getList1().get(i))) {
        parents.add(listPairs.getList1().get(i));
      }

      // falls Knoten noch nicht erzeugt wurde
      for (int j = 0; j < listPairs.getList2().size(); j++) {

        if (!graph.containsVertex(listPairs.getList2().get(j))) {
          graph.addVertex(listPairs.getList2().get(j));
        }

        if (!children.contains(listPairs.getList2().get(j))) {
          children.add(listPairs.getList2().get(j));
        }

        // Erstelle eine Kante
        graph.addEdge(listPairs.getList1().get(i), listPairs.getList2().get(j));
        System.out.println(listPairs.getList1().get(i) + " -> " + listPairs.getList2().get(j));
      }
    }
  }
  private void replaceVertex(JavaClass newClass) {
    List<JavaClass> incomingEdges = getParents(newClass);

    graph.removeVertex(newClass);
    graph.addVertex(newClass);
    for (JavaClass each : incomingEdges) {
      graph.addEdge(each, newClass);
    }
  }
 public synchronized void addBuild(JobInvocation build)
     throws ExecutionException, InterruptedException {
   builds.addVertex(build);
   for (JobInvocation up : state.get().getLastCompleted()) {
     String edge = up.toString() + " => " + build.toString();
     LOGGER.fine("added build to execution graph " + edge);
     builds.addEdge(up, build, edge);
   }
   state.get().setLastCompleted(build);
 }
 private void updateParentReferences(JavaClass parentClass) {
   for (String child : parentClass.getImports()) {
     JavaClass childClass = findJavaClass(child);
     if ((childClass != null) && !childClass.equals(parentClass)) {
       if (graph.containsVertex(childClass)) {
         graph.addEdge(parentClass, childClass);
       }
     }
   }
 }
  private void buildStandardGraph() {
    ArrayList<OrderedPair> orderedpairList = orderedPairOrder.getListOfPairs();
    for (int i = 0; i < orderedpairList.size(); i++) {

      // if the nodes do not exists --> add them
      if (orderedpairList.get(i).getChild() != null
          && !(graph.containsVertex(orderedpairList.get(i).getChild()))) {

        graph.addVertex(orderedpairList.get(i).getChild());
      }

      if (orderedpairList.get(i).getChild() != null) {
        if (orderedpairList.get(i).getParent() == null) {
        } else if (!children.contains(orderedpairList.get(i).getChild())) {
          children.add(orderedpairList.get(i).getChild());
        }
      }
      if (orderedpairList.get(i).getParent() != null
          && !(graph.containsVertex(orderedpairList.get(i).getParent()))) {

        graph.addVertex(orderedpairList.get(i).getParent());
      }
      if (orderedpairList.get(i).getParent() != null
          && !parents.contains(orderedpairList.get(i).getParent())) {
        parents.add(orderedpairList.get(i).getParent());
      }

      // fuegt Kante hinzu, falls beide Knoten nicht NULL sind
      if (orderedpairList.get(i).getParent() != null && orderedpairList.get(i).getChild() != null) {
        graph.addEdge(orderedpairList.get(i).getParent(), orderedpairList.get(i).getChild());
        System.out.println(
            orderedpairList.get(i).getParent() + " -> " + orderedpairList.get(i).getChild());
      }
    }
    DirectedGraph<Object, DefaultEdge> temp_gr = new DefaultDirectedGraph<>(DefaultEdge.class);
    for (Object o : graph.vertexSet()) {
      temp_gr.addVertex(o);
    }
    for (DefaultEdge e : graph.edgeSet()) {
      temp_gr.addEdge(graph.getEdgeSource(e), graph.getEdgeTarget(e));
    }
    originalGraph.add(temp_gr);
  }
  private void completeGraphWithTopAndBottom() {
    // fuege pseudo-knoten hinzu
    addVirtualNodes();
    // erzeuge Kanten v. oberem Pseudo-Knoten zu tops
    Iterator<Object> it1 = tops.iterator();
    while (it1.hasNext()) {
      Object item = it1.next();
      graph.addEdge(VIRTUAL_TOPNODE, item);
      // System.out.println(VIRTUAL_TOPNODE + " -> " + item);
    }

    // erzeuge Kanten v. unterem Pseudo-Knoten zu bottoms
    Iterator<Object> it2 = bottoms.iterator();
    while (it2.hasNext()) {
      Object item = it2.next();
      graph.addEdge(item, VIRTUAL_BOTTOMNODE);
      // System.out.println(item + " -> " + VIRTUAL_BOTTOMNODE);
    }
  }
Beispiel #11
0
  public void testDirectedEdgeList() {
    DirectedGraph<Integer, DefaultEdge> g = new SimpleDirectedGraph<>(DefaultEdge.class);
    g.addVertex(1);
    g.addVertex(2);
    g.addVertex(3);
    g.addVertex(4);
    g.addVertex(5);
    g.addEdge(1, 2);
    g.addEdge(1, 3);
    g.addEdge(3, 1);
    g.addEdge(3, 4);
    g.addEdge(4, 5);
    g.addEdge(5, 1);

    CSVExporter<Integer, DefaultEdge> exporter =
        new CSVExporter<>(nameProvider, CSVFormat.EDGE_LIST, ';');
    StringWriter w = new StringWriter();
    exporter.exportGraph(g, w);
    assertEquals(DIRECTED_EDGE_LIST, w.toString());
  }
Beispiel #12
0
  /**
   * Orders the provided elements based on some partial ordering constraints. Ties are broken by the
   * provided comparator (which may be arbitrary but must be consistent and specified).
   *
   * @param elements The elements to order. These elements must not be equal or they will be
   *     considered identical in the order.
   * @param constraints The constraints on the ordering.
   * @param tieBreaker The comparator used to break ties.
   * @return The provided elements in an order respecting the provided constraints.
   * @throws InconsistentPartialOrderException If the partial order is inconsistent based on the
   *     provided constraints.
   */
  public static <T> List<T> orderByConstraints(
      Collection<T> elements,
      Collection<PartialOrderConstraint<T>> constraints,
      Comparator<T> tieBreaker)
      throws InconsistentPartialOrderException {
    // Create a directed graph describing the partial ordering constraints.
    DirectedGraph<T, DefaultEdge> partialOrderGraph = new DefaultDirectedGraph<>(DefaultEdge.class);
    for (T element : elements) {
      partialOrderGraph.addVertex(element);
    }
    for (PartialOrderConstraint<T> constraint : constraints) {
      if (elements.contains(constraint.getLess()) && elements.contains(constraint.getGreater())) {
        partialOrderGraph.addEdge(constraint.getLess(), constraint.getGreater());
      }
    }

    // Determine if there are any strongly connected components. If so, pick one and scream about
    // it.
    StrongConnectivityInspector<T, DefaultEdge> strongConnectivityInspector =
        new StrongConnectivityInspector<>(partialOrderGraph);
    List<Set<T>> components = strongConnectivityInspector.stronglyConnectedSets();
    for (Set<T> stronglyConnectedComponent : components) {
      if (stronglyConnectedComponent.size() > 1) {
        throw new InconsistentPartialOrderException(components.get(0));
      }
    }

    // Otherwise, there is no cycle in the graph. That means we can just iterate a tie-breaking
    // topological sort
    // to get our ordering.
    TopologicalOrderIterator<T, DefaultEdge> iterator =
        new TopologicalOrderIterator<T, DefaultEdge>(
            partialOrderGraph, new PriorityQueue<T>(elements.size(), tieBreaker));
    List<T> result = new ArrayList<>(elements.size());
    while (iterator.hasNext()) {
      result.add(iterator.next());
    }
    return result;
  }
Beispiel #13
0
  // create dependency graph
  public static DirectedGraph<String, DefaultEdge> createDependencyGraph() {
    DirectedGraph<String, DefaultEdge> g =
        new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);

    // add vertices
    for (String key : State.getInstance().pgMap().keySet()) {
      g.addVertex(key);
    }

    // add edges
    for (Map.Entry<String, PutsAndGets> entry1 : State.getInstance().pgMap().entrySet()) {
      PutsAndGets pg1 = entry1.getValue();

      for (Map.Entry<String, PutsAndGets> entry2 : State.getInstance().pgMap().entrySet()) {
        PutsAndGets pg2 = entry2.getValue();

        if (pg2.isDep(pg1)) {
          g.addEdge(entry2.getKey(), entry1.getKey());
        }
      }
    }

    return g;
  }
  @Override
  public void buildGraph(DirectedGraph<Vertex, Edge> graph, Vertex start, Vertex end) {

    final Vertex vm0 = new ChVertexImpl(this.m0);
    graph.addVertex(vm0);
    final Vertex vm1 = new ChVertexImpl(this.m1);
    graph.addVertex(vm1);
    final Vertex vMinusM0 = new ChVertexImpl(XML.CHAR.minus("COMMENT_END_CHAR_MINUS_M0", this.m0));
    graph.addVertex(vMinusM0);
    final Vertex vMinusM1 = new ChVertexImpl(XML.CHAR.minus("COMMENT_END_CHAR_MINUS_M1", this.m1));
    graph.addVertex(vMinusM1);

    graph.addEdge(start, vm0, new EdgeImpl());
    graph.addEdge(start, vMinusM0, new EdgeImpl());

    graph.addEdge(vm0, vm1, new EdgeImpl());
    graph.addEdge(vm0, vMinusM1, new EdgeImpl());

    graph.addEdge(vm1, end, new EdgeImpl(this.m1.getOperation()));

    graph.addEdge(vMinusM0, start, new EdgeImpl());
    graph.addEdge(vMinusM1, start, new EdgeImpl());
  }
  /**
   * Tests graph types: In case of invalid graph types or invalid combination of graph arguments
   * UnsupportedOperationException or InvalidArgumentException is expected
   */
  @Test
  public void testGraphTypes() {

    DirectedGraph<Integer, DefaultEdge> dg1 = new DefaultDirectedGraph<>(DefaultEdge.class);

    dg1.addVertex(1);
    dg1.addVertex(2);

    dg1.addEdge(1, 2);

    SimpleGraph<Integer, DefaultEdge> sg1 = new SimpleGraph<>(DefaultEdge.class);

    sg1.addVertex(1);
    sg1.addVertex(2);

    sg1.addEdge(1, 2);

    Multigraph<Integer, DefaultEdge> mg1 = new Multigraph<>(DefaultEdge.class);

    mg1.addVertex(1);
    mg1.addVertex(2);

    mg1.addEdge(1, 2);

    Pseudograph<Integer, DefaultEdge> pg1 = new Pseudograph<>(DefaultEdge.class);

    pg1.addVertex(1);
    pg1.addVertex(2);

    pg1.addEdge(1, 2);

    /* GT-0 test graph=null */

    try {
      @SuppressWarnings("unused")
      VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> gt0 =
          new VF2SubgraphIsomorphismInspector<>(null, sg1);
      Assert.fail("Expected UnsupportedOperationException");
    } catch (NullPointerException ex) {
    }

    /* GT-1: multigraphs */

    try {
      @SuppressWarnings("unused")
      VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> gt1 =
          new VF2SubgraphIsomorphismInspector<>(mg1, mg1);
      Assert.fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    }

    /* GT-2: pseudographs */

    try {
      @SuppressWarnings("unused")
      VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> gt2 =
          new VF2SubgraphIsomorphismInspector<>(pg1, pg1);
      Assert.fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    }

    /* GT-3: simple graphs */

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> gt3 =
        new VF2SubgraphIsomorphismInspector<>(sg1, sg1);
    assertEquals(true, gt3.getMappings().hasNext());

    /* GT-4: directed graphs */

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> gt4 =
        new VF2SubgraphIsomorphismInspector<>(dg1, dg1);
    assertEquals("[1=1 2=2]", gt4.getMappings().next().toString());

    /* GT-5: simple graph + multigraph */

    try {
      @SuppressWarnings("unused")
      VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> gt5 =
          new VF2SubgraphIsomorphismInspector<>(sg1, mg1);
      Assert.fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    }

    /* GT-6: simple graph + pseudograph */

    try {
      @SuppressWarnings("unused")
      VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> gt6 =
          new VF2SubgraphIsomorphismInspector<>(sg1, pg1);
      Assert.fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    }

    /* GT-7: directed graph + multigraph */

    try {
      @SuppressWarnings("unused")
      VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> gt7 =
          new VF2SubgraphIsomorphismInspector<>(dg1, mg1);
      Assert.fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    }

    /* GT-8: directed graph + pseudograph */

    try {
      @SuppressWarnings("unused")
      VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> gt8 =
          new VF2SubgraphIsomorphismInspector<>(dg1, pg1);
      Assert.fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    }

    /* GT-9: pseudograph + multigraph */

    try {
      @SuppressWarnings("unused")
      VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> gt9 =
          new VF2SubgraphIsomorphismInspector<>(pg1, mg1);
      Assert.fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    }

    /* GT-10: simple graph + directed graph */

    try {
      @SuppressWarnings("unused")
      VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> gt10 =
          new VF2SubgraphIsomorphismInspector<>(sg1, dg1);
      Assert.fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException ex) {
    }
  }
  @SuppressWarnings({"unchecked", "unchecked"})
  // private mxGraphModel ExtractGraphFromSTD_INPUT() {
  private DirectedGraph<Object, ObjectEdge> ExtractGraphFromSTD_INPUT() {
    ActivityPort stdInput = getInput("STD_INPUT");
    ActivityPort inputfile = getInput("INPUT_FILE_BASE");

    /* @Todo: Reads directly from STD_INPUT. Should (be better) read from file (lots of .log .dot, .dat are generated by vivado workflow */
    Collection<String> lines = UtilStr.SeparateComponents((String) stdInput.getValue(), "\n");
    String startString = "<<BEGIN>><<" + (String) inputfile.getValue() + "_graph_minimized>>";
    String stopString = "<<END>><<" + (String) inputfile.getValue() + "_graph_minimized>>";
    boolean reading = false;

    // rebuild circuit graph
    DirectedGraph<Object, ObjectEdge> graph =
        new DefaultDirectedGraph<Object, ObjectEdge>(ObjectEdge.class);
    Map<String, Object> vertexMap = new HashMap<String, Object>();

    // mxGraphModel circuitGraph = new mxGraphModel();
    // circuitGraph.setCreateIds(false);
    // circuitGraph.beginUpdate();
    // Object rootCell = circuitGraph.getRoot();
    for (String line : lines) {
      if (reading) {
        if (line.equals(stopString)) {
          reading = false;
          break;
        } else {
          // rebuild graph
          Collection<String> wordsCollection = UtilStr.SeparateComponents(line, " ");
          String[] words = wordsCollection.toArray(new String[wordsCollection.size()]);
          int index = 1;
          int i;
          switch (words[0]) {
            case "Vertex":
              mxCell vertexCell = new mxCell();
              vertexCell.setVertex(true);
              vertexCell.setId(words[1]);
              switch (words[2]) {
                case "cell":
                  PLDConfigurableElement pldCell = new PLDConfigurableElement("Cell_0");
                  pldCell.setName(words[5]);
                  pldCell.setInitialValue(words[4]);
                  pldCell.setComponentType(ElectricalComponentType.INTRACHIP);
                  pldCell.setDescription(words[3]);
                  i = 6;
                  while ((i < words.length) && !words[i].equals("=>")) {
                    // String name, Direction direction, Long address, Integer bit, Boolean
                    // mandatory
                    // PhysicalPortPin  pin = new PhysicalPortPin(words[i], Direction.UNKNOWN, 0L,
                    // 0, true);
                    CPSPort pin =
                        new CPSPort(
                            words[i],
                            Direction.UNKNOWN,
                            0L,
                            0,
                            true); // PhysicalPortPin(words[i], Direction.UNKNOWN, 0L, 0, true);
                    if (pldCell.getInterfaceComponent().getChannels().size() == 0) {
                      pldCell
                          .getInterfaceComponent()
                          .getChannels()
                          .add(new ChannelPrimitive("default chanel"));
                    }
                    ChannelAbstract channel =
                        pldCell.getInterfaceComponent().getChannels().iterator().next();
                    channel.getPorts().add(pin);
                    vertexMap.put(words[i], pin);
                    i++;
                  }
                  while (i < words.length - 1) {
                    i++;
                    words[i] = words[i].replaceAll("@", " ");
                    int indexOfEqualSignal = words[i].indexOf("=");
                    String property = words[i].substring(0, indexOfEqualSignal);
                    String propValue = words[i].substring(indexOfEqualSignal + 1);
                    // Boolean isConst, String name, String defaultValue, String type, Direction
                    // direction
                    DiscreteVariable attribute =
                        new DiscreteVariable(
                            false, property, propValue, "UNKNOWN", Direction.UNKNOWN);
                    pldCell.getExtraAttributes().add(attribute);
                  }
                  // pldCell.getInterfaceElectrical().setElectricalFeature(new ElectricalFeature());
                  vertexCell.setValue(pldCell);
                  vertexMap.put(words[1], pldCell);
                  System.out.println("Vertex cell: " + pldCell);
                  graph.addVertex(pldCell);
                  break;
                case "port":
                  // Vertex <NodoIdx> port <Direction> <Name>
                  // EX: Vertex node18 port IN SA
                  Long address = Long.parseLong(words[1].replace("node", ""));
                  Direction dir = Direction.valueOf(words[3]);
                  // String name, Direction direction, Long address, Integer bit, Boolean mandatory
                  PhysicalPortPin pin = new PhysicalPortPin(words[4], dir, address, 0, true);
                  i = 5; // to be ++
                  while (i < words.length - 1) {
                    i++;
                    words[i] = words[i].replaceAll("@", " ");
                    int indexOfEqualSignal = words[i].indexOf("=");
                    String property = words[i].substring(0, indexOfEqualSignal);
                    String propValue = words[i].substring(indexOfEqualSignal + 1);
                    // Boolean isConst, String name, String defaultValue, String type, Direction
                    // direction
                    DiscreteVariable attribute =
                        new DiscreteVariable(
                            false, property, propValue, "UNKNOWN", Direction.UNKNOWN);
                    pin.getExtraAttributes().add(attribute);
                  }
                  vertexCell.setValue(pin);
                  vertexMap.put(words[1], pin); // nodeX
                  vertexMap.put(words[4], pin); // real name
                  System.out.println("Vertex port: " + pin);
                  graph.addVertex(pin);
                  break;
                default:
                  break;
              }
              // circuitGraph.add(rootCell, vertexCell, index++);
              break;
            case "Edge":
              // mxCell edgeCell = new mxCell();
              // edgeCell.setEdge(true);
              // edgeCell.setId(circuitGraph.createId(edgeCell));
              // mxCell source = (mxCell) circuitGraph.getCell(words[1]);
              // assert source != null;
              // edgeCell.setSource(source);
              // mxCell target = (mxCell) circuitGraph.getCell(words[2]);
              // assert target != null;
              // edgeCell.setTarget(target);
              String sourcePinOrPortStr = words[3];
              PhysicalPortPin sourcePinPort, targetPinPort;
              if (sourcePinOrPortStr.indexOf(":") > -1) {
                // is a CELL:PIN (takes only the pin part)
                sourcePinOrPortStr =
                    sourcePinOrPortStr.substring(sourcePinOrPortStr.indexOf(":") + 1);
                sourcePinPort = (PhysicalPortPin) vertexMap.get(sourcePinOrPortStr);
                assert sourcePinPort != null;
                sourcePinPort.setDirection(
                    Direction.OUT); // it is an OUT PIN, so we can read from it
              } else {
                sourcePinPort = (PhysicalPortPin) vertexMap.get(sourcePinOrPortStr);
                assert sourcePinPort != null;
                sourcePinPort.setDirection(
                    Direction.IN); // it is an IN PORT, so we can read from it
              }
              String targetPinOrPortStr = words[4];
              if (targetPinOrPortStr.indexOf(":") > -1) { // is a CELL:PIN (takes only the pin part)
                targetPinOrPortStr =
                    targetPinOrPortStr.substring(targetPinOrPortStr.indexOf(":") + 1);
                targetPinPort = (PhysicalPortPin) vertexMap.get(targetPinOrPortStr);
                assert targetPinPort != null;
                targetPinPort.setDirection(Direction.IN); // it is an IN PIN, so we can write to it
              } else {
                targetPinPort = (PhysicalPortPin) vertexMap.get(targetPinOrPortStr);
                assert targetPinPort != null;
                targetPinPort.setDirection(
                    Direction.OUT); // it is an OUT PORT, so we can write to it
              }
              PhysicalCommunicationNet hardwareNet =
                  new PhysicalCommunicationNet(sourcePinPort, targetPinPort);
              // edgeCell.setValue(hardwareNet);
              //                            i = 5; // to be ++
              //                            while (i < words.length - 1) {
              //                                i++;
              //                                words[i] = words[i].replaceAll("@", " ");
              //                                int indexOfEqualSignal = words[i].indexOf("=");
              //                                String property = words[i].substring(0,
              // indexOfEqualSignal);
              //                                String propValue =
              // words[i].substring(indexOfEqualSignal + 1);
              //                                //Boolean isConst, String name, String defaultValue,
              // String type, Direction direction
              //                                DiscreteVariable attribute = new
              // DiscreteVariable(false, property, propValue, "UNKNOWN", Direction.UNKNOWN);
              //                                //pin.getExtraAttributes().add(attribute);
              //                                /**
              //                                 * @TODO: where net properties are inserted into? *
              //                                 */
              //                            }
              // circuitGraph.add(rootCell, edgeCell, index++);
              Object vSource = vertexMap.get(words[1]);
              Object vTarget = vertexMap.get(words[2]);
              System.out.println(
                  "Edge: "
                      + vSource
                      + " -> "
                      + vTarget
                      + "   << "
                      + sourcePinOrPortStr
                      + " -> "
                      + targetPinOrPortStr
                      + "  >>");
              ObjectEdge edge = graph.addEdge(vSource, vTarget);
              edge.setUserObject(hardwareNet);
              break;
            default:
              Logger.getGlobal()
                  .warning(
                      "Error: Line not recognized when expecting to rebuild the circuit graph: "
                          + line);
              break;
          }
        }
      } else if (line.equals(startString)) {
        reading = true;
      }
    }
    // circuitGraph.endUpdate();
    //        return circuitGraph;
    return graph;
  }
Beispiel #17
0
    @Override
    public String process(File page, Map<String, String> query) {
      loadContigs();

      if (query.get("contigName").matches("^[ACGT]+$")) {
        contigs.put("manual", query.get("contigName"));
        query.put("contigName", "manual");
      } else if (query.get("contigName").matches("^Pf3D7.+$")) {
        String[] pieces = query.get("contigName").split("[:-]");

        int start = Integer.valueOf(pieces[1].replaceAll(",", ""));
        int end = Integer.valueOf(pieces[2].replaceAll(",", ""));

        ReferenceSequence rseq = REF.getSubsequenceAt(pieces[0], start, end);
        contigs.put("manual", new String(rseq.getBases()));
        query.put("contigName", "manual");
      }

      if (query.containsKey("contigName")
          && contigs.containsKey(query.get("contigName"))
          && graphs.containsKey(query.get("graphName"))) {
        boolean showLinks = query.get("showLinks").equals("links_on");

        String contig = contigs.get(query.get("contigName"));
        String originalContig = contigs.get(query.get("contigName"));
        String refFormattedString = "";
        String kmerOrigin = "";

        if (metrics.containsKey(query.get("contigName"))) {
          String[] loc = metrics.get(query.get("contigName")).get("canonicalLocus").split("[:-]");
          if (!loc[0].equals("*")) {
            boolean isRc = metrics.get(query.get("contigName")).get("isRcCanonical").equals("1");

            if (isRc) {
              contig = SequenceUtils.reverseComplement(contig);
              originalContig = SequenceUtils.reverseComplement(originalContig);
            }

            int locStart = Integer.valueOf(loc[1]);
            int locEnd = Integer.valueOf(loc[2]);

            Cigar cigar =
                cigarStringToCigar(metrics.get(query.get("contigName")).get("cigarCanonical"));
            if (cigar.getCigarElement(0).getOperator().equals(CigarOperator.S)) {
              locStart -= cigar.getCigarElement(0).getLength();
            }

            if (cigar
                .getCigarElement(cigar.getCigarElements().size() - 1)
                .getOperator()
                .equals(CigarOperator.S)) {
              locEnd += cigar.getCigarElement(cigar.getCigarElements().size() - 1).getLength();
            }

            String ref = new String(REF.getSubsequenceAt(loc[0], locStart, locEnd).getBases());

            StringBuilder refFormatted = new StringBuilder();
            int pos = 0;
            for (CigarElement ce : cigar.getCigarElements()) {
              CigarOperator co = ce.getOperator();
              switch (co) {
                case S:
                  refFormatted.append(ref.substring(pos, pos + ce.getLength()).toLowerCase());
                  break;
                case M:
                  refFormatted.append(ref.substring(pos, pos + ce.getLength()));
                  break;
                case I:
                  refFormatted.append(StringUtils.repeat("-", ce.getLength()));
                  break;
              }

              if (ce.getOperator().consumesReferenceBases()) {
                pos += ce.getLength();
              }
            }

            refFormattedString = refFormatted.toString();

            kmerOrigin = metrics.get(query.get("contigName")).get("kmerOrigin");
          }
        }

        CortexGraph cg = graphs.get(query.get("graphName"));

        String sampleName = cg.getColor(0).getSampleName();
        Set<CortexLinksMap> links = new HashSet<CortexLinksMap>();
        if (LINKS != null && !LINKS.isEmpty()) {
          for (CortexLinksMap link : LINKS) {
            if (sampleName.equals(link.getCortexLinks().getColor(0).getSampleName())) {
              links.add(link);
            }
          }
        }

        Set<String> contigKmers = new HashSet<String>();
        for (int i = 0; i <= contig.length() - cg.getKmerSize(); i++) {
          String curKmer = contig.substring(i, i + cg.getKmerSize());

          contigKmers.add(curKmer);
        }

        StringBuilder firstFlank = new StringBuilder();
        String firstKmer = contig.substring(0, cg.getKmerSize());
        Set<String> pks = CortexUtils.getPrevKmers(cg, firstKmer, 0);
        Set<String> usedPrevKmers = new HashSet<String>();
        usedPrevKmers.add(firstKmer);
        while (pks.size() == 1 && usedPrevKmers.size() <= 100) {
          String kmer = pks.iterator().next();
          firstFlank.insert(0, kmer.charAt(0));

          if (usedPrevKmers.contains(kmer)) {
            break;
          }
          usedPrevKmers.add(kmer);

          pks = CortexUtils.getPrevKmers(cg, kmer, 0);
        }

        StringBuilder lastFlank = new StringBuilder();
        String lastKmer = contig.substring(contig.length() - cg.getKmerSize(), contig.length());
        Set<String> nks = CortexUtils.getNextKmers(cg, lastKmer, 0);
        Set<String> usedNextKmers = new HashSet<String>();
        usedNextKmers.add(lastKmer);
        while (nks.size() == 1 && usedNextKmers.size() <= 100) {
          String kmer = nks.iterator().next();
          lastFlank.append(kmer.charAt(kmer.length() - 1));

          if (usedNextKmers.contains(kmer)) {
            break;
          }
          usedNextKmers.add(kmer);

          nks = CortexUtils.getNextKmers(cg, kmer, 0);
        }

        contig = firstFlank.toString() + contig + lastFlank.toString();

        DirectedGraph<CtxVertex, MultiEdge> g =
            new DefaultDirectedGraph<CtxVertex, MultiEdge>(MultiEdge.class);
        for (int i = 0; i <= contig.length() - cg.getKmerSize(); i++) {
          String curKmer = contig.substring(i, i + cg.getKmerSize());
          CortexKmer ck = new CortexKmer(curKmer);
          CtxVertex curVer =
              new CtxVertex(
                  curKmer,
                  i,
                  contigKmers.contains(curKmer) ? VertexType.CONTIG : VertexType.CLIPPED,
                  cg.findRecord(ck));

          g.addVertex(curVer);

          String expectedPrevKmer =
              (i > 0) ? contig.substring(i - 1, i - 1 + cg.getKmerSize()) : "";
          String expectedNextKmer =
              (i < contig.length() - cg.getKmerSize())
                  ? contig.substring(i + 1, i + 1 + cg.getKmerSize())
                  : "";

          Set<String> prevKmers = CortexUtils.getPrevKmers(cg, curKmer, 0);
          for (String prevKmer : prevKmers) {
            if (!expectedPrevKmer.equals(prevKmer)) {
              CortexKmer pk = new CortexKmer(prevKmer);
              CtxVertex prevVer = new CtxVertex(prevKmer, i - 1, VertexType.IN, cg.findRecord(pk));

              MultiEdge me =
                  g.containsEdge(prevVer, curVer) ? g.getEdge(prevVer, curVer) : new MultiEdge();
              me.addGraphName(cg.getCortexFile().getName());

              g.addVertex(prevVer);
              g.addEdge(prevVer, curVer, me);
            }
          }

          Set<String> nextKmers = CortexUtils.getNextKmers(cg, curKmer, 0);
          for (String nextKmer : nextKmers) {
            if (!expectedNextKmer.equals(nextKmer)) {
              CortexKmer nk = new CortexKmer(nextKmer);
              CtxVertex nextVer = new CtxVertex(nextKmer, i + 1, VertexType.OUT, cg.findRecord(nk));

              MultiEdge me =
                  g.containsEdge(curVer, nextVer) ? g.getEdge(curVer, nextVer) : new MultiEdge();
              me.addGraphName(cg.getCortexFile().getName());

              g.addVertex(nextVer);
              g.addEdge(curVer, nextVer, me);
            }
          }
        }

        Set<Map<String, Object>> verticesWithLinks = new HashSet<Map<String, Object>>();
        DataFrame<String, String, Integer> hv = new DataFrame<String, String, Integer>(0);

        for (int q = 0; q <= contig.length() - cg.getKmerSize(); q++) {
          // String sk = cv.getBinaryKmer();
          String sk = contig.substring(q, q + cg.getKmerSize());
          CortexKmer ck = new CortexKmer(sk);

          for (CortexLinksMap link : links) {
            if (link.containsKey(ck)) {
              CortexLinksRecord clr = link.get(ck);
              Map<String, Integer> lc =
                  (!showLinks)
                      ? new HashMap<String, Integer>()
                      : CortexUtils.getKmersAndCoverageInLink(cg, sk, clr);

              Map<String, Object> entry = new HashMap<String, Object>();
              entry.put("kmer", sk);
              entry.put("lc", lc);

              verticesWithLinks.add(entry);

              if (showLinks) {
                for (CortexJunctionsRecord cjr : clr.getJunctions()) {
                  List<String> lk = CortexUtils.getKmersInLink(cg, sk, cjr);

                  for (int i = 0; i < lk.size(); i++) {
                    String kili = lk.get(i);

                    for (int j = 0; j < lk.size(); j++) {
                      String kilj = lk.get(j);

                      if (i != j) {
                        hv.set(kili, kilj, hv.get(kili, kilj) + cjr.getCoverage(0));
                      }
                    }
                  }
                }
              }
            }
          }
        }

        /*
        int hvMax = 0;
        Map<String, Integer> hvlin = new HashMap<String, Integer>();
        if (showLinks) {
            for (String kili : hv.getRowNames()) {
                for (String kilj : hv.getColNames()) {
                    int cov = hv.get(kili, kilj);

                    String id = kili + "_" + kilj;
                    hvlin.put(id, cov);

                    if (cov > hvMax) {
                        hvMax = cov;
                    }
                }
            }
        }
        */

        JSONObject jo = new JSONObject();
        jo.put("contig", contig);
        jo.put("originalContig", originalContig);
        jo.put("ref", refFormattedString);
        jo.put("kmerOrigin", kmerOrigin);
        jo.put("kmerSize", cg.getKmerSize());
        jo.put("clipStart", firstFlank.length());
        jo.put("clipEnd", contig.length() - lastFlank.length());

        List<Map<String, Object>> va = new ArrayList<Map<String, Object>>();
        for (CtxVertex v : g.vertexSet()) {
          Map<String, Object> vm = new HashMap<String, Object>();
          vm.put("base", v.getBase());
          vm.put("kmer", v.getKmer());
          vm.put("pos", v.getPos());
          vm.put("type", v.getVertexType().name());
          vm.put("missing", v.isMissingFromGraph());
          vm.put("cov", v.getCoverage());

          va.add(vm);
        }

        jo.put("vertices", va);
        jo.put("verticesWithLinks", verticesWithLinks);
        // jo.put("hvlin", hvlin);
        // jo.put("hvmax", hvMax);

        return jo.toString();
      }

      return null;
    }
  @Test
  public void testExhaustive() {

    /* DET-1:
     *
     *      0   3
     *      |  /|        0 2
     * g1 = | 2 |   g2 = |/
     *      |/  |        1
     *      1   4
     */

    SimpleGraph<Integer, DefaultEdge> g1 = new SimpleGraph<>(DefaultEdge.class),
        g2 = new SimpleGraph<>(DefaultEdge.class);

    g1.addVertex(0);
    g1.addVertex(1);
    g1.addVertex(2);
    g1.addVertex(3);
    g1.addVertex(4);

    g2.addVertex(0);
    g2.addVertex(1);
    g2.addVertex(2);

    g1.addEdge(0, 1);
    g1.addEdge(1, 2);
    g1.addEdge(2, 3);
    g1.addEdge(3, 4);

    g2.addEdge(0, 1);
    g2.addEdge(1, 2);

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf2 =
        new VF2SubgraphIsomorphismInspector<>(g1, g2);

    assertEquals(true, SubgraphIsomorphismTestUtils.containsAllMatchings(vf2, g1, g2));

    /* DET-2:
     *
     * g3 = ...   g4 = ...
     *
     */

    DirectedGraph<Integer, DefaultEdge> g3 = new DefaultDirectedGraph<>(DefaultEdge.class),
        g4 = new DefaultDirectedGraph<>(DefaultEdge.class);

    g3.addVertex(0);
    g3.addVertex(1);
    g3.addVertex(2);
    g3.addVertex(3);
    g3.addVertex(4);
    g3.addVertex(5);

    g4.addVertex(0);
    g4.addVertex(1);
    g4.addVertex(2);
    g4.addVertex(3);

    g3.addEdge(0, 1);
    g3.addEdge(0, 5);
    g3.addEdge(1, 4);
    g3.addEdge(2, 1);
    g3.addEdge(2, 4);
    g3.addEdge(3, 1);
    g3.addEdge(4, 0);
    g3.addEdge(5, 2);
    g3.addEdge(5, 4);

    g4.addEdge(0, 3);
    g4.addEdge(1, 2);
    g4.addEdge(1, 3);
    g4.addEdge(2, 3);
    g4.addEdge(2, 0);

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf3 =
        new VF2SubgraphIsomorphismInspector<>(g3, g4);

    assertEquals(true, SubgraphIsomorphismTestUtils.containsAllMatchings(vf3, g3, g4));

    /* DET-3:
     *
     *      1----0        0---2
     *      |             |  /
     * g5 = |        g6 = | /
     *      |             |/
     *      2----3        1
     */

    SimpleGraph<Integer, DefaultEdge> g5 = new SimpleGraph<>(DefaultEdge.class),
        g6 = new SimpleGraph<>(DefaultEdge.class);

    g5.addVertex(0);
    g5.addVertex(1);
    g5.addVertex(2);
    g5.addVertex(3);

    g6.addVertex(0);
    g6.addVertex(1);
    g6.addVertex(2);

    g5.addEdge(0, 1);
    g5.addEdge(1, 2);
    g5.addEdge(2, 3);

    g6.addEdge(0, 1);
    g6.addEdge(1, 2);
    g6.addEdge(2, 0);

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf4 =
        new VF2SubgraphIsomorphismInspector<>(g5, g6);

    assertEquals(true, SubgraphIsomorphismTestUtils.containsAllMatchings(vf4, g5, g6));
  }
  @BeforeClass
  public static void setUp() {
    // *********** Process Graph 1 ******************
    process1 = new DefaultDirectedGraph<ActivityNode, DefaultEdge>(DefaultEdge.class);
    process1R1 = new ActivityNode("R1", "R", ActivityType.INVOKE);
    process1O1 = new ActivityNode("O1", "O", ActivityType.INVOKE);
    process1O2 = new ActivityNode("O2", "O", ActivityType.INVOKE);
    process1I1 = new ActivityNode("I1", "I", ActivityType.INVOKE);
    process1A1 = new ActivityNode("A1", "A", ActivityType.INVOKE);
    process1A2 = new ActivityNode("A2", "A", ActivityType.INVOKE);
    process1K1 = new ActivityNode("K1", "K", ActivityType.INVOKE);
    process1C1 = new ActivityNode("C1", "C", ActivityType.INVOKE);
    process1C2 = new ActivityNode("C2", "C", ActivityType.INVOKE);
    process1D1 = new ActivityNode("D1", "D", ActivityType.INVOKE);
    process1E1 = new ActivityNode("E1", "E", ActivityType.INVOKE);
    process1F1 = new ActivityNode("F1", "F", ActivityType.INVOKE);
    process1F2 = new ActivityNode("F2", "F", ActivityType.INVOKE);
    process1G1 = new ActivityNode("G1", "G", ActivityType.INVOKE);
    process1H1 = new ActivityNode("H1", "H", ActivityType.INVOKE);
    process1N1 = new ActivityNode("N1", "N", ActivityType.INVOKE);
    process1N2 = new ActivityNode("N2", "N", ActivityType.INVOKE);
    process1M1 = new ActivityNode("M1", "M", ActivityType.INVOKE);
    process1T1 = new ActivityNode("T1", "T", ActivityType.INVOKE);
    process1X1 = new ActivityNode("X1", "X", ActivityType.INVOKE);
    process1X2 = new ActivityNode("X2", "X", ActivityType.INVOKE);
    process1Y1 = new ActivityNode("Y1", "Y", ActivityType.INVOKE);
    process1Y2 = new ActivityNode("Y2", "Y", ActivityType.INVOKE);
    process1Y3 = new ActivityNode("Y3", "Y", ActivityType.INVOKE);
    process1Z1 = new ActivityNode("Z1", "Z", ActivityType.INVOKE);
    process1Z2 = new ActivityNode("Z2", "Z", ActivityType.INVOKE);

    process1.addVertex(process1R1);
    process1.addVertex(process1O1);
    process1.addVertex(process1O2);
    process1.addVertex(process1I1);
    process1.addVertex(process1A1);
    process1.addVertex(process1A2);
    process1.addVertex(process1K1);
    process1.addVertex(process1C1);
    process1.addVertex(process1C2);
    process1.addVertex(process1D1);
    process1.addVertex(process1E1);
    process1.addVertex(process1F1);
    process1.addVertex(process1F2);
    process1.addVertex(process1G1);
    process1.addVertex(process1H1);
    process1.addVertex(process1N1);
    process1.addVertex(process1N2);
    process1.addVertex(process1M1);
    process1.addVertex(process1T1);
    process1.addVertex(process1X1);
    process1.addVertex(process1X2);
    process1.addVertex(process1Y1);
    process1.addVertex(process1Y2);
    process1.addVertex(process1Y3);
    process1.addVertex(process1Z1);
    process1.addVertex(process1Z2);

    process1.addEdge(process1R1, process1O1);
    process1.addEdge(process1R1, process1I1);
    process1.addEdge(process1O1, process1A1);
    process1.addEdge(process1O1, process1X1);
    process1.addEdge(process1O1, process1K1);
    process1.addEdge(process1I1, process1K1);
    process1.addEdge(process1I1, process1O2);
    process1.addEdge(process1A1, process1C1);
    process1.addEdge(process1A1, process1N1);
    process1.addEdge(process1X1, process1N1);
    process1.addEdge(process1K1, process1X2);
    process1.addEdge(process1K1, process1T1);
    process1.addEdge(process1K1, process1A2);
    process1.addEdge(process1O2, process1A2);
    process1.addEdge(process1C1, process1F1);
    process1.addEdge(process1F1, process1N2);
    process1.addEdge(process1F1, process1M1);
    process1.addEdge(process1N1, process1Y1);
    process1.addEdge(process1Y1, process1Z1);
    process1.addEdge(process1X2, process1Y2);
    process1.addEdge(process1T1, process1C2);
    process1.addEdge(process1C2, process1E1);
    process1.addEdge(process1C2, process1D1);
    process1.addEdge(process1C2, process1F2);
    process1.addEdge(process1E1, process1G1);
    process1.addEdge(process1E1, process1H1);
    process1.addEdge(process1A2, process1Y3);
    process1.addEdge(process1Y3, process1Z2);

    processGraph1 = new ProcessGraph("processDAGInexactTest1", "www", "100", process1);

    query1 = new DefaultDirectedGraph<ActivityNode, DefaultEdge>(DefaultEdge.class);
    query1O = new ActivityNode("q1O", "O", ActivityType.INVOKE);
    query1A = new ActivityNode("q1A", "A", ActivityType.INVOKE);
    query1B = new ActivityNode("q1B", "B", ActivityType.INVOKE);
    query1C = new ActivityNode("q1C", "C", ActivityType.INVOKE);
    query1D = new ActivityNode("q1D", "D", ActivityType.INVOKE);
    query1E = new ActivityNode("q1E", "E", ActivityType.INVOKE);
    query1F = new ActivityNode("q1F", "F", ActivityType.INVOKE);
    query1G = new ActivityNode("q1G", "G", ActivityType.INVOKE);
    query1H = new ActivityNode("q1H", "H", ActivityType.INVOKE);
    query1X = new ActivityNode("q1X", "X", ActivityType.INVOKE);
    query1Y = new ActivityNode("q1Y", "Y", ActivityType.INVOKE);
    query1Z = new ActivityNode("q1Z", "Z", ActivityType.INVOKE);

    query1.addVertex(query1O);
    query1.addVertex(query1A);
    query1.addVertex(query1B);
    query1.addVertex(query1C);
    query1.addVertex(query1D);
    query1.addVertex(query1E);
    query1.addVertex(query1F);
    query1.addVertex(query1G);
    query1.addVertex(query1H);
    query1.addVertex(query1X);
    query1.addVertex(query1Y);
    query1.addVertex(query1Z);

    query1.addEdge(query1O, query1A);
    query1.addEdge(query1O, query1B);
    query1.addEdge(query1O, query1X);
    query1.addEdge(query1X, query1Y);
    query1.addEdge(query1Y, query1Z);
    query1.addEdge(query1A, query1Y);
    query1.addEdge(query1A, query1C);
    query1.addEdge(query1B, query1C);
    query1.addEdge(query1C, query1D);
    query1.addEdge(query1C, query1E);
    query1.addEdge(query1C, query1F);
    query1.addEdge(query1E, query1G);
    query1.addEdge(query1E, query1H);

    queryGraph1 = new QueryGraph(query1);
  }
  /** Tests edge cases on directed graphs */
  @Test
  public void testEdgeCasesDirectedGraph() {

    /* ECD-1: graph and subgraph empty */

    DirectedGraph<Integer, DefaultEdge> dg0v = new DefaultDirectedGraph<>(DefaultEdge.class),
        dg0v2 = new DefaultDirectedGraph<>(DefaultEdge.class);

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf1 =
        new VF2SubgraphIsomorphismInspector<>(dg0v, dg0v2);

    assertEquals("[]", vf1.getMappings().next().toString());

    /* ECD-2: graph non-empty, subgraph empty */

    DirectedGraph<Integer, DefaultEdge> dg4v3e = new DefaultDirectedGraph<>(DefaultEdge.class);

    dg4v3e.addVertex(1);
    dg4v3e.addVertex(2);
    dg4v3e.addVertex(3);
    dg4v3e.addVertex(4);

    dg4v3e.addEdge(1, 2);
    dg4v3e.addEdge(3, 2);
    dg4v3e.addEdge(3, 4);

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf2 =
        new VF2SubgraphIsomorphismInspector<>(dg4v3e, dg0v);

    assertEquals("[1=~~ 2=~~ 3=~~ 4=~~]", vf2.getMappings().next().toString());

    /* ECD-3: graph empty, subgraph non-empty */

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf3 =
        new VF2SubgraphIsomorphismInspector<>(dg0v, dg4v3e);

    assertEquals(false, vf3.isomorphismExists());

    /* ECD-4: graph non-empty, subgraph single vertex */

    DirectedGraph<Integer, DefaultEdge> dg1v = new DefaultDirectedGraph<>(DefaultEdge.class);

    dg1v.addVertex(5);

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf4 =
        new VF2SubgraphIsomorphismInspector<>(dg4v3e, dg1v);

    Iterator<GraphMapping<Integer, DefaultEdge>> iter4 = vf4.getMappings();

    Set<String> mappings =
        new HashSet<>(
            Arrays.asList(
                "[1=5 2=~~ 3=~~ 4=~~]",
                "[1=~~ 2=5 3=~~ 4=~~]",
                "[1=~~ 2=~~ 3=5 4=~~]",
                "[1=~~ 2=~~ 3=~~ 4=5]"));
    assertEquals(true, mappings.remove(iter4.next().toString()));
    assertEquals(true, mappings.remove(iter4.next().toString()));
    assertEquals(true, mappings.remove(iter4.next().toString()));
    assertEquals(true, mappings.remove(iter4.next().toString()));
    assertEquals(false, iter4.hasNext());

    /* ECD-5: graph empty, subgraph single vertex */

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf5 =
        new VF2SubgraphIsomorphismInspector<>(dg0v, dg1v);

    assertEquals(false, vf5.isomorphismExists());

    /* ECD-6: subgraph with vertices, but no edges */

    DirectedGraph<Integer, DefaultEdge> dg3v0e = new DefaultDirectedGraph<>(DefaultEdge.class);

    dg3v0e.addVertex(5);
    dg3v0e.addVertex(6);
    dg3v0e.addVertex(7);

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf6 =
        new VF2SubgraphIsomorphismInspector<>(dg4v3e, dg3v0e);

    assertEquals(false, vf6.isomorphismExists());

    /* ECD-7: graph and subgraph with vertices, but no edges */

    DirectedGraph<Integer, DefaultEdge> dg2v0e = new DefaultDirectedGraph<>(DefaultEdge.class);

    dg2v0e.addVertex(1);
    dg2v0e.addVertex(2);

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf7 =
        new VF2SubgraphIsomorphismInspector<>(dg3v0e, dg2v0e);

    Iterator<GraphMapping<Integer, DefaultEdge>> iter7 = vf7.getMappings();

    Set<String> mappings7 =
        new HashSet<>(
            Arrays.asList(
                "[5=1 6=2 7=~~]",
                "[5=1 6=~~ 7=2]",
                "[5=2 6=1 7=~~]",
                "[5=~~ 6=1 7=2]",
                "[5=2 6=~~ 7=1]",
                "[5=~~ 6=2 7=1]"));
    assertEquals(true, mappings7.remove(iter7.next().toString()));
    assertEquals(true, mappings7.remove(iter7.next().toString()));
    assertEquals(true, mappings7.remove(iter7.next().toString()));
    assertEquals(true, mappings7.remove(iter7.next().toString()));
    assertEquals(true, mappings7.remove(iter7.next().toString()));
    assertEquals(true, mappings7.remove(iter7.next().toString()));
    assertEquals(false, iter7.hasNext());

    /* ECD-8: graph no edges, subgraph contains single edge */

    DirectedGraph<Integer, DefaultEdge> dg2v1e = new DefaultDirectedGraph<>(DefaultEdge.class);

    dg2v1e.addVertex(5);
    dg2v1e.addVertex(6);

    dg2v1e.addEdge(5, 6);

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf8 =
        new VF2SubgraphIsomorphismInspector<>(dg3v0e, dg2v1e);

    assertEquals(false, vf8.isomorphismExists());

    /* ECD-9: complete graphs of different size,
     * graph smaller than subgraph */

    DirectedGraph<Integer, DefaultEdge> dg5c = new DefaultDirectedGraph<>(DefaultEdge.class);

    dg5c.addVertex(0);
    dg5c.addVertex(1);
    dg5c.addVertex(2);
    dg5c.addVertex(3);
    dg5c.addVertex(4);

    dg5c.addEdge(0, 1);
    dg5c.addEdge(0, 2);
    dg5c.addEdge(0, 3);
    dg5c.addEdge(0, 4);
    dg5c.addEdge(1, 2);
    dg5c.addEdge(1, 3);
    dg5c.addEdge(1, 4);
    dg5c.addEdge(2, 3);
    dg5c.addEdge(2, 4);
    dg5c.addEdge(3, 4);

    DirectedGraph<Integer, DefaultEdge> dg4c = new DefaultDirectedGraph<>(DefaultEdge.class);

    dg4c.addVertex(0);
    dg4c.addVertex(1);
    dg4c.addVertex(2);
    dg4c.addVertex(3);

    dg4c.addEdge(0, 1);
    dg4c.addEdge(0, 2);
    dg4c.addEdge(0, 3);
    dg4c.addEdge(1, 2);
    dg4c.addEdge(1, 3);
    dg4c.addEdge(2, 3);

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf9 =
        new VF2SubgraphIsomorphismInspector<>(dg4c, dg5c);

    assertEquals(false, vf9.isomorphismExists());

    /* ECD-10: complete graphs of different size,
     * graph bigger than subgraph */

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf10 =
        new VF2SubgraphIsomorphismInspector<>(dg5c, dg4c);

    Iterator<GraphMapping<Integer, DefaultEdge>> iter10 = vf10.getMappings();

    Set<String> mappings10 =
        new HashSet<>(
            Arrays.asList(
                "[0=0 1=1 2=2 3=3 4=~~]",
                "[0=0 1=1 2=2 3=~~ 4=3]",
                "[0=0 1=1 2=~~ 3=2 4=3]",
                "[0=0 1=~~ 2=1 3=2 4=3]",
                "[0=~~ 1=0 2=1 3=2 4=3]"));
    assertEquals(true, mappings10.remove(iter10.next().toString()));
    assertEquals(true, mappings10.remove(iter10.next().toString()));
    assertEquals(true, mappings10.remove(iter10.next().toString()));
    assertEquals(true, mappings10.remove(iter10.next().toString()));
    assertEquals(true, mappings10.remove(iter10.next().toString()));
    assertEquals(false, iter10.hasNext());

    /* ECD-11: isomorphic graphs */

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf11 =
        new VF2SubgraphIsomorphismInspector<>(dg4v3e, dg4v3e);

    Iterator<GraphMapping<Integer, DefaultEdge>> iter11 = vf11.getMappings();

    assertEquals("[1=1 2=2 3=3 4=4]", iter11.next().toString());
    assertEquals(false, iter11.hasNext());

    /* ECD-12: not connected graphs of different size */

    DirectedGraph<Integer, DefaultEdge> dg6v4enc = new DefaultDirectedGraph<>(DefaultEdge.class);

    dg6v4enc.addVertex(0);
    dg6v4enc.addVertex(1);
    dg6v4enc.addVertex(2);
    dg6v4enc.addVertex(3);
    dg6v4enc.addVertex(4);
    dg6v4enc.addVertex(5);

    dg6v4enc.addEdge(1, 2);
    dg6v4enc.addEdge(2, 3);
    dg6v4enc.addEdge(3, 1);
    dg6v4enc.addEdge(4, 5);

    DirectedGraph<Integer, DefaultEdge> dg5v4enc = new DefaultDirectedGraph<>(DefaultEdge.class);

    dg5v4enc.addVertex(6);
    dg5v4enc.addVertex(7);
    dg5v4enc.addVertex(8);
    dg5v4enc.addVertex(9);
    dg5v4enc.addVertex(10);

    dg5v4enc.addEdge(7, 6);
    dg5v4enc.addEdge(9, 8);
    dg5v4enc.addEdge(10, 9);
    dg5v4enc.addEdge(8, 10);

    VF2SubgraphIsomorphismInspector<Integer, DefaultEdge> vf12 =
        new VF2SubgraphIsomorphismInspector<>(dg6v4enc, dg5v4enc);

    Iterator<GraphMapping<Integer, DefaultEdge>> iter12 = vf12.getMappings();

    Set<String> mappings12 =
        new HashSet<>(
            Arrays.asList(
                "[0=~~ 1=8 2=10 3=9 4=7 5=6]",
                "[0=~~ 1=9 2=8 3=10 4=7 5=6]",
                "[0=~~ 1=10 2=9 3=8 4=7 5=6]"));
    assertEquals(true, mappings12.remove(iter12.next().toString()));
    assertEquals(true, mappings12.remove(iter12.next().toString()));
    assertEquals(true, mappings12.remove(iter12.next().toString()));
    assertEquals(false, iter12.hasNext());
  }
 public void addReference(TableToTableReference reference) {
   checkNotNull(reference, "reference must not be null");
   graph.addEdge(reference.getReferencingTable(), reference.getReferencedTable(), reference);
 }