The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
testpy2neo.py
Go to the documentation of this file.
1 #
2 #
3 # This file is part of the Assimilation Project.
4 #
5 # Copyright (C) 2011, 2012 - Alan Robertson <alanr@unix.sh>
6 #
7 # The Assimilation software is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # The Assimilation software is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with the Assimilation Project software. If not, see http://www.gnu.org/licenses/
19 #
20 #
21 from __future__ import print_function
22 
23 import re
24 # Import Neo4j modules
25 from py2neo import neo4j, cypher
26 
27 # Attach to the graph db instance
28 graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")
29 
30 print('Version of Neo4J:', graph_db.neo4j_version)
31 
32 # List of node types along with True if we create a corresponding index
33 nodetypes = {'Ring': True, 'Drone':True, 'Switch':True, 'NIC': False, 'IPaddr':True, 'MACaddr': True}
34 
35 
36 
37 
38 indices = [key for key in nodetypes.keys() if nodetypes[key]]
39 
40 indextbl = {}
41 nodetypetbl = {}
42 
43 for index in indices:
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')
48 
49 nodetypeindex = indextbl['nodetype']
50 Ringindex = indextbl['Ring']
51 for index in nodetypes.keys():
52  topnode = \
53  nodetypeindex.get_or_create('nodetype', index, {'name': '#%sType'%index, 'nodetype':'nodetype'})
54  nodetypetbl[index] = topnode
55 
56 
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)
70  #for pname in properties.keys():
71  # obj[pname] = properties[pname]
72  else:
73  obj = graph_db.create(properties)
74  nt = nodetypetable[nodetype]
75  if nt is not None:
76  graph_db.relate((obj, 'IS_A', nt),)
77  return obj
78 
79 TheOneRing = node_new('Ring', 'TheOneRing')
80 servidor = node_new('Drone', 'servidor')
81 
82 
83 
84 
85 # Build a Cypher query
86 query = "START a=node({A}) MATCH a-[r:IS_A]->b RETURN a, r, b"
87 
88 # Define two row handlers...
89 def print_row(row):
90  a, rel, b = row
91  bname = b['name'][1:]
92  if bname.endswith('Type'):
93  bname=bname[0:len(bname)-4]
94  print(a['name'] + ' ' + str(rel.type) + ' ' + bname + ' [nodetype: %s]' % a['nodetype'])
95 
96 # ...and execute the query
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)
99 # Indexes:
100 # ringindex - index of all Ring objects [nodetype=ring]
101 # droneindex - index of all Drone objects [nodetype=drone]
102 # ipindex - index of all IP address objects [nodetype=ipaddr]
103 # macindex - index of all interfaces by MAC address [nodetype=nic]
104 
105 # Node types [nodetype enumeration values]:
106 # ring - heartbeat ring objects
107 # drone - systems running our nanoprobes
108 # nic - interfaces on drones
109 # ipaddr - IP addresses (ipv4 or ipv6)
110 
111 # Relationship types [reltype enumeration values]
112 # ------------------------------------------
113 # reltype fromnodetype tonodetype
114 # -------- ------------ ----------
115 # nichost nic drone
116 # ipowner ipaddr nic
117 # ringnext drone drone
118 # ringmember ring ipaddr
def print_row
Definition: testpy2neo.py:89
def node_new
Definition: testpy2neo.py:57