Algorithm to create flow chart [A little guidance??]

别来无恙 提交于 2020-02-07 10:54:08

问题


OK, I know it's a vague question, but I seem to be stuck with logic here...I want to create flow charts of the input programs. I hav been thinking about it since two days and can't get a best general approach...So i look desperately at you guyz to help me here....may be there is something small I am missing....

I have an xml file which contains the info about the given java program and looks like this:

<Method modifier="publicstatic" type="void" name="main" >
    <FormalParameter modifier="" type="String[]" var_name="args" />
    <Throw>NullPointerException</Throw>
    <Throw>
        IndexOutofBoundException
    </Throw>
    <Field modifier="" type="int" name="x,y,z" />
    <Field modifier="" type="int" name=" sum[] " />
    <If>
        condition><![CDATA[(x==0)]]></condition>
        <Statement>
            <![CDATA[System.out.Println("I am in true")]]></Statement>
        <If>
            <condition><![CDATA[(y==2)]]></condition>
            <Statement>
                <![CDATA[System.out.Println("I am in
    true of y==2")]]></Statement>
        </If>
        <Statement>
            <![CDATA[System.out.prnitln("I am in
    true again")]]></Statement>
    </If>
    <else>
        <If>
            <condition><![CDATA[(x==2)]]></condition>
            <Statement>
                <![CDATA[System.out.println("I am in
    x==2 true")]]></Statement>
        </If>
        <else>
            <Statement>
                <![CDATA[System.out.println("I am in
     else 2")]]></Statement>
        </else>
    </else>
    <Statement>
        <![CDATA[c=b+d]]></Statement>
    <Statement>
        <![CDATA[a=b+c]]></Statement>
</Method>

Now this is a part of the xml file generated....fir the code snippet:

public static void main(String[] args) throws NullPointerException,IndexOutofBoundException {

    int x,y,z;
    int sum[]={1,2,3,4};
    if(x==0)
    {
        System.out.Println("I am in true");
        if(y==2)
        {
            System.out.Println("I am in true of y==2");
        }
        System.out.prnitln("I am in true again");
    }
    else if(x==2)
    {
        System.out.println("I am in x==2 true");
    }
    else
    {
        System.out.println("I am in else 2");
    }

    c=b+d;
    a=b+c;   
}

Now my way of doing it is:

I have a class reader which reades the xml file and one class createFLowChart which takes care of drawing. I start traversing the method node... If I find a statement, I call a function FundStatement which draws a retangular box and connects it with prv node. Doing this for If-else constructs without adding a lot of state boolean variables. So can any one guide me in this?

Now the prob is with if-else constructs. I can not find a simple way of traversig through the if -else trees and correctly make the edge connections among the nodes without adding a number of variables to contain the state info i.e an if has started or else started etc. Here is my approach but I am finding difficulty in the depth adjustments i.e, connecting the leaves of nested if-else statements :

public void traverse(String path,CreateFlowChart parent)
{
    xPath =  XPathFactory.newInstance().newXPath();
    XPathExpression expr;
    //ArrayList&lt;dataObjects.Interface&gt; parentInterfaces=new ArrayList&lt;dataObjects.Interface&gt;();
    //dataObjects.Class[] classes=new dataObjects.Class[90];

    try {
        expr = xPath.compile(path);
        Object result = expr.evaluate(document, XPathConstants.NODESET);
        NodeList MethodNodes = (NodeList) result;
        NodeList childNodes=MethodNodes.item(0).getChildNodes();

        JOptionPane.showMessageDialog(null, "No of children of "+path+" is "+childNodes.getLength());
        for(int i=0;i&lt;=childNodes.getLength()-1;i++)
        {

            Node node=childNodes.item(i);
            JOptionPane.showMessageDialog(null, "Found Child "+node.getNodeName());
            traverse(node,parent);
        }


        } catch (XPathExpressionException e) {
        JOptionPane.showMessageDialog(null,"error: "+e.toString());
        e.printStackTrace();
    }

}
private void traverse(Node root ,CreateFlowChart parent)
{

    if(root.getNodeName()=="If")
    {
        String condition="";
        NodeList childNodes=root.getChildNodes();
        for(int i=0;i&lt;childNodes.getLength();i++)
        {
            Node child=(Node)childNodes.item(i);
            if(child.getNodeName()=="condition")
            {
                Element ele=(Element)child;
                condition=ele.getTextContent();

            }
        }
        dataObjects.ControlStatements ifstmt=new dataObjects.ControlStatements(condition,null,true);
        parent.foundIf(ifstmt);
        NodeList childs=root.getChildNodes();
        for(int i=0;i&lt;childs.getLength();i++)
        {
            Node child=(Node)childs.item(i);
            traverse(child,parent);


        }

        parent.foundEndIf();


    }
    else if(root.getNodeName()=="else")
    {
        parent.foundElse();
        NodeList childNodes=root.getChildNodes();
        for(int i=0;i&lt;childNodes.getLength();i++)
        {
            Node child=(Node)childNodes.item(i);
            traverse(child,parent);

        }
        parent.foundEndElse();

    }

    else if(root.getNodeName()=="Statement")
    {
        parent.foundStatement(root.getTextContent());

    }
}

Now the three function called from the reader are:

public void foundIf(dataObjects.ControlStatements Ifstatement)
{
    JOptionPane.showMessageDialog(null,"Drawing If");
    graph.getModel().beginUpdate();
    try
    {
        Object v1 = graph.insertVertex(start, null, "If "+Ifstatement.condition, 20, 20, 150,60,"Branch");
        if(isInIf==false && isInElse==false)
        {
            JOptionPane.showMessageDialog(null,"Drawing normally");
            graph.insertEdge(start, null, "", currentNode,v1);
        }
        else if(isInIf==true)
        {
            JOptionPane.showMessageDialog(null,"Drawing inside a previous If");
            graph.insertEdge(start, null, "True", currentNode,v1);
            isInIf=false;
        }
        else if(isInElse==true)
        {
            JOptionPane.showMessageDialog(null,"Drawing inside a previous else");
            graph.insertEdge(start, null, "False", currentNode,v1);
            isInElse=false;
        }
        currentNode=(mxCell)v1;
        JOptionPane.showMessageDialog(null,"Pushing if node inside stack");
        lastIfNode.push(currentNode);
        isInIf=true;
    }
    finally
    {
        graph.getModel().endUpdate();
    }
}
public void foundElse()
{
    currentNode=lastIfNode.pop();
    isInElse=true;
}
public void foundStatement(String st)
{
    JOptionPane.showMessageDialog(null,"Drawing a statement");
    graph.getModel().beginUpdate();
    try
    {
        Object v1 = graph.insertVertex(currentNode, null, st, 20, 20, 150,60,"Statement");
        if(isInIf==false && isInElse==false)
        {
            JOptionPane.showMessageDialog(null,"Drawing normally");
            graph.insertEdge(start, null, "", currentNode,v1);
        }
        else if(isInIf==true)
        {
            JOptionPane.showMessageDialog(null,"Drawing inside a prv If");
            graph.insertEdge(start, null, "True", currentNode,v1);
            isInIf=false;
        }
        else if(isInElse==true)
        {
            JOptionPane.showMessageDialog(null, "Drawing inside else");
            graph.insertEdge(start, null, "False", currentNode,v1);
            isInElse=false;
            reTraceIf=false;
        }
        if(reTraceIf==true)
        {
            JOptionPane.showMessageDialog(null, "Drawing false part");
            graph.insertEdge(start, null, "False", lastIfNode.pop(),v1);
            reTraceIf=false;
        }
        if(branchEnded==true)
        {
            JOptionPane.showMessageDialog(null, "Linking brancehs");
            graph.insertEdge(start, null, "", prvBranchNode,v1);
            branchEnded=false;
        }
        currentNode=(mxCell)v1;
    }
    finally
    {
        graph.getModel().endUpdate();
    }
}
public void foundEndIf()
{
    prvBranchNode=currentNode;
    isInIf=false;
    reTraceIf=true;
}
public void foundEndElse()
{
    branchEnded=true;
}

This works fine for the if else statements one level deep but deteriorates after that i understand that is because the globbal variable prvNode can hld only one node at a time may be a list ll do but still some problems may arise...Can any one improve it please??


回答1:


You can do a recursive approach to do a A DFS traversal, for eg -

1    For each line in block
2        If it is an if statement
3            Render condition in a Diamond shape
4            Get the statement block inside the if statement and go to #1
5        else 
6            Render it in a rectangle shape

This is just an idea, you will need to keep a track of the depth as well, besides keeping in mind the layout of the elements on a 2-D plane. But you should workout the traversal algorithm first and then fit the layout later.



来源:https://stackoverflow.com/questions/6022453/algorithm-to-create-flow-chart-a-little-guidance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!