2
Copyright 2008-2010 Gephi
3
Authors : Mathieu Bastian <mathieu.bastian@gephi.org>
4
Website : http://www.gephi.org
6
This file is part of Gephi.
8
Gephi is free software: you can redistribute it and/or modify
9
it under the terms of the GNU Affero General Public License as
10
published by the Free Software Foundation, either version 3 of the
11
License, or (at your option) any later version.
13
Gephi is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU Affero General Public License for more details.
18
You should have received a copy of the GNU Affero General Public License
19
along with Gephi. If not, see <http://www.gnu.org/licenses/>.
21
package org.gephi.ranking.plugin;
23
import java.math.BigDecimal;
24
import java.math.BigInteger;
25
import java.util.ArrayList;
26
import java.util.Arrays;
27
import java.util.Comparator;
28
import java.util.List;
29
import org.gephi.data.attributes.api.AttributeColumn;
30
import org.gephi.data.attributes.api.AttributeController;
31
import org.gephi.data.attributes.api.AttributeModel;
32
import org.gephi.data.attributes.api.AttributeUtils;
33
import org.gephi.data.attributes.api.Estimator;
34
import org.gephi.data.attributes.type.DynamicType;
35
import org.gephi.data.attributes.type.TimeInterval;
36
import org.gephi.dynamic.api.DynamicController;
37
import org.gephi.dynamic.api.DynamicModel;
38
import org.gephi.graph.api.Attributable;
39
import org.gephi.graph.api.Graph;
40
import org.gephi.graph.api.GraphController;
41
import org.gephi.graph.api.GraphModel;
42
import org.gephi.ranking.api.Ranking;
43
import org.gephi.ranking.api.RankingModel;
44
import org.gephi.ranking.spi.RankingBuilder;
45
import org.openide.util.Lookup;
46
import org.openide.util.lookup.ServiceProvider;
49
* Ranking builder for attributes. Builds the {@link Ranking} instances that
50
* maps to all numerical attribute columns.
52
* The ranking is built for the workspace associated to the given {@link RankingModel}.
54
* When the column is dynamic, the ranking uses the current time interval defined
55
* in the DynamicAPI. The time interval value is set when the ranking is built and
58
* @author Mathieu Bastian
60
@ServiceProvider(service = RankingBuilder.class)
61
public class AttributeRankingBuilder implements RankingBuilder {
63
private final GraphController graphController;
64
private final AttributeController attributeController;
65
private final AttributeUtils attributeUtils;
67
public AttributeRankingBuilder() {
68
attributeController = Lookup.getDefault().lookup(AttributeController.class);
69
attributeUtils = Lookup.getDefault().lookup(AttributeUtils.class);
70
graphController = Lookup.getDefault().lookup(GraphController.class);
74
public Ranking[] buildRanking(RankingModel model) {
75
AttributeModel attributeModel = attributeController.getModel(model.getWorkspace());
76
List<Ranking> rankings = new ArrayList<Ranking>();
77
GraphModel graphModel = graphController.getModel(model.getWorkspace());
78
Graph graph = graphModel.getGraphVisible();
81
for (AttributeColumn col : attributeModel.getNodeTable().getColumns()) {
82
if (attributeUtils.isNumberColumn(col)) {
83
AttributeRanking ranking = new AttributeRanking(Ranking.NODE_ELEMENT, col, graph);
84
rankings.add(ranking);
89
for (AttributeColumn col : attributeModel.getEdgeTable().getColumns()) {
90
if (attributeUtils.isNumberColumn(col)) {
91
AttributeRanking ranking = new AttributeRanking(Ranking.EDGE_ELEMENT, col, graph);
92
rankings.add(ranking);
97
DynamicController dynamicController = Lookup.getDefault().lookup(DynamicController.class);
98
if (dynamicController != null) {
99
DynamicModel dynamicModel = dynamicController.getModel();
100
if (dynamicModel != null) {
101
TimeInterval visibleInterval = dynamicModel.getVisibleInterval();
104
for (AttributeColumn col : attributeModel.getNodeTable().getColumns()) {
105
if (attributeUtils.isDynamicNumberColumn(col)) {
106
DynamicAttributeRanking ranking = new DynamicAttributeRanking(Ranking.NODE_ELEMENT, col, graph, visibleInterval, dynamicModel.getNumberEstimator());
107
rankings.add(ranking);
112
for (AttributeColumn col : attributeModel.getEdgeTable().getColumns()) {
113
if (attributeUtils.isDynamicNumberColumn(col)) {
114
DynamicAttributeRanking ranking = new DynamicAttributeRanking(Ranking.EDGE_ELEMENT, col, graph, visibleInterval, dynamicModel.getNumberEstimator());
115
rankings.add(ranking);
121
//Sort attributes by alphabetical order
122
Ranking[] rankingArray = rankings.toArray(new Ranking[0]);
123
Arrays.sort(rankingArray, new Comparator<Ranking>() {
126
public int compare(Ranking a, Ranking b) {
127
return (a.toString().compareTo(b.toString()));
134
private static class AttributeRanking extends AbstractRanking<Attributable> {
136
private final AttributeColumn column;
137
private final Graph graph;
139
public AttributeRanking(String elementType, AttributeColumn column, Graph graph) {
141
this.column = column;
146
public Number getValue(Attributable attributable) {
147
return (Number) attributable.getAttributes().getValue(column.getIndex());
151
public float normalize(Number value) {
152
return (value.floatValue() - getMinimumValue().floatValue()) / (float) (getMaximumValue().floatValue() - getMinimumValue().floatValue());
156
public Number unNormalize(float normalizedValue) {
157
double val = (normalizedValue * (getMaximumValue().doubleValue() - getMinimumValue().doubleValue())) + getMinimumValue().doubleValue();
158
switch (column.getType()) {
160
return new BigDecimal(val);
162
return new BigInteger("" + val);
164
return new Double(val);
166
return new Float(val);
168
return new Integer((int) val);
170
return new Long((long) val);
172
return new Short((short) val);
174
return new Double(val);
179
public String getName() {
180
return column.getTitle();
184
public Number getMaximumValue() {
185
if (maximum == null) {
186
AbstractRanking.refreshMinMax(this, graph);
192
public Number getMinimumValue() {
193
if (minimum == null) {
194
AbstractRanking.refreshMinMax(this, graph);
200
private static class DynamicAttributeRanking extends AbstractRanking<Attributable> {
202
private final AttributeColumn column;
203
private final Graph graph;
204
private final TimeInterval timeInterval;
205
private final Estimator estimator;
207
public DynamicAttributeRanking(String elementType, AttributeColumn column, Graph graph, TimeInterval timeInterval, Estimator estimator) {
209
this.column = column;
210
this.timeInterval = timeInterval;
211
this.estimator = estimator;
216
public Number getValue(Attributable attributable) {
217
DynamicType<? extends Number> dynamicType = (DynamicType<? extends Number>) attributable.getAttributes().getValue(column.getIndex());
218
if (dynamicType != null) {
219
return (Number) dynamicType.getValue(timeInterval == null ? Double.NEGATIVE_INFINITY : timeInterval.getLow(),
220
timeInterval == null ? Double.POSITIVE_INFINITY : timeInterval.getHigh(), estimator);
226
public float normalize(Number value) {
227
return (value.floatValue() - minimum.floatValue()) / (float) (getMaximumValue().floatValue() - getMinimumValue().floatValue());
231
public Number unNormalize(float normalizedValue) {
232
double val = (normalizedValue * (getMaximumValue().doubleValue() - getMinimumValue().doubleValue())) + getMinimumValue().doubleValue();
233
switch (column.getType()) {
235
return new BigDecimal(val);
237
return new BigInteger("" + val);
239
return new Double(val);
241
return new Float(val);
243
return new Integer((int) val);
245
return new Long((long) val);
247
return new Short((short) val);
249
return new Double(val);
254
public String getName() {
255
return column.getTitle();
259
public Number getMaximumValue() {
260
if (maximum == null) {
261
AbstractRanking.refreshMinMax(this, graph);
267
public Number getMinimumValue() {
268
if (minimum == null) {
269
AbstractRanking.refreshMinMax(this, graph);