use of org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetParser in project gephi by gephi.
the class AbstractImporterSpreadsheet method execute.
@Override
public boolean execute(ContainerLoader container) {
this.container = container;
this.report = new Report();
this.container.setTimeRepresentation(generalConfig.getTimeRepresentation());
this.container.setTimeZone(generalConfig.getTimeZone());
try (SheetParser parser = createParser()) {
switch(getMode()) {
case NODES_TABLE:
importer = new ImportNodesProcess(generalConfig, parser, container, progressTicket);
break;
case EDGES_TABLE:
importer = new ImportEdgesProcess(generalConfig, parser, container, progressTicket);
break;
case ADJACENCY_LIST:
importer = new ImportAdjacencyListProcess(generalConfig, container, progressTicket, parser);
break;
case MATRIX:
importer = new ImportMatrixProcess(generalConfig, container, progressTicket, parser);
break;
default:
throw new IllegalArgumentException("Unknown mode " + getMode());
}
importer.execute();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} finally {
if (importer != null) {
report.append(importer.getReport());
importer = null;
}
}
return !cancel;
}
use of org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetParser 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.io.importer.plugin.file.spreadsheet.sheet.SheetParser in project gephi by gephi.
the class AbstractWizardVisualPanel1 method refreshPreviewTable.
public void refreshPreviewTable() {
try (SheetParser parser = importer.createParser()) {
Map<String, Integer> headerMap = parser.getHeaderMap();
final String[] headers = headerMap.keySet().toArray(new String[0]);
columnCount = headers.length;
hasSourceNodeColumn = false;
hasTargetNodeColumn = false;
int sourceColumnIndex = 0;
int targetColumnIndex = 0;
for (String header : headers) {
if (header.equalsIgnoreCase("source")) {
hasSourceNodeColumn = true;
sourceColumnIndex = headerMap.get(header);
}
if (header.equalsIgnoreCase("target")) {
hasTargetNodeColumn = true;
targetColumnIndex = headerMap.get(header);
}
}
ArrayList<String[]> records = new ArrayList<>();
hasRowsMissingSourcesOrTargets = false;
final SpreadsheetGeneralConfiguration.Mode mode = getSelectedMode();
int maxRowSize = 0;
String[] currentRecord;
Iterator<SheetRow> iterator = parser.iterator();
int count = 0;
while (iterator.hasNext() && count < MAX_ROWS_PREVIEW) {
count++;
final SheetRow row = iterator.next();
final int rowSize = row.size();
maxRowSize = Math.max(maxRowSize, row.size());
currentRecord = new String[rowSize];
for (int i = 0; i < rowSize; i++) {
currentRecord[i] = row.get(i);
}
// Search for missing source or target columns for edges table
if (mode == SpreadsheetGeneralConfiguration.Mode.EDGES_TABLE) {
if (rowSize <= sourceColumnIndex || rowSize <= targetColumnIndex || currentRecord[sourceColumnIndex] == null || currentRecord[targetColumnIndex] == null) {
hasRowsMissingSourcesOrTargets = true;
}
}
records.add(currentRecord);
}
final String[] columnNames = headers;
final String[][] values = records.toArray(new String[0][]);
final int rowSize = maxRowSize;
final JTable table = getPreviewTable();
table.setModel(new TableModel() {
@Override
public int getRowCount() {
return values.length;
}
@Override
public int getColumnCount() {
return rowSize;
}
@Override
public String getColumnName(int columnIndex) {
if (columnIndex > columnNames.length - 1) {
return null;
}
return columnNames[columnIndex];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (values[rowIndex].length > columnIndex) {
return values[rowIndex][columnIndex];
} else {
return null;
}
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
}
@Override
public void addTableModelListener(TableModelListener l) {
}
@Override
public void removeTableModelListener(TableModelListener l) {
}
});
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
boolean needsHeader = headers.length > 0;
getPreviewTableScrollPane().setColumnHeaderView(needsHeader ? table.getTableHeader() : null);
}
});
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
use of org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetParser in project gephi by gephi.
the class AbstractImporterSpreadsheet method autoDetectImportMode.
protected void autoDetectImportMode() {
try {
SheetParser parser = createParserWithoutHeaders();
Mode mode = null;
Iterator<SheetRow> iterator = parser.iterator();
if (iterator.hasNext()) {
SheetRow firstRow = iterator.next();
if (firstRow.get(0) == null || firstRow.get(0).trim().isEmpty()) {
mode = Mode.MATRIX;
} else {
// Detect very probable edges table:
for (int i = 0; i < firstRow.size(); i++) {
String value = firstRow.get(i);
if ("source".equalsIgnoreCase(value) || "target".equalsIgnoreCase(value)) {
mode = Mode.EDGES_TABLE;
break;
}
}
// Detect probable nodes table:
if (mode == null) {
for (int i = 0; i < firstRow.size(); i++) {
String value = firstRow.get(i);
if ("id".equalsIgnoreCase(value) || "label".equalsIgnoreCase(value) || "timeset".equalsIgnoreCase(value)) {
mode = Mode.NODES_TABLE;
}
}
}
}
}
if (mode == null) {
// Default adjacency list:
mode = Mode.ADJACENCY_LIST;
}
setMode(mode);
} catch (IOException ex) {
// NOOP
}
}
Aggregations