Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. import org.apache.commons.configuration.BaseConfiguration;
  2. import org.apache.commons.configuration.Configuration;
  3. import org.apache.tinkerpop.gremlin.process.traversal.P;
  4. import org.apache.tinkerpop.gremlin.process.traversal.Scope;
  5. import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
  6. import org.apache.tinkerpop.gremlin.process.traversal.TraversalSource;
  7. import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
  8. import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
  9. import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
  10. import org.apache.tinkerpop.gremlin.structure.*;
  11. import org.apache.tinkerpop.gremlin.structure.util.GraphFactory;
  12. import org.junit.Test;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.umlg.sqlg.structure.SqlgGraph;
  16.  
  17. import java.util.Map;
  18. import java.util.function.Function;
  19.  
  20. import static org.apache.tinkerpop.gremlin.process.traversal.Pop.last;
  21. import static org.apache.tinkerpop.gremlin.process.traversal.Scope.local;
  22.  
  23.  
  24. public class PathTest {
  25.  
  26. private static final Logger LOG = LoggerFactory.getLogger(PathTest.class);
  27.  
  28. @Test
  29. public void pathTestSqlg() {
  30.  
  31. Configuration configuration = new BaseConfiguration();
  32. configuration.addProperty("jdbc.url", "jdbc:postgresql://localhost:5432/sqlg_test");
  33. configuration.addProperty("jdbc.username", "svs");
  34. configuration.addProperty("jdbc.password", "svs");
  35.  
  36. Graph graph = SqlgGraph.open(configuration);
  37. GraphTraversalSource g = graph.traversal();
  38.  
  39. GraphTraversal traversal = getTraversal(g);
  40.  
  41. LOG.info("start traversal, vertex count: {}", g.V().count().next());
  42.  
  43. while (traversal.hasNext()) {
  44. LOG.info(traversal.next().toString());
  45. }
  46. }
  47.  
  48. @Test
  49. public void pathTestTinkerGraph() {
  50.  
  51. Configuration config = new BaseConfiguration();
  52. config.setProperty("gremlin.graph", "org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph");
  53.  
  54. Graph graph = GraphFactory.open(config);
  55. GraphTraversalSource g = graph.traversal();
  56. loadData(graph);
  57. GraphTraversal traversal = getTraversal(g);
  58.  
  59. LOG.info("start traversal, vertex count: {}", g.V().count().next());
  60.  
  61. while (traversal.hasNext()) {
  62. LOG.info(traversal.next().toString());
  63. }
  64. }
  65.  
  66. public GraphTraversal getTraversal(GraphTraversalSource g) {
  67. Function timeAtWarehouse = new Function() {
  68. @Override
  69. public Long apply(Object o) {
  70. Map m = (Map) o;
  71. Integer dow = ((Edge) (m.get("prev"))).value("dow");
  72. Long readyTime = ((Edge) (m.get("prev"))).value("readyTime");
  73. Integer depDow = ((Edge) (m.get("curr"))).value("dow");
  74. Long depTime = ((Edge) (m.get("curr"))).value("depTime");
  75. return (depTime - readyTime) >= 0 ? (depTime - readyTime) : Long.MAX_VALUE;
  76. }
  77. };
  78.  
  79. return g.V().outE("tsw").as("e").inV().emit().repeat(
  80. __.flatMap(
  81. __.outE("tsw").filter(__.as("edge").select(last, "e").where(P.eq("edge")).by("speed")).
  82. group().by(__.inV()).by(__.project("curr", "prev").by().by(__.select(last, "e")).fold()).
  83. select(Column.values).unfold().order(local).by(timeAtWarehouse).limit(local, 1).select("curr")
  84. ).as("e").inV().simplePath()
  85. ).times(20).map(__.union((Traversal) __.select(last, "e").by("speed"), (Traversal) __.path()).fold());
  86. }
  87.  
  88. @Test
  89. public void loadDataSqlg() {
  90. Configuration configuration = new BaseConfiguration();
  91. configuration.addProperty("jdbc.url", "jdbc:postgresql://localhost:5432/sqlg_test");
  92. configuration.addProperty("jdbc.username", "svs");
  93. configuration.addProperty("jdbc.password", "svs");
  94.  
  95. Graph graph = SqlgGraph.open(configuration);
  96.  
  97. graph.tx().open();
  98. loadData(graph);
  99. graph.tx().commit();
  100. }
  101.  
  102. public void loadData(Graph graph) {
  103.  
  104. Vertex v0 = graph.addVertex("code", "0");
  105. Vertex v1 = graph.addVertex("code", "1");
  106. Vertex v2 = graph.addVertex("code", "2");
  107. Vertex v3 = graph.addVertex("code", "3");
  108.  
  109.  
  110. v0.addEdge("tsw", v1, "speed", "1", "readyTime", 10l, "depTime", 5l, "dow", 1);
  111. v1.addEdge("tsw", v2, "speed", "1", "readyTime", 15l, "depTime", 9l, "dow", 1); //must be ignored in longest path
  112. v1.addEdge("tsw", v2, "speed", "1", "readyTime", 20l, "depTime", 17l, "dow", 1); //must be used in longest path
  113. v2.addEdge("tsw", v3, "speed", "1", "readyTime", 30l, "depTime", 25l, "dow", 1);
  114. v1.addEdge("tsw", v2, "speed", "2", "readyTime", 28l, "depTime", 23l, "dow", 1); //speed 2
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement