def test_strategies(self): statics.load_statics(globals()) connection = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g') # g = Graph().traversal().withRemote(connection). \ withStrategies(TraversalStrategy("SubgraphStrategy", {"vertices": __.hasLabel("person"), "edges": __.hasLabel("created")})) assert 4 == g.V().count().next() assert 0 == g.E().count().next() assert 1 == g.V().label().dedup().count().next() assert "person" == g.V().label().dedup().next() # g = Graph().traversal().withRemote(connection). \ withStrategies(SubgraphStrategy(vertices=__.hasLabel("person"), edges=__.hasLabel("created"))) assert 4 == g.V().count().next() assert 0 == g.E().count().next() assert 1 == g.V().label().dedup().count().next() assert "person" == g.V().label().dedup().next() # g = g.withoutStrategies(SubgraphStrategy). \ withComputer(workers=4, vertices=__.has("name", "marko"), edges=__.limit(0)) assert 1 == g.V().count().next() assert 0 == g.E().count().next() assert "person" == g.V().label().next() assert "marko" == g.V().name.next() # g = Graph().traversal().withRemote(connection).withComputer() assert 6 == g.V().count().next() assert 6 == g.E().count().next() connection.close()
def test_strategies(self, remote_connection): statics.load_statics(globals()) # g = Graph().traversal().withRemote(remote_connection). \ withStrategies(TraversalStrategy("SubgraphStrategy", {"vertices": __.hasLabel("person"), "edges": __.hasLabel("created")})) assert 4 == g.V().count().next() assert 0 == g.E().count().next() assert 1 == g.V().label().dedup().count().next() assert "person" == g.V().label().dedup().next() # g = Graph().traversal().withRemote(remote_connection). \ withStrategies(SubgraphStrategy(vertices=__.hasLabel("person"), edges=__.hasLabel("created"))) assert 4 == g.V().count().next() assert 0 == g.E().count().next() assert 1 == g.V().label().dedup().count().next() assert "person" == g.V().label().dedup().next() # g = g.withoutStrategies(SubgraphStrategy). \ withComputer(vertices=__.has("name", "marko"), edges=__.limit(0)) assert 1 == g.V().count().next() assert 0 == g.E().count().next() assert "person" == g.V().label().next() assert "marko" == g.V().name.next() # g = Graph().traversal().withRemote(remote_connection).withComputer() assert 6 == g.V().count().next() assert 6 == g.E().count().next()
def upload(self, json_graph, source_name, verbose=True, drop_graph=True): def printX(obj=""): if clargs.verbose: print(obj) if isinstance(json_graph, str): input_graph = nx.readwrite.json_graph.node_link_graph( json.loads(json_graph)) elif isinstance(json_graph, io.TextIOBase): input_graph = nx.readwrite.json_graph.node_link_graph( json.load(json_graph)) printX("output Gremlin server: %s, %s" % (clargs.output_server, clargs.output_source)) output_graph = Graph().traversal().withRemote( DriverRemoteConnection(self.server_url, self.traversal_source)) if drop_graph: printX("Clearing ouput graph...") output_graph.V().drop().toList() output_graph.E().drop().toList() printX("done") for id, props in input_graph.nodes(data=True): printX("processing node: %s\nwith data: %s" % (id, props)) new_node = output_graph.addV('node_link_node').next() output_graph.V(new_node).property('original_id', id).toList() output_graph.V(new_node).property('source_name', source_name).toList() for prop, value in props.items(): output_graph.V(new_node).property(prop, value).toList() printX("added properties: %s" % output_graph.V(new_node).properties().toList()) for out_id, in_id, props in input_graph.edges(data=True): printX("processing edge: %s --> %s" % (out_id, in_id)) out_node = output_graph.V().where( __.has('original_id', out_id).has('source_name', source_name)).next() in_node = output_graph.V().where( __.has('original_id', in_id).has('source_name', source_name)).next() new_edge = output_graph.addE('node_link_connection').from_( out_node).to(in_node).next() for prop, value in props.items(): output_graph.E(new_edge).property(prop, value).toList() printX("added properties: %s" % output_graph.E(new_edge).properties().toList()) printX("total nodes added: %d" % output_graph.V().count().next()) printX("total edges added: %d" % output_graph.E().count().next())
def test_text_contains_regex(self): self.container.start() docker_ip = Popen(["docker-machine", "ip"], stdout=PIPE).communicate()[0] docker_ip = docker_ip.strip().decode("utf-8") client = JanusGraphClient() client = client.connect( host=str(docker_ip), port="8182", traversal_source="gods_traversal").get_connection() g = Graph().traversal().withRemote(client) truth = {"f.{3,4}": 2, "shouldNotBeFound": 0} for k, v in truth.items(): count = g.E().has("reason", Text.textContainsRegex(k)).count().next() self.assertEqual(count, v) client.close() self.container.stop()
def __create_lookup_e(remote): g = Graph().traversal().withRemote(remote) # hold a map of the "name"/edge for use in asserting results - "name" in this context is in the form of # outgoingV-label->incomingV return g.E().group(). \ by(lambda: ("it.outVertex().value('name') + '-' + it.label() + '->' + it.inVertex().value('name')", "gremlin-groovy")). \ by(tail()).next()
def test_multi_thread_pool(client): g = Graph().traversal() traversals = [g.V(), g.V().count(), g.E(), g.E().count()] results = [[] for _ in traversals] # Use a condition variable to synchronise a group of threads, which should also inject some # non-determinism into the run-time execution order condition = threading.Condition() def thread_run(tr, result_list): message = RequestMessage('traversal', 'bytecode', { 'gremlin': tr.bytecode, 'aliases': { 'g': 'gmodern' } }) with condition: condition.wait(5) result_set = client.submit(message) for result in result_set: result_list.append(result) threads = [] for i in range(len(results)): thread = threading.Thread(target=thread_run, args=(traversals[i], results[i]), name="test_multi_thread_pool_%d" % i) thread.daemon = True threads.append(thread) thread.start() with condition: condition.notify_all() for t in threads: t.join(5) assert len(results[0][0]) == 6 assert results[1][0][0].object == 6 assert len(results[2][0]) == 6 assert results[3][0][0].object == 6