本文整理汇总了Python中arches.app.models.graph.Graph类的典型用法代码示例。如果您正苦于以下问题:Python Graph类的具体用法?Python Graph怎么用?Python Graph使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Graph类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_resource_graph_file
def load_resource_graph_file(path_to_file):
if isfile(path_to_file) and path_to_file.endswith(suffix):
basepath = path_to_file
name = basepath.split(os.sep)[-1]
with codecs.open(basepath, 'rU', encoding='utf-8') as f:
file = json.load(f)
resource_graph = Graph(file['graph'][0])
resource_graph.save()
示例2: setUp
def setUp(self):
self.NODE_NODETYPE_GRAPHID = '22000000-0000-0000-0000-000000000001'
graph = Graph.new()
graph.name = "TEST GRAPH"
graph.subtitle = "ARCHES TEST GRAPH"
graph.author = "Arches"
graph.description = "ARCHES TEST GRAPH"
graph.ontology_id = "e6e8db47-2ccf-11e6-927e-b8f6b115d7dd"
graph.version = "v1.0.0"
graph.isactive = False
graph.iconclass = "fa fa-building"
graph.nodegroups = []
graph.save()
graph.root.name = 'ROOT NODE'
graph.root.description = 'Test Root Node'
graph.root.ontologyclass = 'http://www.cidoc-crm.org/cidoc-crm/E1_CRM_Entity'
graph.root.datatype = 'semantic'
graph.root.save()
graph = Graph.objects.get(graphid=graph.pk)
self.appended_branch_1 = graph.append_branch('http://www.ics.forth.gr/isl/CRMdig/L54_is_same-as', graphid=self.NODE_NODETYPE_GRAPHID)
self.appended_branch_2 = graph.append_branch('http://www.ics.forth.gr/isl/CRMdig/L54_is_same-as', graphid=self.NODE_NODETYPE_GRAPHID)
graph.save()
self.ROOT_ID = graph.root.nodeid
self.GRAPH_ID = str(graph.pk)
self.NODE_COUNT = 5
self.client = Client()
示例3: import_graph
def import_graph(graphs):
with transaction.atomic():
for resource in graphs:
graph = Graph(resource)
graph.save()
if not hasattr(graph, 'cards_x_nodes_x_widgets'):
print '*********This graph has no attribute cards_x_nodes_x_widgets*********'
sys.exit()
else:
for card_x_node_x_widget in graph.cards_x_nodes_x_widgets:
functions = card_x_node_x_widget['functions']
card_x_node_x_widget.pop('functions', None)
cardxnodexwidget = CardXNodeXWidget.objects.create(**card_x_node_x_widget)
cardxnodexwidget.save()
cardxnodexwidget.functions.set(functions)
if not hasattr(graph, 'forms'):
print '*********This graph has no attribute forms*********'
sys.exit()
else:
for form in graph.forms:
form = Form.objects.create(**form)
form.save()
if not hasattr(graph, 'forms_x_cards'):
print '*********This graph has no attribute forms_x_cards*********'
sys.exit()
else:
for form_x_card in graph.forms_x_cards:
formxcard = FormXCard.objects.create(**form_x_card)
formxcard.save()
if not hasattr(graph, 'reports'):
print '*********This graph has no attribute reports*********'
sys.exit()
else:
for report in graph.reports:
report = Report.objects.create(**report)
report.save()
return Graph
示例4: test_new_graph
def test_new_graph(self):
name = "TEST NEW GRAPH"
author = "ARCHES TEST"
graph = Graph.new(name=name,is_resource=True,author=author)
self.assertEqual(graph.name, name)
self.assertEqual(graph.author, author)
self.assertTrue(graph.isresource)
self.assertFalse(graph.root.is_collector)
self.assertEqual(len(graph.nodes), 1)
self.assertEqual(len(graph.cards), 0)
self.assertEqual(len(graph.get_nodegroups()), 0)
graph = Graph.new(name=name,is_resource=False,author=author)
self.assertEqual(graph.name, name)
self.assertEqual(graph.author, author)
self.assertFalse(graph.isresource)
self.assertTrue(graph.root.is_collector)
self.assertEqual(len(graph.nodes), 1)
self.assertEqual(len(graph.cards), 1)
self.assertEqual(len(graph.get_nodegroups()), 1)
示例5: new
def new(request):
if request.method == 'POST':
data = JSONDeserializer().deserialize(request.body)
isresource = data['isresource'] if 'isresource' in data else False
name = _('New Resource') if isresource else _('New Graph')
author = request.user.first_name + ' ' + request.user.last_name
graph = Graph.new(name=name,is_resource=isresource,author=author)
graph.save()
return JSONResponse(graph)
return HttpResponseNotFound()
示例6: test_get_root_card
def test_get_root_card(self):
"""
test we can get the right parent card
"""
graph = Graph.new(name='TEST',is_resource=False,author='TEST')
graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID)
for card in graph.cards.itervalues():
if card.nodegroup.parentnodegroup is None:
self.assertEqual(graph.get_root_card(), card)
示例7: test_get_root_nodegroup
def test_get_root_nodegroup(self):
"""
test we can get the right parent NodeGroup
"""
graph = Graph.new(name='TEST',is_resource=False,author='TEST')
graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID)
for node in graph.nodes.itervalues():
if node.is_collector:
if node.nodegroup.parentnodegroup is None:
self.assertEqual(graph.get_root_nodegroup(), node.nodegroup)
示例8: post
def post(self, request, graphid=None):
ret = {}
try:
if self.action == 'import_graph':
graph_file = request.FILES.get('importedGraph').read()
graphs = JSONDeserializer().deserialize(graph_file)['graph']
ret = GraphImporter.import_graph(graphs)
else:
if graphid is not None:
graph = Graph.objects.get(graphid=graphid)
data = JSONDeserializer().deserialize(request.body)
if self.action == 'new_graph':
isresource = data['isresource'] if 'isresource' in data else False
name = _('New Resource Model') if isresource else _('New Branch')
author = request.user.first_name + ' ' + request.user.last_name
ret = Graph.new(name=name,is_resource=isresource,author=author)
elif self.action == 'update_node':
graph.update_node(data)
ret = graph
graph.save()
elif self.action == 'update_node_layer':
nodeid = uuid.UUID(str(data.get('nodeid')))
node = graph.nodes[nodeid]
node.config = data['config']
ret = graph
node.save()
elif self.action == 'append_branch':
ret = graph.append_branch(data['property'], nodeid=data['nodeid'], graphid=data['graphid'])
graph.save()
elif self.action == 'move_node':
ret = graph.move_node(data['nodeid'], data['property'], data['newparentnodeid'])
graph.save()
elif self.action == 'clone_graph':
clone_data = graph.copy()
ret = clone_data['copy']
ret.save()
ret.copy_functions(graph, [clone_data['nodes'], clone_data['nodegroups']])
form_map = ret.copy_forms(graph, clone_data['cards'])
ret.copy_reports(graph, [form_map, clone_data['cards'], clone_data['nodes']])
return JSONResponse(ret)
except GraphValidationError as e:
return JSONResponse({'status':'false','message':e.message, 'title':e.title}, status=500)
示例9: test_copy_graph
def test_copy_graph(self):
"""
test that a copy of a graph has the same number of nodes and edges and that the primary keys have been changed
and that the actual node references are different
"""
root = models.Node.objects.get(pk=self.HERITAGE_RESOURCE_FIXTURE)
graph = Graph(root)
graph_copy = graph.copy()
self.assertEqual(len(graph.nodes), len(graph_copy.nodes))
self.assertEqual(len(graph.edges), len(graph_copy.edges))
self.assertEqual(len(graph.nodegroups), len(graph_copy.nodegroups))
def findNodeByName(graph, name):
for key, node in graph.nodes.iteritems():
if node.name == name:
return node
return None
for key, node in graph.nodes.iteritems():
node_copy = findNodeByName(graph_copy, node.name)
self.assertIsNotNone(node_copy)
self.assertNotEqual(node.pk, node_copy.pk)
self.assertNotEqual(id(node), id(node_copy))
self.assertEqual(node.is_collector(), node_copy.is_collector())
if node.nodegroup != None:
self.assertNotEqual(node.nodegroup, node_copy.nodegroup)
for key, newedge in graph_copy.edges.iteritems():
self.assertIsNotNone(graph_copy.nodes[newedge.domainnode_id])
self.assertIsNotNone(graph_copy.nodes[newedge.rangenode_id])
self.assertEqual(newedge.domainnode, graph_copy.nodes[newedge.domainnode.pk])
self.assertEqual(newedge.rangenode, graph_copy.nodes[newedge.rangenode.pk])
with self.assertRaises(KeyError):
graph.edges[newedge.pk]
示例10: test_branch_append
def test_branch_append(self):
"""
test if a branch is properly appended to a graph
"""
nodes_count_before = models.Node.objects.count()
edges_count_before = models.Edge.objects.count()
nodegroups_count_before = models.NodeGroup.objects.count()
graph = Graph(self.rootNode)
graph.append_branch('P1', graphid=self.NODE_NODETYPE_GRAPHID)
graph.save()
self.assertEqual(len(graph.nodes), 3)
self.assertEqual(len(graph.edges), 2)
self.assertEqual(len(graph.nodegroups), 2)
self.assertEqual(models.Node.objects.count()-nodes_count_before, 2)
self.assertEqual(models.Edge.objects.count()-edges_count_before, 2)
self.assertEqual(models.NodeGroup.objects.count()-nodegroups_count_before, 1)
for key, edge in graph.edges.iteritems():
self.assertIsNotNone(graph.nodes[edge.domainnode_id])
self.assertIsNotNone(graph.nodes[edge.rangenode_id])
self.assertEqual(edge.domainnode, graph.nodes[edge.domainnode.pk])
self.assertEqual(edge.rangenode, graph.nodes[edge.rangenode.pk])
for key, node in graph.nodes.iteritems():
if node.istopnode:
self.assertEqual(node, self.rootNode)
appended_branch = graph.append_branch('P1', graphid=self.SINGLE_NODE_GRAPHID)
graph.save()
self.assertEqual(len(graph.nodes), 4)
self.assertEqual(len(graph.edges), 3)
self.assertEqual(len(graph.nodegroups), 2)
self.assertEqual(models.Node.objects.count()-nodes_count_before, 3)
self.assertEqual(models.Edge.objects.count()-edges_count_before, 3)
self.assertEqual(models.NodeGroup.objects.count()-nodegroups_count_before, 1)
self.assertEqual(appended_branch.root.nodegroup,self.rootNode.nodegroup)
示例11: setUp
def setUp(self):
graph = Graph.new()
graph.name = "TEST GRAPH"
graph.subtitle = "ARCHES TEST GRAPH"
graph.author = "Arches"
graph.description = "ARCHES TEST GRAPH"
graph.ontology_id = "e6e8db47-2ccf-11e6-927e-b8f6b115d7dd"
graph.version = "v1.0.0"
graph.isactive = False
graph.iconclass = "fa fa-building"
graph.save()
graph.root.name = 'ROOT NODE'
graph.root.description = 'Test Root Node'
graph.root.ontologyclass = 'E1_CRM_Entity'
graph.root.datatype = 'semantic'
graph.root.save()
self.rootNode = graph.root
示例12: test_delete_node
def test_delete_node(self):
"""
test the node delete method
"""
graph = Graph.new(name='TEST',is_resource=False,author='TEST')
graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID)
graph.save()
node = models.Node.objects.get(graph=graph,name="Node")
nodes_count_before = models.Node.objects.count()
edges_count_before = models.Edge.objects.count()
nodegroups_count_before = models.NodeGroup.objects.count()
card_count_before = models.CardModel.objects.count()
graph.delete_node(node)
nodes_count_after = models.Node.objects.count()
edges_count_after = models.Edge.objects.count()
nodegroups_count_after = models.NodeGroup.objects.count()
card_count_after = models.CardModel.objects.count()
self.assertEqual(nodes_count_before-nodes_count_after, 2)
self.assertEqual(edges_count_before-edges_count_after, 2)
self.assertEqual(nodegroups_count_before-nodegroups_count_after, 1)
self.assertEqual(card_count_before-card_count_after, 1)
graph = Graph.objects.get(graphid=graph.pk)
self.assertEqual(len(graph.nodes), 1)
self.assertEqual(len(graph.edges), 0)
self.assertEqual(len(graph.cards), 1)
self.assertEqual(len(graph.get_nodegroups()), 1)
graph.append_branch('http://www.cidoc-crm.org/cidoc-crm/P1_is_identified_by', graphid=self.NODE_NODETYPE_GRAPHID)
graph.save()
node = models.Node.objects.get(graph=graph,name="Node Type")
graph.delete_node(node)
graph = Graph.objects.get(graphid=graph.pk)
self.assertEqual(len(graph.nodes), 2)
self.assertEqual(len(graph.edges), 1)
self.assertEqual(len(graph.cards), 2)
self.assertEqual(len(graph.get_nodegroups()), 2)
示例13: test_graph_validation_of_branch_with_ontology_appended_to_graph_with_no_ontology
def test_graph_validation_of_branch_with_ontology_appended_to_graph_with_no_ontology(self):
"""
test to make sure we can't append a branch with ontology defined to a graph with no ontology defined
"""
graph = Graph.new()
graph.name = "TEST GRAPH"
graph.ontology = None
graph.save()
graph.root.name = 'ROOT NODE'
graph.root.description = 'Test Root Node'
graph.root.ontologyclass = 'http://www.cidoc-crm.org/cidoc-crm/E1_CRM_Entity'
graph.root.datatype = 'semantic'
graph.root.save()
with self.assertRaises(GraphValidationError) as cm:
graph.save()
the_exception = cm.exception
self.assertEqual(the_exception.code, 1005)
示例14: post
def post(self, request, graphid=None):
ret = {}
if self.action == 'import_graph':
graph_file = request.FILES.get('importedGraph').read()
graphs = JSONDeserializer().deserialize(graph_file)['graph']
GraphImporter.import_graph(graphs)
for graph in graphs:
ret = graph
else:
if graphid is not None:
graph = Graph.objects.get(graphid=graphid)
data = JSONDeserializer().deserialize(request.body)
if self.action == 'new_graph':
isresource = data['isresource'] if 'isresource' in data else False
name = _('New Resource Model') if isresource else _('New Branch')
author = request.user.first_name + ' ' + request.user.last_name
ret = Graph.new(name=name,is_resource=isresource,author=author)
elif self.action == 'update_node':
graph.update_node(data)
ret = graph
graph.save()
elif self.action == 'append_branch':
ret = graph.append_branch(data['property'], nodeid=data['nodeid'], graphid=data['graphid'])
graph.save()
elif self.action == 'move_node':
ret = graph.move_node(data['nodeid'], data['property'], data['newparentnodeid'])
graph.save()
elif self.action == 'clone_graph':
ret = graph.copy()
ret.save()
return JSONResponse(ret)
示例15: import_graph
def import_graph(graphs):
reporter = GraphImportReporter(graphs)
def check_default_configs(default_configs, configs):
if default_configs != None:
if configs == None:
configs = {}
else:
try:
configs.has_key('') #Checking if configs is a dict-like object
except AttributeError:
configs = JSONDeserializer().deserialize(configs)
for default_key in default_configs:
if default_key not in configs:
configs[default_key] = default_configs[default_key]
return configs
with transaction.atomic():
errors = []
for resource in graphs:
reporter.name = resource['name']
reporter.resource_model = resource['isresource']
graph = Graph(resource)
for node in graph.nodes.values():
node_config = node.config
default_config = DDataType.objects.get(datatype=node.datatype).defaultconfig
node.config = check_default_configs(default_config, node_config)
if not hasattr(graph, 'cards'):
errors.append('{0} graph has no attribute cards'.format(graph.name))
else:
if graph.cards == [] or graph.cards == {}:
errors.append('{0} graph has no cards'.format(graph.name))
else:
graph.save()
reporter.update_graphs_saved()
if not hasattr(graph, 'cards_x_nodes_x_widgets'):
errors.append('{0} graph has no attribute cards_x_nodes_x_widgets'.format(graph.name))
else:
for card_x_node_x_widget in graph.cards_x_nodes_x_widgets:
card_x_node_x_widget_config = card_x_node_x_widget['config']
default_config = Widget.objects.get(widgetid=card_x_node_x_widget['widget_id']).defaultconfig
card_x_node_x_widget['config'] = check_default_configs(default_config, card_x_node_x_widget_config)
cardxnodexwidget = CardXNodeXWidget.objects.update_or_create(**card_x_node_x_widget)
if not hasattr(graph, 'forms'):
errors.append('{0} graph has no attribute forms'.format)
else:
for form in graph.forms:
form = Form.objects.update_or_create(**form)
reporter.update_forms_saved()
if not hasattr(graph, 'forms_x_cards'):
errors.append('{0} graph has no attribute forms_x_cards'.format(graph.name))
else:
for form_x_card in graph.forms_x_cards:
formxcard = FormXCard.objects.update_or_create(**form_x_card)
if not hasattr(graph, 'reports'):
errors.append('{0} graph has no attribute reports'.format(graph.name))
else:
for report in graph.reports:
report_config = report['config']
default_config = ReportTemplate.objects.get(templateid=report['template_id']).defaultconfig
report['config'] = check_default_configs(default_config, report_config)
report = Report.objects.update_or_create(**report)
reporter.update_reports_saved()
# try/except block here until all graphs have a resource_2_resource_constraints object.
try:
if not hasattr(graph, 'resource_2_resource_constraints'):
errors.append('{0} graph has no attribute resource_2_resource_constraints'.format(graph.resource_2_resource_constraints))
else:
for resource_2_resource_constraint in graph.resource_2_resource_constraints:
resource2resourceconstraint = Resource2ResourceConstraint.objects.update_or_create(**resource_2_resource_constraint)
except:
pass
return errors, reporter