Search in sources :

Example 1 with GraphType

use of org.jgrapht.GraphType in project symja_android_library by axkr.

the class GraphFunctions method weightedEdgesToVisjs.

private static void weightedEdgesToVisjs(Map<IExpr, Integer> map, StringBuilder buf, Graph<IExpr, ExprWeightedEdge> graph) {
    Set<ExprWeightedEdge> edgeSet = graph.edgeSet();
    GraphType type = graph.getType();
    boolean first = true;
    if (type.isDirected()) {
        buf.append("var edges = new vis.DataSet([\n");
        for (Object object : edgeSet) {
            if (object instanceof ExprWeightedEdge) {
                ExprWeightedEdge edge = (ExprWeightedEdge) object;
                // {from: 1, to: 3},
                if (first) {
                    buf.append("  {from: ");
                } else {
                    buf.append(", {from: ");
                }
                buf.append(map.get(edge.lhs()));
                buf.append(", to: ");
                buf.append(map.get(edge.rhs()));
                buf.append(", label: '");
                buf.append(edge.weight());
                buf.append("'");
                // , arrows: { to: { enabled: true, type: 'arrow'}}
                buf.append(" , arrows: { to: { enabled: true, type: 'arrow'}}");
                buf.append("}\n");
                first = false;
            }
        }
    } else {
        buf.append("var edges = new vis.DataSet([\n");
        for (Object object : edgeSet) {
            if (object instanceof ExprWeightedEdge) {
                ExprWeightedEdge edge = (ExprWeightedEdge) object;
                // {from: 1, to: 3},
                if (first) {
                    buf.append("  {from: ");
                } else {
                    buf.append(", {from: ");
                }
                buf.append(map.get(edge.lhs()));
                buf.append(", to: ");
                buf.append(map.get(edge.rhs()));
                buf.append(", label: '");
                buf.append(edge.weight());
                buf.append("'");
                buf.append("}\n");
                first = false;
            }
        }
    }
    buf.append("]);\n");
}
Also used : GraphType(org.jgrapht.GraphType) ExprWeightedEdge(org.matheclipse.core.expression.data.ExprWeightedEdge)

Example 2 with GraphType

use of org.jgrapht.GraphType in project symja_android_library by axkr.

the class GraphFunctions method createGraph.

private static GraphExpr<ExprEdge> createGraph(final IAST vertices, final IAST edges) {
    Graph<IExpr, ExprEdge> g;
    GraphType t = edges.isListOfEdges();
    if (t != null) {
        if (t.isDirected()) {
            g = new DefaultDirectedGraph<IExpr, ExprEdge>(ExprEdge.class);
        } else {
            g = new DefaultUndirectedGraph<IExpr, ExprEdge>(ExprEdge.class);
        }
        if (vertices.isList()) {
            // Graph<IExpr, IExprEdge> g = new DefaultDirectedGraph<IExpr, IExprEdge>(IExprEdge.class);
            for (int i = 1; i < vertices.size(); i++) {
                g.addVertex(vertices.get(i));
            }
        }
        for (int i = 1; i < edges.size(); i++) {
            IAST edge = edges.getAST(i);
            g.addVertex(edge.arg1());
            g.addVertex(edge.arg2());
            g.addEdge(edge.arg1(), edge.arg2());
        }
        return GraphExpr.newInstance(g);
    }
    return null;
}
Also used : ExprEdge(org.matheclipse.core.expression.data.ExprEdge) GraphType(org.jgrapht.GraphType) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 3 with GraphType

use of org.jgrapht.GraphType in project symja_android_library by axkr.

the class GraphFunctions method edgesToVisjs.

private static void edgesToVisjs(Map<IExpr, Integer> map, StringBuilder buf, Graph<IExpr, ExprEdge> g) {
    Set<ExprEdge> edgeSet = g.edgeSet();
    GraphType type = g.getType();
    boolean first = true;
    if (type.isDirected()) {
        buf.append("var edges = new vis.DataSet([\n");
        for (Object object : edgeSet) {
            if (object instanceof ExprEdge) {
                ExprEdge edge = (ExprEdge) object;
                // {from: 1, to: 3},
                if (first) {
                    buf.append("  {from: ");
                } else {
                    buf.append(", {from: ");
                }
                buf.append(map.get(edge.lhs()));
                buf.append(", to: ");
                buf.append(map.get(edge.rhs()));
                // , arrows: { to: { enabled: true, type: 'arrow'}}
                buf.append(" , arrows: { to: { enabled: true, type: 'arrow'}}");
                buf.append("}\n");
                first = false;
            } else if (object instanceof ExprWeightedEdge) {
                ExprWeightedEdge weightedEdge = (ExprWeightedEdge) object;
                // {from: 1, to: 3},
                if (first) {
                    buf.append("  {from: ");
                } else {
                    buf.append(", {from: ");
                }
                buf.append(map.get(weightedEdge.lhs()));
                buf.append(", to: ");
                buf.append(map.get(weightedEdge.rhs()));
                // , arrows: { to: { enabled: true, type: 'arrow'}}
                buf.append(" , arrows: { to: { enabled: true, type: 'arrow'}}");
                buf.append("}\n");
                first = false;
            }
        }
    } else {
        // 
        buf.append("var edges = new vis.DataSet([\n");
        for (Object object : edgeSet) {
            if (object instanceof ExprEdge) {
                ExprEdge edge = (ExprEdge) object;
                // {from: 1, to: 3},
                if (first) {
                    buf.append("  {from: ");
                } else {
                    buf.append(", {from: ");
                }
                buf.append(map.get(edge.lhs()));
                buf.append(", to: ");
                buf.append(map.get(edge.rhs()));
                buf.append("}\n");
                first = false;
            } else if (object instanceof ExprWeightedEdge) {
                ExprWeightedEdge weightedEdge = (ExprWeightedEdge) object;
                // {from: 1, to: 3},
                if (first) {
                    buf.append("  {from: ");
                } else {
                    buf.append(", {from: ");
                }
                buf.append(map.get(weightedEdge.lhs()));
                buf.append(", to: ");
                buf.append(map.get(weightedEdge.rhs()));
                buf.append("}\n");
                first = false;
            }
        }
    }
    buf.append("]);\n");
}
Also used : ExprEdge(org.matheclipse.core.expression.data.ExprEdge) GraphType(org.jgrapht.GraphType) ExprWeightedEdge(org.matheclipse.core.expression.data.ExprWeightedEdge)

Example 4 with GraphType

use of org.jgrapht.GraphType in project symja_android_library by axkr.

the class GraphFunctions method edgesToIExpr.

private static IASTAppendable[] edgesToIExpr(Graph<IExpr, ?> g) {
    Set<Object> edgeSet = (Set<Object>) g.edgeSet();
    IASTAppendable edges = F.ListAlloc(edgeSet.size());
    IASTAppendable weights = null;
    GraphType type = g.getType();
    for (Object edge : edgeSet) {
        if (edge instanceof ExprWeightedEdge) {
            ExprWeightedEdge weightedEdge = (ExprWeightedEdge) edge;
            if (type.isDirected()) {
                edges.append(F.DirectedEdge(weightedEdge.lhs(), weightedEdge.rhs()));
            } else {
                edges.append(F.UndirectedEdge(weightedEdge.lhs(), weightedEdge.rhs()));
            }
            if (weights == null) {
                weights = F.ListAlloc(edgeSet.size());
            }
            weights.append(weightedEdge.weight());
        } else if (edge instanceof ExprEdge) {
            ExprEdge exprEdge = (ExprEdge) edge;
            if (type.isDirected()) {
                edges.append(F.DirectedEdge(exprEdge.lhs(), exprEdge.rhs()));
            } else {
                edges.append(F.UndirectedEdge(exprEdge.lhs(), exprEdge.rhs()));
            }
        }
    }
    return new IASTAppendable[] { edges, weights };
}
Also used : ExprEdge(org.matheclipse.core.expression.data.ExprEdge) Set(java.util.Set) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) GraphType(org.jgrapht.GraphType) ExprWeightedEdge(org.matheclipse.core.expression.data.ExprWeightedEdge)

Example 5 with GraphType

use of org.jgrapht.GraphType in project symja_android_library by axkr.

the class GraphFunctions method createGraph.

/**
 * Create a <code>Graph<IExpr, ExprWeightedEdge></code> or <code>Graph<IExpr, ExprEdge></code>
 *
 * @param arg1
 * @return
 */
private static GraphExpr<?> createGraph(final IExpr arg1) {
    if (arg1.head().equals(S.Graph) && arg1 instanceof GraphExpr) {
        return (GraphExpr<?>) arg1;
    }
    Graph<IExpr, ExprEdge> g;
    GraphType t = arg1.isListOfEdges();
    if (t != null) {
        if (t.isDirected()) {
            g = new DefaultDirectedGraph<IExpr, ExprEdge>(ExprEdge.class);
        } else {
            g = new DefaultUndirectedGraph<IExpr, ExprEdge>(ExprEdge.class);
        }
        IAST list = (IAST) arg1;
        for (int i = 1; i < list.size(); i++) {
            IAST edge = list.getAST(i);
            g.addVertex(edge.arg1());
            g.addVertex(edge.arg2());
            g.addEdge(edge.arg1(), edge.arg2());
        }
        return GraphExpr.newInstance(g);
    }
    return null;
}
Also used : ExprEdge(org.matheclipse.core.expression.data.ExprEdge) GraphExpr(org.matheclipse.core.expression.data.GraphExpr) GraphType(org.jgrapht.GraphType) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

GraphType (org.jgrapht.GraphType)8 ExprWeightedEdge (org.matheclipse.core.expression.data.ExprWeightedEdge)6 ExprEdge (org.matheclipse.core.expression.data.ExprEdge)5 IAST (org.matheclipse.core.interfaces.IAST)3 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)3 IExpr (org.matheclipse.core.interfaces.IExpr)3 Set (java.util.Set)2 DefaultDirectedWeightedGraph (org.jgrapht.graph.DefaultDirectedWeightedGraph)1 DefaultUndirectedWeightedGraph (org.jgrapht.graph.DefaultUndirectedWeightedGraph)1 GraphExpr (org.matheclipse.core.expression.data.GraphExpr)1