import org.jgrapht.*;
import org.jgrapht.graph.*;
public class Example {
// Cut down version of Job class
private static class Job {
private final int jobNumber;
private int jobTime;
Job(int jobNumber){
this.jobNumber = jobNumber;
this.jobTime = 9999; // A dummy value to make my issue obvious
}
public int getJobNumber(){
return jobNumber;
}
public int getJobTime(){
return jobTime;
}
public void setJobTime(int jobTime){
this.jobTime=jobTime;
}
// eclipse derived
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + jobNumber ;
return result;
}
// eclipse derived
@Override
public boolean equals(Object obj) {
if (this ==obj) return true;
if (obj==null) return false;
if (getClass() != obj.getClass()) return false;
Job other = (Job) obj;
if (jobNumber != other.jobNumber) return false;
return true;
}
@Override
public String toString() {
return Integer.toString(System.identityHashCode(this));
}
}
public static void main(String[] args) {
// Create a graph
final DirectedGraph<Job, DefaultEdge> schedule = new DefaultDirectedGraph<Job,DefaultEdge>(DefaultEdge.class);
// Create some jobs and add to graph
// Job 2 depends on Job 1 depends on Job 0
Job job;
Job dependsOnJob;
int counter=1;
while (counter < 3) {
int jobNumber = counter;
int dependsOnJobNumber = counter - 1;
job = new Job(jobNumber);
dependsOnJob = new Job(dependsOnJobNumber);
schedule.addVertex(job);
schedule.addVertex(dependsOnJob);
schedule.addEdge(dependsOnJob,job);
counter++;
}
// Print the current values of job numbers and times
// 1.
for (Job j : schedule.vertexSet()){
System.out.println("Create Schedule View Jobs " + j + " " + j.getJobNumber() + " " + j.getJobTime());
}
// 2.
for (Job j : schedule.vertexSet()){
for (DefaultEdge e : schedule.incomingEdgesOf(j)){
Job source = schedule.getEdgeSource(e);
System.out.println("Create Schedule View Prior Job " + source + " " + source.getJobNumber() + " " + source.getJobTime());
}
}
// Change all job times to 1111
for (Job j : schedule.vertexSet()){
System.out.println("Setting " + j);
j.setJobTime(1111);
}
// Print the new values of job numbers and times
// 3.
for (Job j : schedule.vertexSet()){
System.out.println("Added Times View Jobs " + j + " " + j.getJobNumber() + " " + j.getJobTime());
}
// 4.
for (Job j : schedule.vertexSet()){
for (DefaultEdge e : schedule.incomingEdgesOf(j)){
Job source = schedule.getEdgeSource(e);
System.out.println("Added Times View Prior Job " + source + " " + source.getJobNumber() + " " + source.getJobTime());
}
}
}
}
Output
Create Schedule View Jobs Example$Job@20 1 9999
Create Schedule View Jobs Example$Job@1f 0 9999
Create Schedule View Jobs Example$Job@21 2 9999
Create Schedule View Prior Job Example$Job@1f 0 9999
Create Schedule View Prior Job Example$Job@20 1 9999
Setting Example$Job@20
Setting Example$Job@1f
Setting Example$Job@21
Added Times View Jobs Example$Job@20 1 1111
Added Times View Jobs Example$Job@1f 0 1111
Added Times View Jobs Example$Job@21 2 1111
Added Times View Prior Job Example$Job@1f 0 1111
Added Times View Prior Job Example$Job@20 1 9999
In step 4, not all the values for job times show as having been changed from 9999 to 1111, despite step 3 showing that they have. I'm talking about the very last row of the above output. Could someone help me understand what is going on? This is driving me crazy!
Apologies if this code doesn't immediately run - I've manually copied it across from a system not connected to the outside world
With the overridden toString:
Create Schedule View Jobs 225534817 1 9999
Create Schedule View Jobs 1878246837 0 9999
Create Schedule View Jobs 929338653 2 9999
Create Schedule View Prior Job 1878246837 0 9999
Create Schedule View Prior Job 1259475182 1 9999
Setting 225534817
Setting 1878246837
Setting 929338653
Added Times View Jobs 225534817 1 1111
Added Times View Jobs 1878246837 0 1111
Added Times View Jobs 929338653 2 1111
Added Times View Prior Job 1878246837 0 1111
Added Times View Prior Job 1259475182 1 9999
I worked out what I was doing wrong.
It was to do with how I was adding vertices and edges to the graph and how I had overridden hashCode() and equals().
addVertex(Job) and addEdge(Job, Job) add an object to the graph only if the graph does not already contain it. 'Contain' uses equals().
For Job1 depends on Job0, the graph contained neither of these Jobs and therefore both were added, along with an edge between them as I had intended.
For Job2 depends on Job1, the graph already contained an 'old' Job1 object equal to the 'new' Job1 object. I have two Job1 objects at this point, which are equal() to each other but separate objects. The edge I created was between Job2 and the 'new' Job1 object, which isn't actually in the graph!