use of org.gephi.io.importer.api.EdgeDraft in project gephi by gephi.
the class ImporterGraphML method readEdge.
private void readEdge(XMLStreamReader reader) throws Exception {
String id = "";
String source = "";
String target = "";
String directed = "";
//Attributes
for (int i = 0; i < reader.getAttributeCount(); i++) {
String attName = reader.getAttributeName(i).getLocalPart();
if (EDGE_SOURCE.equalsIgnoreCase(attName)) {
source = reader.getAttributeValue(i);
} else if (EDGE_TARGET.equalsIgnoreCase(attName)) {
target = reader.getAttributeValue(i);
} else if (EDGE_ID.equalsIgnoreCase(attName)) {
id = reader.getAttributeValue(i);
} else if (EDGE_DIRECTED.equalsIgnoreCase(attName)) {
directed = reader.getAttributeValue(i);
}
}
//Edge Id
EdgeDraft edge;
if (!id.isEmpty()) {
edge = container.factory().newEdgeDraft(id);
} else {
edge = container.factory().newEdgeDraft();
}
NodeDraft nodeSource = container.getNode(source);
NodeDraft nodeTarget = container.getNode(target);
edge.setSource(nodeSource);
edge.setTarget(nodeTarget);
//Type
if (!directed.isEmpty()) {
if (directed.equalsIgnoreCase("true")) {
edge.setDirection(EdgeDirection.DIRECTED);
} else if (directed.equalsIgnoreCase("false")) {
edge.setDirection(EdgeDirection.UNDIRECTED);
} else {
report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_edgetype", directed, edge), Issue.Level.SEVERE));
edge.setDirection(edgeDefault);
}
} else {
edge.setDirection(edgeDefault);
}
boolean end = false;
while (reader.hasNext() && !end) {
int type = reader.next();
switch(type) {
case XMLStreamReader.START_ELEMENT:
if (ATTVALUE.equalsIgnoreCase(xmlReader.getLocalName())) {
readEdgeAttValue(reader, edge);
}
break;
case XMLStreamReader.END_ELEMENT:
if (EDGE.equalsIgnoreCase(xmlReader.getLocalName())) {
end = true;
}
break;
}
}
container.addEdge(edge);
}
use of org.gephi.io.importer.api.EdgeDraft in project gephi by gephi.
the class ImporterPajek method readArcsOrEdges.
private String readArcsOrEdges(String curLine, BufferedReader br) throws Exception {
String nextLine = curLine;
boolean reading_arcs = false;
boolean reading_edges = false;
if (nextLine.toLowerCase().startsWith("*arcs")) {
reading_arcs = true;
} else if (nextLine.toLowerCase().startsWith("*edges")) {
reading_edges = true;
}
if (!(reading_arcs || reading_edges)) {
return nextLine;
}
boolean is_list = false;
if (nextLine.toLowerCase().endsWith("list")) {
is_list = true;
}
while (br.ready()) {
if (cancel) {
return nextLine;
}
nextLine = br.readLine();
if (nextLine == null || nextLine.startsWith("*")) {
break;
}
if (nextLine.equals("")) {
// skip blank lines
report.logIssue(new Issue(NbBundle.getMessage(ImporterPajek.class, "importerNET_error_dataformat2", lineReader.getLineNumber()), Issue.Level.WARNING));
continue;
}
StringTokenizer st = new StringTokenizer(nextLine.trim());
int vid1 = Integer.parseInt(st.nextToken()) - 1;
NodeDraft nodeFrom = verticesArray[vid1];
if (// one source, multiple destinations
is_list) {
do {
int vid2 = Integer.parseInt(st.nextToken()) - 1;
NodeDraft nodeTo = verticesArray[vid2];
EdgeDraft edge = container.factory().newEdgeDraft();
edge.setSource(nodeFrom);
edge.setTarget(nodeTo);
container.addEdge(edge);
} while (st.hasMoreTokens());
} else // one source, one destination, at most one weight
{
int vid2 = Integer.parseInt(st.nextToken()) - 1;
NodeDraft nodeTo = verticesArray[vid2];
EdgeDraft edge = container.factory().newEdgeDraft();
edge.setSource(nodeFrom);
edge.setTarget(nodeTo);
// get the edge weight if we care
if (st.hasMoreTokens()) {
double edgeWeight = 1.0;
try {
edgeWeight = new Double(st.nextToken());
} catch (Exception e) {
report.logIssue(new Issue(NbBundle.getMessage(ImporterPajek.class, "importerNET_error_dataformat7", lineReader.getLineNumber()), Issue.Level.WARNING));
}
edge.setWeight(edgeWeight);
}
container.addEdge(edge);
}
}
return nextLine;
}
use of org.gephi.io.importer.api.EdgeDraft in project gephi by gephi.
the class ImporterDOT method edgeStructure.
protected void edgeStructure(StreamTokenizer streamTokenizer, final NodeDraft nodeDraft) throws Exception {
streamTokenizer.nextToken();
EdgeDraft edge = null;
if (streamTokenizer.ttype == '>' || streamTokenizer.ttype == '-') {
streamTokenizer.nextToken();
if (streamTokenizer.ttype == '{') {
while (true) {
streamTokenizer.nextToken();
if (streamTokenizer.ttype == '}') {
break;
} else {
nodeID(streamTokenizer);
edge = container.factory().newEdgeDraft();
edge.setSource(nodeDraft);
edge.setTarget(getOrCreateNode("" + streamTokenizer.sval));
container.addEdge(edge);
}
}
} else {
nodeID(streamTokenizer);
edge = container.factory().newEdgeDraft();
edge.setSource(nodeDraft);
edge.setTarget(getOrCreateNode("" + streamTokenizer.sval));
container.addEdge(edge);
}
} else {
report.logIssue(new Issue(NbBundle.getMessage(ImporterDOT.class, "importerDOT_error_edgeparsing", streamTokenizer.lineno()), Issue.Level.SEVERE));
if (streamTokenizer.ttype == StreamTokenizer.TT_WORD) {
streamTokenizer.pushBack();
}
return;
}
streamTokenizer.nextToken();
if (streamTokenizer.ttype == '[') {
edgeAttributes(streamTokenizer, edge);
} else {
streamTokenizer.pushBack();
}
}
use of org.gephi.io.importer.api.EdgeDraft 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.EdgeDraft in project gephi by gephi.
the class ImporterEdgeList method getEdges.
private void getEdges(Connection connection) throws SQLException {
//Factory
ElementDraft.Factory factory = container.factory();
//Properties
PropertiesAssociations properties = database.getPropertiesAssociations();
Statement s = connection.createStatement();
ResultSet rs = null;
try {
rs = s.executeQuery(database.getEdgeQuery());
} catch (SQLException ex) {
report.logIssue(new Issue("Failed to execute Edge query", Issue.Level.SEVERE, ex));
return;
}
findEdgeAttributesColumns(rs);
ResultSetMetaData metaData = rs.getMetaData();
int columnsCount = metaData.getColumnCount();
while (rs.next()) {
String id = null;
for (int i = 0; i < columnsCount; i++) {
String columnName = metaData.getColumnLabel(i + 1);
EdgeProperties p = properties.getEdgeProperty(columnName);
if (p != null && p.equals(EdgeProperties.ID)) {
String ide = rs.getString(i + 1);
if (ide != null) {
id = ide;
}
}
}
EdgeDraft edge;
if (id != null) {
edge = factory.newEdgeDraft(id);
} else {
edge = factory.newEdgeDraft();
}
for (int i = 0; i < columnsCount; i++) {
String columnName = metaData.getColumnLabel(i + 1);
EdgeProperties p = properties.getEdgeProperty(columnName);
if (p != null) {
injectEdgeProperty(p, rs, i + 1, edge);
} else {
//Inject edge attributes
ColumnDraft col = container.getEdgeColumn(columnName);
injectElementAttribute(rs, i + 1, col, edge);
}
}
injectTimeIntervalProperty(edge);
container.addEdge(edge);
}
rs.close();
s.close();
}
Aggregations