yFiles API is used do manipulation Graphml files, this sample program is doing CRUD operation .graphml formatted Files
package project1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import y.base.Edge;
import y.base.Graph;
import y.base.Node;
import y.base.NodeCursor;
import y.base.NodeMap;
import y.io.GraphMLIOHandler;
import y.io.graphml.Future;
import y.io.graphml.GraphMLHandler;
import y.io.graphml.KeyScope;
import y.io.graphml.KeyType;
public class GraphMLCURD {
public GraphMLCURD() {
super();
}
public static void main(String[] args) throws Exception {
GraphMLCURD graphmlCURD = new GraphMLCURD();
graphmlCURD.createGraphML();
graphmlCURD.readGraphML();
graphmlCURD.deleteGraphMLContent(new Integer(3));
}
/**
*method to create .graphml File using graph structure
*/
public void createGraphML() throws Exception {
//creating a empty graph
Graph graph = new Graph();
//creating nodes
Node[] nodes = new Node[5];
for (int i = 0; i < nodes.length; i++) {
nodes[i] = graph.createNode();
}
//creating edges
Edge[] edges = new Edge[4];
for (int i = 0; i < edges.length; i++) {
edges[i] = graph.createEdge(nodes[i], nodes[i + 1]);
}
//creating keys inform of NodeMaps
NodeMap nodeMap = graph.createNodeMap();
int nodeValue = 0;
for (NodeCursor nc = graph.nodes(); nc.ok(); nc.next()) {
nodeMap.setInt(nc.node(), nodeValue++);
}
//creating graphml handler from GraphMLIOHandler.graphml handler will be useful for reading/writing graph data
GraphMLIOHandler ioh = new GraphMLIOHandler();
GraphMLHandler graphMLHandler = ioh.getGraphMLHandler();
//below is for attribute data provider
graphMLHandler.addOutputDataProvider("mykey", nodeMap, KeyScope.NODE,
KeyType.INT);
//writing graph to .graphml file
graphMLHandler.write(graph,
new FileOutputStream(new File("mygraph.graphml")));
}
public void updateGraphML() {
//This funtionality is covered as part of delete
}
/**
*method read .graphml and populate graph object
*/
public Map readGraphML() throws Exception {
//creating graphml handler from GraphMLIOHandler.graphml handler will be useful for reading/writing graph data
GraphMLIOHandler ioh = new GraphMLIOHandler();
GraphMLHandler graphMLHandler = ioh.getGraphMLHandler();
//creating a empty graph, later which will be populated with .graphml data
Graph graph = new Graph();
//adding data accepator which will read attribute data
Future future =
graphMLHandler.addInputDataAcceptorFuture("mykey", KeyScope.ALL,
KeyType.INT);
//reading graphml data and populating it to Graph object
graphMLHandler.read(graph,
new FileInputStream(new File("mygraph.graphml")));
System.out.println("Printing populated graph object ::: " + graph);
//constructing graph and attribute data details
Map map = new HashMap();
map.put("GRAPH", graph);
map.put("NODE_DATA", future);
return map;
}
/**
*method to delete and update .graphml file with provided data
*/
public void deleteGraphMLContent(Integer nodeData) throws Exception {
//load graph and future
Map map = readGraphML();
Graph graph = (Graph)map.get("GRAPH");
Future future = (Future)map.get("NODE_DATA");
GraphMLIOHandler ioh = new GraphMLIOHandler();
GraphMLHandler graphMLHandler = ioh.getGraphMLHandler();
graphMLHandler.addOutputDataProvider("mykey",
(NodeMap)future.getValue(),
KeyScope.NODE, KeyType.INT);
for (NodeCursor nc = graph.nodes(); nc.ok(); nc.next()) {
NodeMap nodeMap1 = (NodeMap)future.getValue();
if (((Integer)nodeMap1.get(nc.node())).intValue() == nodeData) {
graph.removeNode(nc.node());
}
}
graphMLHandler.write(graph,
new FileOutputStream(new File("updatedmygraph.graphml")));
}
}
package project1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import y.base.Edge;
import y.base.Graph;
import y.base.Node;
import y.base.NodeCursor;
import y.base.NodeMap;
import y.io.GraphMLIOHandler;
import y.io.graphml.Future;
import y.io.graphml.GraphMLHandler;
import y.io.graphml.KeyScope;
import y.io.graphml.KeyType;
public class GraphMLCURD {
public GraphMLCURD() {
super();
}
public static void main(String[] args) throws Exception {
GraphMLCURD graphmlCURD = new GraphMLCURD();
graphmlCURD.createGraphML();
graphmlCURD.readGraphML();
graphmlCURD.deleteGraphMLContent(new Integer(3));
}
/**
*method to create .graphml File using graph structure
*/
public void createGraphML() throws Exception {
//creating a empty graph
Graph graph = new Graph();
//creating nodes
Node[] nodes = new Node[5];
for (int i = 0; i < nodes.length; i++) {
nodes[i] = graph.createNode();
}
//creating edges
Edge[] edges = new Edge[4];
for (int i = 0; i < edges.length; i++) {
edges[i] = graph.createEdge(nodes[i], nodes[i + 1]);
}
//creating keys inform of NodeMaps
NodeMap nodeMap = graph.createNodeMap();
int nodeValue = 0;
for (NodeCursor nc = graph.nodes(); nc.ok(); nc.next()) {
nodeMap.setInt(nc.node(), nodeValue++);
}
//creating graphml handler from GraphMLIOHandler.graphml handler will be useful for reading/writing graph data
GraphMLIOHandler ioh = new GraphMLIOHandler();
GraphMLHandler graphMLHandler = ioh.getGraphMLHandler();
//below is for attribute data provider
graphMLHandler.addOutputDataProvider("mykey", nodeMap, KeyScope.NODE,
KeyType.INT);
//writing graph to .graphml file
graphMLHandler.write(graph,
new FileOutputStream(new File("mygraph.graphml")));
}
public void updateGraphML() {
//This funtionality is covered as part of delete
}
/**
*method read .graphml and populate graph object
*/
public Map readGraphML() throws Exception {
//creating graphml handler from GraphMLIOHandler.graphml handler will be useful for reading/writing graph data
GraphMLIOHandler ioh = new GraphMLIOHandler();
GraphMLHandler graphMLHandler = ioh.getGraphMLHandler();
//creating a empty graph, later which will be populated with .graphml data
Graph graph = new Graph();
//adding data accepator which will read attribute data
Future future =
graphMLHandler.addInputDataAcceptorFuture("mykey", KeyScope.ALL,
KeyType.INT);
//reading graphml data and populating it to Graph object
graphMLHandler.read(graph,
new FileInputStream(new File("mygraph.graphml")));
System.out.println("Printing populated graph object ::: " + graph);
//constructing graph and attribute data details
Map map = new HashMap();
map.put("GRAPH", graph);
map.put("NODE_DATA", future);
return map;
}
/**
*method to delete and update .graphml file with provided data
*/
public void deleteGraphMLContent(Integer nodeData) throws Exception {
//load graph and future
Map map = readGraphML();
Graph graph = (Graph)map.get("GRAPH");
Future future = (Future)map.get("NODE_DATA");
GraphMLIOHandler ioh = new GraphMLIOHandler();
GraphMLHandler graphMLHandler = ioh.getGraphMLHandler();
graphMLHandler.addOutputDataProvider("mykey",
(NodeMap)future.getValue(),
KeyScope.NODE, KeyType.INT);
for (NodeCursor nc = graph.nodes(); nc.ok(); nc.next()) {
NodeMap nodeMap1 = (NodeMap)future.getValue();
if (((Integer)nodeMap1.get(nc.node())).intValue() == nodeData) {
graph.removeNode(nc.node());
}
}
graphMLHandler.write(graph,
new FileOutputStream(new File("updatedmygraph.graphml")));
}
}
No comments:
Post a Comment