创建流程图的算法[一点指导??] [英] Algorithm to create flow chart [A little guidance??]
问题描述
好的,我知道这是一个模糊的问题,但是我似乎在这里陷入逻辑……我想创建输入程序的流程图.我从两天开始就一直在考虑这个问题,无法获得最佳的一般方法...所以我拼命地看着你,伙计在这里帮助我....也许是我缺少了一些小东西....
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....
我有一个xml文件,其中包含有关给定Java程序的信息,如下所示:
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>
现在,这是生成的xml文件的一部分....请参见代码段:
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;
}
现在我的操作方式是:
我有一个用于读取xml文件的类读取器,以及一个用于处理绘图的createFLowChart类.我开始遍历方法节点...如果找到一条语句,我将调用一个FundStatement函数,该函数绘制一个矩形框并将其与prv节点连接.在不添加很多状态布尔变量的情况下,对If-else构造执行此操作.那么任何人都可以在这方面指导我吗?
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?
现在,概率包含if-else构造.我找不到遍历if -else树并正确地在节点之间进行边缘连接的简单方法,而没有添加许多变量来包含状态信息,即if已经开始或否则开始等.这是我的方法,但是我发现深度调整很困难,例如,连接嵌套的if-else语句的叶子:
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<dataObjects.Interface> parentInterfaces=new ArrayList<dataObjects.Interface>();
//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<=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<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<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<childNodes.getLength();i++)
{
Node child=(Node)childNodes.item(i);
traverse(child,parent);
}
parent.foundEndElse();
}
else if(root.getNodeName()=="Statement")
{
parent.foundStatement(root.getTextContent());
}
}
现在,阅读器调用的三个函数是:
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;
}
这对于if else语句深一层有效,但在此之后恶化,我理解这是因为全局变量prvNode一次只能列出一个节点可能是列表ll,但是仍然可能会出现一些问题...任何人都可以改善它吗?
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??
推荐答案
您可以使用递归方法进行A DFS遍历,例如-
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.
这篇关于创建流程图的算法[一点指导??]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!