Search in sources :

Example 1 with GraphNode

use of edu.mit.csail.sdg.alloy4graph.GraphNode in project org.alloytools.alloy by AlloyTools.

the class StaticGraphMaker method createEdge.

/**
 * Create an edge for a given tuple from a relation (if neither start nor end
 * node is explicitly invisible)
 */
private boolean createEdge(final boolean hidePrivate, final boolean hideMeta, AlloyRelation rel, AlloyTuple tuple, boolean bidirectional, Color magicColor) {
    // An edge will be drawn from A to D, with the label "R [B, C]"
    if ((hidePrivate && tuple.getStart().getType().isPrivate) || (hideMeta && tuple.getStart().getType().isMeta) || !view.nodeVisible(tuple.getStart(), instance))
        return false;
    if ((hidePrivate && tuple.getEnd().getType().isPrivate) || (hideMeta && tuple.getEnd().getType().isMeta) || !view.nodeVisible(tuple.getEnd(), instance))
        return false;
    GraphNode start = createNode(hidePrivate, hideMeta, tuple.getStart());
    GraphNode end = createNode(hidePrivate, hideMeta, tuple.getEnd());
    if (start == null || end == null)
        return false;
    boolean layoutBack = view.layoutBack.resolve(rel);
    String label = view.label.get(rel);
    if (tuple.getArity() > 2) {
        StringBuilder moreLabel = new StringBuilder();
        List<AlloyAtom> atoms = tuple.getAtoms();
        for (int i = 1; i < atoms.size() - 1; i++) {
            if (i > 1)
                moreLabel.append(", ");
            moreLabel.append(atomname(atoms.get(i), false));
        }
        if (label.length() == 0) {
        /* label=moreLabel.toString(); */
        } else {
            label = label + (" [" + moreLabel + "]");
        }
    }
    DotDirection dir = bidirectional ? DotDirection.BOTH : (layoutBack ? DotDirection.BACK : DotDirection.FORWARD);
    DotStyle style = view.edgeStyle.resolve(rel);
    DotColor color = view.edgeColor.resolve(rel);
    int weight = view.weight.get(rel);
    GraphEdge e = new GraphEdge((layoutBack ? end : start), (layoutBack ? start : end), tuple, label, rel);
    if (color == DotColor.MAGIC && magicColor != null)
        e.set(magicColor);
    else
        e.set(color.getColor(view.getEdgePalette()));
    e.set(style);
    e.set(dir != DotDirection.FORWARD, dir != DotDirection.BACK);
    e.set(weight < 1 ? 1 : (weight > 100 ? 10000 : 100 * weight));
    edges.put(e, tuple);
    return true;
}
Also used : DotColor(edu.mit.csail.sdg.alloy4graph.DotColor) DotDirection(edu.mit.csail.sdg.alloy4graph.DotDirection) DotStyle(edu.mit.csail.sdg.alloy4graph.DotStyle) GraphNode(edu.mit.csail.sdg.alloy4graph.GraphNode) GraphEdge(edu.mit.csail.sdg.alloy4graph.GraphEdge)

Example 2 with GraphNode

use of edu.mit.csail.sdg.alloy4graph.GraphNode in project org.alloytools.alloy by AlloyTools.

the class StaticGraphMaker method createNode.

/**
 * Return the node for a specific AlloyAtom (create it if it doesn't exist yet).
 *
 * @return null if the atom is explicitly marked as "Don't Show".
 */
private GraphNode createNode(final boolean hidePrivate, final boolean hideMeta, final AlloyAtom atom) {
    GraphNode node = atom2node.get(atom);
    if (node != null)
        return node;
    if ((hidePrivate && atom.getType().isPrivate) || (hideMeta && atom.getType().isMeta) || !view.nodeVisible(atom, instance))
        return null;
    // Make the node
    DotColor color = view.nodeColor(atom, instance);
    DotStyle style = view.nodeStyle(atom, instance);
    DotShape shape = view.shape(atom, instance);
    String label = atomname(atom, false);
    node = new GraphNode(graph, atom, label).set(shape).set(color.getColor(view.getNodePalette())).set(style);
    // Get the label based on the sets and relations
    String setsLabel = "";
    boolean showLabelByDefault = view.showAsLabel.get(null);
    for (AlloySet set : instance.atom2sets(atom)) {
        String x = view.label.get(set);
        if (x.length() == 0)
            continue;
        Boolean showLabel = view.showAsLabel.get(set);
        if ((showLabel == null && showLabelByDefault) || (showLabel != null && showLabel.booleanValue()))
            setsLabel += ((setsLabel.length() > 0 ? ", " : "") + x);
    }
    if (setsLabel.length() > 0) {
        Set<String> list = attribs.get(node);
        if (list == null)
            attribs.put(node, list = new TreeSet<String>());
        list.add("(" + setsLabel + ")");
    }
    nodes.put(node, atom);
    atom2node.put(atom, node);
    return node;
}
Also used : DotColor(edu.mit.csail.sdg.alloy4graph.DotColor) DotShape(edu.mit.csail.sdg.alloy4graph.DotShape) DotStyle(edu.mit.csail.sdg.alloy4graph.DotStyle) GraphNode(edu.mit.csail.sdg.alloy4graph.GraphNode)

Example 3 with GraphNode

use of edu.mit.csail.sdg.alloy4graph.GraphNode in project org.alloytools.alloy by AlloyTools.

the class StaticGraphMaker method produceGraph.

/**
 * Produces a single Graph from the given Instance and View and choice of
 * Projection
 */
public static JPanel produceGraph(AlloyInstance instance, VizState view, AlloyProjection proj) throws ErrorFatal {
    view = new VizState(view);
    if (proj == null)
        proj = new AlloyProjection();
    Graph graph = new Graph(view.getFontSize() / 12.0D);
    new StaticGraphMaker(graph, instance, view, proj);
    if (graph.nodes.size() == 0)
        new GraphNode(graph, "", "Due to your theme settings, every atom is hidden.", "Please click Theme and adjust your settings.");
    return new GraphViewer(graph);
}
Also used : Graph(edu.mit.csail.sdg.alloy4graph.Graph) GraphViewer(edu.mit.csail.sdg.alloy4graph.GraphViewer) GraphNode(edu.mit.csail.sdg.alloy4graph.GraphNode)

Example 4 with GraphNode

use of edu.mit.csail.sdg.alloy4graph.GraphNode in project org.alloytools.alloy by AlloyTools.

the class StaticGraphMaker method edgesAsAttribute.

/**
 * Attach tuple values as attributes to existing nodes.
 */
private void edgesAsAttribute(AlloyRelation rel) {
    // If this relation wants to be shown as an attribute,
    // then generate the annotations and attach them to each tuple's
    // starting node.
    // Eg.
    // If (A,B) and (A,C) are both in the relation F,
    // then the A node would have a line that says "F: B, C"
    // Eg.
    // If (A,B,C) and (A,D,E) are both in the relation F,
    // then the A node would have a line that says "F: B->C, D->E"
    // Eg.
    // If (A,B,C) and (A,D,E) are both in the relation F, and B belongs to
    // sets SET1 and SET2,
    // and SET1's "show in relational attribute" is on,
    // and SET2's "show in relational attribute" is on,
    // then the A node would have a line that says "F: B (SET1, SET2)->C,
    // D->E"
    // 
    Map<GraphNode, String> map = new LinkedHashMap<GraphNode, String>();
    for (AlloyTuple tuple : instance.relation2tuples(rel)) {
        GraphNode start = atom2node.get(tuple.getStart());
        if (start == null)
            // null means the node won't be shown, so we can't
            continue;
        // show any attributes
        String attr = "";
        List<AlloyAtom> atoms = tuple.getAtoms();
        for (int i = 1; i < atoms.size(); i++) {
            if (i > 1)
                attr += "->";
            attr += atomname(atoms.get(i), true);
        }
        if (attr.length() == 0)
            continue;
        String oldattr = map.get(start);
        if (oldattr != null && oldattr.length() > 0)
            attr = oldattr + ", " + attr;
        if (attr.length() > 0)
            map.put(start, attr);
    }
    for (Map.Entry<GraphNode, String> e : map.entrySet()) {
        GraphNode node = e.getKey();
        Set<String> list = attribs.get(node);
        if (list == null)
            attribs.put(node, list = new TreeSet<String>());
        String attr = e.getValue();
        if (view.label.get(rel).length() > 0)
            attr = view.label.get(rel) + ": " + attr;
        list.add(attr);
    }
}
Also used : GraphNode(edu.mit.csail.sdg.alloy4graph.GraphNode) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

GraphNode (edu.mit.csail.sdg.alloy4graph.GraphNode)4 DotColor (edu.mit.csail.sdg.alloy4graph.DotColor)2 DotStyle (edu.mit.csail.sdg.alloy4graph.DotStyle)2 DotDirection (edu.mit.csail.sdg.alloy4graph.DotDirection)1 DotShape (edu.mit.csail.sdg.alloy4graph.DotShape)1 Graph (edu.mit.csail.sdg.alloy4graph.Graph)1 GraphEdge (edu.mit.csail.sdg.alloy4graph.GraphEdge)1 GraphViewer (edu.mit.csail.sdg.alloy4graph.GraphViewer)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1