21 from __future__
import print_function
25 from py2neo
import neo4j, cypher
28 graph_db = neo4j.GraphDatabaseService(
"http://localhost:7474/db/data/")
30 print(
'Version of Neo4J:', graph_db.neo4j_version)
33 nodetypes = {
'Ring':
True,
'Drone':
True,
'Switch':
True,
'NIC':
False,
'IPaddr':
True,
'MACaddr':
True}
38 indices = [key
for key
in nodetypes.keys()
if nodetypes[key]]
44 print (
'Creating index %s' % index)
45 indextbl[index] = graph_db.get_or_create_index(neo4j.Node, index)
46 print (
'Creating index %s' %
'nodetype')
47 indextbl[
'nodetype'] = graph_db.get_or_create_index(neo4j.Node,
'nodetype')
49 nodetypeindex = indextbl[
'nodetype']
50 Ringindex = indextbl[
'Ring']
51 for index
in nodetypes.keys():
53 nodetypeindex.get_or_create(
'nodetype', index, {
'name':
'#%sType'%index,
'nodetype':
'nodetype'})
54 nodetypetbl[index] = topnode
57 def node_new(nodetype, nodename, properties={}):
58 '''Possibly creates a new node, puts it in its appropriate index and creates an IS_A relationship
59 with the nodetype object corresponding its nodetype.
60 It is created and added to indexes if it doesn't already exist in its corresponding index - if there is one.
61 If it already exists, the pre-existing node is returned.
62 If this object type doesn't have an index, it will always be created.
63 Note that the nodetype has to be in the nodetypetable - even if its NULL (for error detection).
64 The IS_A relationship may be useful -- or not. Hard to say at this point...'''
65 properties[
'nodetype'] = nodetype
66 properties[
'name'] = nodename
67 if indextbl.has_key(nodetype):
68 idx = indextbl[nodetype]
69 obj = idx.get_or_create(nodetype, nodename, properties)
73 obj = graph_db.create(properties)
74 nt = nodetypetable[nodetype]
76 graph_db.relate((obj,
'IS_A', nt),)
86 query =
"START a=node({A}) MATCH a-[r:IS_A]->b RETURN a, r, b"
92 if bname.endswith(
'Type'):
93 bname=bname[0:len(bname)-4]
94 print(a[
'name'] +
' ' + str(rel.type) +
' ' + bname +
' [nodetype: %s]' % a[
'nodetype'])
97 cypher.execute(graph_db, query, {
"A": TheOneRing.id}, row_handler=print_row)
98 cypher.execute(graph_db, query, {
"A": servidor.id}, row_handler=print_row)