-
Notifications
You must be signed in to change notification settings - Fork 623
/
Neo4jImporterImpl.java
204 lines (168 loc) · 8.13 KB
/
Neo4jImporterImpl.java
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
/*
Copyright 2008-2010 Gephi
Authors : Martin Škurla
Website : http://www.gephi.org
This file is part of Gephi.
Gephi is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Gephi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Gephi. If not, see <http://www.gnu.org/licenses/>.
*/
package org.gephi.neo4j.plugin.impl;
import java.util.Collection;
import java.util.Collections;
import org.gephi.neo4j.plugin.api.FilterDescription;
import org.gephi.neo4j.plugin.api.Neo4jImporter;
import org.gephi.neo4j.plugin.api.RelationshipDescription;
import org.gephi.neo4j.plugin.api.TraversalOrder;
import org.gephi.project.api.ProjectController;
import org.gephi.utils.longtask.spi.LongTask;
import org.gephi.utils.progress.Progress;
import org.gephi.utils.progress.ProgressTicket;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.traversal.Evaluator;
import org.neo4j.graphdb.traversal.Evaluators;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.graphdb.traversal.Traverser;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;
/**
*
* @author Martin Škurla
*/
@ServiceProvider(service = Neo4jImporter.class)
public final class Neo4jImporterImpl implements Neo4jImporter, LongTask {
// when we want to iterate through whole graph
private static final int NO_START_NODE = -1;
private ProgressTicket progressTicket;
private boolean cancelImport;
@Override
public boolean cancel() {
cancelImport = true;
return true;
}
@Override
public void setProgressTicket(ProgressTicket progressTicket) {
cancelImport = false;
this.progressTicket = progressTicket;
}
@Override
public void importDatabase(GraphDatabaseService graphDB) {
importDatabase(graphDB, NO_START_NODE, TraversalOrder.DEPTH_FIRST, Integer.MAX_VALUE);
}
@Override
public void importDatabase(GraphDatabaseService graphDB, Collection<FilterDescription> filterDescriptions, boolean restrictMode, boolean matchCase) {
importDatabase(graphDB, NO_START_NODE, TraversalOrder.DEPTH_FIRST, Integer.MAX_VALUE, Collections.<RelationshipDescription>emptyList(),
filterDescriptions, restrictMode, matchCase);
}
@Override
public void importDatabase(GraphDatabaseService graphDB, long startNodeId, TraversalOrder order, int maxDepth) {
importDatabase(graphDB, startNodeId, order, maxDepth, Collections.<RelationshipDescription>emptyList());
}
@Override
public void importDatabase(GraphDatabaseService graphDB, long startNodeId, TraversalOrder order, int maxDepth,
Collection<RelationshipDescription> relationshipDescriptions) {
// last 2 boolean parameters are not important, because if we pass empty collection of filter descriptions, they
// are not needed
importDatabase(graphDB, startNodeId, order, maxDepth, relationshipDescriptions, Collections.<FilterDescription>emptyList(),
false, false);
}
@Override
public void importDatabase(GraphDatabaseService graphDB, long startNodeId, TraversalOrder order, int maxDepth,
Collection<RelationshipDescription> relationshipDescriptions, Collection<FilterDescription> filterDescriptions,
boolean restrictMode, boolean matchCase) {
String longTaskMessage = NbBundle.getMessage(Neo4jImporterImpl.class, "CTL_Neo4j_LocalImportMessage");
Progress.setDisplayName(progressTicket, longTaskMessage);
Progress.start(progressTicket);
Traverser traverser;
NodeReturnFilter nodeReturnFilter = null;
Transaction transaction = graphDB.beginTx();
try {
if (startNodeId != NO_START_NODE) {
TraversalDescription traversalDescription = graphDB.traversalDescription();
traversalDescription = order.update(traversalDescription);
for (RelationshipDescription relationshipDescription : relationshipDescriptions) {
traversalDescription = traversalDescription.relationships(relationshipDescription.getRelationshipType(),
relationshipDescription.getDirection());
}
Evaluator evaluator;
if (filterDescriptions.isEmpty())
evaluator = Evaluators.toDepth(maxDepth);
else
evaluator = new DepthAndNodeFilterEvaluator(filterDescriptions, restrictMode, matchCase, maxDepth);
traverser = traversalDescription.evaluator(evaluator).traverse(graphDB.getNodeById(startNodeId));
} else if (startNodeId == NO_START_NODE && filterDescriptions.size() > 0) {
nodeReturnFilter = new NodeReturnFilter(filterDescriptions, restrictMode, matchCase);
traverser = null;
} else {
traverser = null;
}
doImport(graphDB, traverser, nodeReturnFilter);
transaction.success();
} finally {
transaction.close();
}
Progress.finish(progressTicket);
}
private void doImport(GraphDatabaseService graphDB, Traverser traverser, NodeReturnFilter nodeReturnFilter) {
importGraph(graphDB, traverser, nodeReturnFilter);
}
private void importGraph(GraphDatabaseService graphDB, Traverser traverser, NodeReturnFilter nodeReturnFilter) {
initProject();
GraphModelImportConverter graphModelImportConverter = GraphModelImportConverter.getInstance(graphDB);
graphModelImportConverter.createNeo4jRelationshipTypeGephiColumn();
if (traverser == null) {
importNodes(graphModelImportConverter, graphDB.getAllNodes(), nodeReturnFilter);
for (org.neo4j.graphdb.Node node : graphDB.getAllNodes()) {
importRelationships(graphModelImportConverter, node.getRelationships(Direction.INCOMING));
}
} else {
importNodes(graphModelImportConverter, traverser.nodes(), nodeReturnFilter);
importRelationships(graphModelImportConverter, traverser.relationships());
}
}
private void importNodes(GraphModelImportConverter graphModelImportConverter, Iterable<org.neo4j.graphdb.Node> nodes, NodeReturnFilter nodeReturnFilter) {
for (org.neo4j.graphdb.Node node : nodes) {
if (cancelImport) {
return;
}
if (nodeReturnFilter != null) {
if (nodeReturnFilter.accept(node)) {
processNode(graphModelImportConverter, node);
}
} else {
processNode(graphModelImportConverter, node);
}
}
}
private void processNode(GraphModelImportConverter graphModelImportConverter, org.neo4j.graphdb.Node node) {
graphModelImportConverter.createGephiNodeFromNeoNode(node);
}
private void importRelationships(GraphModelImportConverter graphModelImportConverter, Iterable<Relationship> relationships) {
for (Relationship relationship : relationships) {
if (cancelImport) {
return;
}
processRelationship(graphModelImportConverter, relationship);
}
}
private void processRelationship(GraphModelImportConverter graphModelImportConverter, Relationship neoRelationship) {
graphModelImportConverter.createGephiEdge(neoRelationship);
}
private void initProject() {
ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
if (pc.getCurrentProject() == null) {
pc.newProject();
}
}
}