Пример #1
0
def createPaths():
    #Connect to Neo4j Database
    graph = connectGraph()
    #Let us define few Nodes
    bradley, matthew, lisa = Node(name="Bradley"), Node(name="Matthew"), Node(
        name="Lisa")
    #Connect these Node and form a Path
    path_1 = Path(bradley, "Knows", matthew, Rev("Knows"), lisa)
    #Let us create this Path on the server
    graph.create(path_1)
    #Let us create some more Nodes
    john, annie, ripley = Node(name="John"), Node(name="Annie"), Node(
        name="Ripley")
    #Define another Path for these New Nodes
    path_2 = Path(john, "Knows", annie, "Knows", ripley)
    #Now, we will join path_1 and path_2 using function append(), and it will give us a new path
    path_3 = path_1.append("Knows", path_2)
    #Let us Create this new path in the server
    resultPath = graph.create(path_3)

    #Now we will print and see the results on the Console
    print("Print Raw Data")
    print("Nodes in the Path-1 = ", resultPath[0].nodes)
    print("Relationships in the Path-1 = ", resultPath[0].relationships)

    print("Print - All Relationships")
    for rels in resultPath[0].relationships:
        print(rels)
Пример #2
0
def test_path_inequality():
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    dave = Node(name="Dave")
    path_1 = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
    path_2 = Path(alice, "KNOWS", carol, Rev("KNOWS"), dave)
    assert path_1 != path_2
Пример #3
0
def test_path_equality(graph):
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    dave = Node(name="Dave")
    path_1 = Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob), carol, "KNOWS", dave)
    path_2 = Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob), carol, "KNOWS", dave)
    assert path_1 == path_2
Пример #4
0
def test_path_hashes(graph):
    p1 = Path(Node("Person", name="Alice"), Rel("KNOWS", since=1999),
              Node("Person", name="Bob"))
    p2 = Path(Node("Person", name="Alice"), Rel("KNOWS", since=1999),
              Node("Person", name="Bob"))
    assert hash(p1) == hash(p2)
    graph.create(p1)
    assert hash(p1) != hash(p2)
Пример #5
0
def test_can_hydrate_path_into_existing_instance(graph):
    alice = Node("Person", name="Alice", age=33)
    bob = Node("Person", name="Bob", age=44)
    dehydrated = graph.cypher.post("CREATE p=()-[:KNOWS]->() RETURN p").content["data"][0][0]
    path = Path(alice, "KNOWS", bob)
    if "directions" not in dehydrated:
        dehydrated["directions"] = ["->"]
    hydrated = Path.hydrate(dehydrated, inst=path)
    assert hydrated is path
Пример #6
0
class GregorianDate(object):
    """ A date picked from a :class:`.GregorianCalendar`.
    """

    #: The calendar from which this date was picked.
    calendar = None

    #: The graph associated with this date.
    graph = None

    #: Full :class:`Path <py2neo.Path>` representing this date.
    path = None

    def __init__(self, calendar, year, month=1, day=1):
        self.calendar = calendar
        self.graph = self.calendar.graph
        self.path = Path(
            self.calendar.root, "YEAR",
            Node("Year", key='%04d' % year, year=year), "MONTH",
            Node("Month",
                 key='%04d-%02d' % (year, month),
                 year=year,
                 month=month), "DAY",
            Node("Day",
                 key='%04d-%02d-%02d' % (year, month, day),
                 year=year,
                 month=month,
                 day=day))
        self.graph.merge(self.path)

    @property
    def year(self):
        """ The year node for this date.

        :rtype: :class:`py2neo.Node`

        """
        return self.path.nodes()[1]

    @property
    def month(self):
        """ The month node for this date.

        :rtype: :class:`py2neo.Node`

        """
        return self.path.nodes()[2]

    @property
    def day(self):
        """ The day node for this date.

        :rtype: :class:`py2neo.Node`

        """
        return self.path.nodes()[3]
Пример #7
0
def test_path_addition():
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    assert alice + alice == Path(alice)
    assert alice + "KNOWS" == Path(alice, "KNOWS", None)
    assert alice + "KNOWS" + bob == Path(alice, "KNOWS", bob)
    assert alice + Rev("KNOWS") + bob == Path(alice, Rev("KNOWS"), bob)
    assert ((alice + "KNOWS" + bob) + (bob + "KNOWS" + carol) == Path(
        alice, "KNOWS", bob, "KNOWS", carol))
Пример #8
0
 def test_path_inequality(self):
     alice = Node(name="Alice")
     bob = Node(name="Bob")
     carol = Node(name="Carol")
     dave = Node(name="Dave")
     path_1 = Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob),
                   carol, "KNOWS", dave)
     path_2 = Path(alice, "KNOWS", carol,
                   Relationship(dave, "KNOWS", carol), dave)
     assert path_1 != path_2
     assert path_1 != ""
Пример #9
0
 def test_can_create_path(self, graph):
     path = Path({"name": "Alice"}, "KNOWS", {"name": "Bob"})
     assert path.nodes[0] == {"name": "Alice"}
     assert path.rels[0].type == "KNOWS"
     assert path.nodes[1] == {"name": "Bob"}
     path = path.create(graph)
     assert isinstance(path.nodes[0], Node)
     assert path.nodes[0]["name"] == "Alice"
     assert isinstance(path.relationships[0], neo4j.Relationship)
     assert path.relationships[0].type == "KNOWS"
     assert isinstance(path.nodes[1], Node)
     assert path.nodes[1]["name"] == "Bob"
Пример #10
0
 def test_can_make_new_path_from_path(self, alice, bob, carol, dave):
     # given
     path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
     # when
     new_path = Path(path)
     # then
     new_rels = list(new_path)
     assert new_rels == [
         Relationship(alice, "LOVES", bob),
         Relationship(carol, "HATES", bob),
         Relationship(carol, "KNOWS", dave),
     ]
Пример #11
0
 def test_node_degree(self):
     alice = Node("Person", name="Alice")
     bob = Node("Person", name="Bob")
     carol = Node("Person", name="Carol")
     self.graph.create(alice)
     assert alice.degree() == 0
     self.graph.create(Path(alice, "KNOWS", bob))
     assert alice.degree() == 1
     self.graph.create(Path(alice, "KNOWS", carol))
     assert alice.degree() == 2
     self.graph.create(Path(carol, "KNOWS", alice))
     assert alice.degree() == 3
Пример #12
0
 def test_can_join_paths(self):
     a = Node(name="Alice")
     b = Node(name="Bob")
     c = Node(name="Carol")
     d = Node(name="Dave")
     path1 = Path(a, "KNOWS", b)
     path2 = Path(c, "KNOWS", d)
     path = Path(path1, "KNOWS", path2)
     assert list(path) == [
         Relationship(a, 'KNOWS', b),
         Relationship(b, 'KNOWS', c),
         Relationship(c, 'KNOWS', d),
     ]
Пример #13
0
 def test_can_create_path(self):
     path = Path({"name": "Alice"}, "KNOWS", {"name": "Bob"})
     nodes = path.nodes()
     assert dict(nodes[0]) == {"name": "Alice"}
     assert path[0].type() == "KNOWS"
     assert dict(nodes[1]) == {"name": "Bob"}
     self.graph.create(path)
     assert isinstance(nodes[0], Node)
     assert nodes[0]["name"] == "Alice"
     assert isinstance(path[0], Relationship)
     assert path[0].type() == "KNOWS"
     assert isinstance(nodes[1], Node)
     assert nodes[1]["name"] == "Bob"
    def createFriends(self,graph,people):
        print("Creating Relationships between People")
        
        path_1 = Path(people['bradley'],'FRIEND',people['matthew'],'FRIEND',people['lisa'],'FRIEND',people['john'])
        path_2 = path_1.prepend(people['lisa'],Rev('FRIEND'))
        path_3 = Path(people['annie'],'FRIEND',people['ripley'],'FRIEND',people['lisa'])
        path_4 = Path(people['bradley'],'TEACHES',people['matthew'])
        
        friendsPath = graph.create(path_2,path_3,path_4)
        
        print("Finished Creating Relationships between People")

        return friendsPath
Пример #15
0
 def test_can_create_path_with_rel_properties(self):
     path = Path({"name": "Alice"}, ("KNOWS", {"since": 1999}), {"name": "Bob"})
     assert path.nodes[0] == {"name": "Alice"}
     assert path.rels[0].type == "KNOWS"
     assert path.rels[0].properties == {"since": 1999}
     assert path.nodes[1] == {"name": "Bob"}
     path = path.create(self.graph)
     assert isinstance(path.nodes[0], Node)
     assert path.nodes[0]["name"] == "Alice"
     assert isinstance(path.relationships[0], neo4j.Relationship)
     assert path.relationships[0].type == "KNOWS"
     assert path.relationships[0].properties == {"since": 1999}
     assert isinstance(path.nodes[1], Node)
     assert path.nodes[1]["name"] == "Bob"
Пример #16
0
 def test_can_make_new_path_from_path(self):
     # given
     path = Path(self.alice, "LOVES", self.bob,
                 Relationship(self.carol, "HATES", self.bob), self.carol,
                 "KNOWS", self.dave)
     # when
     new_path = Path(path)
     # then
     new_rels = list(new_path)
     assert new_rels == [
         Relationship(self.alice, "LOVES", self.bob),
         Relationship(self.carol, "HATES", self.bob),
         Relationship(self.carol, "KNOWS", self.dave),
     ]
    def createFriends(self, graph, people):
        print("Creating Relationships between People")

        path_1 = Path(people['bradley'], 'FRIEND', people['matthew'], 'FRIEND',
                      people['lisa'], 'FRIEND', people['john'])
        path_2 = path_1.prepend(people['lisa'], Rev('FRIEND'))
        path_3 = Path(people['annie'], 'FRIEND', people['ripley'], 'FRIEND',
                      people['lisa'])
        path_4 = Path(people['bradley'], 'TEACHES', people['matthew'])

        friendsPath = graph.create(path_2, path_3, path_4)

        print("Finished Creating Relationships between People")

        return friendsPath
Пример #18
0
class GregorianDate(object):
    """ A date picked from a :class:`.GregorianCalendar`.
    """

    #: The calendar from which this date was picked.
    calendar = None

    #: The graph associated with this date.
    graph = None

    #: Full :class:`Path <py2neo.Path>` representing this date.
    path = None

    def __init__(self, calendar, year, month=1, day=1):
        self.calendar = calendar
        self.graph = self.calendar.graph
        self.path = Path(self.calendar.root,
                         "YEAR", Node("Year", key='%04d' % year, year=year),
                         "MONTH", Node("Month", key='%04d-%02d' % (year, month), year=year, month=month),
                         "DAY", Node("Day", key='%04d-%02d-%02d' % (year, month, day), year=year, month=month, day=day))
        self.graph.merge(self.path)

    @property
    def year(self):
        """ The year node for this date.

        :rtype: :class:`py2neo.Node`

        """
        return self.path.nodes()[1]

    @property
    def month(self):
        """ The month node for this date.

        :rtype: :class:`py2neo.Node`

        """
        return self.path.nodes()[2]

    @property
    def day(self):
        """ The day node for this date.

        :rtype: :class:`py2neo.Node`

        """
        return self.path.nodes()[3]
Пример #19
0
def test_can_slice_path(graph):
    a = Node(name="Alice")
    b = Node(name="Bob")
    c = Node(name="Carol")
    d = Node(name="Dave")
    e = Node(name="Eve")
    f = Node(name="Frank")
    path = Path(a, "KNOWS", b, "KNOWS", c, "KNOWS", d, "KNOWS", e, "KNOWS", f)
    assert len(path) == 5
    assert path[0] == Relationship(a, "KNOWS", b)
    assert path[1] == Relationship(b, "KNOWS", c)
    assert path[2] == Relationship(c, "KNOWS", d)
    assert path[-1] == Relationship(e, "KNOWS", f)
    assert path[0:2] == Path(a, "KNOWS", b, "KNOWS", c)
    assert path[3:5] == Path(d, "KNOWS", e, "KNOWS", f)
    assert path[:] == Path(a, "KNOWS", b, "KNOWS", c, "KNOWS", d, "KNOWS", e, "KNOWS", f)
Пример #20
0
 def __init__(self, calendar, year, month=1, day=1):
     self.calendar = calendar
     self.graph = self.calendar.graph
     self.path = Path(
         self.calendar.root, "YEAR",
         Node("Year", key='%04d' % year, year=year), "MONTH",
         Node("Month",
              key='%04d-%02d' % (year, month),
              year=year,
              month=month), "DAY",
         Node("Day",
              key='%04d-%02d-%02d' % (year, month, day),
              year=year,
              month=month,
              day=day))
     self.graph.merge(self.path)
Пример #21
0
def test_can_push_path(graph):
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    dave = Node(name="Dave")
    path = Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob), carol,
                "KNOWS", dave)
    graph.create(path)
    statement = ("MATCH ()-[ab]->() WHERE id(ab)=$ab "
                 "MATCH ()-[bc]->() WHERE id(bc)=$bc "
                 "MATCH ()-[cd]->() WHERE id(cd)=$cd "
                 "RETURN ab.amount, bc.amount, cd.since")
    parameters = {
        "ab": path[0].identity,
        "bc": path[1].identity,
        "cd": path[2].identity
    }
    path[0]["amount"] = "lots"
    path[1]["amount"] = "some"
    path[2]["since"] = 1999
    ab_amount, bc_amount, cd_since = next(graph.run(statement, parameters))
    assert ab_amount is None
    assert bc_amount is None
    assert cd_since is None
    graph.push(path)
    ab_amount, bc_amount, cd_since = next(graph.run(statement, parameters))
    assert ab_amount == "lots"
    assert bc_amount == "some"
    assert cd_since == 1999
Пример #22
0
def test_graph(graph):
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    dave = Node(name="Dave")
    path, = graph.create(Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave))
    assert path.graph == Graph("http://localhost:7474/db/data/")
Пример #23
0
 def test_can_construct_simple_path(self):
     alice = Node(name="Alice")
     bob = Node(name="Bob")
     path = Path(alice, "KNOWS", bob)
     assert order(path) == 2
     assert size(path) == 1
     assert len(path) == 1
Пример #24
0
def test_cannot_build_path_with_two_consecutive_rels():
    try:
        _ = Path(Node(name="Alice"), Rel("KNOWS"), Rel("KNOWS"), Node(name="Bob"))
    except JoinError:
        assert True
    else:
        assert False
Пример #25
0
def test_can_construct_simple_path(graph):
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    path = Path(alice, "KNOWS", bob)
    assert len(path.nodes) == 2
    assert len(path.relationships) == 1
    assert len(path) == 1
Пример #26
0
def _createAuthor(pub, names):
    graph = connectGraph()
    print('   Creating Authors')
    for auth in names:
        matcher = NodeMatcher(graph)
        aNode = matcher.match("Author", name=auth['name']).first()
        if aNode is None:
            aNode = Node('Author', name=auth['name'])
            path_1 = Path(aNode, 'AUTHORED', pub)
            graph.create(path_1)
        else:
            #Check the relationship
            rmatcher = RelationshipMatcher(graph)
            auNode = rmatcher.match((aNode,pub), "AUTHORED").first()
            if auNode is None:
                path_1 = Path(aNode, 'AUTHORED', pub)
                graph.create(path_1)
Пример #27
0
def test_can_join_compatible_paths():
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    path_1 = alice + "KNOWS" + bob
    path_2 = bob + "KNOWS" + carol
    path_3 = path_1 + path_2
    assert path_3 == Path(alice, "KNOWS", bob, "KNOWS", carol)
Пример #28
0
 def __init__(self, calendar, year, month=1, day=1):
     self.calendar = calendar
     self.graph = self.calendar.graph
     self.path = Path(self.calendar.root,
                      "YEAR", Node("Year", key='%04d' % year, year=year),
                      "MONTH", Node("Month", key='%04d-%02d' % (year, month), year=year, month=month),
                      "DAY", Node("Day", key='%04d-%02d-%02d' % (year, month, day), year=year, month=month, day=day))
     self.graph.merge(self.path)
Пример #29
0
def test_node_degree(graph):
    alice = Node("Person", name="Alice")
    bob = Node("Person", name="Bob")
    carol = Node("Person", name="Carol")
    try:
        _ = alice.degree
    except BindError:
        assert True
    else:
        assert False
    graph.create(alice)
    assert alice.degree == 0
    graph.create(Path(alice, "KNOWS", bob))
    assert alice.degree == 1
    graph.create(Path(alice, "KNOWS", carol))
    assert alice.degree == 2
    graph.create(Path(carol, "KNOWS", alice))
    assert alice.degree == 3
Пример #30
0
 def test_can_create_path(self):
     path = Path({"name": "Alice"}, "KNOWS", {"name": "Bob"})
     assert len(path) == 1
     assert path.nodes[0]["name"] == "Alice"
     assert path.rels[0].type == "KNOWS"
     assert path.nodes[-1]["name"] == "Bob"
     path = Path(path, "KNOWS", {"name": "Carol"})
     assert len(path) == 2
     assert path.nodes[0]["name"] == "Alice"
     assert path.relationships[0].type == "KNOWS"
     assert path.nodes[1]["name"] == "Bob"
     path = Path({"name": "Zach"}, "KNOWS", path)
     assert len(path) == 3
     assert path.nodes[0]["name"] == "Zach"
     assert path.relationships[0].type == "KNOWS"
     assert path.nodes[1]["name"] == "Alice"
     assert path.relationships[1].type == "KNOWS"
     assert path.nodes[2]["name"] == "Bob"
Пример #31
0
def _createKeywords(pub, keywords):
    graph = connectGraph()
    print('   Creating Keywords')
    for kw in keywords:
        #Find keywords  TODO : Check for duplicate on re-run
        matcher = NodeMatcher(graph)
        #pbNode = matcher.match("KEYWORDS", kw.upper(), name=kw.lower()).first()
        pbNode = matcher.match("Keywords", kw.lower(), name=kw.lower()).first()
        if pbNode == None:
            pbNode = Node('Keywords', kw.lower(), name=kw.lower())
            path_1 = Path(pub, 'CONTAINS', pbNode)
            graph.create(path_1)
        else:
            #Check for existing relationship
            rmatcher = RelationshipMatcher(graph)
            auNode = rmatcher.match((pbNode,pub), "CONTAINS").first()
            if auNode is None:
                path_1 = Path(pub, 'CONTAINS', pbNode)
                graph.create(path_1)
Пример #32
0
def _createReferences(pub, references):
    graph = connectGraph()
    print('   Creating References')
    for ref in references:
        #Find the Paper details
        matcher = NodeMatcher(graph)
        pbNode = matcher.match("Paper", id=ref).first()
        if pbNode == None:
            #path_1 = Node('PAPER', 'AMINER', name=ref)
            pbNode = Node('Paper', id=ref)
            path_1 = Path(pub, 'REFERENCE', pbNode)
            graph.create(path_1)
        else:
            #Check for existing relationship
            rmatcher = RelationshipMatcher(graph)
            auNode = rmatcher.match((pbNode, pub), "REFERENCE").first()
            if auNode is None:
                path_1 = Path(pub, 'REFERENCE', pbNode)
                graph.create(path_1)
Пример #33
0
def test_service_root_on_bound_path(graph):
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    dave = Node(name="Dave")
    path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
    graph.create(path)
    assert path.service_root == ServiceRoot("http://localhost:7474/")
    path[0].unbind()
    assert path.service_root == ServiceRoot("http://localhost:7474/")
Пример #34
0
def test_path_in_several_ways():
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    dave = Node(name="Dave")
    path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
    assert path.__bool__()
    assert path.__nonzero__()
    assert path[0] == Relationship(alice, "LOVES", bob)
    assert path[1] == Relationship(carol, "HATES", bob)
    assert path[2] == Relationship(carol, "KNOWS", dave)
    assert path[-1] == Relationship(carol, "KNOWS", dave)
    assert path[0:1] == Path(alice, "LOVES", bob)
    assert path[0:2] == Path(alice, "LOVES", bob, Rev("HATES"), carol)
    try:
        _ = path[7]
    except IndexError:
        assert True
    else:
        assert False