1 package com.example.furt.myapplication;
3 import org.w3c.dom.Document;
4 import org.xml.sax.SAXException;
7 import java.io.FileInputStream;
8 import java.io.FileNotFoundException;
9 import java.io.IOException;
10 import java.util.ArrayList;
11 import java.util.List;
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.parsers.ParserConfigurationException;
16 import javax.xml.xpath.XPath;
17 import javax.xml.xpath.XPathFactory;
22 protected static Node root(String esercizio)
26 String cartellaEs=global.esDir;
27 FileInputStream file = new FileInputStream(new File(cartellaEs+esercizio));
28 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
29 DocumentBuilder builder = builderFactory.newDocumentBuilder();
30 Document xmlDocument = builder.parse(file);
31 returnNode=new Node(createFormula(xmlDocument,"/esercizio/tesi/formula[1]"));
32 returnNode.addHPList(ipotesi(xmlDocument));
33 } catch (Exception e) {
39 static public ArrayList<String> getLiteral(String esercizio)
41 String cartellaEs=global.esDir;
42 ArrayList<String> listValue=new ArrayList<String>();
43 ArrayList<String> returnValue=new ArrayList<String>();
45 FileInputStream file = new FileInputStream(new File(cartellaEs + esercizio));
46 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
47 DocumentBuilder builder = builderFactory.newDocumentBuilder();
48 Document xmlDocument = builder.parse(file);
51 listValue.addAll(getLiteralAtFormula(xmlDocument, "/esercizio/tesi/formula[1]"));
52 listValue.addAll(getLiteralAtHypotesis(xmlDocument));
53 for(String elem : listValue)
55 if(!returnValue.isEmpty())
57 if(!returnValue.contains(elem))
58 returnValue.add(elem);
61 returnValue.add(elem);
63 } catch (FileNotFoundException e) {
65 } catch (ParserConfigurationException e) {
67 } catch (SAXException e) {
69 } catch (IOException e) {
75 static public ArrayList<String> getLiteralAtFormula(Document xmlDocument, String position)
77 ArrayList<String> returnList=new ArrayList<String>();
79 XPath xPath = XPathFactory.newInstance().newXPath();
80 if(Integer.parseInt(xPath.compile("count(" + position + ")").evaluate(xmlDocument))==1) {
81 String type = xPath.compile(position + "/@type").evaluate(xmlDocument);
82 int numbOfElements = 5;
83 if (type.contentEquals("and") || type.contentEquals("or") || type.contentEquals("impl"))
85 else if (type.contentEquals("not"))
87 else if (type.contentEquals("literal") || type.contentEquals("atomic"))
89 int count = Integer.parseInt(xPath.compile("count(" + position + "/formula)").evaluate(xmlDocument));
90 if (count == numbOfElements) {
91 if (numbOfElements == 2) {
92 returnList.addAll(getLiteralAtFormula(xmlDocument, position + "/formula[1]"));
93 returnList.addAll(getLiteralAtFormula(xmlDocument, position + "/formula[2]"));
96 } else if (numbOfElements == 1) {
97 return getLiteralAtFormula(xmlDocument, position + "/formula[1]");
98 } else if (numbOfElements == 0) {
99 String child = xPath.compile(position).evaluate(xmlDocument);
100 if (child == null || child.isEmpty())
102 else if (child.contentEquals("top")) {
106 else if (child.contentEquals("bot")){
111 returnList.add(String.valueOf(child.charAt(0)));
117 } catch (Exception e) {
123 static public Formula createFormula(Document xmlDocument, String position) {
125 XPath xPath = XPathFactory.newInstance().newXPath();
126 if(Integer.parseInt(xPath.compile("count(" + position + ")").evaluate(xmlDocument))==1) {
127 String type = xPath.compile(position + "/@type").evaluate(xmlDocument);
128 int numbOfElements = 5;
129 if (type.contentEquals("and") || type.contentEquals("or") || type.contentEquals("impl"))
131 else if (type.contentEquals("not"))
133 else if (type.contentEquals("literal") || type.contentEquals("atomic"))
135 int count = Integer.parseInt(xPath.compile("count(" + position + "/formula)").evaluate(xmlDocument));
136 if (count == numbOfElements) {
137 if (numbOfElements == 2) {
138 Formula firstChild = createFormula(xmlDocument, position + "/formula[1]");
139 Formula secondChild = createFormula(xmlDocument, position + "/formula[2]");
140 if (firstChild == null || secondChild == null)
142 else if (type.contentEquals("and"))
143 return new FormulaAnd(createFormula(xmlDocument, position + "/formula[1]"), createFormula(xmlDocument, position + "/formula[2]"));
144 else if (type.contentEquals("or"))
145 return new FormulaOr(createFormula(xmlDocument, position + "/formula[1]"), createFormula(xmlDocument, position + "/formula[2]"));
146 else if (type.contentEquals("impl"))
147 return new FormulaImpl(createFormula(xmlDocument, position + "/formula[1]"), createFormula(xmlDocument, position + "/formula[2]"));
148 } else if (numbOfElements == 1) {
149 Formula child = createFormula(xmlDocument, position + "/formula[1]");
152 return new FormulaNot(createFormula(xmlDocument, position + "/formula[1]"));
153 } else if (numbOfElements == 0) {
154 String child = xPath.compile(position).evaluate(xmlDocument);
155 if (child == null || child.isEmpty())
157 else if (child.length()>=3 && child.substring(0,3).equals("top"))
158 return new FormulaTOP();
159 else if (child.length()>=3 && child.substring(0,3).equals("bot"))
160 return new FormulaBOT();
162 return new Literal(child.charAt(0));
166 } catch (Exception e) {
172 static public List<Hypothesis> ipotesi(Document xmlDocument)
174 List<Hypothesis> returnValue=new ArrayList<Hypothesis>();
175 XPath xPath = XPathFactory.newInstance().newXPath();
178 int count = Integer.parseInt(xPath.compile("count(/esercizio/ipotesi/formula)").evaluate(xmlDocument));
179 for(int i=1;i<=count;i++)
181 Formula F=createFormula(xmlDocument,"/esercizio/ipotesi/formula["+i+"]");
186 valore=new Hypothesis(F,false);
188 returnValue.add(valore);
190 }catch (Exception e) {
196 static public ArrayList<String> getLiteralAtHypotesis(Document xmlDocument)
198 ArrayList<String> returnValue=new ArrayList<String>();
199 XPath xPath = XPathFactory.newInstance().newXPath();
202 int count = Integer.parseInt(xPath.compile("count(/esercizio/ipotesi/formula)").evaluate(xmlDocument));
203 for(int i=1;i<=count;i++)
204 returnValue.addAll(getLiteralAtFormula(xmlDocument, "/esercizio/ipotesi/formula[" + i + "]"));
205 }catch (Exception e) {
211 static public ArrayList<String> getParameters(String esercizio)
213 ArrayList<String> value=new ArrayList<String>();
215 String cartellaEs=global.esDir;
216 FileInputStream file = new FileInputStream(new File(cartellaEs+esercizio));
217 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
218 DocumentBuilder builder = builderFactory.newDocumentBuilder();
219 Document xmlDocument = builder.parse(file);
220 return parameters(xmlDocument);
221 } catch (Exception e) {
227 static private ArrayList<String> parameters(Document xmlDocument)
229 ArrayList<String> returnValue= new ArrayList<String>();
230 XPath xPath = XPathFactory.newInstance().newXPath();
233 if( Integer.parseInt(xPath.compile("count(/esercizio/valutazione/click)").evaluate(xmlDocument))==1 &&
234 Integer.parseInt(xPath.compile("count(/esercizio/valutazione/tempo)").evaluate(xmlDocument))==1 &&
235 Integer.parseInt(xPath.compile("count(/esercizio/valutazione/altezza)").evaluate(xmlDocument))==1)
237 returnValue.add(xPath.compile("/esercizio/valutazione/click").evaluate(xmlDocument));
238 returnValue.add(xPath.compile("/esercizio/valutazione/tempo").evaluate(xmlDocument));
239 returnValue.add(xPath.compile("/esercizio/valutazione/altezza").evaluate(xmlDocument));
241 }catch (Exception e) {