Revision: 16 Author: luca.cominardi@xxxxxxxxx Date: Mon Oct 19 07:00:40 2009 Log: [No log message] http://code.google.com/p/cooperativefsm/source/detail?r=16 Modified: /trunk/cooperativefsm/fsm.xml /trunk/cooperativefsm/src/cooperativefsm/Fsm.java /trunk/cooperativefsm/src/cooperativefsm/InputTast.java /trunk/cooperativefsm/src/cooperativefsm/InputXML.java /trunk/cooperativefsm/src/cooperativefsm/Stato.java /trunk/cooperativefsm/src/cooperativefsm/Transizione.java ======================================= --- /trunk/cooperativefsm/fsm.xml Thu Oct 15 08:18:27 2009 +++ /trunk/cooperativefsm/fsm.xml Mon Oct 19 07:00:40 2009 @@ -16,7 +16,7 @@ </transition> <transition> <id>c</id> - <s1>2</s1> + <s1>1</s1> <s2>3</s2> </transition> <transition> @@ -44,6 +44,7 @@ <s1>5</s1> <s2>2</s2> </transition> + <current>2</current> </fsm> <fsm> @@ -75,6 +76,7 @@ <s2>1</s2> <!--<length>7</length>--> </transition> + <current>1</current> </fsm> <relation> @@ -99,4 +101,6 @@ </transval> <type>mutua</type> </relation> + + </simulation> =======================================--- /trunk/cooperativefsm/src/cooperativefsm/Fsm.java Thu Oct 15 07:02:36 2009 +++ /trunk/cooperativefsm/src/cooperativefsm/Fsm.java Mon Oct 19 07:00:40 2009
@@ -28,6 +28,7 @@ }/** Costruttore (con argomenti in più...) di una macchina a stati finiti ... non so se servirà!?!
+ * (Serve per l'InputXML) * * @param _id * @param _stati =======================================--- /trunk/cooperativefsm/src/cooperativefsm/InputTast.java Thu Oct 15 07:02:36 2009 +++ /trunk/cooperativefsm/src/cooperativefsm/InputTast.java Mon Oct 19 07:00:40 2009
@@ -119,16 +119,16 @@ public void inizializzaTrans (Fsm x) {boolean continua = this.ciSonoTrans(); //metodo che verrà sovrascritto dalle sottoclassi
- + int k = 0; while (continua) {Stato sorgente = this.leggiStatoConMax("sorgente", x.getNumStati()-1); Stato destinazione = this.leggiStatoConMax("destinazione", x.getNumStati()-1);
- Transizione t = new Transizione (sorgente,destinazione);+ Transizione t = new Transizione (Integer.toString(k),sorgente,destinazione);
x.addTrans(t); System.out.println("Transizione inserita correttamente!"); - + k++; continua = this.ciSonoTrans(); }//while } =======================================--- /trunk/cooperativefsm/src/cooperativefsm/InputXML.java Thu Oct 15 08:42:19 2009 +++ /trunk/cooperativefsm/src/cooperativefsm/InputXML.java Mon Oct 19 07:00:40 2009
@@ -26,8 +26,8 @@ public class InputXML extends Input {private Simulazione.Relazione relazioniTransizioni[][]; //Relazione è un tipo enum che definisce i tipi di relazione
- private Vector<Fsm> listaFsm; - private StatoCorrente statoIniziale; + private Vector<Fsm> listaFsm = new Vector<Fsm>(); + private StatoCorrente statoIniziale = new StatoCorrente(); private Document doc = null; public @Override Simulazione leggiSimulazione() @@ -37,149 +37,269 @@public @Override StatoCorrente leggiStatoIniziale(Vector<Fsm> list) {return new StatoCorrente();}
+ //costruttore di InputXML public InputXML(String URI) - { - try - { - doc = parserXML(new File(URI)); - - insSIM(doc, 0); - } - catch(Exception error) - { - error.printStackTrace(); - } - } - - public void insSIM(Node node, int level) - { - NodeList nl = node.getChildNodes(); - - for(int i=0, cnt=nl.getLength(); i<cnt; i++) - { - String test=nl.item(i).getNodeName(); - //System.out.println(test); - //System.out.println(nl.item(i).getNodeName()); - //System.out.println(nl.item(i).getNodeType()); - if(test.equalsIgnoreCase("fsm")) - { - insFSM(nl.item(i)); - } - else if(test.equalsIgnoreCase("relation")) - { - insREL(nl.item(i)); - } - - insSIM(nl.item(i), level+1); - } - } - - public void insFSM(Node node) - { - Vector<Stato> listaS = new Vector<Stato>(); - String id = ""; - int states = 0; - - NodeList nl = node.getChildNodes(); - - for(int i=0, cnt=nl.getLength(); i<cnt; i++) - { - String test=nl.item(i).getNodeName(); - - //System.out.println(nl.item(i).getNodeName()); - //System.out.println(nl.item(i).getNodeValue()+"\n"); - if(test.equalsIgnoreCase("name")) - { - id = nl.item(i).getFirstChild().getNodeValue(); - } - else if(test.equalsIgnoreCase("states")) - {- states = Integer.parseInt(nl.item(i).getFirstChild().getNodeValue());
+ { + //provo a leggere il file e fare il parser xml + try + { + doc = parserXML(new File(URI)); + insSIM(doc, 0); + } + catch(Exception error) + { + error.printStackTrace(); + } + /* + System.out.println("Le fsm sono: " + listaFsm.size()); + System.out.println("Stati fsm1: " + listaFsm.get(0).getNumStati()); + System.out.println("Stati fsm2: " + listaFsm.get(1).getNumStati()); + System.out.println("Stati fsm1: " + listaFsm.get(0).getNumTr()); + System.out.println("Stati fsm2: " + listaFsm.get(1).getNumTr()); + */ + } ++ //creo una nuova instanza di Simulazione facendo passare tutti i nodi presenti nel file
+ private void insSIM(Node node, int level) + { + NodeList nl = node.getChildNodes(); + + for(int i=0, cnt=nl.getLength(); i<cnt; i++) + { + String test=nl.item(i).getNodeName(); + //System.out.println(test); + //System.out.println(nl.item(i).getNodeName()); + //System.out.println(nl.item(i).getNodeType());+ //Se è definita una fsm la leggo e la aggiungo alla lista delle fsm
+ if(test.equalsIgnoreCase("fsm")) + { + listaFsm.add(insFSM(nl.item(i))); + }+ //Se è definita una relazione la leggo e la aggiungo alla lista delle relazioni
+ else if(test.equalsIgnoreCase("relation")) + { + insREL(nl.item(i)); + } + //Chiamata ricorsiva per far passare tutti i nodi del file + insSIM(nl.item(i), level+1); + } + } + + private Fsm insFSM(Node node) + { + boolean stati=true; + boolean name=true; + boolean trans=true; + String id = ""; + Vector<Stato> listaS = new Vector<Stato>(); + Vector<Transizione> listaT = new Vector<Transizione>(); + + NodeList nl = node.getChildNodes(); + + //prima controllo il name e il numero di stati + for(int i=0, cnt=nl.getLength(); i<cnt; i++) + { + String test=nl.item(i).getNodeName(); + + if(test.equalsIgnoreCase("name")) + { + name=false; + id = nl.item(i).getFirstChild().getNodeValue(); + for(int j=0; j<listaFsm.size(); j++) + { + if(id.equals(listaFsm.get(j).getId())) + {+ System.out.println("Due fsm definite con lo stesso id!!!");
+ //TODO Uscire dal programma + } + } + } + else if(test.equalsIgnoreCase("states")) + { + stati=false; + try + {+ int states = Integer.parseInt(nl.item(i).getFirstChild().getNodeValue());
for(i=0; i<states; i++) { - Stato s = new Stato(i); + Stato s = new Stato(i+1); listaS.add(s); } } - else if(test.equalsIgnoreCase("transition")) - { - insTRANS(nl.item(i)); - } - } - } - - public Vector<Transizione> insTRANS(Node node) - { - Vector<Transizione> listaT = new Vector<Transizione>(); - - NodeList nl = node.getChildNodes(); - - for(int i=0, cnt=nl.getLength(); i<cnt; i++) - { - String id = ""; - Stato s1 = null; - Stato s2 = null; - int length = 0; - - String test=nl.item(i).getNodeName(); - - if(test.equalsIgnoreCase("id")) - { - id = nl.item(i).getFirstChild().getNodeValue(); - } - else if(test.equalsIgnoreCase("s1")) - {- System.out.println("s1: " + nl.item(i).getFirstChild().getNodeValue());
- } - else if(test.equalsIgnoreCase("s2")) - {- System.out.println("s2: " + nl.item(i).getFirstChild().getNodeValue());
- } - else if(test.equalsIgnoreCase("length")) - {- System.out.println("length: " + nl.item(i).getFirstChild().getNodeValue());
+ catch(NumberFormatException ne) + {+ System.out.println("Lo stato dev'essere identificato da un numero intero!!!");
+ } + } + }//end for + //poi controllo le transizioni + for(int i=0, cnt=nl.getLength(); i<cnt; i++) + { + String test=nl.item(i).getNodeName(); + + if(test.equalsIgnoreCase("transition")) + { + trans=false; + listaT.add(insTRANS(nl.item(i), listaS, listaT)); + } + else if(test.equalsIgnoreCase("current")) + { + //TODO finire l'impostazione dello StatoCorrente + try + {+ int stato = Integer.parseInt(nl.item(i).getFirstChild().getNodeValue());
+ if(stato>listaS.size())+ System.out.println("Il numero dello stato corrente non può superare il numero totale degli stati");
+ else + { + //Stato s1=getStatoById(stato, listaS); + //System.out.println(s1.ToString()); + } + } + catch(NumberFormatException ne) + {+ System.out.println("Lo stato dev'essere identificato da un numero intero!!!");
+ } + } + } + if(name) + { + System.out.println("Non è stato definito l'id per una fsm"); + } + if(stati) + {+ System.out.println("Non è stato definito il numero di stati per una fsm");
+ } + if(trans) + {+ System.out.println("Non è stata definita alcuna transizione per una fsm");
+ } + Fsm f = new Fsm(id, listaS, listaT); + + return f; + } ++ private Transizione insTRANS(Node node, Vector<Stato> lS, Vector<Transizione> lT)
+ { + NodeList nl = node.getChildNodes(); + String id = ""; + Stato s1 = null; + Stato s2 = null; + int length = 0; + + for(int i=0, cnt=nl.getLength(); i<cnt; i++) + { + String test=nl.item(i).getNodeName(); + + if(test.equalsIgnoreCase("id")) + { + id = nl.item(i).getFirstChild().getNodeValue(); + for(int j=0; j<lT.size(); j++) + { + if(id.equals(lT.get(j).getId())) + {+ System.out.println("Due transizioni definite con lo stesso id!!!");
+ //TODO Uscire dal programma + } + } + } + else if(test.equalsIgnoreCase("s1")) + { + try + {+ int stato = Integer.parseInt(nl.item(i).getFirstChild().getNodeValue());
+ if(stato>lS.size())+ System.out.println("Il numero dello stato non può superare il numero totale degli stati");
+ else + { + s1=getStatoById(stato, lS); + //System.out.println(s1.ToString()); + } + } + catch(NumberFormatException ne) + {+ System.out.println("Lo stato dev'essere identificato da un numero intero!!!");
+ } + } + else if(test.equalsIgnoreCase("s2")) + { + try + {+ int stato = Integer.parseInt(nl.item(i).getFirstChild().getNodeValue());
+ if(stato>lS.size())+ System.out.println("Il numero dello stato non può superare il numero totale degli stati");
+ else + { + s2=getStatoById(stato, lS); + } + } + catch(NumberFormatException ne) + {+ System.out.println("Lo stato dev'essere identificato da un numero intero!!!");
} } - return listaT; - } - - public void insREL(Node node) - { - NodeList nl = node.getChildNodes(); - - for(int i=0, cnt=nl.getLength(); i<cnt; i++) - { - String test=nl.item(i).getNodeName(); - - if(test.equalsIgnoreCase("transval")) - { - insTRANSVAL(nl.item(i)); - } + /* + else if(test.equalsIgnoreCase("length")) + {+ System.out.println("length: " + nl.item(i).getFirstChild().getNodeValue());
+ }*/ + } + Transizione T = new Transizione(id,s1,s2); + s1.addTransUscente(T); + return T; + } + + private Stato getStatoById(int id, Vector<Stato> lS) + { + Stato k = null; + for(int i=0; i<lS.size(); i++) + { + Stato p = lS.get(i); + if(id==lS.get(i).getId()) + k=p; + } + return k; + } + + private void insREL(Node node) + { + NodeList nl = node.getChildNodes(); + + for(int i=0, cnt=nl.getLength(); i<cnt; i++) + { + String test=nl.item(i).getNodeName(); + + if(test.equalsIgnoreCase("transval")) + { + insTRANSVAL(nl.item(i)); } } - - public void insTRANSVAL(Node node) - { - NodeList nl = node.getChildNodes(); - - for(int i=0, cnt=nl.getLength(); i<cnt; i++) - { - String test=nl.item(i).getNodeName(); - - if(test.equalsIgnoreCase("fsmval")) - {- System.out.println("fsmval: " + nl.item(i).getFirstChild().getNodeValue());
- } - else if(test.equalsIgnoreCase("idval")) - {- System.out.println("idval: " + nl.item(i).getFirstChild().getNodeValue());
- } + } + + public void insTRANSVAL(Node node) + { + NodeList nl = node.getChildNodes(); + + for(int i=0, cnt=nl.getLength(); i<cnt; i++) + { + String test=nl.item(i).getNodeName(); + + if(test.equalsIgnoreCase("fsmval")) + {+ //System.out.println("fsmval: " + nl.item(i).getFirstChild().getNodeValue());
+ } + else if(test.equalsIgnoreCase("idval")) + {+ //System.out.println("idval: " + nl.item(i).getFirstChild().getNodeValue());
} } -- public Document parserXML(File file) throws SAXException, IOException, ParserConfigurationException
- {- return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
- } - -} + } + + ++ public Document parserXML(File file) throws SAXException, IOException, ParserConfigurationException
+ {+ return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
+ } + +} =======================================--- /trunk/cooperativefsm/src/cooperativefsm/Stato.java Tue Oct 13 07:53:08 2009 +++ /trunk/cooperativefsm/src/cooperativefsm/Stato.java Mon Oct 19 07:00:40 2009
@@ -33,5 +33,11 @@ { idStato = id; } + + public String ToString() + { + String s = ("Stato numero: " + idStato); + return s; + } } =======================================--- /trunk/cooperativefsm/src/cooperativefsm/Transizione.java Thu Oct 15 07:02:36 2009 +++ /trunk/cooperativefsm/src/cooperativefsm/Transizione.java Mon Oct 19 07:00:40 2009
@@ -11,11 +11,12 @@ */ public class Transizione {- private int id; //è il numero progressivo assegnato alla transizione, riferito alla fsm cui appartiene + //private int id; //è il numero progressivo assegnato alla transizione, riferito alla fsm cui appartiene
+ private String id;private String nome; //identifica una certa transizione (attributo opzionale)
private Stato stato1; private Stato stato2;- private int numRelazioniSincroneStatoCorrente; //Indica quanti relazioni sincrone ha con altre transizioni nello stato corrente + private int numRelazioniSincroneStatoCorrente; //Indica quante relazioni sincrone ha con altre transizioni nello stato corrente
/*** Costruttore di una transizione a partire da due stati di una stessa fsm
@@ -24,8 +25,9 @@ * @param _stato2 */ - public Transizione(Stato _stato1, Stato _stato2) - { + public Transizione(String _id, Stato _stato1, Stato _stato2) + { + id = _id; stato1 = _stato1; stato2 = _stato2; } @@ -37,6 +39,21 @@ " allo stato " + stato2.getId()); return s; } + + public String getId() + { + return id; + } + + public Stato getStato1() + { + return stato1; + } + + public Stato getStato2() + { + return stato2; + }public void setNumRelazioniSincroneStatoCorrente( Simulazione.Relazione relazioni[][])
{