/************************************************************************
* Copyright (C) 19aa Claudio Reggiani alias Nophiq *
* Questo programma è software libero; è lecito ridistribuirlo e/o *
* modificarlo secondo i termini della Licenza Pubblica Generica GNU *
* come pubblicata dalla Free Software Foundation; o la versione 2 *
* della licenza o (a scelta) una versione successiva. *
* *
* Questo programma è distribuito nella speranza che sia utile, ma *
* SENZA ALCUNA GARANZIA; senza neppure la garanzia implicita di *
* COMMERCIABILITĂ€ o di APPLICABILITĂ€ PER UN PARTICOLARE SCOPO. Si *
* veda la Licenza Pubblica Generica GNU per avere maggiori dettagli. *
* *
* Ognuno dovrebbe avere ricevuto una copia della Licenza Pubblica *
* Generica GNU insieme a questo programma; in caso contrario, la si *
* può ottenere dalla Free Software Foundation, Inc., 675 Mass Ave, *
* Cambridge, MA 02139, Stati Uniti. *
* *
* Per contattarmi attraverso posta elettronica: nophiq@virgilio.it *
************************************************************************/
package albero;
import java.util.Vector;
import java.util.StringTokenizer;
/**
*
* @author nophiq
*/
/***************************************************/
class Node {
private int numNode;
private int x;
private int y;
private int generation;
public Node
(int number,
String name
) {
this.x = -1;
this.y = -1;
this.generation = -1;
this.numNode = number;
this.name = name;
this.gender = null;
}
// Setto la loro posizione nel diagramma
public void setPointX(int i) {this.x = i;}
public void setPointY(int i) {this.y = i;}
public void setGeneration(int a) {this.generation = a;}
public void setGender
(String s
) {this.
gender = s
;}
public int getPointX() {return this.x;}
public int getPointY() {return this.y;}
public int getGeneration() {return this.generation;}
public int getNumNode() {return this.numNode;}
public String getNodeName
() {return this.
name;}
public String getGender
() {return this.
gender;}
}
/***************************************************/
class Arc {
private int start;
private int end;
public Arc(int start, int end) {
this.start = start;
this.end = end;
}
public int getPosStart() {return this.start;}
public int getPosEnd() {return this.end;}
}
/***************************************************/
public class Graph {
private int numNodesActive;
private int numArcsActive;
/** Creates a new instance of Graph */
public Graph() {
this.initialize();
}
private void initialize() {
this.numNodesActive = 0;
this.numArcsActive = 0;
nodes = null;
arcs = null;
relation = null;
}
Node n = new Node(numNodesActive, name);
n.setGender(gender);
nodes.addElement(n);
this.numNodesActive++;
}
public void addArc(int start, int end) {
Arc a = new Arc(start, end);
arcs.addElement(a);
this.numArcsActive++;
}
public void setRelation() {
int start = -1;
int end = -1;
for (int i=0; i<this.getLengthVectorRelation(); i++) {
Vector v
= giveMeRelation
(this.
getRelation(i
).
toString());
for (int j=0; j<this.getLengthVectorNodes(); j++) {
if (this.getNode(j).getNodeName().equals(v.elementAt(0).toString())) start = this.getNode(j).getNumNode();
if (this.getNode(j).getNodeName().equals(v.elementAt(2).toString())) end = this.getNode(j).getNumNode();
}
if (start != -1 && end != -1) {
this.addArc(start, end);
}
else {
System.
err.
println("Non sono stati trovati gli ID dei nodi");
}
}
this.assegnaGenerazione();
}
/* Funzione che mi scompone la relazione in tre parti */
// Al momento la stringa sarà così: nome1'>'nome2
while (st.hasMoreTokens()) {tmp.addElement(st.nextToken());}
return tmp;
}
private void assegnaGenerazione() {
// Creo un clone del vettore nodo
for (int i=0; i<this.getLengthVectorNodes(); i++) {
cloneNode.addElement((Node) this.getNode(i));
}
// Al primo nodo assegno 1000, gli altri di conseguenza
if (this.getLengthVectorNodes() > 0) ((Node) cloneNode.elementAt(0)).setGeneration(1000);
while(this.testGeneration()) {
int position = -1;
for (int i=0; i<this.getLengthVectorNodes(); i++) {
if (((Node) cloneNode.elementAt(i)).getGeneration() == -1) {
position = i;
break;
}
}
if (position != -1) {
// Analizzo tutte le relazioni per assegnare una generazione
for (int c=0; c<this.getLengthVectorRelation(); c++) {
Vector v
= giveMeRelation
(this.
getRelation(c
).
toString());
String name1
= ((Node
) cloneNode.
elementAt(position
)).
getNodeName();
if (v.elementAt(0).toString().equals(name1) &&
this.getNodeAtThisName(v.elementAt(2).toString()).getGeneration() != -1) {
int b = this.getNodeAtThisName(v.elementAt(2).toString()).getGeneration();
if (v.elementAt(1).toString().equals(">_")) {
// name1 > v.elementAt(2).toString()
((Node) cloneNode.elementAt(position)).setGeneration(b-10);
}
else if (v.elementAt(1).toString().equals("=_")) {
// name1 = v.elementAt(2).toString()
((Node) cloneNode.elementAt(position)).setGeneration(b);
}
else if (v.elementAt(1).toString().equals("<_")) {
// name1 < v.elementAt(2).toString()
((Node) cloneNode.elementAt(position)).setGeneration(b+10);
}
}
else if (v.elementAt(2).toString().equals(name1) &&
this.getNodeAtThisName(v.elementAt(0).toString()).getGeneration() != -1) {
int b = this.getNodeAtThisName(v.elementAt(0).toString()).getGeneration();
if (v.elementAt(1).toString().equals(">_")) {
// name1 < v.elementAt(2).toString()
((Node) cloneNode.elementAt(position)).setGeneration(b+10);
}
else if (v.elementAt(1).toString().equals("=_")) {
// name1 = v.elementAt(2).toString()
((Node) cloneNode.elementAt(position)).setGeneration(b);
}
else if (v.elementAt(1).toString().equals("<_")) {
// name1 > v.elementAt(2).toString()
((Node) cloneNode.elementAt(position)).setGeneration(b-10);
}
}
}
}
Node n = ((Node) cloneNode.elementAt(position));
cloneNode.removeElementAt(position);
cloneNode.addElement(n);
}
}
public boolean testGeneration() {
boolean b = false;
for (int i=0; i<this.getLengthVectorNodes(); i++) {
if (this.getNode(i).getGeneration() == -1) b = true;
}
return b;
}
public void clearAll() {this.initialize();}
public int getLengthVectorNodes() {return this.nodes.size();}
public int getLengthVectorArcs() {return this.arcs.size();}
public Node getNodeAtThisId(int idNode) {
for (int i=0; i<this.getLengthVectorNodes(); i++) {
if (this.getNode(i).getNumNode() == idNode) {return (Node) this.getNode(i);}
}
// Questo return non dovrebbe mai verificarsi
return null;
}
public Node getNodeAtThisName
(String nameNode
) {
for (int i=0; i<this.getLengthVectorNodes(); i++) {
if (this.getNode(i).getNodeName().equals(nameNode)) {return (Node) this.getNode(i);}
}
// Questo return non dovrebbe mai verificarsi
return null;
}
public Node getNode(int id) {return (Node) nodes.elementAt(id);}
public Arc getArc(int id) {return (Arc) arcs.elementAt(id);}
public String getRelation
(int id
) {return relation.
elementAt(id
).
toString();}
public Vector getVectorRelation
() {return this.
relation;}
public int getLengthVectorRelation() {return this.relation.size();}
}