Search in sources :

Example 1 with GPath

use of org.antlr.works.visualization.graphics.path.GPath in project antlrworks by antlr.

the class GGraphGroup method addPath.

public void addPath(List path, boolean disabled, Map<Integer, FAState> skippedStates) {
    List<GPathElement> elements = new ArrayList<GPathElement>();
    /** path contains a list of NFAState states (from ANTLR): they represent
         * all the states along the path. The graphical representation of the NFA/SD
         * does not necessarily contains all the states of the path because the representation
         * can be simplified to remove all unecessary states.
         * The problem here is to use the information stored in the transition of the
         * graphical representation to figure out exactly which graphical node corresponds
         * to the path.
         */
    NFAState state;
    GNode node;
    NFAState nextState = null;
    GNode nextNode = null;
    for (pathIndex = 0; getPathIndex() < path.size(); pathIndex = getPathIndex() + 1) {
        if (getPathIndex() == 0) {
            nextState = (NFAState) path.get(getPathIndex());
            nextNode = findNodeForStateNumber(nextState.stateNumber);
            if (nextNode == null) {
                // A path can start from anywhere in the graph. It might happen
                // that the starting state of the path has been skipped by
                // the optimization in FAFactory. We use the skippedStates mapping
                // to find out what is the parent state of the skipped state.
                FAState parentState = skippedStates.get(nextState.stateNumber);
                if (parentState == null) {
                    System.err.println("[GGraphGroup] Starting path state " + nextState.stateNumber + "[" + nextState.enclosingRule.name + "] cannot be found in the graph");
                    return;
                } else {
                    nextNode = findNodeForStateNumber(parentState.stateNumber);
                }
            }
            continue;
        } else {
            state = nextState;
            node = nextNode;
        }
        nextState = (NFAState) path.get(getPathIndex());
        nextNode = findNodeForStateNumber(nextState.stateNumber);
        GNode externalNode = null;
        if (nextNode == null) {
            // The state has probably been skipped during the graphical rendering.
            // Find the next non-skipped state.
            FATransition t = getNodeTransitionToNextNonSkippedState(node, path);
            if (t == null) {
                // No transition found. Look in the skipped states mapping because
                // it might be possible that the next state is in another rule but
                // cannot be found because it has been skipped.
                FAState parentState = skippedStates.get(nextState.stateNumber);
                if (parentState == null) {
                    //  OK. The node really does not exist. Continue by skipping it.
                    nextNode = node;
                    continue;
                } else {
                    nextNode = findNodeForStateNumber(parentState.stateNumber);
                }
            } else {
                // is incrementing it
                if (getPathIndex() >= path.size()) {
                    nextNode = findNodeForStateNumber(t.target.stateNumber);
                } else {
                    nextState = (NFAState) path.get(getPathIndex());
                    if (t.target.stateNumber == nextState.stateNumber) {
                        nextNode = findNodeForStateNumber(t.target.stateNumber);
                    } else {
                        // The only case that the target state of the transition if not
                        // the next state of the path is when the next state of the path
                        // is in another rule. In this case, the target state of the transition
                        // will contain a negative state number indicating an external rule reference:
                        // this external rule reference is added by AW during rendering and is not
                        // part of any ANTLR NFA.
                        // This node is the node representing the external rule reference
                        // before jumping outside of the rule
                        externalNode = findNodeForStateNumber(t.target.stateNumber);
                        // This node is the first node in the other rule
                        nextNode = findNodeForStateNumber(nextState.stateNumber);
                    }
                }
            }
        }
        if (state == null || node == null || nextNode == null)
            continue;
        if (state.enclosingRule.name.equals(nextState.enclosingRule.name))
            addNextElementInSameRule(elements, node, nextNode);
        else
            addNextElementInOtherRule(elements, node, externalNode, nextNode, nextState);
    }
    if (nextNode != null)
        elements.add(GPathElement.createElement(nextNode));
    getPathGroup().addPath(new GPath(elements, disabled));
}
Also used : FAState(org.antlr.works.visualization.fa.FAState) NFAState(org.antlr.analysis.NFAState) FATransition(org.antlr.works.visualization.fa.FATransition) GPath(org.antlr.works.visualization.graphics.path.GPath) GNode(org.antlr.works.visualization.graphics.shape.GNode) ArrayList(java.util.ArrayList) GPathElement(org.antlr.works.visualization.graphics.path.GPathElement) NFAState(org.antlr.analysis.NFAState)

Example 2 with GPath

use of org.antlr.works.visualization.graphics.path.GPath in project antlrworks by antlr.

the class GGraphGroup method addUnreachableAlt.

public void addUnreachableAlt(NFAState state, Integer alt) {
    List<GPathElement> elements = new ArrayList<GPathElement>();
    GNode node = findNodeForStateNumber(state.stateNumber);
    if (node == null) {
        System.err.println("[GGraphGroup] Decision state " + state.stateNumber + "[" + state.enclosingRule.name + "] cannot be found in the graph");
        return;
    }
    List<FATransition> transitions = node.state.transitions;
    int altNum = alt - 1;
    if (altNum >= transitions.size()) {
        System.err.println("[GGraphGroup] Unreachable alt " + altNum + "[" + state.enclosingRule.name + "] is out of bounds: " + transitions.size());
        return;
    }
    FATransition t = transitions.get(altNum);
    elements.add(GPathElement.createElement(node));
    elements.add(GPathElement.createElement(node.getLink(t)));
    /** This path has to be visible but not selectable */
    GPath path = new GPath(elements, true);
    path.setVisible(true);
    path.setSelectable(false);
    getPathGroup().addPath(path);
}
Also used : FATransition(org.antlr.works.visualization.fa.FATransition) GPath(org.antlr.works.visualization.graphics.path.GPath) GNode(org.antlr.works.visualization.graphics.shape.GNode) ArrayList(java.util.ArrayList) GPathElement(org.antlr.works.visualization.graphics.path.GPathElement)

Example 3 with GPath

use of org.antlr.works.visualization.graphics.path.GPath in project antlrworks by antlr.

the class GView method pathCurrentElementDidChange.

public void pathCurrentElementDidChange() {
    GPath path = getCurrentPath();
    Rectangle rect = path.getBoundsOfSelectedElement();
    if (!rect.isEmpty()) {
        // Expand the rectangle a little bit so the rectangle is "more" visible
        rect.x -= 50;
        rect.y -= 50;
        rect.width += 100;
        rect.height += 100;
        smoothScrolling.scrollTo(rect);
    }
}
Also used : GPath(org.antlr.works.visualization.graphics.path.GPath)

Aggregations

GPath (org.antlr.works.visualization.graphics.path.GPath)3 ArrayList (java.util.ArrayList)2 FATransition (org.antlr.works.visualization.fa.FATransition)2 GPathElement (org.antlr.works.visualization.graphics.path.GPathElement)2 GNode (org.antlr.works.visualization.graphics.shape.GNode)2 NFAState (org.antlr.analysis.NFAState)1 FAState (org.antlr.works.visualization.fa.FAState)1