好的,我知道这是一个模糊的问题,但我似乎在这里坚持逻辑......我想创建输入程序的流程图。我两天以来一直在思考这个问题并且不能得到最好的一般方法......所以我绝望地看着你帮我在这里帮助我....可能有一些我想念的东西...... / p>
我有一个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,它绘制一个矩形框并连接它与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语句有效,但是在我明白这是因为全局变量prvNode一次只能有一个节点可能只是一个节点但是仍然会出现一些问题...可以任何一个改进吗?
答案 0 :(得分:0)
您可以执行递归方法来执行A 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
这只是一个想法,除了要记住二维平面上元素的布局外,还需要跟踪深度。但是你应该首先训练遍历算法,然后再适应布局。