def undecided_user_audience_check(g, transient_id, website_url,
                                  thank_you_page_url, since,
                                  min_visited_count):
    """
    Given transient id, check whether it belongs to an audience.

    It's simple yes, no question.

    User belongs to an audience whenever all of the following criteria are met:
        * visited some website url at least X times since specific timestamp
        * did not visit thank you page url since specific timestamp
    """
    return (g.V(transient_id).hasLabel("transientId").in_("has_identity").out(
        "has_identity").outE("visited").has("ts", P.gt(since)).choose(
            has("visited_url", website_url),
            groupCount("visits").by(constant("page_visits"))).choose(
                has("visited_url", thank_you_page_url),
                groupCount("visits").by(
                    constant("thank_you_page_vists"))).cap("visits").coalesce(
                        and_(
                            coalesce(select("thank_you_page_vists"),
                                     constant(0)).is_(0),
                            select("page_visits").is_(
                                P.gt(min_visited_count))).choose(
                                    count().is_(1), constant(True)),
                        constant(False)))
async def drop_vertices(goblin_app, timestamp):
    session = await goblin_app.session()
    for attempt in range(10):
        traversal = session.g.V().has("last_modified", P.lt(timestamp)).count()
        try:
            total = await traversal.next()
        except GremlinServerError as e:
            logger.error(f"Backing off: {e!s}\n{traceback.format_exc()}")
            await backoff(attempt)
    logger.info(f"Found {total} stale vertices to drop.")
    if not total:
        return
    with TQDMK8S(
        desc="Purging Old Vertices", dynamic_ncols=True, file=sys.stdout, total=None,
    ) as progress_bar:
        batch = 100
        dropped = 1
        while dropped:
            for attempt in range(10):
                traversal = (
                    session.g.V()
                    .has("last_modified", P.lt(timestamp))
                    .limit(batch)
                    .sideEffect(__.drop())
                    .count()
                )
                try:
                    dropped = await traversal.next()
                except GremlinServerError as e:
                    logger.error(f"Backing off: {e!s}\n{traceback.format_exc()}")
                    await backoff(attempt)
            progress_bar.update(1)
Пример #3
0
def search_gpsalbum(g, gpsrange, dataset):
    """search the image with the specific gps range from the albumgraph.
    Args:
        g - graph from gremlin server
        gpsrange - gps range user want [[latitude_lb,latitude_ub],[longitude_lb,longitude_ub]]
        dataset- 'l' for large dataset, 's' for small dataset
    Returns:
        gps_album - an album of images with same tag.
    """
    print "searching for: ", gpsrange
    gps_album = {}
    image_id = g.V().has('latitude', P.inside(
        gpsrange[0][0], gpsrange[0][1])).has(
            'longitude', P.inside(gpsrange[1][0],
                                  gpsrange[1][1])).values('image_id').toList()
    image_path = g.V().has('latitude', P.inside(
        gpsrange[0][0], gpsrange[0][1])).has(
            'longitude',
            P.inside(gpsrange[1][0],
                     gpsrange[1][1])).values('image_path').toList()
    for i in range(len(image_id)):
        if image_id[i] not in gps_album:
            gps_album[image_id[i]] = image_path[i]
            print image_id[i], image_path[i]
    return gps_album
Пример #4
0
    def test_P(self):
        result = {
            '@type': 'g:P',
            '@value': {
                'predicate':
                'and',
                'value': [{
                    '@type': 'g:P',
                    '@value': {
                        'predicate':
                        'or',
                        'value': [{
                            '@type': 'g:P',
                            '@value': {
                                'predicate': 'lt',
                                'value': 'b'
                            }
                        }, {
                            '@type': 'g:P',
                            '@value': {
                                'predicate': 'gt',
                                'value': 'c'
                            }
                        }]
                    }
                }, {
                    '@type': 'g:P',
                    '@value': {
                        'predicate': 'neq',
                        'value': 'd'
                    }
                }]
            }
        }

        assert result == json.loads(
            self.graphson_writer.writeObject(
                P.lt("b").or_(P.gt("c")).and_(P.neq("d"))))

        result = {
            '@type': 'g:P',
            '@value': {
                'predicate': 'within',
                'value': {
                    '@type':
                    'g:List',
                    '@value': [{
                        "@type": "g:Int32",
                        "@value": 1
                    }, {
                        "@type": "g:Int32",
                        "@value": 2
                    }]
                }
            }
        }
        assert result == json.loads(
            self.graphson_writer.writeObject(P.within([1, 2])))
Пример #5
0
 def test_P(self):
     # verify that the order of operations is respected
     assert "and(eq(a),lt(b))" == str(P.eq("a").and_(P.lt("b")))
     assert "and(or(lt(b),gt(c)),neq(d))" == str(
         P.lt("b").or_(P.gt("c")).and_(P.neq("d")))
     assert "and(or(lt(b),gt(c)),or(neq(d),gte(e)))" == str(
         P.lt("b").or_(P.gt("c")).and_(P.neq("d").or_(P.gte("e"))))
def _get_subgraph(g, website_url, thank_you_page_url, since):
    return (g.V().hasLabel("website").has(
        "url", P.within([
            website_url, thank_you_page_url
        ])).in_("visited").in_("has_identity").dedup().limit(20).project(
            "persistent_id", "transient_ids", "visited_events").by(
                values("pid")).by(out("has_identity").values("uid").fold()).by(
                    out("has_identity").outE("visited").has(
                        "visited_url",
                        P.within([website_url, thank_you_page_url
                                  ])).valueMap("visited_url", "ts",
                                               "uid").dedup().fold()))
Пример #7
0
def get_locality_loop_traversal(
    countries=None,
    formats=None,
    formats_op="or",
    genres=None,
    labels=None,
    main_releases_only=True,
    offset=0,
    roles=None,
    styles=None,
    styles_op="or",
    years=None,
):
    primacy = get_primacy(labels, main_releases_only)
    traversal = (__.timeLimit(1000).bothE("relationship").dedup().has(
        "primacy", P.within(*primacy)))
    if roles:
        traversal = traversal.has("name", P.within(*roles))
    other_traversal = __.otherV()
    if labels:
        other_traversal = other_traversal.hasLabel(*(label.lower()
                                                     for label in labels))
    if 1 in primacy or 2 in primacy:
        traversals = []
        # country, format, genre, style, year
        if countries:
            traversals.append(__.has("country", P.within(*countries)))
        if formats:
            if formats_op == "or":
                traversals.append(__.has("formats", P.within(*formats)))
            else:
                for format_ in formats:
                    traversals.append(__.has("formats", format_))
        if genres:
            traversals.append(__.has("genres", P.within(*genres)))
        if styles:
            if styles_op == "or":
                traversals.append(__.has("styles", P.within(*styles)))
            else:
                for style in styles:
                    traversals.append(__.has("styles", style))
        if years:
            traversals.append(__.has("year", P.within(*years)))
        if traversals:
            traversal = traversal.where(
                other_traversal.choose(
                    __.label().is_(P.within("release", "track")),
                    __.and_(*traversals),
                    __.identity(),
                ))
        elif labels:
            traversal = traversal.where(other_traversal)
    elif labels:
        traversal = traversal.where(other_traversal)
    traversal = traversal.choose(
        __.loops().is_(0),
        __.aggregate("pageableEdges").order().range(offset, offset + 50),
        __.limit(10),
    )
    return __.local(traversal).dedup().aggregate("edges").otherV().dedup()
Пример #8
0
    def persons(self, *args):
        traversal = self.get_graph_traversal().V().hasLabel("person")

        if len(args) > 0:
            traversal = traversal.has("name", P.within(*args))

        return traversal
Пример #9
0
def get_comp_links_by_grouping(graph_traversal: GraphTraversalSource,
                               grouping: str) -> List[Dict[str, str]]:
    """ Gets a list of component connection dictionaries. These describe all
    source->stream->destination connections with the specified grouping value
    in the topology available via the supplied graph traversal source.

    Arguments:
        graph_traversal (GraphTraversalSource): A GraphTraversalSource instance
                                                linked to the topology subgraph
                                                whose connections are to be
                                                queried.
        grouping (str): The stream grouping of the connections to be returned.

    Returns:
        A list of dictionaries each containing "source", "stream" and
        "destination" keys of the component and stream name respectively.
    """

    component_connections: List[Dict[str, str]] = \
        (graph_traversal.V().hasLabel(P.within("bolt", "spout")).as_("source")
         .outE("logically_connected").has("grouping", grouping).as_("stream")
         .inV().as_("destination").select("source", "stream", "destination")
         .by("component").by("stream").by("component").dedup().toList())

    return component_connections
Пример #10
0
 def test_bytecode(self):
     g = Graph().traversal()
     bytecode = g.V().out("created").bytecode
     assert 0 == len(bytecode.bindings.keys())
     assert 0 == len(bytecode.source_instructions)
     assert 2 == len(bytecode.step_instructions)
     assert "V" == bytecode.step_instructions[0][0]
     assert "out" == bytecode.step_instructions[1][0]
     assert "created" == bytecode.step_instructions[1][1]
     assert 1 == len(bytecode.step_instructions[0])
     assert 2 == len(bytecode.step_instructions[1])
     ##
     bytecode = g.withSack(1).E().groupCount().by("weight").bytecode
     assert 0 == len(bytecode.bindings.keys())
     assert 1 == len(bytecode.source_instructions)
     assert "withSack" == bytecode.source_instructions[0][0]
     assert 1 == bytecode.source_instructions[0][1]
     assert 3 == len(bytecode.step_instructions)
     assert "E" == bytecode.step_instructions[0][0]
     assert "groupCount" == bytecode.step_instructions[1][0]
     assert "by" == bytecode.step_instructions[2][0]
     assert "weight" == bytecode.step_instructions[2][1]
     assert 1 == len(bytecode.step_instructions[0])
     assert 1 == len(bytecode.step_instructions[1])
     assert 2 == len(bytecode.step_instructions[2])
     ##
     bytecode = g.V(('a', [1, 2, 3])).out(('b', 'created')).where(
         __.in_(('c', 'created'), ('d', 'knows')).count().is_(
             ('e', P.gt(2)))).bytecode
     assert 'V' == bytecode.step_instructions[0][0]
     assert 'out' == bytecode.step_instructions[1][0]
     assert 'where' == bytecode.step_instructions[2][0]
     assert ('b', 'created') == bytecode.step_instructions[1][1]
     assert isinstance(hash(bytecode.step_instructions[1][1]), int)
Пример #11
0
    def regex(value):
        """
        Search for this regular expression inside the text property targeted.

        :param value: the value to look for.
        """
        return P('regex', value)
Пример #12
0
def test_traversal_modern_graph(modern_graph_data_dir):
    from gremlin_python.process.traversal import Order
    from gremlin_python.process.traversal import P

    gs_image, gie_manager_image = get_gs_image_on_ci_env()
    sess = graphscope.session(
        show_log=True,
        num_workers=1,
        k8s_gs_image=gs_image,
        k8s_gie_graph_manager_image=gie_manager_image,
        k8s_coordinator_cpu=0.5,
        k8s_coordinator_mem="2500Mi",
        k8s_vineyard_cpu=0.1,
        k8s_vineyard_mem="512Mi",
        k8s_engine_cpu=0.1,
        k8s_engine_mem="1500Mi",
        k8s_vineyard_shared_mem="2Gi",
        k8s_volumes=get_k8s_volumes(),
    )
    graph = load_modern_graph(sess, modern_graph_data_dir)
    interactive = sess.gremlin(graph)
    g = interactive.traversal_source()
    assert g.V().has("name", "marko").count().toList()[0] == 1
    assert g.V().has("person", "name", "marko").count().toList()[0] == 1
    assert g.V().has("person", "name",
                     "marko").outE("created").count().toList()[0] == 1
    assert (g.V().has("person", "name",
                      "marko").outE("created").inV().count().toList()[0] == 1)
    assert g.V().has("person", "name",
                     "marko").out("created").count().toList()[0] == 1
    assert (g.V().has(
        "person", "name",
        "marko").out("created").values("name").count().toList()[0] == 1)
    assert (g.V().hasLabel("person").has("age", P.gt(30)).order().by(
        "age", Order.desc).count().toList()[0] == 2)
async def drop_properties(session, xml_entity, entity_map, **kwargs):
    desired = {}
    search = {**dataclasses.asdict(xml_entity), **kwargs}
    for key, desired_values in search.items():
        if key not in entity_map:
            continue
        if not isinstance(desired_values, (list, set)):
            continue
        if sorted(desired_values) == sorted(entity_map[key]):
            continue
        desired[key] = desired_values
    if not desired:
        return
    label = type(xml_entity).__name__.lower()
    for attempt in range(10):
        traversal = session.g.V().has(label, f"{label}_id", xml_entity.entity_id)
        for key, desired_values in sorted(desired.items()):
            traversal = traversal.sideEffect(
                __.properties(key).hasValue(P.without(*desired_values)).drop()
            )
        traversal = traversal.id()
        try:
            return await traversal.next()
        except GremlinServerError as e:
            logger.error(f"Backing off: {e!s}\n{traceback.format_exc()}")
            await backoff(attempt)
Пример #14
0
    def token_prefix(value):
        """
        Search any instance of a certain token prefix withing the text property targeted.

        :param value: the value to look for.
        """
        return P('tokenPrefix', value)
Пример #15
0
    def recommend(self,
                  recommendations,
                  minimum_rating,
                  include=AnonymousTraversal.__(),
                  recommender=Recommender.SMALL_SAMPLE):
        """A simple recommendation algorithm.

        Starts from a "user" and examines movies the user has seen filtered by the minimum_rating which removes
        movies that hold a rating lower than the value specified. It then samples the actors in the movies the
        user has seen and uses that to find other movies those actors have been in that the user has not yet
        seen. Those movies are grouped, counted and sorted based on that count to produce the recommendation.
        """

        if recommendations <= 0:
            raise ValueError('recommendations must be greater than zero')
        if not isinstance(recommender, Recommender):
            raise ValueError(
                'recommender argument must be of type Recommender')

        return (self.rated(minimum_rating, 0).aggregate("seen").local(
            recommender.traversal).unfold().in_(EDGE_ACTOR).where(
                P.without(
                    ["seen"])).where(include).groupCount().order(local).by(
                        values,
                        decr).limit(local,
                                    recommendations).select(keys).unfold())
Пример #16
0
    def token_regex(value):
        """
        Search any instance of the provided regular expression for the targeted property.

        :param value: the value to look for.
        """
        return P('tokenRegex', value)
Пример #17
0
    def prefix(value):
        """
        Search for a specific prefix at the beginning of the text property targeted.

        :param value: the value to look for.
        """
        return P('prefix', value)
Пример #18
0
    def persons(self, *args):
        traversal = self.get_graph_traversal().V().hasLabel("person")

        if len(args) > 0:
            traversal = traversal.has("name", P.within(*args))

        return traversal
Пример #19
0
    def token(value):
        """
        Search any instance of a certain token within the text property targeted.

        :param value: the value to look for.
        """
        return P('token', value)
async def drop_edges(session, label, entity_id, timestamp=None, both=False, names=None):
    for attempt in range(10):
        traversal = session.g.V().has(label, f"{label}_id", entity_id)
        if both:
            traversal = traversal.bothE("relationship")
        else:
            traversal = traversal.outE("relationship")
        if names:
            traversal = traversal.has("name", P.within(*names))
        traversal = traversal.has("last_modified", P.lt(timestamp))
        traversal = traversal.drop()
        try:
            return await traversal.toList()
        except GremlinServerError as e:
            logger.error(f"Backing off: {e!s}\n{traceback.format_exc()}")
            await backoff(attempt)
Пример #21
0
def query_users_active_in_n_days(g,
                                 n=30,
                                 today=datetime(2016, 6, 22, 23, 59),
                                 limit=1000):
    """Get users that were active in last 30 days."""

    dt_condition = [has("ts", P.gt(today - timedelta(days=n)))]
    return query_users_active_in_given_date_intervals(g, dt_condition, limit)
Пример #22
0
 def _clean_up_relationships(self, urn: PartialUrn,
                             cutoff: datetime) -> None:
     logger.debug("Cleaning up edges owned by %s discovered before %s",
                  self.generate_vertex_id(urn), cutoff)
     (self.g.V(self.generate_vertex_id(urn)).bothE().as_("edge").has(
         "_edge_owner", self.generate_vertex_id(urn)).where(
             __.values("_discovery_time").is_(P.lt(
                 cutoff.isoformat()))).select("edge").drop()).iterate()
Пример #23
0
    def test_P(self):
        result = {'@type': 'g:P',
                  '@value': {
                      'predicate': 'and',
                      'value': [{
                          '@type': 'g:P',
                          '@value': {
                              'predicate': 'or',
                              'value': [{
                                  '@type': 'g:P',
                                  '@value': {'predicate': 'lt', 'value': 'b'}
                              },
                                  {'@type': 'g:P', '@value': {'predicate': 'gt', 'value': 'c'}}
                              ]
                          }
                      },
                          {'@type': 'g:P', '@value': {'predicate': 'neq', 'value': 'd'}}]}}

        assert result == json.loads(
            self.graphson_writer.writeObject(P.lt("b").or_(P.gt("c")).and_(P.neq("d"))))

        result = {'@type': 'g:P', '@value': {'predicate':'within','value': [{"@type": "g:Int32", "@value": 1},{"@type": "g:Int32", "@value": 2}]}}
        assert result == json.loads(self.graphson_writer.writeObject(P.within([1, 2])))
        assert result == json.loads(self.graphson_writer.writeObject(P.within(1, 2)))

        result = {'@type': 'g:P', '@value': {'predicate':'within','value': [{"@type": "g:Int32", "@value": 1}]}}
        assert result == json.loads(self.graphson_writer.writeObject(P.within([1])))
        assert result == json.loads(self.graphson_writer.writeObject(P.within(1)))
Пример #24
0
def get_onestop_flights_from_janus(from_, to, context):
    try:
        res = (
            g.V()
            .hasLabel("airport")
            .has("id", "9600276f-608f-4325-a037-f185848f2e28")
            .bothE("departing")
            .otherV()
            .as_("flight1")
            .hasLabel("flight")
            .values("flight_duration")
            .as_("fd")
            .select("flight1")
            .values("flight_time")
            .math("_ + fd + 60")
            .as_("flight1_time")
            .select("flight1")
            .bothE("arriving")
            .otherV()
            .hasLabel("airport")
            .bothE("departing")
            .otherV()
            .as_("flight2")
            .hasLabel("flight")
            .values("flight_time")
            .math("_ - flight1_time")
            .is_(P.gte(0))
            .where("flight1", P.eq("flight2"))
            .by("airlines")
            .select("flight2")
            .bothE("arriving")
            .otherV()
            .hasLabel("airport")
            .has("id", "ebc645cd-ea42-40dc-b940-69456b64d2dd")
            .select("flight1", "flight2")
            .by(__.valueMap())
            .limit(20)
            .toList()
        )

        for i in range(0, len(res)):
            res[i] = merge_flight_data(get_values(res[i]))
        return res
    except Exception as e:
        print(e)
Пример #25
0
def recommend_similar_audience(g,
                               website_url,
                               categories_limit=3,
                               search_time_limit_in_seconds=15):
    """Given website url, categories_limit, categories_coin recommend similar audience in n most popular categories.

    Similar audience - audience of users that at least once visited subpage of domain that contains IAB-category codes
    that are most popular across users of given website
    """
    average_guy = (g.V(website_url).in_("visited").in_(
        "has_identity").dedup().hasLabel("persistentId").group().by().by(
            out("has_identity").out("visited").in_("links_to").groupCount().by(
                "categoryCode")).select(
                    Column.values).unfold().unfold().group().by(
                        Column.keys).by(select(
                            Column.values).mean()).unfold().order().by(
                                Column.values,
                                Order.desc).limit(categories_limit))

    most_popular_categories = dict(
        chain(*category.items()) for category in average_guy.toList())

    guy_stats_subquery = (out("has_identity").out("visited").in_(
        "links_to").groupCount().by("categoryCode").project(
            *most_popular_categories.keys()))

    conditions_subqueries = []
    for i in most_popular_categories:
        guy_stats_subquery = guy_stats_subquery.by(
            choose(select(i), select(i), constant(0)))
        conditions_subqueries.append(
            select(Column.values).unfold().select(i).is_(
                P.gt(int(most_popular_categories[i]))))

    return (g.V().hasLabel("websiteGroup").has(
        "categoryCode", P.within(list(
            most_popular_categories.keys()))).out("links_to").in_("visited").
            dedup().in_("has_identity").dedup().hasLabel("persistentId").where(
                out("has_identity").out("visited").has(
                    "url", P.neq(website_url))).timeLimit(
                        search_time_limit_in_seconds * 1000).local(
                            group().by().by(guy_stats_subquery).where(
                                or_(*conditions_subqueries))).select(
                                    Column.keys).unfold().out(
                                        "has_identity").values("uid"))
Пример #26
0
def people_you_may_know(g, user_name, limit=10):
  from gremlin_python.process.traversal import Scope, Column, Order

  recommendations = (g.V().hasLabel('person').has('_name', user_name.lower()).as_('person').
    both('knows').aggregate('friends').
    both('knows').
      where(P.neq('person')).where(P.without('friends')).
    groupCount().by('id').
    order(Scope.local).by(Column.values, Order.decr).
    next())

  vertex_scores = [(key, score) for key, score in recommendations.items()][:limit]
  res = []
  for key, score in vertex_scores:
    value = {k: v for k, v in g.V(key).valueMap().next().items() if not (k == 'id' or k.startswith('_'))}
    value['score'] = float(score)
    res.append(value)
  return res
Пример #27
0
    def persons(self, *args):
        traversal = self.get_graph_traversal()
        traversal.bytecode.add_step("V")
        traversal.bytecode.add_step("hasLabel", "person")

        if len(args) > 0:
            traversal.bytecode.add_step("has", "name", P.within(args))

        return traversal
Пример #28
0
def get_locality_vertex_projection():
    return (__.cap("vertices").unfold().dedup().project(
        "id", "kind", "label", "values", "total_edge_count",
        "child_count").by(__.id()).by(__.constant("vertex")).by(__.label()).by(
            __.valueMap()).by(__.bothE().count()).by(
                __.inE("relationship").has(
                    "name",
                    P.within("Member Of", "Subsidiary Of",
                             "Subrelease Of")).count()))
Пример #29
0
def get_activity_of_early_adopters(g,
                                   thank_you_page_url,
                                   skip_single_transients=False,
                                   limit=5):
    """
    Given thank you page url, find first early adopters of the product.

    In other words:
        * find first few persistent identities (or transient if they're not matched with any user)
          that visited given thank you page
        * extract their *whole* activity on the domain of the thank_you_page
    """
    return (g.V(thank_you_page_url).hasLabel("website").as_("thank_you").in_(
        "links_to").as_("website_group").select("thank_you").inE(
            "visited").order().by("ts").choose(
                constant(skip_single_transients).is_(P.eq(True)),
                where(outV().in_("has_identity")), identity()).choose(
                    outV().in_("has_identity"),
                    project("type", "id",
                            "purchase_ts").by(constant("persistent")).by(
                                outV().in_("has_identity")).by(values("ts")),
                    project("type", "id", "purchase_ts").by(
                        constant("transient")).by(outV()).by(values("ts"))).
            dedup("id").limit(limit).choose(
                select("type").is_("persistent"),
                project("persistent_id", "transient_id",
                        "purchase_ts").by(select("id").values("pid")).by(
                            select("id").out("has_identity").fold()).by(
                                select("purchase_ts")),
                project("persistent_id", "transient_id", "purchase_ts").by(
                    constant("")).by(select("id").fold()).by(
                        select("purchase_ts"))).project(
                            "persistent_id", "purchase_ts", "devices",
                            "visits").by(select("persistent_id")).by(
                                select("purchase_ts")).by(
                                    select("transient_id").unfold().group().by(
                                        values("uid")).by(values("type"))).
            by(
                select("transient_id").unfold().outE("visited").order().by(
                    "ts").where(inV().in_("links_to").where(
                        P.eq("website_group"))).project(
                            "transientId", "url",
                            "ts").by("uid").by("visited_url").by("ts").fold()))
Пример #30
0
def _query_users_activities_stats(g,
                                  website_url,
                                  most_popular_categories,
                                  search_time_limit_in_seconds=30):
    return (g.V().hasLabel("websiteGroup").has(
        "categoryCode", P.within(list(
            most_popular_categories.keys()))).out("links_to").in_("visited").
            dedup().in_("has_identity").dedup().hasLabel("persistentId").where(
                out("has_identity").out("visited").has(
                    "url", P.neq(website_url))).timeLimit(
                        search_time_limit_in_seconds * 1000).
            local(group().by().by(
                out("has_identity").out("visited").in_(
                    "links_to").groupCount().by("categoryCode")).project(
                        "pid", "iabs",
                        "tids").by(select(Column.keys).unfold()).by(
                            select(Column.values).unfold()).by(
                                select(Column.keys).unfold().out(
                                    "has_identity").values("uid").fold())))
Пример #31
0
 def test_enums(self):
     statics.load_statics(globals())
     assert isinstance(list_, Cardinality)
     assert list_ is Cardinality.list_
     #
     assert isinstance(eq(2), P)
     assert eq(2) == P.eq(2)
     #
     assert isinstance(first, Pop)
     assert first == Pop.first
     statics.unload_statics(globals())
Пример #32
0
    def test_P(self):
        result = {'@type': 'g:P',
                  '@value': {
                     'predicate': 'and',
                     'value': [{
                        '@type': 'g:P',
                        '@value': {
                            'predicate': 'or',
                            'value': [{
                                '@type': 'g:P',
                                '@value': {'predicate': 'lt', 'value': 'b'}
                            },
                            {'@type': 'g:P', '@value': {'predicate': 'gt', 'value': 'c'}}
                            ]
                        }
                    },
                    {'@type': 'g:P', '@value': {'predicate': 'neq', 'value': 'd'}}]}}

        assert  result == json.loads(
            self.graphson_writer.writeObject(P.lt("b").or_(P.gt("c")).and_(P.neq("d"))))
Пример #33
0
 def test_enums(self):
     statics.load_statics(globals())
     assert isinstance(list_, Cardinality)
     assert list_ is Cardinality.list_
     #
     assert isinstance(eq(2), P)
     assert eq(2) == P.eq(2)
     #
     assert isinstance(first, Pop)
     assert first == Pop.first
     statics.unload_statics(globals())
Пример #34
0
 def test_bytecode(self):
     g = Graph().traversal()
     bytecode = g.V().out("created").bytecode
     assert 0 == len(bytecode.bindings.keys())
     assert 0 == len(bytecode.source_instructions)
     assert 2 == len(bytecode.step_instructions)
     assert "V" == bytecode.step_instructions[0][0]
     assert "out" == bytecode.step_instructions[1][0]
     assert "created" == bytecode.step_instructions[1][1]
     assert 1 == len(bytecode.step_instructions[0])
     assert 2 == len(bytecode.step_instructions[1])
     ##
     bytecode = g.withSack(1).E().groupCount().by("weight").bytecode
     assert 0 == len(bytecode.bindings.keys())
     assert 1 == len(bytecode.source_instructions)
     assert "withSack" == bytecode.source_instructions[0][0]
     assert 1 == bytecode.source_instructions[0][1]
     assert 3 == len(bytecode.step_instructions)
     assert "E" == bytecode.step_instructions[0][0]
     assert "groupCount" == bytecode.step_instructions[1][0]
     assert "by" == bytecode.step_instructions[2][0]
     assert "weight" == bytecode.step_instructions[2][1]
     assert 1 == len(bytecode.step_instructions[0])
     assert 1 == len(bytecode.step_instructions[1])
     assert 2 == len(bytecode.step_instructions[2])
     ##
     bytecode = g.V(Bindings.of('a', [1,2,3])) \
         .out(Bindings.of('b','created')) \
         .where(__.in_(Bindings.of('c','created'), Bindings.of('d','knows')) \
         .count().is_(Bindings.of('e',P.gt(2)))).bytecode
     assert 5 == len(bytecode.bindings.keys())
     assert [1,2,3] == bytecode.bindings['a']
     assert 'created' == bytecode.bindings['b']
     assert 'created' == bytecode.bindings['c']
     assert 'knows' == bytecode.bindings['d']
     assert P.gt(2) == bytecode.bindings['e']
     assert Binding('b','created') == bytecode.step_instructions[1][1]
     assert 'binding[b=created]' == str(bytecode.step_instructions[1][1])
     assert isinstance(hash(bytecode.step_instructions[1][1]),int)
    def test_iteration(self, remote_connection):
        statics.load_statics(globals())
        assert "remoteconnection[ws://localhost:45940/gremlin,gmodern]" == str(remote_connection)
        g = traversal().withRemote(remote_connection)

        t = g.V().count()
        assert t.hasNext()
        assert t.hasNext()
        assert t.hasNext()
        assert t.hasNext()
        assert t.hasNext()
        assert 6 == t.next()
        assert not(t.hasNext())
        assert not(t.hasNext())
        assert not(t.hasNext())
        assert not(t.hasNext())
        assert not(t.hasNext())

        t = g.V().has('name', P.within('marko', 'peter')).values('name').order()
        assert "marko" == t.next()
        assert t.hasNext()
        assert t.hasNext()
        assert t.hasNext()
        assert t.hasNext()
        assert t.hasNext()
        assert "peter" == t.next()
        assert not(t.hasNext())
        assert not(t.hasNext())
        assert not(t.hasNext())
        assert not(t.hasNext())
        assert not(t.hasNext())

        try:
            t.next()
            assert False
        except StopIteration:
            assert True
Пример #36
0
 def test_P(self):
     # verify that the order of operations is respected
     assert "and(eq(a),lt(b))" == str(P.eq("a").and_(P.lt("b")))
     assert "and(or(lt(b),gt(c)),neq(d))" == str(P.lt("b").or_(P.gt("c")).and_(P.neq("d")))
     assert "and(or(lt(b),gt(c)),or(neq(d),gte(e)))" == str(
         P.lt("b").or_(P.gt("c")).and_(P.neq("d").or_(P.gte("e"))))
Пример #37
0
 def createdAtLeast(self, number):
     return self.outE("created").count().is_(P.gte(number))
Пример #38
0
 def test_P(self):
     assert """{"@type":"g:P","@value":{"predicate":"and","value":[{"@type":"g:P","@value":{"predicate":"or","value":[{"@type":"g:P","@value":{"predicate":"lt","value":"b"}},{"@type":"g:P","@value":{"predicate":"gt","value":"c"}}]}},{"@type":"g:P","@value":{"predicate":"neq","value":"d"}}]}}""" == self.graphson_writer.writeObject(
         P.lt("b").or_(P.gt("c")).and_(P.neq("d")))