Convert graph between JGraphT and OrientDB

563 views Asked by At

I use OrientDB to store graph data but it has few graph algorith and can not process weighted graph so I want to use JGraphT. Is there any tool to automatically convert between 2 types of graph. After process in JGraphT graph, I want to save back to database, but I don't want to re-save the whole graph, just changed part of the graph.

Does anyone have other solutions for my problem?

--Edit--

I solved it myself, I write a class assump that OrientDB has DirectedGraph interface, here is it for anyone need later. Hope that it's correct.

package Utilities;

import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import org.jboss.weld.util.collections.ArraySet;
import org.jgrapht.DirectedGraph;
import org.jgrapht.EdgeFactory;

/**
 *
 * @author bachn_000
 */
public class OrientDBDirectedGraph implements DirectedGraph<Vertex, Edge> {

private final OrientGraph graph;

public OrientDBDirectedGraph(OrientGraph g) {
    this.graph = g;
}

public Vertex getVertex(Object id) {
    return graph.getVertex(id);
}

public Set<Vertex> getVertices(String name) {
    Set<Vertex> temp = new ArraySet<>();
    Iterator<Vertex> iterator = graph.getVertices().iterator();
    while (iterator.hasNext()) {
        Vertex vertex = iterator.next();
        if(vertex.getProperty("name").equals(name)) {
            temp.add(vertex);
        }
    }
    return temp;
}

public Set<Edge> getEdges(String className) {
    Iterator<Edge> iterator = graph.getEdgesOfClass(className).iterator();
    Set<Edge> temp = new ArraySet<>();
    while (iterator.hasNext()) {
        Edge edge = iterator.next();
        temp.add(edge);
    }
    return temp;
}

@Override
public int inDegreeOf(Vertex vertex) {
    int count = 0;
    Iterator<Edge> edges = vertex.getEdges(Direction.IN).iterator();
    while (edges.hasNext()) {
        count++;
    }
    return count;
}

@Override
public Set<Edge> incomingEdgesOf(Vertex vertex) {
    Set<Edge> temp = new ArraySet<>();
    Iterator<Edge> edges = vertex.getEdges(Direction.IN).iterator();
    while (edges.hasNext()) {
        Edge edge = edges.next();
        temp.add(edge);
    }
    return temp;
}

@Override
public int outDegreeOf(Vertex vertex) {
    int count = 0;
    Iterator<Edge> edges = vertex.getEdges(Direction.OUT).iterator();
    while (edges.hasNext()) {
        count++;
    }
    return count;
}

@Override
public Set<Edge> outgoingEdgesOf(Vertex vertex) {
    Set<Edge> temp = new ArraySet<>();
    Iterator<Edge> edges = vertex.getEdges(Direction.OUT).iterator();
    while (edges.hasNext()) {
        Edge edge = edges.next();
        temp.add(edge);
    }
    return temp;
}

@Override
public Set<Edge> getAllEdges(Vertex sourceVertex, Vertex targetVertex) {
    Set<Edge> temp = new ArraySet<>();
    Iterator<Edge> edges = sourceVertex.getEdges(Direction.OUT).iterator();
    while (edges.hasNext()) {
        Edge edge = edges.next();
        if (edge.getVertex(Direction.IN).equals(targetVertex)) {
            temp.add(edge);
        }
    }
    return temp;
}

@Override
public Edge getEdge(Vertex sourceVertex, Vertex targetVertex) {
    Iterator<Edge> edges = sourceVertex.getEdges(Direction.OUT).iterator();
    while (edges.hasNext()) {
        Edge edge = edges.next();
        if (edge.getVertex(Direction.IN).equals(targetVertex)) {
            return edge;
        }
    }
    return null;
}

@Override
public EdgeFactory<Vertex, Edge> getEdgeFactory() {
    return new EdgeFactory<Vertex, Edge>() {
        @Override
        public Edge createEdge(Vertex sourceVertex, Vertex targetVertex) {
            return graph.addEdge(null, sourceVertex, targetVertex, "default_edge");
        }
    };
}

@Override
public Edge addEdge(Vertex sourceVertex, Vertex targetVertex) {
    Edge temp = null;
    Iterator<Edge> edges = sourceVertex.getEdges(Direction.OUT).iterator();
    while (edges.hasNext()) {
        Edge edge = edges.next();
        if (edge.getVertex(Direction.IN).equals(targetVertex)) {
            temp = edge;
        }
    }
    if (temp == null) {
        return graph.addEdge(null, sourceVertex, targetVertex, "default_edge");
    } else {
        return temp;
    }
}

@Override
public boolean addEdge(Vertex sourceVertex, Vertex targetVertex, Edge e) {
    Edge temp = null;
    Iterator<Edge> edges = sourceVertex.getEdges(Direction.OUT).iterator();
    while (edges.hasNext()) {
        Edge edge = edges.next();
        if (edge.equals(e)) {
            temp = edge;
            break;
        }
    }
    if (temp == null) {
        graph.addEdge(null, sourceVertex, targetVertex, "default_edge");
        return true;
    } else {
        return false;
    }
}

@Override
public boolean addVertex(Vertex v) {
    Vertex vertex = graph.getVertex(v.getId());
    if (vertex == null) {
        graph.addVertex(v.getId());
        return true;
    } else {
        return false;
    }
}

@Override
public boolean containsEdge(Vertex sourceVertex, Vertex targetVertex) {
    Iterator<Edge> edges = sourceVertex.getEdges(Direction.OUT).iterator();
    while (edges.hasNext()) {
        Edge edge = edges.next();
        if (edge.getVertex(Direction.IN).equals(targetVertex)) {
            return true;
        }
    }
    return false;
}

@Override
public boolean containsEdge(Edge e) {
    return (graph.getEdge(e.getId()) != null);
}

@Override
public boolean containsVertex(Vertex v) {
    return (graph.getEdge(v.getId()) != null);
}

@Override
public Set<Edge> edgeSet() {
    Set<Edge> temp = new ArraySet<>();
    Iterator<Edge> edges = graph.getEdges().iterator();
    while (edges.hasNext()) {
        Edge edge = edges.next();
        temp.add(edge);
    }
    return temp;
}

@Override
public Set<Edge> edgesOf(Vertex vertex) {
    Set<Edge> temp = new ArraySet<>();
    Iterator<Edge> edges = graph.getEdges().iterator();
    while (edges.hasNext()) {
        Edge edge = edges.next();
        if (edge.getVertex(Direction.OUT).equals(vertex) || edge.getVertex(Direction.IN).equals(vertex)) {
            temp.add(edge);
        }
    }
    return temp;
}

@Override
public boolean removeAllEdges(Collection<? extends Edge> edges) {
    for (Edge edge : edges) {
        graph.removeEdge(edge);
    }
    return true;
}

@Override
public Set<Edge> removeAllEdges(Vertex sourceVertex, Vertex targetVertex) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean removeAllVertices(Collection<? extends Vertex> vertices) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Edge removeEdge(Vertex sourceVertex, Vertex targetVertex) {
    Edge temp = this.getEdge(sourceVertex, targetVertex);
    if (temp != null) {
        graph.removeEdge(temp);
    }
    return temp;
}

@Override
public boolean removeEdge(Edge e) {
    if (this.containsEdge(e)) {
        graph.removeEdge(e);
        return true;
    } else {
        return false;
    }
}

@Override
public boolean removeVertex(Vertex v) {
    if (this.containsVertex(v)) {
        graph.removeVertex(v);
        return true;
    } else {
        return false;
    }
}

@Override
public Set<Vertex> vertexSet() {
    Set<Vertex> temp = new ArraySet<>();
    Iterator<Vertex> vertices = graph.getVertices().iterator();
    while (vertices.hasNext()) {
        Vertex vertex = vertices.next();
        temp.add(vertex);
    }
    return temp;
}

@Override
public Vertex getEdgeSource(Edge e) {
    return e.getVertex(Direction.OUT);
}

@Override
public Vertex getEdgeTarget(Edge e) {
    return e.getVertex(Direction.IN);
}

@Override
public double getEdgeWeight(Edge e) {
    Object property = e.getProperty("weight");
    if (property != null) {
        return Double.valueOf(property.toString());
    } else {
        return 1.0;
    }
}
}
0

There are 0 answers