5 Replies - 2395 Views - Last Post: 14 February 2011 - 11:26 PM Rate Topic: -----

#1 cwleung  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-February 11

JUNG loading graph question

Posted 14 February 2011 - 03:18 AM

i get this error when i try to load the graph from the text file

java.io.IOException: specified 'source' attribute "A" does not match any node ID
        at edu.uci.ics.jung.io.GraphMLReader.parse(GraphMLReader.java:246)
        at edu.uci.ics.jung.io.GraphMLReader.load(GraphMLReader.java:192)
        at hyperGraph.loadGraph(hyperGraph.java:189)
        at hyperGraph.main(hyperGraph.java:233)


here is my loading graph code
class VertexM {
        String name = "";


        public VertexM() {
        }

        public VertexM(String name) {
                this.name = name;
        }

        public String toString() { 
                return name; 
        }

        public boolean equals(Object obj) {
                if (obj == this) {
                        return true;
                }
                
                if (obj == null) {
                        return false;
                }
                VertexM other = (VertexM) obj;

                boolean a = this.getName().equals(other.getName());
                return a;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getName() {
                return name;
        }

        public int getCount() {
                return 1;
        }

        public int hashCode() {
                return name.hashCode();
        }
}


class EdgeM {
        float count;
        String id = "";

        public EdgeM() {
                this.count = 0;
        }

        public EdgeM(String d, String v1, String v2) {
                this.id = v1 + v2;
                this.count = Integer.parseInt(d);
        }

        public String toString() { // Always good for debugging
                return id;
        }

        public void addCount() {
                count++;
        }

        public void setId(String id) {
                this.id = id;
        }

        public float getCount() {
                return count;
        }

        public boolean equals(Object obj) {
                if (obj == this) {
                        return true;
                }
)
                if (obj == null) {
                        return false;
                }
                EdgeM other = (EdgeM) obj;

                boolean a = this.toString().equals(other.toString());
                return a;
        }

        @Override
        public int hashCode() {
                int result = Math.round(count) * 37 + id.hashCode();
                return result;
        }
}


public static UndirectedSparseGraph<VertexM, EdgeM> loadGraph(Reader reader) {
                class EdgeMFactory implements Factory<EdgeM> {

                        @Override
                        public EdgeM create() {
                                return new EdgeM();
                        }

                }

                class VertexMFactory implements Factory<VertexM> {
                        public VertexM create() {
                                return new VertexM();
                        }

                }
                GraphMLReader<UndirectedSparseGraph<VertexM, EdgeM>, VertexM, EdgeM> graphReader =
null;
                try {
                        graphReader = new GraphMLReader<UndirectedSparseGraph<VertexM, EdgeM>, VertexM,
EdgeM>(
                                        new VertexMFactory(), new EdgeMFactory());
                } catch (ParserConfigurationException e1) {
                        e1.printStackTrace();
                } catch (SAXException e1) {
                        e1.printStackTrace();
                }

                UndirectedSparseGraph<VertexM, EdgeM> fixGraph = new
UndirectedSparseGraph<VertexM, EdgeM>();
                try {
                        graphReader.load(reader, fixGraph);
                        Map<String, GraphMLMetadata<VertexM>> nodeMetaData = graphReader
                                        .getVertexMetadata();

                        for (VertexM node : fixGraph.getVertices()) {
                                String nodeName = ((String)
(nodeMetaData.get("NodeName").transformer.transform(node)));
                                System.out.println("NODE" + nodeName);

                                node.setName(nodeName);
                        }

                } catch (IOException e) {
                        e.printStackTrace();
                }

                return fixGraph;
        }

        /**
         * @param args
         * @throws SAXException
         * @throws ParserConfigurationException
         */
        public static void main(String[] args) {
                
                  try { FileReader in = new FileReader(
                  "C:\\Program Files\\Eclipse for FYP\\output\\test.txt"); 
                  Hypergraph<VertexM, EdgeM> hg = loadGraph(in);
                  System.out.println(hg.getVertices()); } catch
                  (ArrayIndexOutOfBoundsException e) {
                  
                  System.out.println("Usage: java ReadFile filename\n");
                  
                  } catch (IOException e) { e.printStackTrace(); }
}



my save file is
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns/graphml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns/graphml">
<key id="NodeName" for="node">
<desc>File Name</desc>
<default></default>
</key>
<key id="EdgeName" for="edge">
<desc>Edge Visit Name</desc>
<default></default>
</key>
<key id="EdgeCount" for="edge">
<desc>Edge Visit Count</desc>
<default></default>
</key>
<graph edgedefault="undirected">
<node id="A">
<data key="NodeName">A</data>
</node>
<node id="B">
<data key="NodeName">B</data>
</node>
<node id="C">
<data key="NodeName">C</data>
</node>
<edge source="A" target="C">
<data key="EdgeName">AC</data>
<data key="EdgeCount">3.0</data>
</edge>
<edge source="A" target="B">
<data key="EdgeName">AB</data>
<data key="EdgeCount">2.0</data>
</edge>
</graph>
</graphml>



is there any problem for my code?

Edited by Dogstopper: :code:

Is This A Good Question/Topic? 0
  • +

Replies To: JUNG loading graph question

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2873
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: JUNG loading graph question

Posted 14 February 2011 - 03:42 AM

I think your program is trowing an exception because of this XML:
<node id="A">



I don't know how your parser is implemented (as you did not include that code), but it is causing an issue.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10575
  • View blog
  • Posts: 39,151
  • Joined: 27-December 08

Re: JUNG loading graph question

Posted 14 February 2011 - 07:19 AM

In your equals() methods, you'll run into problems if the passed params aren't of the corresponding classes. For example, if you pass a String in the VertexM equals() method, you'll get a ClassCastException b/c ("someString" instanceof VertexM) == false.
Was This Post Helpful? 1
  • +
  • -

#4 cwleung  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-February 11

Re: JUNG loading graph question

Posted 14 February 2011 - 09:01 AM

View Postmacosxnerd101, on 14 February 2011 - 07:19 AM, said:

In your equals() methods, you'll run into problems if the passed params aren't of the corresponding classes. For example, if you pass a String in the VertexM equals() method, you'll get a ClassCastException b/c ("someString" instanceof VertexM) == false.



thank you for your help.
However, if i did not use the equal() method. i cannot use the method--.containsVertex()
because i need to do like this(g.containVertex(new Vertex("A"))
for the "A", i have the for-loop to get the variable so i must need to new the Vertex again and it will cause the problem that i need to override the equal.
How can i do this?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10575
  • View blog
  • Posts: 39,151
  • Joined: 27-December 08

Re: JUNG loading graph question

Posted 14 February 2011 - 09:08 AM

I'm not saying don't use equals(). Add in an instanceof check though in your respective equals() methods to make sure the appropriate type.

Example in VertexM:
if(!(obj instanceof VertexM)) return false;


Was This Post Helpful? 1
  • +
  • -

#6 cwleung  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-February 11

Re: JUNG loading graph question

Posted 14 February 2011 - 11:26 PM

View Postmacosxnerd101, on 14 February 2011 - 09:08 AM, said:

I'm not saying don't use equals(). Add in an instanceof check though in your respective equals() methods to make sure the appropriate type.

Example in VertexM:
if(!(obj instanceof VertexM)) return false;


i have fixed my problem, thsnk you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1