| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2017 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | import json |
| 18 | import logging |
| 19 | import copy |
| 20 | |
| 21 | logging.basicConfig(level=logging.DEBUG) |
| 22 | log = logging.getLogger('RdclGraph') |
| 23 | |
| 24 | |
| 25 | class RdclGraph(object): |
| 26 | """ Operates on the graph representation used for the GUI graph views """ |
| 27 | |
| 28 | node_t3d_base = { |
| 29 | 'info': { |
| 30 | 'property': { |
| 31 | 'custom_label': '', |
| 32 | }, |
| 33 | 'type': '', |
| 34 | 'group': [] |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | def __init__(self): |
| 39 | pass |
| 40 | |
| 41 | def add_link(self, source, target, view, group, graph_object, optional={}): |
| 42 | if (source is None) or (target is None): |
| 43 | return |
| 44 | edge_obj = { |
| 45 | 'source': source, |
| 46 | 'target': target, |
| 47 | 'view': view, |
| 48 | 'group': [group], |
| 49 | |
| 50 | } |
| 51 | |
| 52 | edge_obj.update(optional) |
| 53 | if edge_obj not in graph_object['edges']: |
| 54 | graph_object['edges'].append(edge_obj) |
| 55 | |
| 56 | def add_node(self, id, type, group, positions, graph_object, optional={}): |
| 57 | if id is None: |
| 58 | return |
| 59 | node = next((x for x in graph_object['vertices'] if x['id'] == id), None) |
| 60 | if node is not None: |
| 61 | node['info']['group'].append(group) |
| 62 | else: |
| 63 | node = copy.deepcopy(self.node_t3d_base) |
| 64 | node['id'] = id |
| 65 | node['info']['type'] = type |
| 66 | if group is not None: |
| 67 | node['info']['group'].append(group) |
| 68 | if positions and id in positions['vertices'] and 'x' in positions['vertices'][id] and 'y' in positions['vertices'][id]: |
| 69 | node['fx'] = positions['vertices'][id]['x'] |
| 70 | node['fy'] = positions['vertices'][id]['y'] |
| 71 | node['info'].update(optional) |
| 72 | graph_object['vertices'].append(node) |
| 73 | |
| 74 | def is_directed_edge(self, source_type=None, target_type=None, layer=None, model={}): |
| 75 | if source_type is None or target_type is None or layer is None: |
| 76 | return None |
| 77 | if layer in model['layer'] and 'allowed_edges' in model['layer'][layer]: |
| 78 | if source_type in model['layer'][layer]['allowed_edges'] and target_type in model['layer'][layer]['allowed_edges'][source_type]['destination']: |
| 79 | edge_pro = model['layer'][layer]['allowed_edges'][source_type]['destination'][target_type] |
| 80 | return edge_pro['direct_edge'] if 'direct_edge' in edge_pro else False |
| 81 | |
| 82 | return None |