use of org.gephi.graph.api.types.TimeSet in project gephi by gephi.
the class TimeSetStringConverter method getString.
@Override
public String getString(Object value) {
String str = null;
if (value != null) {
TimeSet timeSet = (TimeSet) value;
str = timeSet.toString(graphModelProvider.getGraphModel().getTimeFormat(), graphModelProvider.getGraphModel().getTimeZone());
}
return str;
}
use of org.gephi.graph.api.types.TimeSet in project gephi by gephi.
the class AbstractTimeSetGraphicsComponentProvider method getTextFromValue.
private String getTextFromValue(Object value) {
TimeSet timeSet = (TimeSet) value;
String text = null;
if (timeSet != null) {
text = timeSet.toString(graphModelProvider.getGraphModel().getTimeFormat(), graphModelProvider.getGraphModel().getTimeZone());
}
return text;
}
use of org.gephi.graph.api.types.TimeSet in project gephi by gephi.
the class ImportContainerImpl method mergeParallelEdges.
protected void mergeParallelEdges(EdgeDraftImpl[] sources, EdgeDraftImpl dest) {
Object val = dest.getValue("weight");
if (val == null || !(val instanceof TimeMap)) {
EdgeWeightMergeStrategy mergeStrategy = parameters.getEdgesMergeStrategy();
int count = 1 + sources.length;
double sum = dest.getWeight();
double min = dest.getWeight();
double max = dest.getWeight();
for (EdgeDraftImpl edge : sources) {
sum += edge.getWeight();
min = Math.min(min, edge.getWeight());
max = Math.max(max, edge.getWeight());
}
double result = dest.getWeight();
switch(mergeStrategy) {
case AVG:
result = sum / count;
break;
case MAX:
result = max;
break;
case MIN:
result = min;
break;
case SUM:
result = sum;
break;
default:
break;
}
dest.setWeight(result);
}
//Add dest to sources for convenience
sources = Arrays.copyOf(sources, sources.length + 1);
sources[sources.length - 1] = dest;
//Merge dynamic attributes
for (ColumnDraft columnDraft : getEdgeColumns()) {
if (columnDraft.isDynamic()) {
TimeMap timeMap = null;
for (EdgeDraftImpl edge : sources) {
TimeMap t = (TimeMap) edge.getValue(columnDraft.getId());
if (t != null && timeMap == null) {
timeMap = t;
} else if (t != null && timeMap != null) {
for (Object key : t.toKeysArray()) {
timeMap.put(key, t.get(key, null));
}
}
}
if (timeMap != null) {
dest.setValue(columnDraft.getId(), timeMap);
}
}
}
//Merge timeset
TimeSet timeSet = null;
for (EdgeDraftImpl edge : sources) {
TimeSet t = edge.getTimeSet();
if (t != null && timeSet == null) {
timeSet = t;
} else if (t != null && timeSet != null) {
for (Object key : t.toArray()) {
timeSet.add(key);
}
}
}
if (timeSet != null) {
dest.timeSet = timeSet;
}
}
use of org.gephi.graph.api.types.TimeSet in project gephi by gephi.
the class AbstractImporterSpreadsheet method autoDetectColumnTypes.
protected void autoDetectColumnTypes() {
try (SheetParser parser = createParser()) {
List<SheetRow> rows = getFirstRows(parser, MAX_ROWS_TO_ANALYZE_COLUMN_TYPES);
int rowCount = rows.size();
if (rowCount == 0) {
return;
}
Map<String, Integer> headerMap = parser.getHeaderMap();
if (headerMap.isEmpty()) {
return;
}
Map<String, LinkedHashSet<Class>> classMatchByHeader = new HashMap<>();
List<Class> classesToTry = Arrays.asList(new Class[] { // Classes to check, in order of preference
Boolean.class, Integer.class, Long.class, BigInteger.class, Double.class, BigDecimal.class, IntervalIntegerMap.class, IntervalLongMap.class, IntervalDoubleMap.class, IntervalStringMap.class, IntervalSet.class, TimestampIntegerMap.class, TimestampLongMap.class, TimestampDoubleMap.class, TimestampStringMap.class, TimestampSet.class });
// Initialize:
for (String column : headerMap.keySet()) {
classMatchByHeader.put(column, new LinkedHashSet<Class>());
// First assume all values match
classMatchByHeader.get(column).addAll(classesToTry);
}
// Try to parse all types:
for (SheetRow row : rows) {
for (Map.Entry<String, Integer> entry : headerMap.entrySet()) {
String column = entry.getKey();
int index = entry.getValue();
String value = row.get(index);
if (value != null) {
value = value.trim();
}
LinkedHashSet<Class> columnMatches = classMatchByHeader.get(column);
for (Class clazz : classesToTry) {
if (columnMatches.contains(clazz)) {
if (value != null && !value.isEmpty()) {
if (clazz.equals(Boolean.class)) {
// Special case for booleans to not accept 0/1, only true or false
if (!value.equalsIgnoreCase("true") && !value.equalsIgnoreCase("false")) {
columnMatches.remove(clazz);
}
} else {
try {
Object parsed;
if (clazz.equals(Integer.class)) {
parsed = Integer.parseInt(value);
} else if (clazz.equals(Long.class)) {
parsed = Long.parseLong(value);
} else if (clazz.equals(BigInteger.class)) {
parsed = new BigInteger(value);
} else if (clazz.equals(Double.class)) {
parsed = Double.parseDouble(value);
} else if (clazz.equals(BigDecimal.class)) {
parsed = new BigDecimal(value);
} else {
parsed = AttributeUtils.parse(value, clazz);
}
if (parsed instanceof TimeMap && ((TimeMap) parsed).isEmpty()) {
// Actually invalid
parsed = null;
}
if (parsed instanceof TimeSet && ((TimeSet) parsed).isEmpty()) {
// Actually invalid
parsed = null;
}
if (parsed == null) {
// Non empty value produced null, invalid parsing
columnMatches.remove(clazz);
}
} catch (Exception parseError) {
// Invalid value
columnMatches.remove(clazz);
}
}
}
}
}
}
}
// Obtain best match for each column:
TimeRepresentation foundTimeRepresentation = TimeRepresentation.INTERVAL;
for (String column : headerMap.keySet()) {
LinkedHashSet<Class> columnMatches = classMatchByHeader.get(column);
// Default
Class detectedClass = String.class;
// Use the detected type matching if any:
if (!columnMatches.isEmpty() && columnMatches.size() != classesToTry.size()) {
// First match
detectedClass = columnMatches.iterator().next();
}
// Change some typical column types to expected types when possible:
if (column.equalsIgnoreCase("id") || column.equalsIgnoreCase("label")) {
detectedClass = String.class;
}
if (detectedClass.equals(String.class)) {
// No other thing than String found, try to guess very probable dynamic types:
if (column.toLowerCase().contains("interval")) {
detectedClass = IntervalSet.class;
}
if (column.toLowerCase().contains("timestamp")) {
detectedClass = TimestampSet.class;
}
if (column.equalsIgnoreCase("timeset")) {
if (foundTimeRepresentation == TimeRepresentation.INTERVAL) {
detectedClass = IntervalSet.class;
} else {
detectedClass = TimestampSet.class;
}
}
}
if (getMode() == Mode.EDGES_TABLE) {
if (column.equalsIgnoreCase("source") || column.equalsIgnoreCase("target") || column.equalsIgnoreCase("type") || column.equalsIgnoreCase("kind")) {
detectedClass = String.class;
}
// Favor double types for weight column:
if (column.equalsIgnoreCase("weight")) {
if (columnMatches.contains(Double.class)) {
detectedClass = Double.class;
} else if (columnMatches.contains(IntervalDoubleMap.class)) {
detectedClass = IntervalDoubleMap.class;
} else if (columnMatches.contains(TimestampDoubleMap.class)) {
detectedClass = TimestampDoubleMap.class;
}
}
}
setColumnClass(column, detectedClass);
if (TimestampSet.class.isAssignableFrom(detectedClass) || TimestampMap.class.isAssignableFrom(detectedClass)) {
foundTimeRepresentation = TimeRepresentation.TIMESTAMP;
}
}
setTimeRepresentation(foundTimeRepresentation);
} catch (IOException ex) {
// NOOP
}
}
use of org.gephi.graph.api.types.TimeSet in project gephi by gephi.
the class AbstractProcessor method flushToElementAttributes.
protected void flushToElementAttributes(ContainerUnloader container, ElementDraft elementDraft, Element element) {
for (ColumnDraft columnDraft : elementDraft.getColumns()) {
if (elementDraft instanceof EdgeDraft && columnDraft.getId().equalsIgnoreCase("weight")) {
// Special weight column
continue;
}
Object val = elementDraft.getValue(columnDraft.getId());
Column column = element.getTable().getColumn(columnDraft.getId());
if (column == null) {
// The column might be not present, for cases when it cannot be added due to time representation mismatch, etc
continue;
}
if (column.isReadOnly()) {
continue;
}
Class columnDraftTypeClass = columnDraft.getResolvedTypeClass(container);
if (!column.getTypeClass().equals(columnDraftTypeClass)) {
if (!columnsTypeMismatchAlreadyWarned.contains(column)) {
columnsTypeMismatchAlreadyWarned.add(column);
String error = NbBundle.getMessage(AbstractProcessor.class, "AbstractProcessor.error.columnTypeMismatch", column.getId(), column.getTypeClass().getSimpleName(), columnDraftTypeClass.getSimpleName());
report.logIssue(new Issue(error, Issue.Level.SEVERE));
}
// Incompatible types!
continue;
}
if (val != null) {
Object processedNewValue = val;
Object existingValue = element.getAttribute(columnDraft.getId());
if (columnDraft.isDynamic() && existingValue != null) {
if (TimeMap.class.isAssignableFrom(columnDraft.getTypeClass())) {
TimeMap existingMap = (TimeMap) existingValue;
if (!existingMap.isEmpty()) {
TimeMap valMap = (TimeMap) val;
TimeMap newMap = (TimeMap) existingMap;
Object[] keys = valMap.toKeysArray();
Object[] vals = valMap.toValuesArray();
for (int i = 0; i < keys.length; i++) {
try {
newMap.put(keys[i], vals[i]);
} catch (IllegalArgumentException e) {
// Overlapping intervals, ignore
}
}
processedNewValue = newMap;
}
} else if (TimeSet.class.isAssignableFrom(columnDraft.getTypeClass())) {
TimeSet existingTimeSet = (TimeSet) existingValue;
processedNewValue = mergeTimeSets(existingTimeSet, (TimeSet) val);
}
}
element.setAttribute(columnDraft.getId(), processedNewValue);
}
}
}
Aggregations