src/main/java/de/dfki/lt/loot/digraph/io/SimpleGraphReader.java
public static <EI> void readGraphGeneric(Reader in, Graph<EI> result,
      BiConsumer<Integer, String> nodeInfo,
      BiFunction<Edge<EI>, String, EI> edgeInfo)
    throws IOException {
    BufferedReader bin = new BufferedReader(in);

    EqualsPredicate<String> eqPred = new EqualsPredicate<String>();

    VertexPropertyMap<String> names = new VertexListPropertyMap<String>(result);
    result.register("names", names);

    String nextLine = null;
    while((nextLine = bin.readLine()) != null) {
      String[] fromTo = nextLine.split("\\s*-->\\s*");
      if (fromTo.length < 1)
        continue;
      Matcher node = pat.matcher(fromTo[0]);
      if (! node.matches()) {
        throw new IOException("Wrong input line: " + nextLine);
      }

      String fromName = node.group(1);
      List<Integer> nodes = names.findVertices(fromName, eqPred);
      int from = -1;
      if (nodes.isEmpty()) {
        from = result.newVertex();
        names.put(from, fromName);
      } else {
        from = nodes.get(0);
      }
      if (node.group(2) != null && ! node.group(2).trim().isEmpty()) {
        nodeInfo.accept(from, node.group(2).trim());
      }

      if (fromTo.length == 2) {
        Matcher nameAndWeights = pat.matcher(fromTo[1]);
        int start = 0;
        do {
          if (nameAndWeights.find(start)) {
            String toName = nameAndWeights.group(1);
            nodes = names.findVertices(toName, eqPred);
            int to = -1;
            if (nodes.isEmpty()) {
              to = result.newVertex();
              names.put(to, toName);
            } else {
              to = nodes.get(0);
            }
            Edge<EI> e = result.newEdge(null, from, to);
            //if (nameAndWeights.group(2) != null
            //    && ! nameAndWeights.group(2).trim().isEmpty()) {
              e.setInfo(edgeInfo.apply(e, nameAndWeights.group(2)));
            //}
            start = nameAndWeights.end();
          } else break;
        } while (true);
      }
    }
  }
/**
   * create a graph from a readable specification, for exampes see the Utils
   * class in the test directory.
   */
create a graph from a readable specification, for exampes see the Utils
class in the test directory.
create a graph from a readable specification for exampes see the Utils class in the test directory .
public static <EI> void readGraphGeneric(Reader in, Graph<EI> result,
      BiConsumer<Integer, String> nodeInfo,
      BiFunction<Edge<EI>, String, EI> edgeInfo)
    throws IOException {
    BufferedReader bin = new BufferedReader(in);
    EqualsPredicate<String> eqPred = new EqualsPredicate<String>();
    VertexPropertyMap<String> names = new VertexListPropertyMap<String>(result);
    result.register("names", names);
    String nextLine = null;
    while((nextLine = bin.readLine()) != null) {
      String[] fromTo = nextLine.split("\\s*-->\\s*");
      if (fromTo.length < 1)
        continue;
      Matcher node = pat.matcher(fromTo[0]);
      if (! node.matches()) {
        throw new IOException("Wrong input line: " + nextLine);
      }
      String fromName = node.group(1);
      List<Integer> nodes = names.findVertices(fromName, eqPred);
      int from = -1;
      if (nodes.isEmpty()) {
        from = result.newVertex();
        names.put(from, fromName);
      } else {
        from = nodes.get(0);
      }
      if (node.group(2) != null && ! node.group(2).trim().isEmpty()) {
        nodeInfo.accept(from, node.group(2).trim());
      }
      if (fromTo.length == 2) {
        Matcher nameAndWeights = pat.matcher(fromTo[1]);
        int start = 0;
        do {
          if (nameAndWeights.find(start)) {
            String toName = nameAndWeights.group(1);
            nodes = names.findVertices(toName, eqPred);
            int to = -1;
            if (nodes.isEmpty()) {
              to = result.newVertex();
              names.put(to, toName);
            } else {
              to = nodes.get(0);
            }
            Edge<EI> e = result.newEdge(null, from, to);
              e.setInfo(edgeInfo.apply(e, nameAndWeights.group(2)));
            start = nameAndWeights.end();
          } else break;
        } while (true);
      }
    }
  }
public static < EI > void readGraphGeneric ( Reader in , Graph < EI > result , BiConsumer < Integer , String > nodeInfo , BiFunction < Edge < EI > , String , EI > edgeInfo ) throws IOException { BufferedReader bin = new BufferedReader ( in ) ; EqualsPredicate < String > eqPred = new EqualsPredicate < String > ( ) ; VertexPropertyMap < String > names = new VertexListPropertyMap < String > ( result ) ; result . register ( "names" , names ) ; String nextLine = null ; while ( ( nextLine = bin . readLine ( ) ) != null ) { String [ ] fromTo = nextLine . split ( "\\s*-->\\s*" ) ; if ( fromTo . length < 1 ) continue ; Matcher node = pat . matcher ( fromTo [ 0 ] ) ; if ( ! node . matches ( ) ) { throw new IOException ( "Wrong input line: " + nextLine ) ; } String fromName = node . group ( 1 ) ; List < Integer > nodes = names . findVertices ( fromName , eqPred ) ; int from = - 1 ; if ( nodes . isEmpty ( ) ) { from = result . newVertex ( ) ; names . put ( from , fromName ) ; } else { from = nodes . get ( 0 ) ; } if ( node . group ( 2 ) != null && ! node . group ( 2 ) . trim ( ) . isEmpty ( ) ) { nodeInfo . accept ( from , node . group ( 2 ) . trim ( ) ) ; } if ( fromTo . length == 2 ) { Matcher nameAndWeights = pat . matcher ( fromTo [ 1 ] ) ; int start = 0 ; do { if ( nameAndWeights . find ( start ) ) { String toName = nameAndWeights . group ( 1 ) ; nodes = names . findVertices ( toName , eqPred ) ; int to = - 1 ; if ( nodes . isEmpty ( ) ) { to = result . newVertex ( ) ; names . put ( to , toName ) ; } else { to = nodes . get ( 0 ) ; } Edge < EI > e = result . newEdge ( null , from , to ) ; e . setInfo ( edgeInfo . apply ( e , nameAndWeights . group ( 2 ) ) ) ; start = nameAndWeights . end ( ) ; } else break ; } while ( true ) ; } } }
create a graph from a readable specification, for exampes see the Utils
 class in the test directory.
create a graph from a readable specification for exampes see the Utils class in the test directory .
//if (nameAndWeights.group(2) != null //    && ! nameAndWeights.group(2).trim().isEmpty()) { //}