Need help with my project

2008-02-17
2013-05-29
1 2 > >> (Page 1 of 2)
  • Harvinder Singh

    Harvinder Singh - 2008-02-17

    Hello everyone,

    I have looked in most of the features from the sample graphs of the JUNG2 package and it is extremely useful stuff for me, but i don't know where to start. I am working on a project to develop an application that deals with the graphical representation of a process algebra. I have developed a small parser by myself to get the code about the model and stored them in linked lists. I have a list of all the vertices and also the edges I need to connect them with. However I do not know how can I use this data in the graph, I also want the user to see the labels for each pf the vertices and edges and this data is included in those linked lists I have.

    Not all of the vertices are connected, in most cases the data represents small seperate graphs that are not connected to each other. The aim is to develop an animator where I will generate a list of legal moves in the graph depended on the model I parsed. I want the the current vertex to have a different colour and also to highlight some special restricted edges, in essence I want to have control over the colors of edges and also the vertices. So that when the user clicks on any of the entry from my generated list, I want the chosent vertex to change colour to show the user that the state has moved forward. So in a way I want to control the colours of the edges and vertices with their ids, assuming there ids are equal to the index values of the lists I have and their labels with the entries in the lists.

    I wonder if it will be possible to have the first nodes of each of the small graphs to be saved in a list so that I can track and advance the transitions at each point. Therefore when I will have the list of a current node from each small graphs, I can generate the legal moves to the user and then I will update the list by myself reflecting the move made by the user.

    I know its a little too much to ask, I was just wondering how should I go about in using the correct libraries from JUNG2 I would realyy appreciate if some one could put me in the righ direction.

    So in summary:
    1.) Controlling (changing colours) individual edges and vertices through their id [corresponding to the index location of the lists I have the data in].
    2.) Generate a list of first nodes from each small graphs which are not connected to each other.

    Thanks in advance for your help.

     
    • Harvinder Singh

      Harvinder Singh - 2008-02-18

      I have managed to develop the graph with the vertices and edges labelled from my listed data. Here is the code:

      public class SimpleGraphFactory extends JApplet {

          Graph<String, Number> graph;

          VisualizationViewer<String,Number> vv;
         
          VertexLabelRenderer vertexLabelRenderer;
          EdgeLabelRenderer edgeLabelRenderer;
         
          ScalingControl scaler = new CrossoverScalingControl();
         
          DataSet dataSet = new DataSet();
         
          public SimpleGraphFactory(DataSet data) {
             
              this.dataSet = data;
             
              // create a simple graph for the demo
              graph = new SparseMultigraph<String, Number>();

              createVertices();
              createEdges();
             
              Layout<String,Number> layout = new KKLayout<String, Number>(graph);
             
              vv =  new VisualizationViewer<String, Number>(layout, new Dimension(850, 700));

              vertexLabelRenderer = vv.getRenderContext().getVertexLabelRenderer();
              edgeLabelRenderer = vv.getRenderContext().getEdgeLabelRenderer();
             
              Transformer<Number, String> edgeStringer = new Transformer<Number, String>(){
                  public String transform(Number e) {
                      int i = e.intValue();
                      return "(" + dataSet.nodes.get(i).label + ", " + dataSet.nodes.get(i).rate + ")";
                  }
              };
             
              vv.getRenderContext().setEdgeLabelTransformer(edgeStringer);
       
              Transformer<String, String> vertexStringer = new Transformer<String, String>(){
                  public String transform(String s) {
                      return s;
                  }
              };
             
              vv.getRenderContext().setVertexLabelTransformer(vertexStringer);
             
              vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.AUTO);
             
              // add a listener for ToolTips
              vv.setVertexToolTipTransformer(new ToStringLabeller<String>());
             
              vv.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer<String,Number>(vv.getPickedEdgeState(), Color.black, Color.cyan));
              vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<String>(vv.getPickedVertexState(), Color.red, Color.yellow));
              // add my listener for ToolTips
              vv.setVertexToolTipTransformer(new ToStringLabeller<String>());

              // create a frome to hold the graph
              final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
              Container content = getContentPane();
              content.add(panel);
             
              final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();
              vv.setGraphMouse(graphMouse);
              graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
           vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.QuadCurve<String,Number>());
          }
         
          private void createVertices() {
              for(int i = 0; i < dataSet.vertices.size(); i++){
                  graph.addVertex(dataSet.vertices.get(i));
              }
          }

          void createEdges() {
              for(int i = 0; i < dataSet.nodes.size(); i++){
                  graph.addEdge(i, dataSet.nodes.get(i).nodeA, dataSet.nodes.get(i).nodeB, EdgeType.DIRECTED);
              }
          }

          public static void main(String[] args) {
              JFrame frame = new JFrame();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              Container content = frame.getContentPane();
              content.add(new EdgeLabelDemo());
              frame.pack();
              frame.setVisible(true);
          }
         
      }

      Is it possible to change the colour of just one of the vertex? from with in the program. The graph holds numbers of small graphs that are not connected to each other, I was also wondering if it would be possible to get a starting vertex (id or string) from each of these small graphs. Also I would like to change colour of a vertex from all the available vertices in the graph, without the user interaction, because it is necessary for the animator to simulate some number of moves.

      Also wondering if it possible to provide a minimum distance between two connected vertices, this is to display the labels clearly for  the edges.

      I would really appreciate your help, thanks!

       
    • Harvinder Singh

      Harvinder Singh - 2008-02-18

      Just wondering if it possible to drag the edges (I am using quad curves) to get bigger curve, similar dragging like we do with the vertexes.

       
      • Joshua O'Madadhain

        It's possible in the sense that such functionality would be compatible with existing code, but we don't provide support for it.  You'd need to supply your own renderer (so that the control point would be changeable) and your own mouse control (so that it would know to look for control points near clicks).

        Joshua

         
        • Tom Nelson

          Tom Nelson - 2008-02-19

          Our EdgeLabelDemo has a slider that changes the control point of the curved edges.
          The control point is what controls how much curve the edge shap has.
          Some of that code my be useful. You'd still need to detect edge clicks and somehow
          translate mouse dragging into edge control point offset. It will be tricky because
          the edge shape is based on an edge at the origin, then it is stretched to span the endpoints,
          translated to one vertex, and rotated into position. All of that would have to be taken
          into consideration when you try to map mouse dragging to control-point offset.

          Tom Nelson

           
    • noone

      noone - 2008-02-19

      Hi,
      There's a new thread about coloring the vertexes with different colors which I think should be helpful:
      https://sourceforge.net/forum/message.php?msg_id=4783425

      As for your second question, you can set minimum distance between vertexes, for TreeLayout for example, in the constructor:
      TreeLayout(Forest<V,E> g, int distx, int disty)
      distx/disty is the Horizontal/Vertical distance between verteces. I could not find a better way to do it, and if you do find one please let me know :)
      I have not checked for different layouts, but I suppose something similar should be provided for them as well

      Good luck :)
      Eman

       
    • Harvinder Singh

      Harvinder Singh - 2008-02-19

      Thanks Joshua

      I think I will rather clear this away from my features list as it not that critical now. Everything is working great, and I am very happy that I have chosen JUNG for my project because it has so much to offer.

      As I mentioned above I have number of small graphs not connected to each other, I was wondering if there is a way of getting a list of a vertex (prefferable the first node) from each of these small graphs? It would be very helpful for my animation feature as I would have different colour of that vertex for the user as their current position.

      Thanks

       
      • Joshua O'Madadhain

        Harvinder:

        Glad JUNG is working for you.

        Not sure what you mean by "getting a list of vertices".  If you know which vertices these are, you can  make your own list.  If you don't, I'm not sure what method you want to use to identify them.  If you need to algorithmically identify the small graphs themselves, you can use WeakComponentClusterer.

        Joshua

         
    • Harvinder Singh

      Harvinder Singh - 2008-02-19

      Thanks Tom

      I did looked at the EdgeLabelDemo and found the solution for that, and now the edges are curved enough for the user to see the labels properly and so I am not going to do something about dragging edges stuff. Your information about it was very usefull though, thanks a bunch for that.

      Now that I have the colours of selected vertices to have different colours by this:

      From the class calling the VertexFillColor method as Eman mentioned:
          vv.getRenderContext().setVertexFillPaintTransformer(new VertexFillColor<String>( vv.getPickedVertexState() ));

      This is the method:
          public class VertexFillColor<V> implements Transformer<V,Paint> {
              protected PickedInfo<V> pi;

              public VertexFillColor(PickedInfo<V> pi){
                  this.pi = pi;
              }
             
              public Paint transform(V v){
                  Color color = Color.RED;
                  if (pi.isPicked(v)){
                      color =  Color.YELLOW; 
                  }
                  //current is the list of vertices, for the time being these are just inserted manually by me
                  else if(current.contains((Object) v)){
                      color = Color.GREEN;
                      //Increase the esdge stroke as well possibly over here.
                  }
                  return color;
              }
          }

      Joshua mentioned something about edge stroke transformer, but I am not sure how can I use it with in this or do I need to do something similar by writing another function that implements transformer may be.

      One more thing about the list of vertices, lets say I have a graph:

      (first graph)
      A -> B
      B -> C
      C -> B

      (another graph)
      D -> F
      F -> E

      As there is no connection between the first graph and the other one, I want to get the vertices such as "A" fromthe first graph and "D" from the other graph, and so on for the rest of the graphs. These will be stored in a list to be used by the colouring method to reflect the current position of the user with in each of these graphs.

      Hopefully this describes what I am after.

      Thanks a lot guys for the help.

       
      • Joshua O'Madadhain

        Harvinder:

        Yes, you would need to write another Transformer to change the edge stroke.  (Again, see PluggableRendererDemo for examples; we tried hard to make that source code readable for just this purpose.)

        As for your list of vertices: if all your graphs are ordered in this way, then you can build a set of the "first" vertex for each subgraph by scanning over the vertices and identifying those that have no predecessors (indegree 0).

        Joshua

         
    • Harvinder Singh

      Harvinder Singh - 2008-02-19

      Thanks a lot Joshua,

      The Predecessros of a vertex method is very usefull, I think I should have used a better example because not all of the graphs have a vertex with 0 Predecessros i.e.:

      (first graph)
      A -> B
      B -> C
      C -> B
      B -> A
      A -> A

      (another graph)
      D -> F
      F -> E
      F -> D

      and so on, so any ideas for this stuff?

      Thanks

       
      • Joshua O'Madadhain

        Harvinder:

        You have some criterion, I assume, which defines which vertex in each graph ought to be considered "first".  Whatever that criterion is, you can use it to define a list of "first vertices".  I can't be more specific or helpful than that because you haven't said anything about what your criterion is. 

        Joshua

         
    • Harvinder Singh

      Harvinder Singh - 2008-02-21

      Hi everyone,

      Thanks to you guys my project work is going very nicely. However I still have some things to take care of, I still could not find a solution for getting a vertex from each small graphs as described in my previous post. I am using KKLayout and each time it generates some sort of random layout, even this does not make a difference:

      //layout is a new KKLayout, and layoutSize is a dimension
      layout.setInitializer(new RandomLocationTransformer<String>(layoutSize,0));

      I wonder if it is possible to produce certain style of the layout all the time when exactly the same set of data for the graph is provided. As there are some styles within the random layout I like and some I don't, so thought if I could make it produce non random layout but with a certain style, hopefully it makes sence.

      I was able to control the distance between the vertices for better readability of the edge labels by this method implemented within the KKLayout class:

      setLengthFactor(double length_factor)

      so I called it from my class as layout.setLengthFactor(1.2); for reasonable distance.

      One more thing I would like to do with the layout is to align all the small graphs verticall or horizontally, and I cannot find any solution for that possibly because I might not be looking hard enough hehe.

      Thanks guys for the help, I lok forward for more ;-)

       
      • Joshua O'Madadhain

        Harvinder:

        RandomLocationTransformer initializes the vertex positions randomly (surprise!).  If you want a consistent layout, you can either get a layout that you like, save the vertex coordinates, and then use a StaticLayout based on those coordinates thereafter, or you can provide a different initializer.

        Joshua

         
    • Harvinder Singh

      Harvinder Singh - 2008-02-22

      Hello again guys, I looked in the highlighting a vertex from each of the small graphs. Here is what I did but I still need some help.

      1.) Get list of all the vertices in the graph
      2.) for each of the entry in the list check for its predecessors and successors and remove them from the list, here is the code.

              for(int i = 0; i < current.size(); i++){
                  Collection<String> pre = graph.getPredecessors(dataSet.vertices.get(i));
                  Collection<String> suc = graph.getSuccessors(dataSet.vertices.get(i));
                  for(Iterator<String> it = pre.iterator(); it.hasNext();){
                      String vertex = (String) it.next();
                      if(!current.get(i).equals(vertex))current.remove(vertex);
                  }
                  for(Iterator<String> it = suc.iterator(); it.hasNext();){
                      String vertex = (String) it.next();
                      if(!current.get(i).equals(vertex))current.remove(vertex);
                  }

              System.out.println(pre.size() + "\t:" + current.get(i) + "\tCount: " + graph.getPredecessorCount(current.get(i)));
                 
              }

      3.) Now this is working fine but it only deals with direct predecessors and sucessors of the node in question, the small graphs I have also have indirect predecessors and sucessors aswell, here is picture: http://s247.photobucket.com/albums/gg140/harvindersingh/?action=view&current=graph.jpg

      Any ideas how can I remove the remaining vertices from the graph i.e. either S1 or S3 from the picture, which ever comes later in the list of vertices.

      I was thinking in lines of getting all the indirect predecessors and sucessors together with the direct ones, if this is possible because vertices in different small graphs will have no connection as in the picture.

      Thanks

       
    • Harvinder Singh

      Harvinder Singh - 2008-02-22

      Another thought on finding these edges, if it is possible?

      I wonder if I could run the ShortestPathDemo behind the scenes to check a vertex has a path to another and remove the connected vertices, leaving me with only one vertex per small graph as the vertices in other small graphs will not be connected.

      However I have no idea how would I be able to use the ShortestPathDemo to do this and make it return the list of these individual vertices. I mean how would I check if a vertex has a path connected to another vertex i.e. given two vertices get true or false if the path exists or not.

      Although my actual system is working on a DirectedSparseMultiGraph, the ShortestPathDemo is running on undirected graph.

      Any ideas about this approach or should it something else.

      And yes there is not criteria from my end for getting these individual vertices from the small graphs, I am just getting the vertices depended upon its order of adding in the graph.

       
      • Joshua O'Madadhain

        Harvinder:

        If you're really selecting a vertex from each small graph according to the order in which it was added to the graph, then you're making this much, much harder than it needs to be.  Just associate an index with each vertex according to when it was inserted.  Then find the weakly connected components using WeakComponentClusterer.  Then go over each cluster and find the vertex with the lowest index.

        Joshua

         
    • Harvinder Singh

      Harvinder Singh - 2008-02-22

      I already have a list of vertices from which tha graph is being built, So how would I go about using this WeakComponentClusterer because I have no idea about this approach.

      Help is much appreciated.

      Thanks

       
      • Joshua O'Madadhain

        Harvinder:

        Please look at the Javadoc for this class and then let us know if you still have any questions.

        Joshua

         
    • Harvinder Singh

      Harvinder Singh - 2008-02-22

      Thanks Joshua,

      I have finally done it, well actually you have done it hehehe. It was rather simple, actually this is what I was trying to explane in the first place to get a callection set of all the subgraphs, here is what I did to get it:

              current = new LinkedList<String>();
              WeakComponentVertexClusterer<String, Number> weak = new WeakComponentVertexClusterer<String, Number>();
              Collection<Set<String>> test = weak.transform(graph);
              for(Iterator<Set<String>> it = test.iterator(); it.hasNext();test.iterator().next()){
                  String out = it.next().iterator().next();
                  current.add(out);
              }

      Brilliant.

      I am very gratefull of you Joshua, Thanks!!!

       
    • Harvinder Singh

      Harvinder Singh - 2008-02-22

      Just to clear up things, the previous method was getting a vertex from each from the subgraphs but not in the order as they were added. After some modification it now produces the correct solution.

              current = new LinkedList<String>();
              WeakComponentVertexClusterer<String, Number> weakClusterer = new WeakComponentVertexClusterer<String, Number>();
              Collection<Set<String>> clusters = weakClusterer.transform(graph);

              for(Iterator<Set<String>> subGraphs = clusters.iterator(); subGraphs.hasNext();){

                  Iterator<String> vertices = subGraphs.next().iterator();
                  String out = vertices.next();
                  int oldPosition = dataSet.vertices.indexOf((Object) out);
                 
                  while(vertices.hasNext()){
                      String newOut = vertices.next();
                      int newPosition = dataSet.vertices.indexOf((Object) newOut);

                      if(newPosition<oldPosition){
                          out = newOut;
                          oldPosition = newPosition;
                      }

                  }
                 
                  current.add(out);
                 
              }

      All thanks to Joshua for helping me to the solution. I am only posting this if anyone needs it later.

      Thanks

       
      • Joshua O'Madadhain

        Harvinder:

        Collections, in general, are not guaranteed to iterate over their elements in a predictable or consistent order.  (Exceptions include things like Lists and SortedSets.)  If you really want to identify the first element to have been added to a particular component ("first" meaning "first to have been added to the original graph"), then you'll want to use something like the solution I mentioned earlier.  The code above may give you different vertices each time you run it.

        Joshua

         
    • Harvinder Singh

      Harvinder Singh - 2008-02-22

      Johsua,

      I thought I did what you mentioned in your previous post, in terms of collections being unpredictable at the moment it is providing good enough solution each time the same vertices. I am not an expert in Java so dont have a lot of experience in it, I might just stick with it for the time being.

      At the moment I am trying to generate SVG's of the networks, unfortunately have no idea how to work around batik to this. The samples included in the Batik downloads are working fine but there is no example on sending Graphics2D objects to the SVGGenerator. I am completely lost in that.

      The project is about to be completed, only a number of features are left:

      1.) SVG and EPS Generator
      2.) Animator
      3.) Graph Alignments

      Is it possible to align all the subgraphs vertically or horizantally?

      Thanks

       
      • Joshua O'Madadhain

        Harvinder:

        The solution you mentioned last time is not what I proposed only in that indexOf() is not guaranteed to return the same answer from one invocation to the next (I think).  That's why I recommended defining your own index value based on vertex addition time.  But if that's working for you, that's what's important.

        I haven't done anything with generating SVGs from Graphics2D objects, so I can't help there.

        There's an EPS generator that we've used in the past called EPSDump; you can find it here:
        http://drzaius.ics.uci.edu/blogs/danyelf/archives/000100.html

        As for aligning the subgraphs: I would recommend putting each subgraph in its own VisualizationViewer; you can then organize the VVs with whatever Swing layout you like.

        Joshua

         
      • adver

        adver - 2008-02-23

        The call is :
        protected VisualizationViewer<MyNode, MyLink> vv;
        ...
        SaveAs.exec(vv, SaveAs.CODEC.SVG);

        /*class that writes PNG, JPG, GIF, SVG */
            protected static class SaveAs {
                private static final String svgNS = "http://www.w3.org/2000/svg";

                static enum CODEC {
                    PNG, JPG, GIF, SVG
                };

                /**
                 * SVG batik.
                 *
                 * @param myCanvas
                 * @param fileName
                 * @throws IOException
                 */
                private static void generateSVG(JPanel myCanvas, String fileName) {
                    try {
                        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                                new FileOutputStream(fileName), "UTF-8"));

                        DOMImplementation domImpl = GenericDOMImplementation
                                .getDOMImplementation();
                        org.w3c.dom.Document document = domImpl.createDocument(svgNS,
                                "svg", null);
                        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
                        svgGenerator.setSVGCanvasSize(new Dimension(
                                myCanvas.getWidth(), myCanvas.getHeight()));
                        myCanvas.paint(svgGenerator);
                        boolean useCSS = true;
                        svgGenerator.stream(out, useCSS);
                        out.flush();
                        out.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                /**
                 * PNG, JPG, GIF
                 *
                 * @param myCanvas
                 * @param fileName
                 * @param codec
                 */
                private static void encodeImage(JPanel myCanvas, String fileName,
                        CODEC codec) {
                    try {
                        BufferedImage bi = new BufferedImage(myCanvas.getWidth(),
                                myCanvas.getHeight(), BufferedImage.TYPE_INT_BGR);
                        Graphics2D graphics = bi.createGraphics();
                        // graphics.fillRect(0,0, width, height);
                        myCanvas.paint(graphics);
                        ImageIO.write(bi, codec.toString(), new File(fileName));
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                /**
                 *
                 *
                 * @param myCanvas
                 * @param codec
                 */
                public static void exec(JPanel myCanvas, CODEC codec) {
                    String cd = codec.toString().toLowerCase();
                    JFileChooser fc = new JFileChooser(".");
                    FileNameExtensionFilter filter = new FileNameExtensionFilter((cd
                            + "(*." + cd + ")"), cd);
                    fc.setFileFilter(filter);
                    int choice = fc.showSaveDialog(null);
                    if (choice == JFileChooser.APPROVE_OPTION) {
                        String fn = fc.getSelectedFile().getPath();
                        if (!fn.toLowerCase().endsWith("." + cd)) {
                            fn = fn + "." + cd;
                        }
                        if (codec == CODEC.SVG) {
                            generateSVG(myCanvas, fn);
                        } else {
                            encodeImage(myCanvas, fn, codec);
                        }
                        ;
                    }
                }
            }

         
1 2 > >> (Page 1 of 2)

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks