Search in sources :

Example 1 with ColumnDraft

use of org.gephi.io.importer.api.ColumnDraft 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;
    }
}
Also used : ColumnDraft(org.gephi.io.importer.api.ColumnDraft) TimeSet(org.gephi.graph.api.types.TimeSet) EdgeWeightMergeStrategy(org.gephi.io.importer.api.EdgeWeightMergeStrategy) TimeMap(org.gephi.graph.api.types.TimeMap)

Example 2 with ColumnDraft

use of org.gephi.io.importer.api.ColumnDraft in project gephi by gephi.

the class AbstractProcessor method flushColumns.

protected void flushColumns(ContainerUnloader container) {
    TimeRepresentation timeRepresentation = container.getTimeRepresentation();
    Table nodeTable = graphModel.getNodeTable();
    for (ColumnDraft col : container.getNodeColumns()) {
        if (!nodeTable.hasColumn(col.getId())) {
            Class typeClass = col.getTypeClass();
            if (col.isDynamic()) {
                if (timeRepresentation.equals(TimeRepresentation.TIMESTAMP)) {
                    typeClass = AttributeUtils.getTimestampMapType(typeClass);
                } else {
                    typeClass = AttributeUtils.getIntervalMapType(typeClass);
                }
            }
            nodeTable.addColumn(col.getId(), col.getTitle(), typeClass, Origin.DATA, col.getDefaultValue(), !col.isDynamic());
        }
    }
    Table edgeTable = graphModel.getEdgeTable();
    for (ColumnDraft col : container.getEdgeColumns()) {
        if (!edgeTable.hasColumn(col.getId())) {
            Class typeClass = col.getTypeClass();
            if (col.isDynamic()) {
                if (timeRepresentation.equals(TimeRepresentation.TIMESTAMP)) {
                    typeClass = AttributeUtils.getTimestampMapType(typeClass);
                } else {
                    typeClass = AttributeUtils.getIntervalMapType(typeClass);
                }
            }
            edgeTable.addColumn(col.getId(), col.getTitle(), typeClass, Origin.DATA, col.getDefaultValue(), !col.isDynamic());
        }
    }
}
Also used : ColumnDraft(org.gephi.io.importer.api.ColumnDraft) Table(org.gephi.graph.api.Table) TimeRepresentation(org.gephi.graph.api.TimeRepresentation)

Example 3 with ColumnDraft

use of org.gephi.io.importer.api.ColumnDraft in project gephi by gephi.

the class ImporterGraphML method readNodeAttValue.

private void readNodeAttValue(XMLStreamReader reader, NodeDraft node) throws Exception {
    String fore = "";
    String value = "";
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        String attName = reader.getAttributeName(i).getLocalPart();
        if (ATTVALUE_FOR.equalsIgnoreCase(attName)) {
            fore = reader.getAttributeValue(i).trim();
        }
    }
    if (fore.isEmpty()) {
        report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_datakey", node), Issue.Level.SEVERE));
        return;
    }
    boolean end = false;
    while (reader.hasNext() && !end) {
        int xmltype = reader.next();
        switch(xmltype) {
            case XMLStreamReader.CHARACTERS:
                if (!xmlReader.isWhiteSpace()) {
                    value += xmlReader.getText();
                }
                break;
            case XMLStreamReader.END_ELEMENT:
                if (ATTVALUE.equalsIgnoreCase(xmlReader.getLocalName())) {
                    end = true;
                }
                break;
        }
    }
    if (!value.isEmpty()) {
        // Property
        NodeProperties prop = nodePropertiesAttributes.get(fore);
        if (prop != null) {
            try {
                switch(prop) {
                    case X:
                        node.setX(parseFloat(value));
                        break;
                    case Y:
                        node.setY(parseFloat(value));
                        break;
                    case Z:
                        node.setZ(parseFloat(value));
                        break;
                    case SIZE:
                        node.setSize(parseFloat(value));
                        break;
                    case LABEL:
                        node.setLabel(value);
                        break;
                    case COLOR:
                        node.setColor(value);
                        break;
                    case R:
                        if (node.getColor() == null) {
                            node.setColor(Integer.parseInt(value), 0, 0);
                        } else {
                            node.setColor(Integer.parseInt(value), node.getColor().getGreen(), node.getColor().getBlue());
                        }
                        break;
                    case G:
                        if (node.getColor() == null) {
                            node.setColor(0, Integer.parseInt(value), 0);
                        } else {
                            node.setColor(node.getColor().getRed(), Integer.parseInt(value), node.getColor().getBlue());
                        }
                        break;
                    case B:
                        if (node.getColor() == null) {
                            node.setColor(0, 0, Integer.parseInt(value));
                        } else {
                            node.setColor(node.getColor().getRed(), node.getColor().getGreen(), Integer.parseInt(value));
                        }
                        break;
                }
            } catch (Exception e) {
                report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_datavalue", fore, node, prop.toString()), Issue.Level.SEVERE));
            }
        }
        // Data attribute value
        ColumnDraft column = container.getNodeColumn(fore);
        if (column != null) {
            try {
                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));
            }
        }
    }
}
Also used : ColumnDraft(org.gephi.io.importer.api.ColumnDraft) Issue(org.gephi.io.importer.api.Issue) NodeProperties(org.gephi.io.importer.api.PropertiesAssociations.NodeProperties) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 4 with ColumnDraft

use of org.gephi.io.importer.api.ColumnDraft in project gephi by gephi.

the class ImporterGraphML method readAttribute.

private void readAttribute(XMLStreamReader reader) throws Exception {
    String id = "";
    String type = "";
    String title = "";
    String defaultStr = "";
    String forStr = "all";
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        String attName = reader.getAttributeName(i).getLocalPart();
        if (ATTRIBUTE_ID.equalsIgnoreCase(attName)) {
            id = reader.getAttributeValue(i).trim();
        } else if (ATTRIBUTE_TYPE.equalsIgnoreCase(attName)) {
            type = reader.getAttributeValue(i).trim();
        } else if (ATTRIBUTE_TITLE.equalsIgnoreCase(attName)) {
            title = reader.getAttributeValue(i).trim();
        } else if (ATTRIBUTE_FOR.equalsIgnoreCase(attName)) {
            forStr = reader.getAttributeValue(i).trim();
        }
    }
    if (title.isEmpty()) {
        title = id;
    }
    boolean property = false;
    if (!id.isEmpty()) {
        // Properties
        if (forStr.equalsIgnoreCase("node")) {
            NodeProperties prop = properties.getNodeProperty(id) == null ? properties.getNodeProperty(title) : properties.getNodeProperty(id);
            if (prop != null) {
                nodePropertiesAttributes.put(id, prop);
                report.log(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_log_nodeproperty", title));
                property = true;
            }
        } else if (forStr.equalsIgnoreCase("edge")) {
            EdgeProperties prop = properties.getEdgeProperty(id) == null ? properties.getEdgeProperty(title) : properties.getEdgeProperty(id);
            if (prop != null) {
                edgePropertiesAttributes.put(id, prop);
                report.log(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_log_edgeproperty", title));
                property = true;
            }
        }
        if (property) {
            return;
        }
    } else {
        report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributeempty", title), Issue.Level.SEVERE));
        return;
    }
    if (!property && type.isEmpty()) {
        report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributetype1", title), Issue.Level.SEVERE));
        type = "string";
    }
    if (!property) {
        // Class type
        if (forStr.isEmpty() || !(forStr.equalsIgnoreCase("node") || forStr.equalsIgnoreCase("edge") || forStr.equalsIgnoreCase("all"))) {
            report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributeclass", title), Issue.Level.SEVERE));
            return;
        }
        // Default?
        boolean end = false;
        boolean defaultFlag = false;
        while (reader.hasNext() && !end) {
            int xmltype = reader.next();
            switch(xmltype) {
                case XMLStreamReader.START_ELEMENT:
                    if (ATTRIBUTE_DEFAULT.equalsIgnoreCase(xmlReader.getLocalName())) {
                        defaultFlag = true;
                    }
                    break;
                case XMLStreamReader.CHARACTERS:
                    if (defaultFlag && !xmlReader.isWhiteSpace()) {
                        defaultStr = xmlReader.getText();
                    }
                    break;
                case XMLStreamReader.END_ELEMENT:
                    if (ATTRIBUTE.equalsIgnoreCase(xmlReader.getLocalName())) {
                        end = true;
                    }
                    break;
            }
        }
        // Type
        Class attributeType = String.class;
        if (type.equalsIgnoreCase("boolean") || type.equalsIgnoreCase("bool")) {
            attributeType = boolean.class;
        } else if (type.equalsIgnoreCase("integer") || type.equalsIgnoreCase("int")) {
            attributeType = int.class;
        } else if (type.equalsIgnoreCase("long")) {
            attributeType = Long.class;
        } else if (type.equalsIgnoreCase("float")) {
            attributeType = Float.class;
        } else if (type.equalsIgnoreCase("double")) {
            attributeType = Double.class;
        } else if (type.equalsIgnoreCase("string")) {
            attributeType = String.class;
        } else if (type.equalsIgnoreCase("bigdecimal")) {
            attributeType = BigDecimal.class;
        } else if (type.equalsIgnoreCase("biginteger")) {
            attributeType = BigInteger.class;
        } else if (type.equalsIgnoreCase("byte")) {
            attributeType = Byte.class;
        } else if (type.equalsIgnoreCase("char")) {
            attributeType = Character.class;
        } else if (type.equalsIgnoreCase("short")) {
            attributeType = Short.class;
        } else if (type.equalsIgnoreCase("listboolean")) {
            attributeType = boolean[].class;
        } else if (type.equalsIgnoreCase("listint")) {
            attributeType = int[].class;
        } else if (type.equalsIgnoreCase("listlong")) {
            attributeType = long[].class;
        } else if (type.equalsIgnoreCase("listfloat")) {
            attributeType = float[].class;
        } else if (type.equalsIgnoreCase("listdouble")) {
            attributeType = double[].class;
        } else if (type.equalsIgnoreCase("liststring")) {
            attributeType = String[].class;
        } else if (type.equalsIgnoreCase("listbigdecimal")) {
            attributeType = BigDecimal[].class;
        } else if (type.equalsIgnoreCase("listbiginteger")) {
            attributeType = BigInteger[].class;
        } else if (type.equalsIgnoreCase("listbyte")) {
            attributeType = byte[].class;
        } else if (type.equalsIgnoreCase("listchar")) {
            attributeType = char[].class;
        } else if (type.equalsIgnoreCase("listshort")) {
            attributeType = short[].class;
        } else {
            report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributetype2", type), Issue.Level.SEVERE));
            return;
        }
        // Add to model
        ColumnDraft column = null;
        if ("node".equalsIgnoreCase(forStr) || "all".equalsIgnoreCase(forStr)) {
            if (container.getNodeColumn(id) != null) {
                report.log(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributecolumn_exist", id));
                return;
            }
            column = container.addNodeColumn(id, attributeType);
            column.setTitle(title);
            report.log(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_log_nodeattribute", title, attributeType.getCanonicalName()));
        }
        if ("edge".equalsIgnoreCase(forStr) || "all".equalsIgnoreCase(forStr)) {
            if (container.getEdgeColumn(id) != null) {
                report.log(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributecolumn_exist", id));
                return;
            }
            column = container.addEdgeColumn(id, attributeType);
            column.setTitle(title);
            report.log(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_log_edgeattribute", title, attributeType.getCanonicalName()));
        }
        if (column != null && !defaultStr.isEmpty()) {
            try {
                column.setDefaultValueString(defaultStr);
                report.log(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_log_default", defaultStr, title));
            } catch (Exception e) {
                report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributedefault", title, attributeType.getCanonicalName()), Issue.Level.SEVERE));
            }
        }
    } else {
        report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributeempty", title), Issue.Level.SEVERE));
    }
}
Also used : ColumnDraft(org.gephi.io.importer.api.ColumnDraft) Issue(org.gephi.io.importer.api.Issue) XMLStreamException(javax.xml.stream.XMLStreamException) NodeProperties(org.gephi.io.importer.api.PropertiesAssociations.NodeProperties) EdgeProperties(org.gephi.io.importer.api.PropertiesAssociations.EdgeProperties) BigInteger(java.math.BigInteger)

Example 5 with ColumnDraft

use of org.gephi.io.importer.api.ColumnDraft in project gephi by gephi.

the class ImporterGraphML method readEdgeAttValue.

private void readEdgeAttValue(XMLStreamReader reader, EdgeDraft edge) throws Exception {
    String fore = "";
    String value = "";
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        String attName = reader.getAttributeName(i).getLocalPart();
        if (ATTVALUE_FOR.equalsIgnoreCase(attName)) {
            fore = reader.getAttributeValue(i).trim();
        }
    }
    if (fore.isEmpty()) {
        report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_datakey", edge), Issue.Level.SEVERE));
        return;
    }
    boolean end = false;
    while (reader.hasNext() && !end) {
        int xmltype = reader.next();
        switch(xmltype) {
            case XMLStreamReader.CHARACTERS:
                if (!xmlReader.isWhiteSpace()) {
                    value += xmlReader.getText();
                }
                break;
            case XMLStreamReader.END_ELEMENT:
                if (ATTVALUE.equalsIgnoreCase(xmlReader.getLocalName())) {
                    end = true;
                }
                break;
        }
    }
    if (!value.isEmpty()) {
        EdgeProperties prop = edgePropertiesAttributes.get(fore);
        if (prop != null) {
            try {
                switch(prop) {
                    case WEIGHT:
                        edge.setWeight(parseFloat(value));
                        break;
                    case LABEL:
                        edge.setLabel(value);
                        break;
                    case COLOR:
                        edge.setColor(value);
                        break;
                    case R:
                        if (edge.getColor() == null) {
                            edge.setColor(Integer.parseInt(value), 0, 0);
                        } else {
                            edge.setColor(Integer.parseInt(value), edge.getColor().getGreen(), edge.getColor().getBlue());
                        }
                        break;
                    case G:
                        if (edge.getColor() == null) {
                            edge.setColor(0, Integer.parseInt(value), 0);
                        } else {
                            edge.setColor(edge.getColor().getRed(), Integer.parseInt(value), edge.getColor().getBlue());
                        }
                        break;
                    case B:
                        if (edge.getColor() == null) {
                            edge.setColor(0, 0, Integer.parseInt(value));
                        } else {
                            edge.setColor(edge.getColor().getRed(), edge.getColor().getGreen(), Integer.parseInt(value));
                        }
                        break;
                }
            } catch (Exception e) {
                report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_datavalue", fore, edge, prop.toString()), Issue.Level.SEVERE));
            }
        }
        // Data attribute value
        ColumnDraft column = container.getEdgeColumn(fore);
        if (column != null) {
            try {
                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));
            }
        }
    }
}
Also used : ColumnDraft(org.gephi.io.importer.api.ColumnDraft) Issue(org.gephi.io.importer.api.Issue) EdgeProperties(org.gephi.io.importer.api.PropertiesAssociations.EdgeProperties) XMLStreamException(javax.xml.stream.XMLStreamException)

Aggregations

ColumnDraft (org.gephi.io.importer.api.ColumnDraft)20 Issue (org.gephi.io.importer.api.Issue)14 TimeMap (org.gephi.graph.api.types.TimeMap)4 EdgeDraft (org.gephi.io.importer.api.EdgeDraft)4 XMLStreamException (javax.xml.stream.XMLStreamException)3 EdgeProperties (org.gephi.io.importer.api.PropertiesAssociations.EdgeProperties)3 NodeProperties (org.gephi.io.importer.api.PropertiesAssociations.NodeProperties)3 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 Column (org.gephi.graph.api.Column)2 TimeRepresentation (org.gephi.graph.api.TimeRepresentation)2 TimeSet (org.gephi.graph.api.types.TimeSet)2 ElementDraft (org.gephi.io.importer.api.ElementDraft)2 NodeDraft (org.gephi.io.importer.api.NodeDraft)2 PropertiesAssociations (org.gephi.io.importer.api.PropertiesAssociations)2 BigInteger (java.math.BigInteger)1 Random (java.util.Random)1 Configuration (org.gephi.graph.api.Configuration)1