Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Find Nodes in a Graph
Find Nodes in a Graph [message #558190] Fri, 10 September 2010 13:31 Go to next message
ouri.maler is currently offline ouri.malerFriend
Messages: 22
Registered: July 2010
Junior Member
I am currently using Zest to create a graphic representation of a model as part of a GUI I'm making. It creates a Graph object by doing an in-depth search of the model.

public void makeGraph(DNode node, GraphNode father)  //the DNode class is dependent on the project I'm working on, not Zest itself.
{
  GraphNode newNode = new GraphNode(graph, SWT.NONE, node.getNodeName();
  if(father!=null)
    new GraphConnection(graph, SWT.NONE, newNode, father);
  /* some actions irrelevant to the matter at hand */
  for(DNode comp: node.children())
    makeGraph(comp, newNode);
}


Unfortunately, the search method will often find the same node twice; to avoid creating duplicates, I need a way to check if the node already exists within the graph.

My first idea was to do something like this:

public void makeGraph(DNode node, GraphNode father)
{
  if(graph.getNodes().contains(node.getNodeName())
    /* the node already exists; just add a connection to it. */
  else
    /* New node; create it, connect to it, call makeGraph() for its children. */


Unfortunately, that didn't work either. I'm not sure why, but the "if" was never answered positively, and I still ended up with a lot of duplicates.
So I'm left wondering...How does one actually check if a node exists in a Graph?
Re: Find Nodes in a Graph [message #558225 is a reply to message #558190] Fri, 10 September 2010 15:23 Go to previous messageGo to next message
Fabian Steeg is currently offline Fabian SteegFriend
Messages: 76
Registered: July 2009
Member
Since the collection returned by getNodes contains the actual nodes and not their names, testing for containment would basically work like this (if your node was a Zest GraphNode):
if (graph.getNodes().contains(node))

If I understand you correctly, you have a different type here that is not a GraphNode. You could search the nodes yourself:
boolean contains(List<GraphNode> nodes, String name) {
  for (GraphNode graphNode : nodes) {
    if (graphNode.getText().equals(name)) {
      return true;
    }
  }
  return false;
}

And call it like this:
if (contains(graph.getNodes(), node.getNodeName()))
Re: Find Nodes in a Graph [message #558542 is a reply to message #558225] Mon, 13 September 2010 12:40 Go to previous messageGo to next message
ouri.maler is currently offline ouri.malerFriend
Messages: 22
Registered: July 2010
Junior Member
Thanks a lot - that worked like a charm. I also used a similar nodeNumber() method to obtain the node's position within the List.

Still having different issues, though - namely, when I create the graph, all the nodes are cluttered at the upper left corner of the display. I wonder if this is somehow caused by the vast number of nodes...? (Since this problem didn't occur when I created an example graph with only four nodes.)
Re: Find Nodes in a Graph [message #558774 is a reply to message #558542] Tue, 14 September 2010 11:44 Go to previous messageGo to next message
Fabian Steeg is currently offline Fabian SteegFriend
Messages: 76
Registered: July 2009
Member
Not sure what might be causing this, but you could try some different layouts, see http://www.vogella.de/articles/EclipseZest/article.html#layo utmanager
Re: Find Nodes in a Graph [message #559050 is a reply to message #558774] Wed, 15 September 2010 12:56 Go to previous messageGo to next message
ouri.maler is currently offline ouri.malerFriend
Messages: 22
Registered: July 2010
Junior Member
Fabian Steeg wrote on Tue, 14 September 2010 07:44
Not sure what might be causing this, but you could try some different layouts, see http://www.vogella.de/articles/EclipseZest/article.html#layo utmanager


Didn't help, I'm afraid...Tried Tree, HorizontalTree, Radial, Grid, and SpringLayoutAlgorithm, but with all of them, I get all the nodes clumped in the upper left corner until I manually move them from it.
Re: Find Nodes in a Graph [message #559620 is a reply to message #558190] Fri, 17 September 2010 12:11 Go to previous messageGo to next message
ouri.maler is currently offline ouri.malerFriend
Messages: 22
Registered: July 2010
Junior Member
OK, found the problem by accident while adding comments to the program: Because of modifications I had made to the program's structure, graph.appLayout() got called before I created the nodes.
Corrected now. Thanks for the help - I probably wouldn't have noticed it if you hadn't brought the layout thing to my attention.
Re: Find Nodes in a Graph [message #559641 is a reply to message #559620] Fri, 17 September 2010 13:36 Go to previous message
Fabian Steeg is currently offline Fabian SteegFriend
Messages: 76
Registered: July 2009
Member
No Real Name wrote on Fri, 17 September 2010 14:11
OK, found the problem by accident while adding comments to the program: Because of modifications I had made to the program's structure, graph.appLayout() got called before I created the nodes.


Great - and thanks for telling your solution!
Previous Topic:GraphicsToGraphics2DAdaptor drawTextLayout renders as image. Help?
Next Topic:Zoom funtion in a View?
Goto Forum:
  


Current Time: Thu Mar 28 10:51:08 GMT 2024

Powered by FUDForum. Page generated in 0.05252 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top