Example #1
0
  public void testTopologicalSort() {
    SemanticGraph gr = SemanticGraph.valueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]");
    verifyTopologicalSort(gr);

    List<IndexedWord> vertices = gr.vertexListSorted();
    gr.addEdge(
        vertices.get(1),
        vertices.get(2),
        UniversalEnglishGrammaticalRelations.DIRECT_OBJECT,
        1.0,
        false);
    verifyTopologicalSort(gr);

    gr = SemanticGraph.valueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]");
    vertices = gr.vertexListSorted();
    gr.addEdge(
        vertices.get(2),
        vertices.get(1),
        UniversalEnglishGrammaticalRelations.DIRECT_OBJECT,
        1.0,
        false);
    verifyTopologicalSort(gr);

    gr = SemanticGraph.valueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]");
    vertices = gr.vertexListSorted();
    gr.addEdge(
        vertices.get(1),
        vertices.get(3),
        UniversalEnglishGrammaticalRelations.DIRECT_OBJECT,
        1.0,
        false);
    verifyTopologicalSort(gr);

    // now create a graph with a directed loop, which we should not
    // be able to topologically sort
    gr = SemanticGraph.valueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]");
    vertices = gr.vertexListSorted();
    gr.addEdge(
        vertices.get(3),
        vertices.get(0),
        UniversalEnglishGrammaticalRelations.DIRECT_OBJECT,
        1.0,
        false);
    try {
      verifyTopologicalSort(gr);
      throw new RuntimeException("Expected to fail");
    } catch (IllegalStateException e) {
      // yay, correctly caught error
    }
  }
Example #2
0
  public void testHasChildren() {
    SemanticGraph gr = SemanticGraph.valueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]");

    List<IndexedWord> vertices = gr.vertexListSorted();
    for (IndexedWord word : vertices) {
      if (word.word().equals("ate") || word.word().equals("muffins")) {
        assertTrue(gr.hasChildren(word));
      } else {
        assertFalse(gr.hasChildren(word));
      }
    }
  }