2017-11-22 1 views
0

私はグラフオブジェクトマッピングを使用しています。ここでは、2つのノードを表す2つのオブジェクトがあります。私は関係のプロパティも持っていたい、プロパティとの関係のクラスを定義するにはどうすればいいですか? Here、私は例を見ましたが、それはオブジェクトを使用していません。py2neo:オブジェクトを使用して関係プロパティを取得する

私のコードは次のようになります。

class Intent(GraphObject): 
    name = Property() 
    message = RelatedTo("Message", "HAS") 

class Message(GraphObject): 
    name = Property() 
    owner = RelatedFrom("Intent","HAS") 

graph = Graph(password='1234') 
intent = Intent() 
intent.name = "ABC" 
complete_intent = graph.pull(intent) 

私は「HAS」関係のためのいくつかのプロパティを持っているしたい場合は、どのように私はクラスが持つ定義していますか?

答えて

1

py2neoの現在のバージョンでは、「関係オブジェクト」が許可されていないため、パラメータを配置することができません(ノードのGraphObjectのように、継承する基底クラスは何か)標準的なOOP-yの関係に結びついています。

OGMは、プロジェクトの開始、および迅速な挿入/検索トランザクションのために非常に便利ですが、最も複雑なトランザクションの場合は、目的を達成するためにcypherを作成する必要があります。

ただし、頻繁にこれらのクエリをGraphObjectに整理し、必要な機能を提供する方法を追加するためにGraphObjectを拡張することができます。

あなたのユースケースでは、以下のようないくつかのメソッドを追加して、関係プロパティを取得または設定できます。私は関係のプロパティを取得するためのデモを簡単に書いたので、関係を設定するための同様のメソッドを追加することもできます。

from py2neo.ogm import (GraphObject, 
         INCOMING, 
         OUTGOING, 
         Property, 
         RelatedFrom, 
         RelatedTo,) 
# this function makes this kind of thing tonnes easier 
from py2neo.types import remote 


class GraphObjectWithRelProps(GraphObject): 
    """Simple extension to add get method for a relationship 
    between self and another node""" 

    @staticmethod 
    def _get_relationship(source, target, relationship): 
     """Returns the relationship connecting source and target as set 
     in the class definition. 
     Copy this method, adding an argument for the params 
     and altering the cypher to add a set function 

     Only works if two classes are rleated by only one relationship 

     :param source: GraphObject instance - the starting node 
     :param target: GraphObject instance - the ending node 
     :param relationship: str name of the relationship on source""" 

     # get relationship pattern 
     rel = getattr(source, relationship) 
     # the pattern is calculated for us on this object 
     pattern = rel._RelatedObjects__relationship_pattern 

     # basic cypher query 
     q = ('MATCH {0} ' 
      'WHERE id(a) = $sId AND id(b) = $tId ' 
      'RETURN _').format(pattern) 
     # for a set operation you would add 
     # DELETE _ CREATE {0} SET _ += $relParams 
     # where $relParams is supplied as a dict with your params 
     # you could also add a patch method using _ += $relParams 
     # the remote function allows us to get a reference to the graph 
     graph = remote(source.__ogm__.node).graph 
     # as well as the node IDs of the GraphObject 
     params = { 
      'sId': remote(source.__ogm__.node)._id, 
      'tId': remote(target.__ogm__.node)._id, 
     } 

     return graph.run(q, params).evaluate() 


class Intent(GraphObjectWithRelProps): 
    name = Property() 
    message = RelatedTo("Message", "HAS") 

    def get_message_rel(self, n): 
     graph = remote(self.__ogm__.node).graph 
     return self._get_relationship(graph, self, n, 'message') 


class Message(GraphObjectWithRelProps): 
    name = Property() 
    owner = RelatedFrom("Intent","HAS") 

    def get_owner_rel(self, n): 
     graph = remote(self.__ogm__.node).graph 
     return self._get_relationship(graph, self, n, 'owner') 
+0

ありがとうDom Weldon。これを試みます。もう一つの質問ですが、関係は将来、オブジェクトとしてサポートされるでしょうか? –

+0

py2neoの将来に関する最新のアップデートは、この問題から最善を尽くすhttps://github.com/technige/py2neo/issues/631 –

+0

私がホットテイクを追加すると、py2neoとOGMはあなたがPythonやオブジェクトリレーショナルマッピングに精通しているなら、グラフをつかむことになります。グラフを真剣に扱い、分析や生産に使う場合は、cypherを書く必要があります。それは素晴らしい言語であり、オープンソースであり、それをつかむとあなたは離れます。 –

関連する問題