Biblioteca Java - Blame information for rev 18
Subversion Repositories:
(root)/Frameworks and Technologies/Neo4J Samples/Neo4JTutorial/src/main/java/com/linkscreens/graphsin/repository/SocNet.java
Rev | Author | Line No. | Line |
---|---|---|---|
18 | mihai | 1 | package com.linkscreens.graphsin.repository; |
2 | |||
3 | /** | ||
4 | * Licensed to Neo Technology under one or more contributor | ||
5 | * license agreements. See the NOTICE file distributed with | ||
6 | * this work for additional information regarding copyright | ||
7 | * ownership. Neo Technology licenses this file to you under | ||
8 | * the Apache License, Version 2.0 (the "License"); you may | ||
9 | * not use this file except in compliance with the License. | ||
10 | * You may obtain a copy of the License at | ||
11 | * | ||
12 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
13 | * | ||
14 | * Unless required by applicable law or agreed to in writing, | ||
15 | * software distributed under the License is distributed on an | ||
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
17 | * KIND, either express or implied. See the License for the | ||
18 | * specific language governing permissions and limitations | ||
19 | * under the License. | ||
20 | */ | ||
21 | |||
22 | import java.util.ArrayList; | ||
23 | import java.util.Iterator; | ||
24 | import java.util.Random; | ||
25 | |||
26 | import org.neo4j.graphdb.GraphDatabaseService; | ||
27 | import org.neo4j.graphdb.Node; | ||
28 | import org.neo4j.graphdb.Transaction; | ||
29 | import org.neo4j.graphdb.factory.GraphDatabaseFactory; | ||
30 | import org.neo4j.graphdb.index.Index; | ||
31 | import org.neo4j.helpers.collection.IteratorUtil; | ||
32 | |||
33 | public class SocNet | ||
34 | { | ||
35 | private static final Random r = new Random( System.currentTimeMillis() ); | ||
36 | private static final int nrOfPersons = 10; | ||
37 | private static final int nrOffFriends = 3; | ||
38 | private static final int nrOfPosts = 5; | ||
39 | private static final int nrOfAlbums = 5; | ||
40 | private static final int nrOfMediaItems = 4; | ||
41 | private static final int nrOfComments = 3; | ||
42 | |||
43 | private GraphDatabaseService graphDb; | ||
44 | private PersonRepository personRepository; | ||
45 | private AlbumRepository albumRepository; | ||
46 | private MediaItemRepository mediaRepository; | ||
47 | |||
48 | public void setup(String storeDir) throws Exception | ||
49 | { | ||
50 | this.graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( storeDir ); | ||
51 | try ( Transaction tx = graphDb.beginTx() ) | ||
52 | { | ||
53 | Index<Node> index = graphDb.index().forNodes( "nodes" ); | ||
54 | personRepository = new PersonRepository( graphDb, index ); | ||
55 | |||
56 | Index<Node> indexA = graphDb.index().forNodes( "albums" ); | ||
57 | albumRepository = new AlbumRepository(graphDb, indexA); | ||
58 | |||
59 | Index<Node> indexM = graphDb.index().forNodes( "mediaitems" ); | ||
60 | mediaRepository = new MediaItemRepository(graphDb, indexM); | ||
61 | |||
62 | createPersons(); | ||
63 | setupFriendsBetweenPeople( nrOffFriends ); | ||
64 | |||
65 | multipleStatusesGenerator(); | ||
66 | tx.success(); | ||
67 | } | ||
68 | |||
69 | Transaction tx = graphDb.beginTx(); | ||
70 | try{ | ||
71 | |||
72 | createAlbums(); | ||
73 | |||
74 | tx.success(); | ||
75 | }catch(Exception e){ | ||
76 | e.printStackTrace(); | ||
77 | |||
78 | }finally{ | ||
79 | try{ | ||
80 | tx.close(); | ||
81 | }catch(org.neo4j.graphdb.TransactionFailureException exx){ | ||
82 | exx.printStackTrace(); | ||
83 | } | ||
84 | } | ||
85 | |||
86 | |||
87 | } | ||
88 | |||
89 | //GRAPH GENERATOR | ||
90 | |||
91 | private void createAlbums() throws Exception{ | ||
92 | System.out.println("Create albums"); | ||
93 | |||
94 | //iterate all persons | ||
95 | for ( PersonNode person : personRepository.getAllPersons() ) | ||
96 | { | ||
97 | int albumsCount = r.nextInt( nrOfAlbums ) + 1; | ||
98 | //add a random number of albums to user | ||
99 | for ( int j = 0; j < albumsCount; j++ ) | ||
100 | { | ||
101 | |||
102 | AlbumNode a = albumRepository.createAlbum(person.getName()+" album "+j); | ||
103 | person.addAlbum(a); //add album to peson | ||
104 | |||
105 | //for each album add a number of media items and for each item a number of comments | ||
106 | for ( int k = 0; k < nrOfMediaItems; k++ ){ | ||
107 | MediaItemNode item = mediaRepository.createMediaItem(person.getName()+a.getTitle()+" item "+k); | ||
108 | a.addMediaItemInAlbum(item); | ||
109 | |||
110 | ArrayList<String> comments = new ArrayList<>(); | ||
111 | //create some comments | ||
112 | for(int i=0;i<nrOfComments;i++){ | ||
113 | comments.add( "Comment msg "+i ); | ||
114 | } | ||
115 | |||
116 | for(String text:comments){ | ||
117 | item.addStatus(text); | ||
118 | } | ||
119 | } | ||
120 | |||
121 | System.out.println("Add item for"+person.getName()+" in album "+a.getTitle()); | ||
122 | } | ||
123 | } | ||
124 | |||
125 | } | ||
126 | |||
127 | private void setupFriendsBetweenPeople( int maxNrOfFriendsEach ) | ||
128 | { | ||
129 | for ( PersonNode person : personRepository.getAllPersons() ) | ||
130 | { | ||
131 | int nrOfFriends = r.nextInt( maxNrOfFriendsEach ) + 1; | ||
132 | for ( int j = 0; j < nrOfFriends; j++ ) | ||
133 | { | ||
134 | person.addFriend( getRandomPerson() ); | ||
135 | } | ||
136 | } | ||
137 | } | ||
138 | |||
139 | private PersonNode getRandomPerson() | ||
140 | { | ||
141 | return personRepository.getPersonByEmail( "person#" | ||
142 | + r.nextInt( nrOfPersons ) ); | ||
143 | } | ||
144 | |||
145 | private void deleteSocialGraph() | ||
146 | { | ||
147 | try ( Transaction tx = graphDb.beginTx() ) | ||
148 | { | ||
149 | for ( PersonNode person : personRepository.getAllPersons() ) | ||
150 | { | ||
151 | personRepository.deletePerson( person ); | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | |||
156 | private PersonNode getRandomFriendOf( PersonNode p ) | ||
157 | { | ||
158 | ArrayList<PersonNode> friends = new ArrayList<>(); | ||
159 | IteratorUtil.addToCollection( p.getFriends().iterator(), friends ); | ||
160 | return friends.get( r.nextInt( friends.size() ) ); | ||
161 | } | ||
162 | |||
163 | public String getRandomPersonWithFriends() | ||
164 | { | ||
165 | PersonNode p; | ||
166 | try ( Transaction tx = graphDb.beginTx() ){ | ||
167 | do | ||
168 | { | ||
169 | p = getRandomPerson(); | ||
170 | } | ||
171 | while ( p.getNrOfFriends() == 0 ); | ||
172 | return p.getName(); | ||
173 | } | ||
174 | |||
175 | } | ||
176 | |||
177 | private void createPersons() throws Exception | ||
178 | { | ||
179 | for ( int i = 0; i < nrOfPersons; i++ ) | ||
180 | { | ||
181 | personRepository.createPerson( "person#" + i ); | ||
182 | } | ||
183 | } | ||
184 | |||
185 | private void multipleStatusesGenerator() throws Exception | ||
186 | { | ||
187 | ArrayList<String> statuses = new ArrayList<>(); | ||
188 | for(int i=0;i<r.nextInt( nrOfPosts );i++){ | ||
189 | statuses.add( "Test msg "+i ); | ||
190 | |||
191 | } | ||
192 | |||
193 | |||
194 | for(int i=0;i<r.nextInt( nrOfPersons );i++){ | ||
195 | PersonNode person = getRandomPerson(); | ||
196 | for ( String status : statuses ) | ||
197 | { | ||
198 | person.addStatus( status ); | ||
199 | } | ||
200 | } | ||
201 | |||
202 | } | ||
203 | |||
204 | ////////////// END NETWORK GENERATOR //////////// | ||
205 | |||
206 | |||
207 | ///////////////////////// API ///////////////////////// | ||
208 | |||
209 | public void addStatus(String name, String text){ | ||
210 | try ( Transaction tx = graphDb.beginTx() ) | ||
211 | { | ||
212 | PersonNode p = personRepository.getPersonByEmail(name); | ||
213 | p.addStatus(text); | ||
214 | } | ||
215 | } | ||
216 | |||
217 | public void createFriend(String name1, String name2){ | ||
218 | try ( Transaction tx = graphDb.beginTx() ) | ||
219 | { | ||
220 | PersonNode p1 = this.personRepository.getPersonByEmail(name1); | ||
221 | PersonNode p2 = this.personRepository.getPersonByEmail(name2); | ||
222 | p1.addFriend(p2); | ||
223 | } | ||
224 | } | ||
225 | |||
226 | public String getFriendRecommandation(String name){ | ||
227 | String reco = null; | ||
228 | try ( Transaction tx = graphDb.beginTx() ) | ||
229 | { | ||
230 | PersonNode p = this.personRepository.getPersonByEmail(name); | ||
231 | PersonNode recommendation = IteratorUtil.single( p.getFriendRecommendation( 1 ).iterator() ); | ||
232 | reco = recommendation.getName(); | ||
233 | } | ||
234 | return reco; | ||
235 | } | ||
236 | |||
237 | public ArrayList<String> getFriendsOf(String name){ | ||
238 | |||
239 | ArrayList<String> friends = new ArrayList<>(); | ||
240 | try ( Transaction tx = graphDb.beginTx() ) | ||
241 | { | ||
242 | PersonNode p = personRepository.getPersonByEmail(name); | ||
243 | for (Iterator iterator = p.getFriends().iterator(); iterator.hasNext();) { | ||
244 | PersonNode person = (PersonNode) iterator.next(); | ||
245 | friends.add(person.getName()); | ||
246 | } | ||
247 | } | ||
248 | return friends; | ||
249 | } | ||
250 | |||
251 | public ArrayList<String> getAllPersons(){ | ||
252 | ArrayList<String> persons = new ArrayList<>(); | ||
253 | try ( Transaction tx = graphDb.beginTx() ) | ||
254 | { | ||
255 | for (Iterator iterator =personRepository.getAllPersons().iterator(); iterator.hasNext();) { | ||
256 | PersonNode person = (PersonNode) iterator.next(); | ||
257 | persons.add(person.getName()); | ||
258 | } | ||
259 | } | ||
260 | return persons; | ||
261 | } | ||
262 | |||
263 | public ArrayList<String> retrieveStatusUpdates(String name) throws Exception | ||
264 | { | ||
265 | PersonNode person; | ||
266 | |||
267 | ArrayList<String> updates = new ArrayList<String>(); | ||
268 | try ( Transaction tx = graphDb.beginTx() ) | ||
269 | { | ||
270 | |||
271 | person = this.personRepository.getPersonByEmail(name); | ||
272 | Iterator<StatusUpdateNode> i = person.friendStatuses(); | ||
273 | while(i.hasNext()){ | ||
274 | StatusUpdateNode su = i.next(); | ||
275 | updates.add(su.getPerson().getName()+":+"+su.getDate().toString()+":"+su.getStatusText()); | ||
276 | } | ||
277 | |||
278 | } | ||
279 | |||
280 | return updates; | ||
281 | } | ||
282 | |||
283 | public ArrayList<String> retreivePersonMediaAlbums(String name){ | ||
284 | PersonNode person; | ||
285 | |||
286 | ArrayList<String> list = new ArrayList<String>(); | ||
287 | try ( Transaction tx = graphDb.beginTx() ) | ||
288 | { | ||
289 | person = this.personRepository.getPersonByEmail(name); | ||
290 | Iterator<AlbumNode> i = person.getAlbums().iterator(); | ||
291 | while(i.hasNext()){ | ||
292 | AlbumNode su = i.next(); | ||
293 | list.add(su.getId()); | ||
294 | } | ||
295 | } | ||
296 | return list; | ||
297 | |||
298 | } | ||
299 | |||
300 | public ArrayList<String> getMediaItems(String albumId){ | ||
301 | ArrayList<String> list = new ArrayList(); | ||
302 | try ( Transaction tx = graphDb.beginTx() ) | ||
303 | { | ||
304 | AlbumNode album = albumRepository.getAlbumById(albumId); | ||
305 | Iterator<MediaItemNode> i = album.getMediaItems().iterator(); | ||
306 | while(i.hasNext()) | ||
307 | list.add(i.next().getId()); | ||
308 | } | ||
309 | return list; | ||
310 | } | ||
311 | |||
312 | public ArrayList<String> getMediaItemComments(String itemId){ | ||
313 | ArrayList<String> list = new ArrayList(); | ||
314 | try ( Transaction tx = graphDb.beginTx() ) | ||
315 | { | ||
316 | MediaItemNode item = this.mediaRepository.getMediaItemById(itemId); | ||
317 | Iterator<CommentNode> i = item.getComments().iterator(); | ||
318 | while(i.hasNext()){ | ||
319 | list.add(i.next().getCommentText()); | ||
320 | } | ||
321 | } | ||
322 | return list; | ||
323 | } | ||
324 | |||
325 | ///////////////Utility methods///////////////////////////////////// | ||
326 | |||
327 | private <T> ArrayList<T> fromIterableToArrayList( Iterator<T> iterable ) | ||
328 | { | ||
329 | ArrayList<T> collection = new ArrayList<>(); | ||
330 | IteratorUtil.addToCollection( iterable, collection ); | ||
331 | return collection; | ||
332 | } | ||
333 | |||
334 | public void displayCollection(ArrayList<String> list){ | ||
335 | for (Iterator iterator = list.iterator(); iterator.hasNext();) { | ||
336 | String string = (String) iterator.next(); | ||
337 | System.out.println(string); | ||
338 | } | ||
339 | } | ||
340 | |||
341 | //////////////////////////////////////////////////// | ||
342 | |||
343 | public static void main(String[] args) throws Exception{ | ||
344 | SocNet net = new SocNet(); | ||
345 | net.setup("d://temp//_socnet4"); | ||
346 | System.out.println("Network created!"); | ||
347 | |||
348 | |||
349 | //System.out.println("All persons in the net:"); | ||
350 | //net.displayCollection(list); | ||
351 | |||
352 | for(int i=0;i<1;i++){ | ||
353 | T t = new T(net); | ||
354 | t.start(); | ||
355 | } | ||
356 | |||
357 | } | ||
358 | |||
359 | |||
360 | } | ||
361 | |||
362 | class T extends Thread{ | ||
363 | |||
364 | SocNet net; | ||
365 | |||
366 | T(SocNet n){net = n;} | ||
367 | |||
368 | public void run(){ | ||
369 | long t = System.currentTimeMillis(); | ||
370 | String p = net.getRandomPersonWithFriends(); | ||
371 | |||
372 | /*ArrayList list = net.getAllPersons(); | ||
373 | |||
374 | System.out.println(">>>>Get all persons in "+(t - System.currentTimeMillis())); | ||
375 | t = System.currentTimeMillis(); | ||
376 | String reco = net.getFriendRecommandation(p); | ||
377 | System.out.println("Recommandation for person :"+p+" is "+reco); | ||
378 | System.out.println(this.getName()+">>>>Get friend recommandation in "+(t - System.currentTimeMillis())); | ||
379 | |||
380 | t = System.currentTimeMillis(); | ||
381 | System.out.println("Friends of :"+reco); | ||
382 | list = net.getFriendsOf(reco); | ||
383 | //net.displayCollection(list); | ||
384 | System.out.println(this.getName()+">>>>Get friends of in"+(t - System.currentTimeMillis())); | ||
385 | */ | ||
386 | t = System.currentTimeMillis(); | ||
387 | p = net.getRandomPersonWithFriends(); | ||
388 | System.out.println(this.getName()+"Status updates for "+p+" :"); | ||
389 | |||
390 | ArrayList<String> sups = null; | ||
391 | try { | ||
392 | sups = net.retrieveStatusUpdates(p); | ||
393 | } catch (Exception e) { | ||
394 | // TODO Auto-generated catch block | ||
395 | e.printStackTrace(); | ||
396 | } | ||
397 | System.out.println(this.getName()+">>>>Get status updates in "+(t - System.currentTimeMillis())); | ||
398 | //net.displayCollection(sups); | ||
399 | |||
400 | |||
401 | |||
402 | String name = net.getRandomPersonWithFriends(); | ||
403 | System.out.println("List of media albums for user "+name); | ||
404 | sups = net.retreivePersonMediaAlbums(name); | ||
405 | net.displayCollection(sups); | ||
406 | |||
407 | //show comments on all media items in each person albums | ||
408 | System.out.println("Show comments on all media items in each person albums."); | ||
409 | for(String aid:sups){ | ||
410 | ArrayList<String> items = net.getMediaItems(aid); | ||
411 | System.out.println("Media items of album:"+aid+" are:"); | ||
412 | net.displayCollection(items); | ||
413 | for(String itemid:items){ | ||
414 | System.out.println("Comments of media item "+itemid+" are:"); | ||
415 | net.displayCollection( | ||
416 | net.getMediaItemComments(itemid) | ||
417 | ); | ||
418 | |||
419 | } | ||
420 | } | ||
421 | |||
422 | } | ||
423 | |||
424 | } | ||
425 |