提交 017f801f 编写于 作者: M Mathieu Bastian

Add dynamic attribute support in ImportAPI

上级 f4888f5d
......@@ -55,6 +55,8 @@ public interface ColumnDraft {
public Object getDefaultValue();
public boolean isDynamic();
public void setTitle(String title);
public void setDefaultValue(Object value);
......
......@@ -174,6 +174,10 @@ public interface ContainerLoader {
public ColumnDraft addEdgeColumn(String key, Class typeClass);
public ColumnDraft addNodeColumn(String key, Class typeClass, boolean dynamic);
public ColumnDraft addEdgeColumn(String key, Class typeClass, boolean dynamic);
/**
* Returns the <b>factory</b> for building nodes and edges instances.
*
......
......@@ -65,7 +65,15 @@ public interface ElementDraft {
public void setValue(String key, Object value);
public void setValueString(String key, String value);
public void setValue(String key, Object value, double timestamp);
public void setValue(String key, Object value, String dateTime);
public void parseAndSetValue(String key, String value);
public void parseAndSetValue(String key, String value, double timestamp);
public void parseAndSetValue(String key, String value, String dateTime);
public void setLabel(String label);
......@@ -92,4 +100,8 @@ public interface ElementDraft {
public void setLabelColor(int r, int g, int b);
public void setLabelColor(String color);
public void addTimestamp(double timestamp);
public void addTimestamp(String dateTime);
}
......@@ -53,13 +53,15 @@ public class ColumnDraftImpl implements ColumnDraft {
protected final int index;
protected final String id;
protected final Class typeClass;
protected final boolean dynamic;
protected String title;
protected Object defaultValue;
public ColumnDraftImpl(String id, int index, Class typeClass) {
public ColumnDraftImpl(String id, int index, boolean dynamic, Class typeClass) {
this.id = id;
this.index = index;
this.typeClass = typeClass;
this.dynamic = dynamic;
}
@Override
......@@ -100,4 +102,9 @@ public class ColumnDraftImpl implements ColumnDraft {
public void setDefaultValueString(String value) {
this.defaultValue = AttributeUtils.parse(value, typeClass);
}
@Override
public boolean isDynamic() {
return true;
}
}
......@@ -41,7 +41,6 @@
*/
package org.gephi.io.importer.impl;
import org.gephi.attribute.api.AttributeUtils;
import org.gephi.io.importer.api.ColumnDraft;
import org.gephi.io.importer.api.EdgeDirection;
import org.gephi.io.importer.api.EdgeDraft;
......@@ -257,15 +256,7 @@ public class EdgeDraftImpl extends ElementDraftImpl implements EdgeDraft {
}
@Override
public void setValue(String key, Object value) {
ColumnDraft column = container.addEdgeColumn(key, value.getClass());
setAttributeValue(((ColumnDraftImpl) column).getIndex(), value);
}
@Override
public void setValueString(String key, String value) {
ColumnDraft column = container.addEdgeColumn(key, value.getClass());
Object val = AttributeUtils.parse(value, column.getTypeClass());
setAttributeValue(((ColumnDraftImpl) column).getIndex(), val);
ColumnDraft getColumn(String key, Class type) {
return container.addEdgeColumn(key, type);
}
}
......@@ -42,7 +42,10 @@
*/
package org.gephi.io.importer.impl;
import it.unimi.dsi.fastutil.doubles.Double2ObjectMap;
import java.awt.Color;
import org.gephi.attribute.api.AttributeUtils;
import org.gephi.io.importer.api.ColumnDraft;
import org.gephi.io.importer.api.ElementDraft;
/**
......@@ -63,13 +66,20 @@ public abstract class ElementDraftImpl implements ElementDraft {
protected boolean labelVisible = true;
//Attributes
protected Object[] attributes;
//Timestamps
protected double[] timeStamps;
//Dynamic values
protected Double2ObjectMap[] dynamicAttributes;
public ElementDraftImpl(ImportContainerImpl container, String id) {
this.container = container;
this.id = id;
this.attributes = new Object[0];
this.timeStamps = new double[0];
}
abstract ColumnDraft getColumn(String key, Class type);
@Override
public String getId() {
return id;
......@@ -171,6 +181,54 @@ public abstract class ElementDraftImpl implements ElementDraft {
setLabelColor(Color.getColor(color));
}
@Override
public void setValue(String key, Object value) {
ColumnDraft column = getColumn(key, value.getClass());
setAttributeValue(((ColumnDraftImpl) column).getIndex(), value);
}
@Override
public void setValue(String key, Object value, String dateTime) {
setValue(key, value, AttributeUtils.parseDateTime(dateTime));
}
@Override
public void setValue(String key, Object value, double timestamp) {
ColumnDraft column = getColumn(key, value.getClass());
setAttributeValue(((ColumnDraftImpl) column).getIndex(), value, timestamp);
}
@Override
public void parseAndSetValue(String key, String value) {
ColumnDraft column = getColumn(key, value.getClass());
Object val = AttributeUtils.parse(value, column.getTypeClass());
setAttributeValue(((ColumnDraftImpl) column).getIndex(), val);
}
@Override
public void parseAndSetValue(String key, String value, String dateTime) {
parseAndSetValue(key, value, AttributeUtils.parseDateTime(dateTime));
}
@Override
public void parseAndSetValue(String key, String value, double timestamp) {
ColumnDraft column = getColumn(key, value.getClass());
Object val = AttributeUtils.parse(value, column.getTypeClass());
setAttributeValue(((ColumnDraftImpl) column).getIndex(), val, timestamp);
}
@Override
public void addTimestamp(String dateTime) {
addTimestamp(AttributeUtils.parseDateTime(dateTime));
}
@Override
public void addTimestamp(double timestamp) {
int index = timeStamps.length;
ensureTimestampArraySize(index);
timeStamps[index] = timestamp;
}
//UTILITY
protected void setAttributeValue(int index, Object value) {
if (index >= attributes.length) {
......@@ -181,10 +239,21 @@ public abstract class ElementDraftImpl implements ElementDraft {
attributes[index] = value;
}
protected void setAttributeValue(int index, Object value, double timestamp) {
}
protected Object getAttributeValue(int index) {
if (index < attributes.length) {
return attributes[index];
}
return null;
}
protected void ensureTimestampArraySize(int index) {
if (index >= timeStamps.length) {
double[] newArray = new double[index + 1];
System.arraycopy(timeStamps, 0, newArray, 0, timeStamps.length);
timeStamps = newArray;
}
}
}
......@@ -423,10 +423,15 @@ public class ImportContainerImpl implements Container, ContainerLoader, Containe
@Override
public ColumnDraft addNodeColumn(String key, Class typeClass) {
return addNodeColumn(key, typeClass, false);
}
@Override
public ColumnDraft addNodeColumn(String key, Class typeClass, boolean dynamic) {
ColumnDraft column = nodeColumns.get(key);
if (column == null) {
int index = nodeColumns.size();
column = new ColumnDraftImpl(key, index, typeClass);
column = new ColumnDraftImpl(key, index, dynamic, typeClass);
nodeColumns.put(key, column);
} else {
if (!column.getTypeClass().equals(typeClass)) {
......@@ -438,10 +443,15 @@ public class ImportContainerImpl implements Container, ContainerLoader, Containe
@Override
public ColumnDraft addEdgeColumn(String key, Class typeClass) {
return addEdgeColumn(key, typeClass, false);
}
@Override
public ColumnDraft addEdgeColumn(String key, Class typeClass, boolean dynamic) {
ColumnDraft column = edgeColumns.get(key);
if (column == null) {
int index = edgeColumns.size();
column = new ColumnDraftImpl(key, index, typeClass);
column = new ColumnDraftImpl(key, index, dynamic, typeClass);
edgeColumns.put(key, column);
} else {
if (!column.getTypeClass().equals(typeClass)) {
......
......@@ -41,7 +41,6 @@
*/
package org.gephi.io.importer.impl;
import org.gephi.attribute.api.AttributeUtils;
import org.gephi.io.importer.api.ColumnDraft;
import org.gephi.io.importer.api.NodeDraft;
......@@ -258,15 +257,7 @@ public class NodeDraftImpl extends ElementDraftImpl implements NodeDraft {
}
@Override
public void setValue(String key, Object value) {
ColumnDraft column = container.addNodeColumn(key, value.getClass());
setAttributeValue(((ColumnDraftImpl) column).getIndex(), value);
}
@Override
public void setValueString(String key, String value) {
ColumnDraft column = container.addNodeColumn(key, value.getClass());
Object val = AttributeUtils.parse(value, column.getTypeClass());
setAttributeValue(((ColumnDraftImpl) column).getIndex(), val);
ColumnDraft getColumn(String key, Class type) {
return container.addNodeColumn(key, type);
}
}
......@@ -348,7 +348,7 @@ public class ImporterGraphML implements FileImporter, LongTask {
ColumnDraft column = container.getNodeColumn(fore);
if (column != null) {
try {
node.setValueString(column.getId(), value);
node.parseAndSetValue(column.getId(), value);
} catch (Exception e) {
report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_datavalue", fore, node, column.getTitle()), Issue.Level.SEVERE));
}
......@@ -500,7 +500,7 @@ public class ImporterGraphML implements FileImporter, LongTask {
ColumnDraft column = container.getEdgeColumn(fore);
if (column != null) {
try {
edge.setValueString(column.getId(), value);
edge.parseAndSetValue(column.getId(), value);
} catch (Exception e) {
report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_datavalue", fore, edge, column.getTitle()), Issue.Level.SEVERE));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册