Search in sources :

Example 1 with SheetParser

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;
}
Also used : ImportMatrixProcess(org.gephi.io.importer.plugin.file.spreadsheet.process.ImportMatrixProcess) Report(org.gephi.io.importer.api.Report) ImportAdjacencyListProcess(org.gephi.io.importer.plugin.file.spreadsheet.process.ImportAdjacencyListProcess) ImportNodesProcess(org.gephi.io.importer.plugin.file.spreadsheet.process.ImportNodesProcess) ImportEdgesProcess(org.gephi.io.importer.plugin.file.spreadsheet.process.ImportEdgesProcess) SheetParser(org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetParser) IOException(java.io.IOException)

Example 2 with SheetParser

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
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) TimeMap(org.gephi.graph.api.types.TimeMap) SheetParser(org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetParser) TimeSet(org.gephi.graph.api.types.TimeSet) TimestampMap(org.gephi.graph.api.types.TimestampMap) TimestampSet(org.gephi.graph.api.types.TimestampSet) TimeRepresentation(org.gephi.graph.api.TimeRepresentation) IntervalDoubleMap(org.gephi.graph.api.types.IntervalDoubleMap) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) IOException(java.io.IOException) BigInteger(java.math.BigInteger) SheetRow(org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow) BigInteger(java.math.BigInteger) TimestampLongMap(org.gephi.graph.api.types.TimestampLongMap) HashMap(java.util.HashMap) IntervalLongMap(org.gephi.graph.api.types.IntervalLongMap) TimestampMap(org.gephi.graph.api.types.TimestampMap) Map(java.util.Map) TimestampIntegerMap(org.gephi.graph.api.types.TimestampIntegerMap) IntervalIntegerMap(org.gephi.graph.api.types.IntervalIntegerMap) IntervalStringMap(org.gephi.graph.api.types.IntervalStringMap) TimestampDoubleMap(org.gephi.graph.api.types.TimestampDoubleMap) TimestampStringMap(org.gephi.graph.api.types.TimestampStringMap) IntervalDoubleMap(org.gephi.graph.api.types.IntervalDoubleMap) TimeMap(org.gephi.graph.api.types.TimeMap)

Example 3 with SheetParser

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);
    }
}
Also used : ArrayList(java.util.ArrayList) SheetParser(org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetParser) IOException(java.io.IOException) SheetRow(org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow) SpreadsheetGeneralConfiguration(org.gephi.io.importer.plugin.file.spreadsheet.process.SpreadsheetGeneralConfiguration) JTable(javax.swing.JTable) TableModelListener(javax.swing.event.TableModelListener) TableModel(javax.swing.table.TableModel)

Example 4 with SheetParser

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
    }
}
Also used : SheetRow(org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow) Mode(org.gephi.io.importer.plugin.file.spreadsheet.process.SpreadsheetGeneralConfiguration.Mode) SheetParser(org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetParser) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)4 SheetParser (org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetParser)4 SheetRow (org.gephi.io.importer.plugin.file.spreadsheet.sheet.SheetRow)3 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 JTable (javax.swing.JTable)1 TableModelListener (javax.swing.event.TableModelListener)1 TableModel (javax.swing.table.TableModel)1 TimeRepresentation (org.gephi.graph.api.TimeRepresentation)1 IntervalDoubleMap (org.gephi.graph.api.types.IntervalDoubleMap)1 IntervalIntegerMap (org.gephi.graph.api.types.IntervalIntegerMap)1 IntervalLongMap (org.gephi.graph.api.types.IntervalLongMap)1 IntervalStringMap (org.gephi.graph.api.types.IntervalStringMap)1 TimeMap (org.gephi.graph.api.types.TimeMap)1 TimeSet (org.gephi.graph.api.types.TimeSet)1 TimestampDoubleMap (org.gephi.graph.api.types.TimestampDoubleMap)1