use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.
the class ImporterGDF method importData.
private void importData(LineNumberReader reader) throws Exception {
//Progress
Progress.start(progressTicket);
//Verify a node line exists and puts nodes and edges lines in arrays
walkFile(reader);
//Progress
Progress.switchToDeterminate(progressTicket, nodeLines.size() + edgeLines.size());
//Magix regex
Pattern pattern = Pattern.compile("(?<=(?:,|^)\")(.*?)(?=(?<=(?:[^\\\\]))\",|\"$)|(?<=(?:,|^)')(.*?)(?=(?<=(?:[^\\\\]))',|'$)|(?<=(?:,|^))(?=[^'\"])(.*?)(?=(?:,|$))|(?<=,)($)");
//Nodes
for (String nodeLine : nodeLines) {
if (cancel) {
return;
}
//Create Node
NodeDraft node = null;
Matcher m = pattern.matcher(nodeLine);
int count = 0;
String id = "";
while (m.find()) {
int start = m.start();
int end = m.end();
if (start != end) {
String data = nodeLine.substring(start, end);
data = data.trim();
if (!data.isEmpty() && !data.toLowerCase().equals("null")) {
if (count == 0) {
//Id
id = data;
if (node == null) {
node = container.factory().newNodeDraft(id);
}
} else if (count - 1 < nodeColumns.length) {
if (nodeColumns[count - 1] != null) {
setNodeData(node, nodeColumns[count - 1], data);
}
} else {
report.logIssue(new Issue(NbBundle.getMessage(ImporterGDF.class, "importerGDF_error_dataformat7", id), Issue.Level.SEVERE));
}
}
}
count++;
}
container.addNode(node);
//Progress
Progress.progress(progressTicket);
}
//Edges
for (String edgeLine : edgeLines) {
if (cancel) {
return;
}
//Create Edge
EdgeDraft edge = container.factory().newEdgeDraft();
//Default to undirected unless stated
edge.setDirection(EdgeDirection.UNDIRECTED);
Matcher m = pattern.matcher(edgeLine);
int count = 0;
String id = "";
while (m.find()) {
int start = m.start();
int end = m.end();
if (start != end) {
String data = edgeLine.substring(start, end);
data = data.trim();
if (!data.isEmpty() && !data.toLowerCase().equals("null")) {
if (count == 0) {
NodeDraft nodeSource = container.getNode(data);
edge.setSource(nodeSource);
id = data;
} else if (count == 1) {
NodeDraft nodeTarget = container.getNode(data);
edge.setTarget(nodeTarget);
id += "," + data;
} else if (count - 2 < edgeColumns.length) {
if (edgeColumns[count - 2] != null) {
setEdgeData(edge, edgeColumns[count - 2], data);
}
} else {
report.logIssue(new Issue(NbBundle.getMessage(ImporterGDF.class, "importerGDF_error_dataformat7", id), Issue.Level.SEVERE));
}
}
}
count++;
}
container.addEdge(edge);
//Progress
Progress.progress(progressTicket);
}
}
use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.
the class ImporterCSV method addNode.
private void addNode(String id, String label) {
NodeDraft node;
if (!container.nodeExists(id)) {
node = container.factory().newNodeDraft(id);
node.setLabel(label);
container.addNode(node);
}
}
use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.
the class ImporterCSV method addEdge.
private void addEdge(String source, String target, float weight) {
NodeDraft sourceNode;
if (!container.nodeExists(source)) {
sourceNode = container.factory().newNodeDraft(source);
container.addNode(sourceNode);
} else {
sourceNode = container.getNode(source);
}
NodeDraft targetNode;
if (!container.nodeExists(target)) {
targetNode = container.factory().newNodeDraft(target);
container.addNode(targetNode);
} else {
targetNode = container.getNode(target);
}
EdgeDraft edge = container.factory().newEdgeDraft();
edge.setSource(sourceNode);
edge.setTarget(targetNode);
edge.setWeight(weight);
container.addEdge(edge);
}
use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.
the class ImporterDL method readEdgelistRow.
private void readEdgelistRow(String row, int pointer, double startTime, double endTime) {
StringTokenizer rowkonizer = new StringTokenizer(row);
if (!rowkonizer.hasMoreTokens()) {
return;
}
// should have three entries, int from, int to, weight
String from = rowkonizer.nextToken();
if (!rowkonizer.hasMoreTokens()) {
return;
}
String to = rowkonizer.nextToken();
double weight = 1.0;
if (rowkonizer.hasMoreTokens()) {
String weightParse = rowkonizer.nextToken();
try {
weight = Double.parseDouble(weightParse);
} catch (Exception e) {
report.logIssue(new Issue(NbBundle.getMessage(ImporterDL.class, "importerDL_error_edgeparseweight", weightParse, getLineNumber(pointer)), Issue.Level.WARNING));
}
}
NodeDraft sourceNode = container.getNode(from);
NodeDraft targetNode = container.getNode(to);
EdgeDraft edgeDraft = container.factory().newEdgeDraft();
edgeDraft.setSource(sourceNode);
edgeDraft.setTarget(targetNode);
edgeDraft.setWeight((float) weight);
container.addEdge(edgeDraft);
}
use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.
the class ImporterDL method readMatrixRow.
private void readMatrixRow(String line, int pointer, int row, int startTime, int endTime) {
StringTokenizer rowkonizer = new StringTokenizer(line, " ");
int from = row + 1;
int to = 1;
double weight = 0;
while (rowkonizer.hasMoreTokens()) {
String toParse = (String) rowkonizer.nextToken();
if (to > numNodes) {
report.logIssue(new Issue(NbBundle.getMessage(ImporterDL.class, "importerDL_error_matrixentriescount", row, startTime, getLineNumber(pointer)), Issue.Level.SEVERE));
}
try {
weight = Double.parseDouble(toParse);
} catch (Exception e) {
report.logIssue(new Issue(NbBundle.getMessage(ImporterDL.class, "importerDL_error_weightparseerror", toParse, startTime, getLineNumber(pointer)), Issue.Level.SEVERE));
}
if (weight != 0) {
NodeDraft sourceNode = container.getNode("" + from);
NodeDraft targetNode = container.getNode("" + to);
EdgeDraft edgeDraft = container.factory().newEdgeDraft();
edgeDraft.setSource(sourceNode);
edgeDraft.setTarget(targetNode);
edgeDraft.setWeight((float) weight);
container.addEdge(edgeDraft);
}
to++;
}
}
Aggregations