次のjung2
ビジュアライゼーションを作成しました。私のコードは、テキストをツリーに視覚化します。基本的に、一度に 1 つずつ、2 つの入力を使用しています。
/*
* INPUT 1
*/
String text = "=IF(A2=1;0;IF(D2=D3;IF(C2=1;TRUE;FALSE);4))";
/*
* INPUT 2
*/
String text = "=IF(A2=1;0;IF(D2=D3;IF(C2=1;TRUE;FALSE);TRUE))";
私の問題は、最初の入力が期待どおりに機能することですが、 の代わりにInput 2
最後の要素が異なる を使用すると、エラーが発生します。4
TRUE
Exception in thread "main" java.lang.IllegalArgumentException: Tree must not already contain child D2=D3
at edu.uci.ics.jung.graph.DelegateTree.addChild(DelegateTree.java:182)
at edu.uci.ics.jung.graph.DelegateTree.addEdge(DelegateTree.java:102)
at edu.uci.ics.jung.graph.DelegateTree.addEdge(DelegateTree.java:346)
at edu.uci.ics.jung.graph.util.TreeUtils.growSubTree(TreeUtils.java:76)
at edu.uci.ics.jung.graph.util.TreeUtils.growSubTree(TreeUtils.java:80)
at edu.uci.ics.jung.graph.DelegateForest.getTrees(DelegateForest.java:295)
at edu.uci.ics.jung.graph.util.TreeUtils.getRoots(TreeUtils.java:34)
at edu.uci.ics.jung.algorithms.layout.TreeLayout.buildTree(TreeLayout.java:102)
at edu.uci.ics.jung.algorithms.layout.TreeLayout.<init>(TreeLayout.java:97)
at edu.uci.ics.jung.algorithms.layout.TreeLayout.<init>(TreeLayout.java:75)
at justDelete.me.codeTestingPackage.Test.<init>(Test.java:131)
at justDelete.me.codeTestingPackage.Test.main(Test.java:396)
紛らわしいのは、文字列が正しく解析され、次のようにプログラムで正しく使用できることです。
[IF, A2=1, 0, IF, D2=D3, IF, C2=1, TRUE, FALSE, TRUE]
input 2
コメントで(「壊れた」入力)で実行可能な私のプログラムのバージョンは次のとおりです。
@SuppressWarnings("serial")
public class Test extends JApplet {
/**
* the graph
*/
Forest<String,Integer> graph;
private static final Logger log = Logger.getLogger(Test.class);
Factory<DirectedGraph<String,Integer>> graphFactory =
new Factory<DirectedGraph<String,Integer>>() {
public DirectedGraph<String, Integer> create() {
return new DirectedSparseMultigraph<String,Integer>();
}
};
Factory<Tree<String,Integer>> treeFactory =
new Factory<Tree<String,Integer>> () {
public Tree<String, Integer> create() {
return new DelegateTree<String,Integer>(graphFactory);
}
};
Factory<Integer> edgeFactory = new Factory<Integer>() {
int i=0;
public Integer create() {
return i++;
}};
Factory<String> vertexFactory = new Factory<String>() {
int i=0;
public String create() {
return "V"+i++;
}};
/**
* the visual component and renderer for the graph
*/
VisualizationViewer<String,Integer> vv;
VisualizationServer.Paintable rings;
String root;
TreeLayout<String,Integer> layout;
@SuppressWarnings("unchecked")
FRLayout layout1;
TreeCollapser collapser;
RadialTreeLayout<String,Integer> radialLayout;
@SuppressWarnings("unchecked")
public Test() {
// create a simple graph for the demo
graph = new DelegateForest<String,Integer>();
createTree();
layout = new TreeLayout<String,Integer>(graph);
collapser = new TreeCollapser();
radialLayout = new RadialTreeLayout<String,Integer>(graph);
radialLayout.setSize(new Dimension(600,600));
vv = new VisualizationViewer<String,Integer>(layout, new Dimension(600,600));
vv.setBackground(Color.white);
vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setVertexShapeTransformer(new ClusterVertexShapeFunction());
// add a listener for ToolTips
vv.setVertexToolTipTransformer(new ToStringLabeller());
vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray));
rings = new Rings();
Container content = getContentPane();
final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
content.add(panel);
final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();
vv.setGraphMouse(graphMouse);
JComboBox modeBox = graphMouse.getModeComboBox();
modeBox.addItemListener(graphMouse.getModeListener());
graphMouse.setMode(ModalGraphMouse.Mode.TRANSFORMING);
final ScalingControl scaler = new CrossoverScalingControl();
JButton plus = new JButton("+");
plus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scaler.scale(vv, 1.1f, vv.getCenter());
}
});
JButton minus = new JButton("-");
minus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scaler.scale(vv, 1/1.1f, vv.getCenter());
}
});
JToggleButton radial = new JToggleButton("Radial");
radial.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
// layout.setRadial(true);
vv.setGraphLayout(radialLayout);
vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
vv.addPreRenderPaintable(rings);
} else {
// layout.setRadial(false);
vv.setGraphLayout(layout);
vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
vv.removePreRenderPaintable(rings);
}
vv.repaint();
}});
JButton collapse = new JButton("Collapse");
collapse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Collection picked =new HashSet(vv.getPickedVertexState().getPicked());
if(picked.size() == 1) {
Object root = picked.iterator().next();
Forest inGraph = (Forest)layout.getGraph();
try {
collapser.collapse(vv.getGraphLayout(), inGraph, root);
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
vv.getPickedVertexState().clear();
vv.repaint();
}
}});
JButton expand = new JButton("Expand");
expand.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Collection picked = vv.getPickedVertexState().getPicked();
for(Object v : picked) {
if(v instanceof Forest) {
Forest inGraph = (Forest)layout.getGraph();
collapser.expand(inGraph, (Forest)v);
}
vv.getPickedVertexState().clear();
vv.repaint();
}
}});
JPanel scaleGrid = new JPanel(new GridLayout(1,0));
scaleGrid.setBorder(BorderFactory.createTitledBorder("Zoom"));
JPanel controls = new JPanel();
scaleGrid.add(plus);
scaleGrid.add(minus);
controls.add(radial);
controls.add(scaleGrid);
controls.add(modeBox);
controls.add(collapse);
controls.add(expand);
content.add(controls, BorderLayout.SOUTH);
}
class Rings implements VisualizationServer.Paintable {
Collection<Double> depths;
public Rings() {
depths = getDepths();
}
private Collection<Double> getDepths() {
Set<Double> depths = new HashSet<Double>();
Map<String,PolarPoint> polarLocations = radialLayout.getPolarLocations();
for(String v : graph.getVertices()) {
PolarPoint pp = polarLocations.get(v);
depths.add(pp.getRadius());
}
return depths;
}
public void paint(Graphics g) {
g.setColor(Color.lightGray);
Graphics2D g2d = (Graphics2D)g;
Point2D center = radialLayout.getCenter();
Ellipse2D ellipse = new Ellipse2D.Double();
for(double d : depths) {
ellipse.setFrameFromDiagonal(center.getX()-d, center.getY()-d,
center.getX()+d, center.getY()+d);
Shape shape = vv.getRenderContext().
getMultiLayerTransformer().getTransformer(Layer.LAYOUT).transform(ellipse);
g2d.draw(shape);
}
}
public boolean useTransform() {
return true;
}
}
/**
* create Tree
*/
private void createTree() {
/*
* INPUT 1
*/
// String text = "=IF(A2=1;0;IF(D2=D3;IF(C2=1;TRUE;FALSE);4))";
/*
* INPUT 2
*/
String text = "=IF(A2=1;0;IF(D2=D3;IF(C2=1;TRUE;FALSE);TRUE))";
text.toUpperCase();
//START
String[] operands = text.substring(1, text.length()).split("[;()]+");
System.out.println(Arrays.toString(operands));
int numIfs = operands.length / 3; // actually (operands.length - 1) / 3 but int division makes it the same
String[] nodes = new String[numIfs]; // stores the nodes (test strings)
int[] operandNos = new int[numIfs]; // stores the number of operands the if currently has
int nodesIndex = -1; // the index of the if node currently parsed
for (String s : operands) {
if (s.equals("IF")) {
// new if found -> increase position in the "stack" (nodes)
operandNos[++nodesIndex] = 0;
} else {
// addVertex(s);
graph.addVertex(s);
switch (operandNos[nodesIndex]++) {
case 0:
// first operand = node name
nodes[nodesIndex] = s;
break;
case 1:
// second operand found -> add edge
graph.addEdge(edgeFactory.create(), s, nodes[nodesIndex]);
break;
case 2:
// last operand found -> add edge and go back
do {
graph.addEdge(edgeFactory.create(), s, nodes[nodesIndex]);
s = nodes[nodesIndex--];
} while (nodesIndex >= 0 && operandNos[nodesIndex]++ == 2);
if (nodesIndex >= 0) {
// was not the last operand of the IF
graph.addEdge(edgeFactory.create(), s, nodes[nodesIndex]);
}
}
}
}
//END
}
class ClusterVertexShapeFunction<V> extends EllipseVertexShapeTransformer<V>
{
ClusterVertexShapeFunction() {
setSizeTransformer(new ClusterVertexSizeFunction<V>(20));
}
@SuppressWarnings("unchecked")
@Override
public Shape transform(V v) {
if(v instanceof Graph) {
int size = ((Graph)v).getVertexCount();
if (size < 8) {
int sides = Math.max(size, 3);
return factory.getRegularPolygon(v, sides);
}
else {
return factory.getRegularStar(v, size);
}
}
return super.transform(v);
}
}
/**
* A demo class that will make vertices larger if they represent
* a collapsed collection of original vertices
* @author Tom Nelson
*
* @param <V>
*/
class ClusterVertexSizeFunction<V> implements Transformer<V,Integer> {
int size;
public ClusterVertexSizeFunction(Integer size) {
this.size = size;
}
public Integer transform(V v) {
if(v instanceof Graph) {
return 30;
}
return size;
}
}
/**
* a driver for this demo
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
Container content = frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content.add(new Test());
frame.pack();
frame.setVisible(true);
}
}
入力 2 でコードを実行すると、投稿されたエラーが表示されます。これが当てはまる理由はありますか?木の葉は固有のものでなければならないので、これはちょっとばかげているかもしれませんが、弦は同じだと思います。したがって、この問題に対処するにはどうすればよいでしょうか。
あなたの答えに感謝します!