def test_strategies(self, remote_connection): statics.load_statics(globals()) g = traversal().withRemote(remote_connection). \ withStrategies(TraversalStrategy("SubgraphStrategy", {"vertices": __.hasLabel("person"), "edges": __.hasLabel("created")}, "org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.SubgraphStrategy")) assert 4 == g.V().count().next() assert 0 == g.E().count().next() assert 1 == g.V().label().dedup().count().next() assert 4 == g.V().filter( lambda: ("x -> true", "gremlin-groovy")).count().next() assert "person" == g.V().label().dedup().next() # g = 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 = traversal().withRemote(remote_connection). \ withStrategies(SubgraphStrategy(edges=__.hasLabel("created"))) assert 6 == g.V().count().next() assert 4 == g.E().count().next() assert 1 == g.E().label().dedup().count().next() assert "created" == g.E().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 = traversal().withRemote(remote_connection).withComputer() assert 6 == g.V().count().next() assert 6 == g.E().count().next() # g = traversal().withRemote(remote_connection).withStrategies( SeedStrategy(12345)) shuffledResult = g.V().values("name").order().by( Order.shuffle).toList() assert shuffledResult == g.V().values("name").order().by( Order.shuffle).toList() assert shuffledResult == g.V().values("name").order().by( Order.shuffle).toList() assert shuffledResult == g.V().values("name").order().by( Order.shuffle).toList() # g = traversal().withRemote(remote_connection). \ withStrategies(ReservedKeysVerificationStrategy(throw_exception=True)) try: g.addV("person").property("id", "please-don't-use-id").iterate() assert False except GremlinServerError as gse: assert gse.status_code == 500
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): # we have a proxy model for now given that we don't want to have to have g:XXX all registered on the Gremlin traversal machine (yet) assert {"@type": "g:SubgraphStrategy", "@value": {}} == json.loads( self.graphson_writer.writeObject(SubgraphStrategy)) assert {"@type": "g:SubgraphStrategy", "@value": { "vertices": {"@type": "g:Bytecode", "@value": {"step": [["has", "name", "marko"]]}}}} == json.loads( self.graphson_writer.writeObject(SubgraphStrategy(vertices=__.has("name", "marko"))))
async 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 == await g.V().count().next() assert 0 == await g.E().count().next() assert 1 == await g.V().label().dedup().count().next() assert "person" == await g.V().label().dedup().next() # g = Graph().traversal().withRemote(remote_connection). \ withStrategies(SubgraphStrategy(vertices=__.hasLabel("person"), edges=__.hasLabel("created"))) assert 4 == await g.V().count().next() assert 0 == await g.E().count().next() assert 1 == await g.V().label().dedup().count().next() assert "person" == await g.V().label().dedup().next() # g = g.withoutStrategies(SubgraphStrategy). \ withComputer(vertices=__.has("name", "marko"), edges=__.limit(0)) assert 1 == await g.V().count().next() assert 0 == await g.E().count().next() assert "person" == await g.V().label().next() assert "marko" == await g.V().name.next() # g = Graph().traversal().withRemote(remote_connection).withComputer() assert 6 == await g.V().count().next() assert 6 == await g.E().count().next() await remote_connection.close()
def topology_subgraph(self, topology_id: str, topology_ref: str) -> GraphTraversalSource: """ Gets a gremlin graph traversal source limited to the sub-graph of vertices with the supplied topology ID and topology reference properties. Arguments: topology_id (str): The topology identification string. topology_ref (str): The reference string for the version of the topology you want to sub-graph. Returns: A GraphTraversalSource instance linked to the desired sub-graph """ LOG.debug( "Creating traversal source for topology %s subgraph with " "reference: %s", topology_id, topology_ref, ) topo_graph_traversal: GraphTraversalSource = self.graph_traversal.withStrategies( SubgraphStrategy(vertices=has("topology_ref", topology_ref).has( "topology_id", topology_id))) return topo_graph_traversal
def get_traversal(self): graph = Graph() try: # take only non deleted resources return graph.traversal().withRemote( DriverRemoteConnection( 'ws://%s/gremlin' % self.gremlin_server, 'g')).withStrategies( SubgraphStrategy(vertices=__.has('deleted', 0))) except (HTTPError, socket.error) as e: raise CommandError('Failed to connect to Gremlin server: %s' % e)
def get_graph(self): time_point = int(time.time()) - 5 * 60 graph = Graph() try: # take only resources updated at least 5min ago and not deleted return graph.traversal().withRemote( DriverRemoteConnection('ws://%s/gremlin' % self.gremlin_server, 'g') ).withStrategies( SubgraphStrategy(vertices=__.has('updated', lt(time_point)).has('deleted', 0)) ) except (HTTPError, socket.error) as e: raise CommandError('Failed to connect to Gremlin server: %s' % e)
def test_strategies(self, remote_connection): statics.load_statics(globals()) # g = 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 4 == g.V().filter( lambda: ("lambda x: True", "gremlin-python")).count().next() assert "person" == g.V().label().dedup().next() # g = 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 = traversal().withRemote(remote_connection). \ withStrategies(SubgraphStrategy(edges=__.hasLabel("created"))) assert 6 == g.V().count().next() assert 4 == g.E().count().next() assert 1 == g.E().label().dedup().count().next() assert "created" == g.E().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 = traversal().withRemote(remote_connection).withComputer() assert 6 == g.V().count().next() assert 6 == g.E().count().next()