GEF5 Node building [message #1767070] |
Fri, 30 June 2017 11:18 |
|
Im trying to make such scenario:
1.User enters a sentence in UI and presses a button
2.Sentence is converted to tokens
tokens = str.split(delims);
3.Graph is being created
static Graph createGraph() {
Builder b = new Graph.Builder();
addNodes(b,1);
Graph g = b.build();
System.out.println(g);
return g;
} addNodes method:
private static void addNodes(Graph.Builder graphBuilder,int startNumber) {
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < tokens.length; i++) {
nodes.add(new Node.Builder().attr(tokens[i], (startNumber + i)).buildNode());
}
graphBuilder.nodes(nodes);
}
4. Graph set to Iviewer
viewer.getContents().setAll(Collections.singletonList(graph));
It does work, however:
1.node names are not displayed ( how to add token as a node name?)
2.all nodes are overlapping eachother ( layoutAlgorithm is missing i guess)
Any ways of correcting my code? Maybe there is other way to build nodes of a graph when sentence size is variable?
UPD: Fixed the overlapping with adding LayoutAlgorithm
.attr(ZestProperties.LAYOUT_ALGORITHM__G,new HorizontalShiftAlgorithm());
UPD 2:
Fixed names using simplier builder for nodes
static Graph createGraph() {
Builder b = new Graph.Builder()
.attr(ZestProperties.LAYOUT_ALGORITHM__G,new HorizontalShiftAlgorithm());
for (int i = 0; i < tokens.length; i++) {
b.node(tokens[i]).attr(LABEL, tokens[i]);
}
Graph g = b.build();
System.out.println(g);
return g;
}
Case closed :P
[Updated on: Fri, 30 June 2017 12:23] Report message to a moderator
|
|
|
Re: GEF5 Node building [message #1767076 is a reply to message #1767070] |
Fri, 30 June 2017 12:23 |
|
Hi Mike,
1) You need to set the correct attribute when creating the nodes, for the label it is ZestProperties#LABEL__NE.
2) You can specify the layout algorithm via the ZestProperties#LAYOUT_ALGORITHM__G attribute.
Simple example: public Graph buildForSentence(String sentence) {
Builder b = new Graph.Builder();
b.attr(ZestProperties.LAYOUT_ALGORITHM__G, new GridLayoutAlgorithm());
String[] words = sentence.split(" ");
for (int i = 0; i < words.length; i++) {
b.node(i).attr(ZestProperties.LABEL__NE, words[i]);
if (i > 0) {
b.edge(i - 1, i).attr(ZestProperties.TARGET_DECORATION__E, new Polygon(-10, 0, 0, 5, 0, -5));
}
}
return b.build();
}
@Override
protected Graph createGraph() {
String sentence = "The quick brown fox jumps over the lazy dog.";
return buildForSentence(sentence);
}
Note that you can assign a key to the nodes during construction, to which you can refer to add edges or attributes later.
Best regards,
Matthias
|
|
|
|
Powered by
FUDForum. Page generated in 0.03876 seconds