-
Notifications
You must be signed in to change notification settings - Fork 1
/
GenGraph.java
233 lines (198 loc) · 11.5 KB
/
GenGraph.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.jazzgraph;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.gephi.data.attributes.api.AttributeColumn;
import org.gephi.data.attributes.api.AttributeController;
import org.gephi.data.attributes.api.AttributeModel;
import org.gephi.graph.api.DirectedGraph;
import org.gephi.graph.api.GraphController;
import org.gephi.graph.api.GraphModel;
import org.gephi.io.database.drivers.MySQLDriver;
import org.gephi.io.exporter.api.ExportController;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.EdgeDefault;
import org.gephi.io.importer.api.ImportController;
import org.gephi.io.importer.plugin.database.EdgeListDatabaseImpl;
import org.gephi.io.importer.plugin.database.ImporterEdgeList;
import org.gephi.io.processor.plugin.DefaultProcessor;
import org.gephi.layout.plugin.AutoLayout;
import org.gephi.layout.plugin.force.StepDisplacement;
import org.gephi.layout.plugin.force.yifanHu.YifanHuLayout;
import org.gephi.layout.plugin.forceAtlas.ForceAtlasLayout;
import org.gephi.partition.api.Partition;
import org.gephi.partition.api.PartitionController;
import org.gephi.partition.plugin.NodeColorTransformer;
import org.gephi.preview.api.PreviewController;
import org.gephi.preview.api.PreviewModel;
import org.gephi.preview.api.PreviewProperty;
import org.gephi.preview.types.EdgeColor;
import org.gephi.project.api.ProjectController;
import org.gephi.project.api.Workspace;
import org.gephi.ranking.api.Ranking;
import org.gephi.ranking.api.RankingController;
import org.gephi.ranking.api.Transformer;
import org.gephi.ranking.plugin.transformer.AbstractColorTransformer;
import org.gephi.ranking.plugin.transformer.AbstractSizeTransformer;
import org.gephi.statistics.plugin.GraphDistance;
import org.gephi.statistics.plugin.Modularity;
import org.openide.util.Lookup;
/**
*
* @author tpinville
*/
public class GenGraph {
public void script(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
String artist;
int delay = 5;
String export = "gexf";
if (args.length > 0)
{
artist = args[0] ;
if (args.length > 1)
delay = Integer.parseInt(args[1]);
if (args.length > 2)
export = args[2];
}
else
//artist = "'James Carter'";
artist = "'Tony Williams', 'John Coltrane', 'Miles Davis', 'Elvin Jones', 'Charlie Parker'";
//String query = "%" + artist + "%";
String query = artist ;
System.out.println(artist);
//Init a project - and therefore a workspace
ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
pc.newProject();
Workspace workspace = pc.getCurrentWorkspace();
//Get controllers and models
ImportController importController = Lookup.getDefault().lookup(ImportController.class);
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
AttributeModel attributeModel = Lookup.getDefault().lookup(AttributeController.class).getModel();
//Import database
EdgeListDatabaseImpl db = new EdgeListDatabaseImpl();
db.setDBName("jazzgraph");
db.setHost("localhost");
db.setUsername("root");
db.setPasswd("");
db.setSQLDriver(new MySQLDriver());
//db.setSQLDriver(new PostgreSQLDriver());
//db.setSQLDriver(new SQLServerDriver());
db.setPort(3306);
//db.setNodeQuery("SELECT nodes.id AS id, nodes.label AS label, nodes.url FROM nodes");
//String artist = "Lee morgan%' OR name like 'Miles Davis%";
/*String requete = "select id as id, label as label FROM Nodes where id in "
+ "(SELECT sourceid FROM Edges LEFT JOIN Artists ON targetid = Artists.id "
+ "where name like '"+ query + "'"
+ ")";*/
String requete = "SELECT p.artistid as id,"
+"ar.name as label "
+"FROM Credits c,LinksArtistCategory l,ArtistCategories ac, Albums a, AlbumPrimaryArtists p, Artists ar "
+"where a.id = c.albumid "
//+"and l.artistcategoryid = 2 "
+"and c.artistid = l.artistid "
+"and ar.id = p.artistid "
+"and p.albumid = a.id "
+"and c.artistid in (select id from Artists where name in ("+ query +")) "
// +"and p.artistid in (select artistid from LinksArtistCategory where artistcategoryid = 2) "
+"group by c.artistid,a.id, p.artistid";
db.setNodeQuery(requete);
System.out.println(requete);
requete = "SELECT p.artistid as target, c.artistid as source, a.title as label "
+"FROM Credits c,LinksArtistCategory l,ArtistCategories ac, Albums a, AlbumPrimaryArtists p, Artists ar "
+"where a.id = c.albumid "
//+"and l.artistcategoryid = 2 "
+"and a.id = c.albumid "
+"and c.artistid = l.artistid "
+"and ar.id = p.artistid "
+"and p.albumid = a.id "
+"and c.artistid in (select id from Artists where name in ("+ query +")) "
//+"and p.artistid in (select artistid from LinksArtistCategory where artistcategoryid = 2) "
+"";
db.setEdgeQuery(requete);
System.out.println(requete);
//+ "where sourceid in (select id from Artists where name like '"+ query + "')");
ImporterEdgeList edgeListImporter = new ImporterEdgeList();
Container container = importController.importDatabase(db, edgeListImporter);
container.setAllowAutoNode(false); //Don't create missing nodes
container.getLoader().setEdgeDefault(EdgeDefault.DIRECTED);
//Append imported data to GraphAPI
importController.process(container, new DefaultProcessor(), workspace);
//See if graph is well imported
DirectedGraph graph = graphModel.getDirectedGraph();
System.out.println("Nodes: " + graph.getNodeCount());
System.out.println("Edges: " + graph.getEdgeCount());
//Get Centrality
GraphDistance distance = new GraphDistance();
distance.setDirected(true);
distance.execute(graphModel, attributeModel);
//Rank color by Degree
RankingController rankingController = Lookup.getDefault().lookup(RankingController.class);
Ranking degreeRanking = rankingController.getModel().getRanking(Ranking.NODE_ELEMENT, Ranking.DEGREE_RANKING);
AbstractColorTransformer colorTransformer = (AbstractColorTransformer) rankingController.getModel().getTransformer(Ranking.NODE_ELEMENT, Transformer.RENDERABLE_COLOR);
colorTransformer.setColors(new Color[]{new Color(0xFEF0D9), new Color(0xB30000)});
rankingController.transform(degreeRanking,colorTransformer);
//Rank size by centrality
AttributeColumn centralityColumn = attributeModel.getNodeTable().getColumn(GraphDistance.BETWEENNESS);
Ranking centralityRanking = rankingController.getModel().getRanking(Ranking.NODE_ELEMENT, centralityColumn.getId());
AbstractSizeTransformer sizeTransformer = (AbstractSizeTransformer) rankingController.getModel().getTransformer(Ranking.NODE_ELEMENT, Transformer.RENDERABLE_SIZE);
sizeTransformer.setMinSize(10);
sizeTransformer.setMaxSize(100);
rankingController.transform(centralityRanking,sizeTransformer);
//Rank label size - set a multiplier size
Ranking centralityRanking2 = rankingController.getModel().getRanking(Ranking.NODE_ELEMENT, centralityColumn.getId());
AbstractSizeTransformer labelSizeTransformer = (AbstractSizeTransformer) rankingController.getModel().getTransformer(Ranking.NODE_ELEMENT, Transformer.LABEL_SIZE);
labelSizeTransformer.setMinSize(1);
labelSizeTransformer.setMaxSize(3);
rankingController.transform(centralityRanking2,labelSizeTransformer);
//Set 'show labels' option in Preview - and disable node size influence on text size
PreviewModel previewModel = Lookup.getDefault().lookup(PreviewController.class).getModel();
previewModel.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.TRUE);
previewModel.getProperties().putValue(PreviewProperty.NODE_LABEL_PROPORTIONAL_SIZE, Boolean.TRUE);
previewModel.getProperties().putValue(PreviewProperty.SHOW_EDGE_LABELS, Boolean.TRUE);
//Run modularity algorithm - community detection
Modularity modularity = new Modularity();
modularity.execute(graphModel, attributeModel);
//Partition with 'modularity_class', just created by Modularity algorithm
PartitionController partitionController = Lookup.getDefault().lookup(PartitionController.class);
AttributeColumn modColumn = attributeModel.getNodeTable().getColumn(Modularity.MODULARITY_CLASS);
Partition p2 = partitionController.buildPartition(modColumn, graph);
System.out.println(p2.getPartsCount() + " partitions found");
NodeColorTransformer nodeColorTransformer2 = new NodeColorTransformer();
nodeColorTransformer2.randomizeColors(p2);
partitionController.transform(p2, nodeColorTransformer2);
//Preview
PreviewModel model = Lookup.getDefault().lookup(PreviewController.class).getModel();
model.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.TRUE);
model.getProperties().putValue(PreviewProperty.EDGE_COLOR, new EdgeColor(Color.GRAY));
model.getProperties().putValue(PreviewProperty.EDGE_THICKNESS, new Float(0.1f));
model.getProperties().putValue(PreviewProperty.NODE_LABEL_FONT, model.getProperties().getFontValue(PreviewProperty.NODE_LABEL_FONT).deriveFont(8));
//Layout for 1 minute
AutoLayout autoLayout = new AutoLayout(delay , TimeUnit.SECONDS);
autoLayout.setGraphModel(graphModel);
YifanHuLayout firstLayout = new YifanHuLayout(null, new StepDisplacement(1f));
ForceAtlasLayout secondLayout = new ForceAtlasLayout(null);
AutoLayout.DynamicProperty adjustBySizeProperty = AutoLayout.createDynamicProperty("forceAtlas.adjustSizes.name", Boolean.TRUE, 0.1f);//True after 10% of layout time
AutoLayout.DynamicProperty repulsionProperty = AutoLayout.createDynamicProperty("forceAtlas.repulsionStrength.name", new Double(1500.), 0f);//500 for the complete period
autoLayout.addLayout(firstLayout, 0.5f);
autoLayout.addLayout(secondLayout, 0.5f, new AutoLayout.DynamicProperty[]{adjustBySizeProperty, repulsionProperty});
autoLayout.execute();
//Export
ExportController ec = Lookup.getDefault().lookup(ExportController.class);
try {
//ec.exportFile(new File("/home/tpinville/git/jazzgraph/web/gexf/"+artist.replace(" ", "_") +"." + export));
ec.exportFile(new File("/home/tpinville/git/jazzgraph/web/gexf/John_Coltrane." + export));
ec.exportFile(new File("/home/tpinville/git/jazzgraph/web/gexf/Miles_Davis.json"));
} catch (IOException ex) {
ex.printStackTrace();
return;
}
}
}