Skip to content

Commit e05d902

Browse files
authored
Merge pull request #63 from dothebart/addExampleSocialGraph
Add example social graph
2 parents 724fb69 + 50b8d86 commit e05d902

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed

README.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,22 @@ pyArango collections have a caching system for documents that performs insertion
278278
279279
#disable the cache
280280
humans.deactivateCache()
281+
282+
Examples
283+
========
284+
More examples can be found in the examples directory.
285+
To try them out change the connection strings according to your local setup.
286+
287+
Debian Dependency Graph
288+
-----------------------
289+
If you are on a Debian / Ubuntu you can install packages with automatic dependency resolution.
290+
In the end this is a graph. This example parses debian package files using the `deb_pkg_tools`,
291+
and will then create vertices and edges from packages and their relations.
292+
293+
Use `examples/debiangraph.py` to install it, or `examples/fetchDebianDependencyGraph.py` to browse
294+
it as an ascii tree.
295+
296+
ArangoDB Social Graph
297+
---------------------
298+
You can create the `ArangoDB SocialGraph <https://docs.arangodb.com/latest/Manual/Graphs/#the-social-graph>`_ using `examples/createSocialGraph.py`.
299+
It resemples `The original ArangoDB Javascript implementation <https://github.com/arangodb/arangodb/blob/devel/js/common/modules/%40arangodb/graph-examples/example-graph.js#L56>`_ in python.

examples/createSocialGraph.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/python
2+
import sys
3+
from pyArango.connection import *
4+
from pyArango.graph import *
5+
from pyArango.collection import *
6+
7+
8+
class Social(object):
9+
class male(Collection) :
10+
_fields = {
11+
"name" : Field()
12+
}
13+
14+
class female(Collection) :
15+
_fields = {
16+
"name" : Field()
17+
}
18+
19+
class relation(Edges) :
20+
_fields = {
21+
"number" : Field()
22+
}
23+
24+
class social(Graph) :
25+
26+
_edgeDefinitions = (EdgeDefinition ('relation',
27+
fromCollections = ["female", "male"],
28+
toCollections = ["female", "male"]),)
29+
_orphanedCollections = []
30+
31+
32+
def __init__(self):
33+
self.conn = Connection(username="USERNAME", password="SECRET")
34+
35+
self.db = self.conn["_system"]
36+
if self.db.hasGraph('social'):
37+
raise Exception("The social graph was already provisioned! remove it first")
38+
39+
self.female = self.db.createCollection("female")
40+
self.male = self.db.createCollection("male")
41+
42+
self.relation = self.db.createCollection("relation")
43+
44+
g = self.db.createGraph("social")
45+
46+
a = g.createVertex('female', {"name": 'Alice', "_key": 'alice'});
47+
b = g.createVertex('male', {"name": 'Bob', "_key": 'bob'});
48+
c = g.createVertex('male', {"name": 'Charly', "_key": 'charly'});
49+
d = g.createVertex('female', {"name": 'Diana', "_key": 'diana'});
50+
a.save()
51+
b.save()
52+
c.save()
53+
d.save()
54+
55+
g.link('relation', a, b, {"type": 'married', "_key": 'aliceAndBob'})
56+
g.link('relation', a, c, {"type": 'friend', "_key": 'aliceAndCharly'})
57+
g.link('relation', c, d, {"type": 'married', "_key": 'charlyAndDiana'})
58+
g.link('relation', b, d, {"type": 'friend', "_key": 'bobAndDiana'})
59+
60+
61+
Social()

examples/debiangraph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from pyArango.theExceptions import *
3030

3131
# Configure your ArangoDB server connection here
32-
conn = Connection(arangoURL="http://localhost:8529", username="USERNAME", password="SECRET")
32+
conn = Connection(arangoURL="http://localhost:8529", username="root", password="")
3333

3434
db = None
3535
edgeCols = {}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/python
2+
import sys
3+
from pyArango.connection import *
4+
from pyArango.graph import *
5+
from asciitree import *
6+
7+
conn = Connection(username="USERNAME", password="SECRET")
8+
9+
db = conn["ddependencyGrahp"]
10+
11+
if not db.hasGraph('debian_dependency_graph'):
12+
raise Exception("didn't find the debian dependency graph, please import first!")
13+
14+
ddGraph = db.graphs['debian_dependency_graph']
15+
16+
graphQuery = '''
17+
FOR package, depends, path IN
18+
1..2 ANY
19+
@startPackage Depends RETURN path
20+
'''
21+
22+
startNode = sys.argv[1]
23+
24+
bindVars = { "startPackage": "packages/" + startNode }
25+
26+
queryResult = db.AQLQuery(graphQuery, bindVars=bindVars, rawResults=True)
27+
28+
# sub iterateable object to build up the tree for draw_tree:
29+
class Node(object):
30+
def __init__(self, name, children):
31+
self.name = name
32+
self.children = children
33+
34+
def getChild(self, searchName):
35+
for child in self.children:
36+
if child.name == searchName:
37+
return child
38+
return None
39+
40+
def __str__(self):
41+
return self.name
42+
43+
def iteratePath(path, depth, currentNode):
44+
pname = path[depth]['name']
45+
subNode = currentNode.getChild(pname)
46+
if subNode == None:
47+
subNode = Node(pname, [])
48+
currentNode.children.append(subNode)
49+
if len(path) > depth + 1:
50+
iteratePath(path, depth + 1, subNode)
51+
52+
# Now we fold the paths substructure into the tree:
53+
rootNode = Node(startNode, [])
54+
for path in queryResult:
55+
p = path['edges']
56+
iteratePath(p, 0, rootNode)
57+
58+
print draw_tree(rootNode)

0 commit comments

Comments
 (0)