summaryrefslogtreecommitdiff
path: root/src/server/model/graph.py
blob: 6244af6cefb67d12588b453573f1c39834e32c7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#    This file is part of rhizi, a collaborative knowledge graph editor.
#    Copyright (C) 2014-2015  Rhizi
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


import json

class Attr_Diff(dict):
    """
    Represents a change to note attributes, where nodes can represent
    either logical nodes or logical links, and attributes can be added,
    changed or removed
    
    Example:
            attr_diff = {'__type_node' : {n_id: {'__attr_write': {'attr_0': 0,
                                                                  'attr_1': 'a'},
                                                '__attr_remove': ['attr_2'] }}
                         '__type_link' : {l_id: ... }
                        }
    """
    def __init__(self):
        self['__type_node'] = {}
        self['__type_link'] = {}
        self['meta'] = {}

    def init_node_attr_diff(self, n_id):
        ret = {'__attr_write': {},
               '__attr_remove': []}
        self['__type_node'][n_id] = ret
        return ret

    def init_link_attr_diff(self, l_id):
        ret = {'__attr_write': {},
               '__attr_remove': []}
        self['__type_link'][l_id] = ret
        return ret

    @staticmethod
    def from_json_dict(json_dict):
        ret = Attr_Diff()
        for obj_type, writer, remover in [
            ('__type_node', ret.add_node_attr_write, ret.add_node_attr_rm),
            ('__type_link', ret.add_link_attr_write, ret.add_link_attr_rm),
            ]:
            obj_ad_set = json_dict.get(obj_type)
            if None != obj_ad_set:
                for o_id, ad in obj_ad_set.items():
                    if None != ad.get('__attr_write'):
                        for k, v in ad['__attr_write'].items():
                            writer(o_id, k, v)
                    if None != ad.get('__attr_remove'):
                        for k in ad['__attr_remove']:
                            remover(o_id, k)
        return ret

    @property
    def type__node(self):
        return self['__type_node']

    @property
    def type__link(self):
        return self['__type_link']

    @property
    def meta(self):
        return self['meta']

    def add_node_attr_write(self, n_id, attr_name, attr_val):

        assert 'id' != attr_name.lower(), 'Attr_Diff: attempt to write to \'id\' attribute'

        n_attr_diff = self['__type_node'].get(n_id)
        if None == n_attr_diff:
            n_attr_diff = self.init_node_attr_diff(n_id)
        n_attr_diff['__attr_write'][attr_name] = attr_val

    def add_node_attr_rm(self, n_id, attr_name):

        assert 'id' != attr_name.lower(), 'Attr_Diff: attempt to remove \'id\' attribute'

        n_attr_diff = self['__type_node'].get(n_id)
        if None == n_attr_diff:
            n_attr_diff = self.init_node_attr_diff(n_id)
        n_attr_diff['__attr_remove'].append(attr_name)

    def add_link_attr_write(self, l_id, attr_name, attr_val):

        assert 'id' != attr_name.lower(), 'Attr_Diff: attempt to write to \'id\' attribute'

        l_attr_diff = self['__type_link'].get(l_id)
        if None == l_attr_diff:
            l_attr_diff = self.init_link_attr_diff(l_id)
        l_attr_diff['__attr_write'][attr_name] = attr_val

    def add_link_attr_rm(self, l_id, attr_name):

        assert 'id' != attr_name.lower(), 'Attr_Diff: attempt to remove \'id\' attribute'
        assert 'name' != attr_name.lower(), 'Attr_Diff: attempt to remove link \'name\' attribute, use write instead'

        l_attr_diff = self['__type_link'].get(l_id)
        if None == l_attr_diff:
            l_attr_diff = self.init_link_attr_diff(l_id)
        l_attr_diff['__attr_remove'].append(attr_name)

class Topo_Diff(object):
    """
    Represents a change to the graph topology
    """

    class Commit_Result_Type(dict):
        """
        Topo_Diff graph commit result type
        """
        def __init__(self, node_id_set_add=[],
                           link_id_set_add=[],
                           node_id_set_rm=[],
                           link_id_set_rm=[]):
            self['node_id_set_add'] = node_id_set_add
            self['link_id_set_add'] = link_id_set_add
            self['node_id_set_rm'] = node_id_set_rm
            self['link_id_set_rm'] = link_id_set_rm
            self['meta'] = {}

        @staticmethod
        def from_json_dict(json_dict):
            ret = Topo_Diff.Commit_Result_Type()
            ret.__dict__ = json_dict
            return ret

    class JSON_Encoder(json.JSONEncoder):
        """
        Topo_Diff is not a plain dict, so we provide a json encoder
        """
        def default(self, obj):
            return obj.to_json_dict()

    @staticmethod
    def from_json_dict(json_dict):
        """
        construct from dict - no node/link constructor set must be provided
        """
        ret = Topo_Diff()

        # merge keys - this allows constructor argument omission (link_id_set_rm,
        # node_id_set_rm, etc.) such as when constructing from POST JSON data
        for k, _ in ret.__dict__.items():
            v = json_dict.get(k)
            if None != v:
                ret.__dict__[k] = v
        return ret

    def __init__(self, link_id_set_rm=[],
                       node_id_set_rm=[],
                       node_set_add=[],
                       link_set_add=[],
                       meta={}):

        self.link_id_set_rm = link_id_set_rm
        self.node_id_set_rm = node_id_set_rm
        self.node_set_add = node_set_add
        self.link_set_add = link_set_add
        self.meta = {}

    def __str__(self):
        return __name__ + ': ' + ', '.join('%s: %s' % (k, v) for k, v in self.__dict__.items())

    def len__n_add(self):
        return len(self.node_set_add)

    def len__n_id_rm(self):
        return len(self.node_id_set_rm)

    def len__l_add(self):
        return len(self.link_set_add)

    def len__l_id_rm(self):
        return len(self.link_id_set_rm)

    def is_empty(self):
        return self.len__n_add() + self.len__n_id_rm() + self.len__l_add() + self.len__l_id_rm() == 0

    def to_json_dict(self):
        ret = {k: getattr(self, k) for k in ['link_id_set_rm',
                                             'node_id_set_rm',
                                             'node_set_add',
                                             'link_set_add',
                                             'meta'] }
        return ret

    def to_str__commit_name(self):
        return 'TD: +n:%d l:%d -n:%d -l:%d' % (len(self.node_set_add),
                                               len(self.link_set_add),
                                               len(self.node_id_set_rm),
                                               len(self.link_id_set_rm))

    def check_validity(self, topo_diff_dict):
        """
        Topo_Diff may represent invalid operations, eg. adding a link while
         removing it's end-point - this stub should check for that
        """
        pass