Convertir un SingleGraph à un Graph

Résolu
Aaa_dh Messages postés 10 Date d'inscription   Statut Membre Dernière intervention   -  
Aaa_dh Messages postés 10 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour,
J'ai construit un graphe avec la bibliothèque GraphStream( SingleGraph) et je voudrais utiliser ce meme graphe comme input pour l'algorithme HITS, cependant cet algo prend en paramètre un "Graph" et non pas un "SingleGraph". Est -il possible de faire une conversion d'un SingleGraph à un Graph?
c'est tres urgent SVP. Est ce que qlq peut m'aider.
Merci d'avance.
A voir également:

1 réponse

KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Bonjour,

La classe SingleGraph implémente l'interface Graph, donc tu peux donner n'importe quelle implémentation de graphe à cette méthode...

Graph g = new SingleGraph("");
0
Aaa_dh Messages postés 10 Date d'inscription   Statut Membre Dernière intervention  
 
Bonjour KX;
Meme en essayant votre proposition ça marche pas!!!!
moi j'ai déja construit le graphe avec la biblio GraphStream et j'ai ça:
SingleGraph graph=FileHelper.createGraph("Graphe", adjacencyMatrixboolean);
for (org.graphstream.graph.Node node : graph.getNodeSet())
node.addAttribute("ui.label", node.getId());
for (Edge edge : graph.getEdgeSet())
edge.addAttribute("ui.label", edge.getId());
graph.display();

Ce même graphe la je voudrais l'introduire comme input à l'algo HITS de JUNG.
J'utilise ça:
HITS ranker = new HITS((edu.uci.ics.jung.graph.Graph)graph);
ranker.evaluate();
ranker.printRankings(true,false);

mais eclipse m'affiche ça:
Exception in thread "main" java.lang.ClassCastException: org.graphstream.graph.implementations.SingleGraph cannot be cast to com.sun.corba.se.impl.orbutil.graph.Graph
at com.tweets.test.Main.main(Main.java:69)

je sais très bien qu'il y a une erreur de cast mais je ne sais pas comment la résoudre.
Merci d'avance pour votre aide.
PS: le code de l'algo HITS de JUNG est dispo sur ce lien:
http://logic.cse.unt.edu/tarau/teaching/GraphTheory/jung/OLD/jung/src/edu/uci/ics/jung/algorithms/importance/HITS.java


C'est tres urgent SVP est ce que vous pouvez m'aider?
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Je n'avais pas compris que HITS était lié à une deuxième librairie JUNG, moi je parlais de Graph et SingleGraph de GraphStream...

Du coup je ne sais pas, mais je doute que l'on puisse facilement passer de GraphStream à JUNG directement.

GraphStream permet de lire des graphes depuis différents formats, il faudrait voir lesquels JUNG supporte et en choisir un commun.

https://graphstream-project.org/doc/Tutorials/Reading-files-using-FileSource/
0
Aaa_dh Messages postés 10 Date d'inscription   Statut Membre Dernière intervention   > KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention  
 
Meme en assayant le tuyau qyu vous m'aviez fourni ça ne marche pas. J'ai beau cherché sur internet mais je ne trouve aucune solution. Je bloque la dessus et je ne peux plus avancer dans le projet.
J'ai essayé ça:

String filePath = "D:\\SavingGraph.DGS";
//Graph g = new DefaultGraph("g");
FileSource fs = FileSourceFactory.sourceFor(filePath);

fs.addSink(graph);

try {
fs.begin(filePath);

while (fs.nextEvents()) {
// Optionally some code here ...
}
} catch( IOException e) {
e.printStackTrace();
}

try {
fs.end();
} catch( IOException e) {
e.printStackTrace();
} finally {
fs.removeSink(graph);
}

mais cette erreur s'affiche:
Exception in thread "main" java.io.IOException: not a regular file 'D:\SavingGraph.DGS'
at org.graphstream.stream.file.FileSourceFactory.sourceFor(FileSourceFactory.java:83)
at com.tweets.test.Main.main(Main.java:77)
0
Aaa_dh Messages postés 10 Date d'inscription   Statut Membre Dernière intervention  
 
Bonjour;
En cherchant sur le net, j'ai trouvé que JUNG est plutôt manipulable plutôt a travers GraphML et non pas GraphStream, cependant moi j'ai déja crée mon graphe avec GraphSTream. Je crois que je dois plutôt changer l'étape de la création de mon graphe avec GraphML. Est ce que vous avez une idée de comment créer le graphe avec GraphML en partant d'une matrice d'adjacence.
0
Aaa_dh Messages postés 10 Date d'inscription   Statut Membre Dernière intervention  
 
Bonjour;
J'ai réussi à avoir le graphe compatible avec la librairie jung, mais pour le contruire j'ai pas utilisé GraphStreamn mais jung lui meme avec ce petit code:


private static String getId(int nodeId) 
{
return "Node " + nodeId;
}

private static String getId(int nodeId, int neighborId)
{
return "Edge " + nodeId + " -> " + neighborId;
}

public static Graph<String, Integer> createGraph1(String graphId, boolean[][] adjacencyMatrix)
{
Graph<String,Integer> g = new DirectedSparseGraph <String,Integer>();

for (int nodeId = 0; nodeId < adjacencyMatrix.length; nodeId++)
g.addVertex(getId(nodeId));



for (int nodeId = 0; nodeId < adjacencyMatrix.length; nodeId++)
for (int neighborId = 0; neighborId < adjacencyMatrix[nodeId].length; neighborId++)
if (adjacencyMatrix[nodeId][neighborId])
// g.addEdge(getId(nodeId, neighborId),nodeId , neighborId);
g.addEdge(neighborId,getId(nodeId),getId(neighborId));

return(g);

}


mais il fallait aussi éliminer la biblio GraphStream et la remplacer par la biblio JUNG.
Dans mon main j'ai ajouté qlq lignes afin de créer le graphe et le sauvgarder dans un fichier:


Graph<String,Integer> g=FileHelper.createGraph1("MyGraph", adjacencyMatrixboolean);
GraphMLWriter<String,Integer> graphWriter =new GraphMLWriter<String,Integer> ();

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D:\\my graph.xml")));
graphWriter.save(g, out);

Voila.
0