Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Logic Familiar Hierarchy - Graph.java

Graph.java

Caricato da: Nophiq
Scarica il programma completo

  1. /************************************************************************
  2.  * Copyright (C) 19aa Claudio Reggiani alias Nophiq                     *
  3.  * Questo programma è software libero; è lecito ridistribuirlo e/o      *
  4.  * modificarlo secondo i termini della Licenza Pubblica Generica GNU    *
  5.  * come pubblicata dalla Free Software Foundation; o la versione 2      *
  6.  * della licenza o (a scelta) una versione successiva.                  *
  7.  *                                                                      *
  8.  * Questo programma è distribuito  nella speranza che sia utile, ma     *
  9.  * SENZA ALCUNA GARANZIA; senza neppure la garanzia implicita di        *
  10.  * COMMERCIABILITĂ€ o di APPLICABILITĂ€ PER UN PARTICOLARE SCOPO. Si      *
  11.  * veda la Licenza Pubblica Generica GNU per avere maggiori dettagli.   *
  12.  *                                                                      *
  13.  * Ognuno dovrebbe avere ricevuto una copia  della Licenza Pubblica     *
  14.  * Generica GNU insieme a questo programma; in caso contrario, la si    *
  15.  * può ottenere dalla Free Software Foundation, Inc., 675 Mass Ave,     *
  16.  * Cambridge, MA 02139, Stati Uniti.                                    *
  17.  *                                                                      *
  18.  * Per contattarmi attraverso posta elettronica: nophiq@virgilio.it     *
  19.  ************************************************************************/
  20.  
  21. package albero;
  22.  
  23. import java.util.Vector;
  24. import java.util.StringTokenizer;
  25.  
  26. /**
  27.  *
  28.  * @author nophiq
  29.  */
  30.  
  31. /***************************************************/
  32.  
  33. class Node {
  34.   private int numNode;
  35.   private String name;
  36.   private int x;
  37.   private int y;
  38.   private int generation;
  39.   private String gender;
  40.  
  41.   public Node(int number, String name) {
  42.     this.x = -1;
  43.     this.y = -1;
  44.     this.generation = -1;
  45.     this.numNode = number;
  46.     this.name = name;
  47.     this.gender = null;
  48.   }
  49.  
  50.     // Setto la loro posizione nel diagramma
  51.   public void setPointX(int i) {this.x = i;}
  52.   public void setPointY(int i) {this.y = i;}
  53.   public void setGeneration(int a) {this.generation = a;}
  54.   public void setGender(String s) {this.gender = s;}
  55.   public int getPointX() {return this.x;}
  56.   public int getPointY() {return this.y;}
  57.   public int getGeneration() {return this.generation;}
  58.  
  59.   public int getNumNode() {return this.numNode;}
  60.   public String getNodeName() {return this.name;}
  61.   public String getGender() {return this.gender;}
  62. }
  63.  
  64. /***************************************************/
  65.  
  66. class Arc {
  67.   private int start;
  68.   private int end;
  69.  
  70.   public Arc(int start, int end) {
  71.     this.start = start;
  72.     this.end = end;
  73.   }
  74.  
  75.   public int getPosStart() {return this.start;}
  76.   public int getPosEnd() {return this.end;}
  77. }
  78.  
  79. /***************************************************/
  80.  
  81. public class Graph {
  82.   private int numNodesActive;
  83.   private int numArcsActive;
  84.   private Vector nodes;
  85.   private Vector arcs;
  86.   private Vector relation;
  87.  
  88.   /** Creates a new instance of Graph */
  89.   public Graph() {
  90.     this.initialize();
  91.   }
  92.  
  93.   private void initialize() {
  94.     this.numNodesActive = 0;
  95.     this.numArcsActive = 0;
  96.     nodes = null;
  97.     arcs = null;
  98.     relation = null;
  99.     nodes = new Vector();
  100.     arcs = new Vector();
  101.     relation = new Vector();
  102.   }
  103.  
  104.   public void addNode(String name, String gender) {
  105.     Node n = new Node(numNodesActive, name);
  106.     n.setGender(gender);
  107.     nodes.addElement(n);
  108.     this.numNodesActive++;
  109.   }
  110.  
  111.   public void addArc(int start, int end) {
  112.     Arc a = new Arc(start, end);
  113.     arcs.addElement(a);
  114.     this.numArcsActive++;
  115.   }
  116.  
  117.   public void setRelation() {    
  118.     int start = -1;
  119.     int end = -1;
  120.    
  121.     for (int i=0; i<this.getLengthVectorRelation(); i++) {
  122.       Vector v = giveMeRelation(this.getRelation(i).toString());
  123.      
  124.       for (int j=0; j<this.getLengthVectorNodes(); j++) {
  125.         if (this.getNode(j).getNodeName().equals(v.elementAt(0).toString())) start = this.getNode(j).getNumNode();
  126.         if (this.getNode(j).getNodeName().equals(v.elementAt(2).toString())) end = this.getNode(j).getNumNode();
  127.       }
  128.       if (start != -1 && end != -1) {
  129.         this.addArc(start, end);
  130.       }
  131.       else {
  132.         System.err.println("Non sono stati trovati gli ID dei nodi");
  133.         System.exit(1);
  134.       }
  135.     }
  136.    
  137.     this.assegnaGenerazione();
  138.   }
  139.  
  140.     /* Funzione che mi scompone la relazione in tre parti */
  141.   public Vector giveMeRelation(String s) {
  142.     StringTokenizer st = new StringTokenizer(s, "'");
  143.     Vector tmp = new Vector();
  144.    
  145.       // Al momento la stringa sarĂ  così: nome1'>'nome2
  146.     while (st.hasMoreTokens()) {tmp.addElement(st.nextToken());}
  147.     return tmp;
  148.   }
  149.  
  150.   private void assegnaGenerazione() {
  151.       // Creo un clone del vettore nodo
  152.     Vector cloneNode = new Vector();
  153.     for (int i=0; i<this.getLengthVectorNodes(); i++) {
  154.       cloneNode.addElement((Node) this.getNode(i));
  155.     }
  156.       // Al primo nodo assegno 1000, gli altri di conseguenza
  157.     if (this.getLengthVectorNodes() > 0) ((Node) cloneNode.elementAt(0)).setGeneration(1000);
  158.    
  159.     while(this.testGeneration()) {
  160.       int position = -1;
  161.       for (int i=0; i<this.getLengthVectorNodes(); i++) {
  162.         if (((Node) cloneNode.elementAt(i)).getGeneration() == -1) {
  163.           position = i;
  164.           break;
  165.         }
  166.       }
  167.  
  168.       if (position != -1) {
  169.           // Analizzo tutte le relazioni per assegnare una generazione
  170.         for (int c=0; c<this.getLengthVectorRelation(); c++) {
  171.           Vector v = giveMeRelation(this.getRelation(c).toString());
  172.           String name1 = ((Node) cloneNode.elementAt(position)).getNodeName();
  173.          
  174.           if (v.elementAt(0).toString().equals(name1) &&
  175.             this.getNodeAtThisName(v.elementAt(2).toString()).getGeneration() != -1) {
  176.             int b = this.getNodeAtThisName(v.elementAt(2).toString()).getGeneration();
  177.            
  178.             if (v.elementAt(1).toString().equals(">_")) {
  179.               // name1 > v.elementAt(2).toString()
  180.               ((Node) cloneNode.elementAt(position)).setGeneration(b-10);
  181.             }
  182.             else if (v.elementAt(1).toString().equals("=_")) {
  183.               // name1 = v.elementAt(2).toString()
  184.               ((Node) cloneNode.elementAt(position)).setGeneration(b);
  185.             }
  186.             else if (v.elementAt(1).toString().equals("<_")) {
  187.               // name1 < v.elementAt(2).toString()
  188.               ((Node) cloneNode.elementAt(position)).setGeneration(b+10);
  189.             }
  190.            
  191.            
  192.           }
  193.           else if (v.elementAt(2).toString().equals(name1) &&
  194.             this.getNodeAtThisName(v.elementAt(0).toString()).getGeneration() != -1) {
  195.             int b = this.getNodeAtThisName(v.elementAt(0).toString()).getGeneration();
  196.            
  197.             if (v.elementAt(1).toString().equals(">_")) {
  198.               // name1 < v.elementAt(2).toString()
  199.               ((Node) cloneNode.elementAt(position)).setGeneration(b+10);
  200.             }
  201.             else if (v.elementAt(1).toString().equals("=_")) {
  202.               // name1 = v.elementAt(2).toString()
  203.               ((Node) cloneNode.elementAt(position)).setGeneration(b);
  204.             }
  205.             else if (v.elementAt(1).toString().equals("<_")) {
  206.               // name1 > v.elementAt(2).toString()
  207.               ((Node) cloneNode.elementAt(position)).setGeneration(b-10);
  208.             }
  209.           }
  210.          
  211.          
  212.         }
  213.       }
  214.      
  215.       Node n = ((Node) cloneNode.elementAt(position));
  216.       cloneNode.removeElementAt(position);
  217.       cloneNode.addElement(n);
  218.     }
  219.    
  220.    
  221.   }
  222.  
  223.   public boolean testGeneration() {
  224.     boolean b = false;
  225.     for (int i=0; i<this.getLengthVectorNodes(); i++) {
  226.       if (this.getNode(i).getGeneration() == -1) b = true;
  227.     }
  228.     return b;
  229.   }
  230.  
  231.   public void clearAll() {this.initialize();}
  232.   public int getLengthVectorNodes() {return this.nodes.size();}
  233.   public int getLengthVectorArcs() {return this.arcs.size();}
  234.  
  235.   public Node getNodeAtThisId(int idNode) {
  236.     for (int i=0; i<this.getLengthVectorNodes(); i++) {
  237.       if (this.getNode(i).getNumNode() == idNode) {return (Node) this.getNode(i);}
  238.     }
  239.       // Questo return non dovrebbe mai verificarsi
  240.     return null;
  241.   }
  242.   public Node getNodeAtThisName(String nameNode) {
  243.     for (int i=0; i<this.getLengthVectorNodes(); i++) {
  244.       if (this.getNode(i).getNodeName().equals(nameNode)) {return (Node) this.getNode(i);}
  245.     }
  246.       // Questo return non dovrebbe mai verificarsi
  247.     return null;
  248.   }
  249.   public Node getNode(int id) {return (Node) nodes.elementAt(id);}
  250.   public Arc getArc(int id) {return (Arc) arcs.elementAt(id);}
  251.  
  252.   public String getRelation(int id) {return relation.elementAt(id).toString();}
  253.   public Vector getVectorRelation() {return this.relation;}
  254.   public int getLengthVectorRelation() {return this.relation.size();}
  255. }