本文整理汇总了Python中arches.app.models.graph.Graph.save方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.save方法的具体用法?Python Graph.save怎么用?Python Graph.save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类arches.app.models.graph.Graph
的用法示例。
在下文中一共展示了Graph.save方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_resource_graph_file
# 需要导入模块: from arches.app.models.graph import Graph [as 别名]
# 或者: from arches.app.models.graph.Graph import save [as 别名]
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: test_branch_append
# 需要导入模块: from arches.app.models.graph import Graph [as 别名]
# 或者: from arches.app.models.graph.Graph import save [as 别名]
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)
示例3: import_graph
# 需要导入模块: from arches.app.models.graph import Graph [as 别名]
# 或者: from arches.app.models.graph.Graph import save [as 别名]
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_move_node
# 需要导入模块: from arches.app.models.graph import Graph [as 别名]
# 或者: from arches.app.models.graph.Graph import save [as 别名]
def test_move_node(self):
"""
test if a node can be successfully moved to another node in the graph
"""
# test moving a single node to another branch
# this node should be grouped with it's new parent nodegroup
graph = Graph(self.rootNode)
branch_one = graph.append_branch('P1', graphid=self.NODE_NODETYPE_GRAPHID)
branch_two = graph.append_branch('P1', graphid=self.NODE_NODETYPE_GRAPHID)
branch_three = graph.append_branch('P1', graphid=self.SINGLE_NODE_GRAPHID)
branch_three_nodeid = branch_three.nodes.iterkeys().next()
branch_one_rootnodeid = branch_one.root.nodeid
graph.move_node(branch_three_nodeid, 'P1', branch_one_rootnodeid)
new_parent_nodegroup = None
moved_branch_nodegroup = None
for node_id, node in graph.nodes.iteritems():
if node_id == branch_one_rootnodeid:
new_parent_nodegroup = node.nodegroup
if node_id == branch_three_nodeid:
moved_branch_nodegroup = node.nodegroup
self.assertIsNotNone(new_parent_nodegroup)
self.assertIsNotNone(moved_branch_nodegroup)
self.assertEqual(new_parent_nodegroup, moved_branch_nodegroup)
# test moving a branch to another branch
# this branch should NOT be grouped with it's new parent nodegroup
branch_two_rootnodeid = branch_two.root.nodeid
graph.move_node(branch_one_rootnodeid, 'P1', branch_two_rootnodeid)
new_parent_nodegroup = None
moved_branch_nodegroup = None
for node_id, node in graph.nodes.iteritems():
if node_id == branch_two_rootnodeid:
new_parent_nodegroup = node.nodegroup
if node_id == branch_one_rootnodeid:
moved_branch_nodegroup = node.nodegroup
self.assertIsNotNone(new_parent_nodegroup)
self.assertIsNotNone(moved_branch_nodegroup)
self.assertNotEqual(new_parent_nodegroup, moved_branch_nodegroup)
updated_edge = None
for edge_id, edge in graph.edges.iteritems():
if (edge.domainnode_id == branch_two_rootnodeid and
edge.rangenode_id == branch_one_rootnodeid):
updated_edge = edge
self.assertIsNotNone(updated_edge)
# save and retrieve the graph from the database and confirm that
# the graph shape has been saved properly
graph.save()
graph = Graph(self.rootNode)
tree = graph.get_tree()
self.assertEqual(len(tree['children']), 1)
level_one_node = tree['children'][0]
self.assertEqual(branch_two_rootnodeid, level_one_node['node'].nodeid)
self.assertEqual(len(level_one_node['children']), 2)
for child in level_one_node['children']:
if child['node'].nodeid == branch_one_rootnodeid:
self.assertEqual(len(child['children']), 2)
found_branch_three = False
for child in child['children']:
if child['node'].nodeid == branch_three_nodeid:
found_branch_three = True
self.assertTrue(found_branch_three)
else:
self.assertEqual(len(child['children']), 0)
示例5: import_graph
# 需要导入模块: from arches.app.models.graph import Graph [as 别名]
# 或者: from arches.app.models.graph.Graph import save [as 别名]
def import_graph(graphs, overwrite_graphs=True):
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:
try:
if resource['ontology_id'] != None:
if resource['ontology_id'] not in [str(f['ontologyid']) for f in Ontology.objects.all().values('ontologyid')]:
errors.append('The ontologyid of the graph you\'re trying to load does not exist in Arches.')
reporter.name = resource['name']
reporter.resource_model = resource['isresource']
graph = Graph(resource)
ontology_classes = [str(f['source']) for f in OntologyClass.objects.all().values('source')]
for node in graph.nodes.values():
if resource['ontology_id'] != None:
if node.ontologyclass not in ontology_classes:
errors.append('The ontology class of this node does not exist in the indicated ontology scheme.')
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:
if len(Graph.objects.filter(pk=graph.graphid)) == 0 or overwrite_graphs == True:
graph.save()
reporter.update_graphs_saved()
else:
overwrite_input = raw_input('Overwrite {0} (Y/N) ? '.format(graph.name))
if overwrite_input.lower() in ('t', 'true', 'y', 'yes'):
graph.save()
else:
raise GraphImportException('{0} - already exists. Skipping import.'.format(graph.name))
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
except Exception as e:
print e
return errors, reporter
示例6: import_graph
# 需要导入模块: from arches.app.models.graph import Graph [as 别名]
# 或者: from arches.app.models.graph.Graph import save [as 别名]
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