use of org.matheclipse.core.expression.data.ExprEdge 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;
}
use of org.matheclipse.core.expression.data.ExprEdge 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");
}
use of org.matheclipse.core.expression.data.ExprEdge 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 };
}
use of org.matheclipse.core.expression.data.ExprEdge 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;
}
use of org.matheclipse.core.expression.data.ExprEdge in project symja_android_library by axkr.
the class GraphFunctions method edgesToRules.
private static IASTAppendable[] edgesToRules(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;
edges.append(F.Rule(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;
edges.append(F.Rule(exprEdge.lhs(), exprEdge.rhs()));
}
}
return new IASTAppendable[] { edges, weights };
}
Aggregations