]> matita.cs.unibo.it Git - logicplayer.git/blob - mainActivity/src/com/example/furt/myapplication/parser.java
JOURNAL updated
[logicplayer.git] / mainActivity / src / com / example / furt / myapplication / parser.java
1 package com.example.furt.myapplication;
2
3 import org.w3c.dom.Document;
4 import org.xml.sax.SAXException;
5
6 import java.io.File;
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;
12
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;
18
19 public class parser {
20     parser(){}
21
22     protected static Node root(String esercizio)
23     {
24         Node returnNode=null;
25         try {
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) {
34             e.printStackTrace();
35         }
36         return returnNode;
37     }
38
39     static public ArrayList<String> getLiteral(String esercizio)
40     {
41         String cartellaEs=global.esDir;
42         ArrayList<String> listValue=new ArrayList<String>();
43         ArrayList<String> returnValue=new ArrayList<String>();
44         try {
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);
49             listValue.add("Ț");
50             listValue.add("⊥");
51             listValue.addAll(getLiteralAtFormula(xmlDocument, "/esercizio/tesi/formula[1]"));
52             listValue.addAll(getLiteralAtHypotesis(xmlDocument));
53             for(String elem : listValue)
54             {
55                 if(!returnValue.isEmpty())
56                 {
57                     if(!returnValue.contains(elem))
58                         returnValue.add(elem);
59                 }
60                 else
61                     returnValue.add(elem);
62             }
63         } catch (FileNotFoundException e) {
64             e.printStackTrace();
65         } catch (ParserConfigurationException e) {
66             e.printStackTrace();
67         } catch (SAXException e) {
68             e.printStackTrace();
69         } catch (IOException e) {
70             e.printStackTrace();
71         }
72         return returnValue;
73     }
74
75     static public ArrayList<String> getLiteralAtFormula(Document xmlDocument, String position)
76     {
77         ArrayList<String> returnList=new ArrayList<String>();
78         try {
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"))
84                     numbOfElements = 2;
85                 else if (type.contentEquals("not"))
86                     numbOfElements = 1;
87                 else if (type.contentEquals("literal") || type.contentEquals("atomic"))
88                     numbOfElements = 0;
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]"));
94
95                         return returnList;
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())
101                             return null;
102                         else if (child.contentEquals("top")) {
103                             returnList.add("Ț");
104                             return returnList;
105                         }
106                         else if (child.contentEquals("bot")){
107                             returnList.add("⊥");
108                             return returnList;
109                         }
110                         else{
111                             returnList.add(String.valueOf(child.charAt(0)));
112                             return returnList;
113                         }
114                     }
115                 }
116             }
117         } catch (Exception e) {
118             e.printStackTrace();
119         }
120         return null;
121     }
122
123     static public Formula createFormula(Document xmlDocument, String position) {
124         try {
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"))
130                     numbOfElements = 2;
131                 else if (type.contentEquals("not"))
132                     numbOfElements = 1;
133                 else if (type.contentEquals("literal") || type.contentEquals("atomic"))
134                     numbOfElements = 0;
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)
141                             return 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]");
150                         if (child == null)
151                             return null;
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())
156                             return null;
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();
161                         else
162                             return new Literal(child.charAt(0));
163                     }
164                 }
165             }
166         } catch (Exception e) {
167             e.printStackTrace();
168         }
169         return null;
170     }
171
172     static public List<Hypothesis> ipotesi(Document xmlDocument)
173     {
174         List<Hypothesis> returnValue=new ArrayList<Hypothesis>();
175         XPath xPath = XPathFactory.newInstance().newXPath();
176         try
177         {
178             int count = Integer.parseInt(xPath.compile("count(/esercizio/ipotesi/formula)").evaluate(xmlDocument));
179             for(int i=1;i<=count;i++)
180             {
181                 Formula F=createFormula(xmlDocument,"/esercizio/ipotesi/formula["+i+"]");
182                 Hypothesis valore;
183                 if(F==null)
184                     valore=null;
185                 else
186                     valore=new Hypothesis(F,false);
187                 if(valore!=null)
188                     returnValue.add(valore);
189             }
190         }catch (Exception e) {
191             e.printStackTrace();
192         }
193         return returnValue;
194     }
195
196     static public ArrayList<String> getLiteralAtHypotesis(Document xmlDocument)
197     {
198         ArrayList<String> returnValue=new ArrayList<String>();
199         XPath xPath = XPathFactory.newInstance().newXPath();
200         try
201         {
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) {
206             e.printStackTrace();
207         }
208         return returnValue;
209     }
210
211     static public ArrayList<String> getParameters(String esercizio)
212     {
213         ArrayList<String> value=new ArrayList<String>();
214         try {
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) {
222             e.printStackTrace();
223         }
224         return value;
225     }
226
227     static private ArrayList<String> parameters(Document xmlDocument)
228     {
229         ArrayList<String> returnValue= new ArrayList<String>();
230         XPath xPath = XPathFactory.newInstance().newXPath();
231         try
232         {
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)
236                 {
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));
240                 }
241         }catch (Exception e) {
242             e.printStackTrace();
243         }
244         return returnValue;
245     }
246 }