好吧,我知道这是一个模糊的问题,但我似乎在这里被逻辑所困......我想创建输入程序的流程图。我从两天开始就一直在考虑这个问题,但无法获得最佳的总体方法......所以我拼命地看着你们在这里帮我......可能是我错过了一些小东西....
我有一个xml文件,其中包含有关给定java程序的信息,如下所示:
<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 文件的一部分。冷杉代码片段:
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,它绘制一个reangular box并将其与prv节点连接。对 If-else 构造执行此操作,而无需添加大量状态布尔变量。那么有人可以指导我吗?
现在问题在于 if-else 构造。我找不到一种简单的方法来遍历 if -else 树并在节点之间正确建立边缘连接,而无需添加许多变量来包含状态信息,即 if 已启动或启动等。这是我的方法,但我发现深度调整很困难,即连接嵌套的 if-else 语句的叶子:
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 语句很有效,但之后我会恶化,我知道这是因为 globbal 变量 prvNode 一次只能 hld 一个节点,可能是一个列表 ll 做,但仍然可能会出现一些问题......任何人都可以改进它吗?
你可以做一个递归方法来做一个DFS遍历,例如 -
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
这只是一个想法,除了记住 2-D 平面上元素的布局外,您还需要跟踪深度。但是您应该先制定遍历算法,然后再拟合布局。