1

我使用 OrientDB 存储图形数据,但它的图形算法很少,无法处理加权图,所以我想使用 JGraphT。是否有任何工具可以在两种类型的图形之间自动转换。在 JGraphT 图形中处理后,我想保存回数据库,但我不想重新保存整个图形,只是更改了图形的一部分。

有人对我的问题有其他解决方案吗?

- 编辑 -

我自己解决了,我写了一个类假设 OrientDB 有 DirectedGraph 接口,这里供以后需要的人使用。希望是正确的。

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;
    }
}
}
4

0 回答 0