Revision: 14 Author: luca.cominardi@xxxxxxxxx Date: Thu Oct 15 08:18:27 2009 Log: [No log message] http://code.google.com/p/cooperativefsm/source/detail?r=14 Added: /trunk/cooperativefsm/fsm.xml Modified: /trunk/cooperativefsm/src/cooperativefsm/InputXML.java /trunk/cooperativefsm/src/cooperativefsm/Main.java ======================================= --- /dev/null +++ /trunk/cooperativefsm/fsm.xml Thu Oct 15 08:18:27 2009 @@ -0,0 +1,102 @@ +<?xml version="1.0"?> +<simulation> + <fsm> + <name>A</name> + <states>5</states> + <transition> + <id>a</id> + <s1>1</s1> + <s2>2</s2> + </transition> + <transition> + <id>b</id> + <s1>1</s1> + <s2>3</s2> + <!--<length>10</length>--> + </transition> + <transition> + <id>c</id> + <s1>2</s1> + <s2>3</s2> + </transition> + <transition> + <id>d</id> + <s1>2</s1> + <s2>4</s2> + </transition> + <transition> + <id>e</id> + <s1>3</s1> + <s2>1</s2> + </transition> + <transition> + <id>f</id> + <s1>3</s1> + <s2>5</s2> + </transition> + <transition> + <id>g</id> + <s1>4</s1> + <s2>3</s2> + </transition> + <transition> + <id>h</id> + <s1>5</s1> + <s2>2</s2> + </transition> + </fsm> + + <fsm> + <name>B</name> + <states>3</states> + <transition> + <id>a</id> + <s1>1</s1> + <s2>2</s2> + </transition> + <transition> + <id>b</id> + <s1>1</s1> + <s2>3</s2> + </transition> + <transition> + <id>c</id> + <s1>2</s1> + <s2>3</s2> + </transition> + <transition> + <id>d</id> + <s1>2</s1> + <s2>1</s2> + </transition> + <transition> + <id>e</id> + <s1>3</s1> + <s2>1</s2> + <!--<length>7</length>--> + </transition> + </fsm> + + <relation> + <transval> + <fsmval>A</fsmval> + <idval>a</idval> + </transval> + <transval> + <fsmval>B</fsmval> + <idval>a</idval> + </transval> + <type>sync</type> + </relation> + <relation> + <transval> + <fsmval>A</fsmval> + <idval>b</idval> + </transval> + <transval> + <fsmval>B</fsmval> + <idval>e</idval> + </transval> + <type>mutua</type> + </relation> +</simulation> =======================================--- /trunk/cooperativefsm/src/cooperativefsm/InputXML.java Thu Oct 15 07:02:36 2009 +++ /trunk/cooperativefsm/src/cooperativefsm/InputXML.java Thu Oct 15 08:18:27 2009
@@ -10,7 +10,16 @@ * @author Renato */ - +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; import java.util.Vector; @@ -19,12 +28,7 @@private Simulazione.Relazione relazioniTransizioni[][]; //Relazione è un tipo enum che definisce i tipi di relazione
private Vector<Fsm> listaFsm; private StatoCorrente statoIniziale; - - - public InputXML() - { - - } + private Document doc = null; public @Override Simulazione leggiSimulazione() { @@ -33,6 +37,139 @@public @Override StatoCorrente leggiStatoIniziale(Vector<Fsm> list) {return new StatoCorrente();}
- + 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) + { + NodeList nl = node.getChildNodes(); + String id = ""; + int states = 0; + + 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());
+ for(i=0; i<states; i++) + { + Stato s = new Stato(i); + //listaS.add(s); + } + } + else if(test.equalsIgnoreCase("transition")) + { + insTRANS(nl.item(i)); + } + } + } + + public void insTRANS(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("id")) + {+ System.out.println("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());
+ } + } + } + + 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)); + } + } + } + + 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);
+ } } =======================================--- /trunk/cooperativefsm/src/cooperativefsm/Main.java Thu Oct 15 07:02:36 2009 +++ /trunk/cooperativefsm/src/cooperativefsm/Main.java Thu Oct 15 08:18:27 2009
@@ -37,7 +37,7 @@ case 1: {in = new InputTast(); break; } - case 2: {in = new InputXML();+ case 2: {in = new InputXML("fsm.xml"); //l'uri del file xml dovrà essere passato da tastiera
break; } case 0: {