This repository has been archived by the owner on Nov 1, 2018. It is now read-only.
forked from turtlebender/agamemnon
-
Notifications
You must be signed in to change notification settings - Fork 6
/
usage.html
209 lines (201 loc) · 16.7 KB
/
usage.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Usage — Agamemnon v0.2.2.3 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
VERSION: '0.2.2.3',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="Agamemnon v0.2.2.3 documentation" href="index.html" />
<link rel="prev" title="Agamemnon Architecture" href="arch.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="arch.html" title="Agamemnon Architecture"
accesskey="P">previous</a> |</li>
<li><a href="index.html">Agamemnon v0.2.2.3 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="usage">
<h1>Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h1>
<p>The following is an example of how to use Agamemnon in your own code</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">agamemnon.factory</span> <span class="kn">import</span> <span class="n">load_from_settings</span>
</pre></div>
</div>
<p>First, we can decide which kind of data store we are creating. In this case we’re creating an InMemory data store</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="c">#config = {'agamemnon.keyspace':'agamemnontest', 'agamemnon.host_list':'["localhost:9160"]'}</span>
<span class="gp">>>> </span><span class="n">config</span> <span class="o">=</span> <span class="p">{</span><span class="s">'agamemnon.keyspace'</span><span class="p">:</span><span class="s">'memory'</span><span class="p">}</span>
<span class="gp">>>> </span><span class="n">graph_db</span> <span class="o">=</span> <span class="n">load_from_settings</span><span class="p">(</span><span class="n">config</span><span class="p">)</span>
</pre></div>
</div>
<p>In honor of The Simpsons Movie, we’ll create a node called spiderpig</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">spiderpig</span> <span class="o">=</span> <span class="n">graph_db</span><span class="o">.</span><span class="n">create_node</span><span class="p">(</span><span class="s">'test_type'</span><span class="p">,</span> <span class="s">'spiderpig'</span><span class="p">,</span> <span class="p">{</span><span class="s">'sound'</span><span class="p">:</span><span class="s">'oink'</span><span class="p">})</span>
</pre></div>
</div>
<p>Now we will retrieve the spiderpig from the graph and check that the attributes were correct.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">spiderpig</span> <span class="o">=</span> <span class="n">graph_db</span><span class="o">.</span><span class="n">get_node</span><span class="p">(</span><span class="s">'test_type'</span><span class="p">,</span> <span class="s">'spiderpig'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">spiderpig</span><span class="p">[</span><span class="s">'sound'</span><span class="p">]</span>
<span class="go">'oink'</span>
</pre></div>
</div>
<p>Now we will create a friend for the spiderpig (who also happens to be his alter ego). Again, let’s check to
confirm that the node and it’s attributes were created correctly.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">harry_plopper</span> <span class="o">=</span> <span class="n">graph_db</span><span class="o">.</span><span class="n">create_node</span><span class="p">(</span><span class="s">'test_type'</span><span class="p">,</span> <span class="s">'Harry Plopper'</span><span class="p">,</span> <span class="p">{</span><span class="s">'sound'</span><span class="p">:</span><span class="s">'plop'</span><span class="p">})</span>
<span class="gp">>>> </span><span class="n">harry_plopper</span> <span class="o">=</span> <span class="n">graph_db</span><span class="o">.</span><span class="n">get_node</span><span class="p">(</span><span class="s">'test_type'</span><span class="p">,</span><span class="s">'Harry Plopper'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">harry_plopper</span><span class="p">[</span><span class="s">'sound'</span><span class="p">]</span>
<span class="go">'plop'</span>
</pre></div>
</div>
<p>Nodes can have different types as well. Here we create a node of type simpson, with name Homer. This node has
different attributes than the previous nodes.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">homer</span> <span class="o">=</span> <span class="n">graph_db</span><span class="o">.</span><span class="n">create_node</span><span class="p">(</span><span class="s">'simpson'</span><span class="p">,</span> <span class="s">'Homer'</span><span class="p">,</span> <span class="p">{</span><span class="s">'sound'</span><span class="p">:</span><span class="s">'Doh'</span><span class="p">,</span> <span class="s">'job'</span><span class="p">:</span><span class="s">'Safety Technician'</span><span class="p">})</span>
<span class="gp">>>> </span><span class="n">homer</span> <span class="o">=</span> <span class="n">graph_db</span><span class="o">.</span><span class="n">get_node</span><span class="p">(</span><span class="s">'simpson'</span><span class="p">,</span> <span class="s">'Homer'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">homer</span><span class="p">[</span><span class="s">'sound'</span><span class="p">]</span>
<span class="go">'Doh'</span>
<span class="gp">>>> </span><span class="n">homer</span><span class="p">[</span><span class="s">'job'</span><span class="p">]</span>
<span class="go">'Safety Technician'</span>
</pre></div>
</div>
<p>Nodes by themselves are not very useful. Let’s create a relationship between spiderpig and Harry Plopper.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">rel</span> <span class="o">=</span> <span class="n">spiderpig</span><span class="o">.</span><span class="n">friend</span><span class="p">(</span><span class="n">harry_plopper</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="s">'spiderpig_harry_plopper_alliance'</span><span class="p">,</span> <span class="n">alter_ego</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">best</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</pre></div>
</div>
<p>This creates a relationship of type friend. The key has been specified in this case, although it is not necessary.
If no key is supplied a uuid will be generated for the relationship.</p>
<p>Every node type has a “reference node”. This is a metanode for the type and functions as an index for all nodes of a
given type.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">reference_node</span> <span class="o">=</span> <span class="n">graph_db</span><span class="o">.</span><span class="n">get_reference_node</span><span class="p">(</span><span class="s">'test_type'</span><span class="p">)</span>
</pre></div>
</div>
<p>Getting the instances from the test_type reference node should return the Harry Plopper node and the spiderpig node.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">sorted</span><span class="p">([</span><span class="n">rel</span><span class="o">.</span><span class="n">target_node</span><span class="o">.</span><span class="n">key</span> <span class="k">for</span> <span class="n">rel</span> <span class="ow">in</span> <span class="n">reference_node</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">outgoing</span><span class="p">])</span>
<span class="go">['Harry Plopper', 'spiderpig']</span>
</pre></div>
</div>
<p>The spiderpig should only have one friend at this point, and it should be Harry Plopper</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">friends</span> <span class="o">=</span> <span class="p">[</span><span class="n">rel</span> <span class="k">for</span> <span class="n">rel</span> <span class="ow">in</span> <span class="n">spiderpig</span><span class="o">.</span><span class="n">friend</span><span class="p">]</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">friends</span><span class="p">)</span>
<span class="go">1</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">friends</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">target_node</span><span class="o">.</span><span class="n">key</span>
<span class="go">'Harry Plopper'</span>
</pre></div>
</div>
<p>Now let’s confirm that Harry Plopper is friends with spider pig as well:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">'spiderpig'</span> <span class="ow">in</span> <span class="n">harry_plopper</span><span class="o">.</span><span class="n">friend</span>
<span class="go">True</span>
</pre></div>
</div>
<p>And, once more, make sure that spider pig is Harry Plopper’s only friend:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">friends</span> <span class="o">=</span> <span class="p">[</span><span class="n">rel</span> <span class="k">for</span> <span class="n">rel</span> <span class="ow">in</span> <span class="n">harry_plopper</span><span class="o">.</span><span class="n">friend</span><span class="p">]</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">friends</span><span class="p">)</span>
<span class="go">1</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">friends</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">source_node</span><span class="o">.</span><span class="n">key</span>
<span class="go">'spiderpig'</span>
</pre></div>
</div>
<p>They should not be best friends. Let’s confirm this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">friends</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s">'best'</span><span class="p">]</span>
<span class="go">False</span>
</pre></div>
</div>
<p>Homer is spiderpig’s best friend:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">rel</span> <span class="o">=</span> <span class="n">homer</span><span class="o">.</span><span class="n">friend</span><span class="p">(</span><span class="n">spiderpig</span><span class="p">,</span> <span class="n">best</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">alter_ego</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">'love'</span><span class="p">,</span> <span class="n">strength</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
<p>Here we added additional attributes to the relationship.</p>
<p>Now spiderpig should have 2 friends.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">friends</span> <span class="o">=</span> <span class="p">[</span><span class="n">rel</span> <span class="k">for</span> <span class="n">rel</span> <span class="ow">in</span> <span class="n">spiderpig</span><span class="o">.</span><span class="n">friend</span><span class="p">]</span>
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">friends</span><span class="p">)</span>
<span class="go">2</span>
</pre></div>
</div>
<p>You can get a list of all of the relationships of a particular type between a node and other nodes with a particular key</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">homer_spiderpig_love</span> <span class="o">=</span> <span class="n">spiderpig</span><span class="o">.</span><span class="n">friend</span><span class="o">.</span><span class="n">relationships_with</span><span class="p">(</span><span class="s">'Homer'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">homer_spiderpig_love</span><span class="p">)</span>
<span class="go">1</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">homer_spiderpig_love</span> <span class="o">=</span> <span class="n">spiderpig</span><span class="o">.</span><span class="n">friend</span><span class="o">.</span><span class="n">relationships_with</span><span class="p">(</span><span class="s">'Homer'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">homer_spiderpig_love</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s">'strength'</span><span class="p">]</span>
<span class="go">100</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="arch.html"
title="previous chapter">Agamemnon Architecture</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/usage.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" size="18" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="arch.html" title="Agamemnon Architecture"
>previous</a> |</li>
<li><a href="index.html">Agamemnon v0.2.2.3 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2011, Tom Howe.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
</div>
</body>
</html>