@Test
  public void shouldHaveNoCycles() {
    Graph<String> graph = new Graph<>();
    packageDependencies.forEach(
        (key, set) -> {
          Node<String> node = graph.findOrCreateNode(key);
          set.stream()
              .filter(packageDependencies::containsKey)
              .forEach(target -> node.linkedTo(graph.findOrCreateNode(target)));
        });

    graph.topologicalSort();

    System.out.println("----------------------- dependencies:");
    System.out.println(graph.toString());
    System.out.println("-----------------------");
  }
Beispiel #2
0
  @Test
  public void offsetMinMaxAndPrecedingFollowing() {
    Graph g = new Graph();

    Anchor a = new Anchor("test", null);
    g.addAnchor(a);

    assertNull("offsetMin - null offset, no preceding", a.getOffsetMin());

    g.addAnchor(new Anchor("early", null));
    g.addAnnotation(new Annotation("toEarly", "direct to early", "layer1", "early", "test"));
    assertNull("offsetMin - null offsets, one preceding", a.getOffsetMin());

    g.addAnchor(new Anchor("earlier", null));
    g.addAnnotation(
        new Annotation("toEarlier", "chained to earlier", "layer1", "earlier", "early"));
    assertNull("offsetMin - null offsets, chain preceding", a.getOffsetMin());

    g.addAnchor(new Anchor("earliest", null));
    g.addAnnotation(
        new Annotation("toEarliest", "direct to earliest", "layer2", "earliest", "test"));
    assertNull("offsetMin - null offsets, multiple paths preceding", a.getOffsetMin());

    g.getAnchor("earliest").setOffset(1.0);
    assertEquals("offsetMin - earliest", new Double(1.0), a.getOffsetMin());

    g.getAnchor("earlier").setOffset(2.0);
    assertEquals(
        "offsetMin - chain overrides direct connection", new Double(2.0), a.getOffsetMin());

    g.getAnchor("early").setOffset(3.0);
    assertEquals(
        "offsetMin - direct connection overrides chain", new Double(3.0), a.getOffsetMin());

    assertNull("offsetMax - null offset, no following", a.getOffsetMax());

    g.addAnchor(new Anchor("late", null));
    g.addAnnotation(new Annotation("toLate", "direct to late", "layer1", "test", "late"));
    assertNull("offsetMax - null offsets, one following", a.getOffsetMax());

    g.addAnchor(new Anchor("later", null));
    g.addAnnotation(new Annotation("toLater", "chained to later", "layer1", "late", "later"));
    assertNull("offsetMax - null offsets, chain following", a.getOffsetMax());

    g.addAnchor(new Anchor("latest", null));
    g.addAnnotation(new Annotation("toLatest", "direct to latest", "layer2", "test", "latest"));
    assertNull("offsetMax - null offsets, multiple paths following", a.getOffsetMax());

    g.getAnchor("latest").setOffset(7.0);
    assertEquals("offsetMax - latest", new Double(7.0), a.getOffsetMax());

    g.getAnchor("later").setOffset(6.0);
    assertEquals(
        "offsetMax - chain overrides direct connection", new Double(6.0), a.getOffsetMax());

    g.getAnchor("late").setOffset(5.0);
    assertEquals(
        "offsetMax - direct connection overrides chain", new Double(5.0), a.getOffsetMax());

    a.setOffset(5.0);
    assertEquals("offsetMin - offset set", new Double(5.0), a.getOffsetMin());
    assertEquals("offsetMax - offset set", new Double(5.0), a.getOffsetMax());

    // preceding
    LinkedHashSet<Anchor> preceding = a.getPreceding();
    assertTrue("preceding", preceding.contains(g.getAnchor("earliest")));
    assertTrue("preceding", preceding.contains(g.getAnchor("earlier")));
    assertTrue("preceding", preceding.contains(g.getAnchor("early")));
    assertEquals("preceding size", 3, preceding.size());

    // following
    LinkedHashSet<Anchor> following = a.getFollowing();
    assertTrue("following", following.contains(g.getAnchor("late")));
    assertTrue("following", following.contains(g.getAnchor("later")));
    assertTrue("following", following.contains(g.getAnchor("latest")));
    assertEquals("following size", 3, following.size());

    // follows
    assertTrue("follows", a.follows(g.getAnchor("early")));
    assertTrue("follows", a.follows(g.getAnchor("earlier")));
    assertTrue("follows", a.follows(g.getAnchor("early")));
    assertFalse("follows - self", a.follows(a));
    assertFalse("follows", a.follows(g.getAnchor("late")));
    assertFalse("follows", a.follows(g.getAnchor("later")));
    assertFalse("follows", a.follows(g.getAnchor("latest")));

    // precedes
    assertFalse("precedes", a.precedes(g.getAnchor("early")));
    assertFalse("precedes", a.precedes(g.getAnchor("earlier")));
    assertFalse("precedes", a.precedes(g.getAnchor("early")));
    assertFalse("precedes - self", a.precedes(a));
    assertTrue("precedes", a.precedes(g.getAnchor("late")));
    assertTrue("precedes", a.precedes(g.getAnchor("later")));
    assertTrue("precedes", a.precedes(g.getAnchor("latest")));

    // add some instants
    g.addAnnotation(
        new Annotation("middleInstant", "instant at test anchor", "layer1", "test", "test"));
    g.addAnnotation(
        new Annotation("firstInstant", "instant at earliest", "layer2", "earliest", "earliest"));
    g.addAnnotation(
        new Annotation("lastInstant", "instant at latest", "layer2", "latest", "latest"));

    // ending
    LinkedHashSet<Annotation> ending = a.getEndingAnnotations();
    assertTrue("ending", ending.contains(g.getAnnotation("toEarly")));
    assertTrue("ending", ending.contains(g.getAnnotation("toEarliest")));
    assertTrue("ending", ending.contains(g.getAnnotation("middleInstant")));
    assertEquals("ending size", 3, ending.size());

    // starting
    LinkedHashSet<Annotation> starting = a.getStartingAnnotations();
    assertTrue("starting", starting.contains(g.getAnnotation("toLate")));
    assertTrue("starting", starting.contains(g.getAnnotation("toLatest")));
    assertTrue("starting", starting.contains(g.getAnnotation("middleInstant")));
    assertEquals("starting size", 3, starting.size());

    // linking
    assertNull("linking - none", a.annotationTo(g.getAnchor("later")));
    assertEquals("linking", g.getAnnotation("toLate"), a.annotationTo(g.getAnchor("late")));
    assertNull("linking - not symmetric", g.getAnchor("late").annotationTo(a));
  }