Go to the documentation of this file.00001 package myShell;
00002
00003 import java.io.BufferedReader;
00004 import java.io.BufferedWriter;
00005 import java.io.File;
00006 import java.io.FileInputStream;
00007 import java.io.FileOutputStream;
00008 import java.io.FileWriter;
00009 import java.io.InputStream;
00010 import java.io.InputStreamReader;
00011 import java.util.*;
00012
00013 import myDataBases.MediaBase;
00014
00015 import org.jdom.*;
00016 import org.jdom.input.SAXBuilder;
00017 import org.jdom.output.Format;
00018 import org.jdom.output.XMLOutputter;
00019
00020
00021
00034 public class Interpreter {
00035
00036
00037
00038
00039
00043 private MediaBase mediaBase;
00047 private Integer numberOfMedia;
00048
00049
00050
00051
00052
00053
00062 public Interpreter (MediaBase database)
00063 {
00064
00065 this.mediaBase = database;
00066 this.numberOfMedia = 0;
00067 for (Integer[] array : this.mediaBase.getAllMedia())
00068 this.numberOfMedia += array.length;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00089 public static Map <String,String> getConfig()
00090 {
00091 Element rootConfig;
00092 org.jdom.Document documentConfig;
00093 Map <String,String> conf = new Hashtable <String,String>() ;
00094 SAXBuilder sxb = new SAXBuilder();
00095 try
00096 {
00097 documentConfig= sxb.build(new File("../data/config.xml"));
00098 rootConfig = documentConfig.getRootElement();
00099 Element constants = rootConfig.getChild("constants");
00100 Iterator it = constants.getChildren().iterator();
00101 while (it.hasNext())
00102 {
00103 Element current = (Element)it.next();
00104 conf.put(current.getName(), current.getValue());
00105 }
00106 }
00107 catch(Exception e){ e.printStackTrace(); }
00108 return conf ;
00109 }
00110
00111
00120 public static List<String> getAxes()
00121 {
00122
00123 Element rootConfig;
00124 org.jdom.Document documentConfig;
00125 SAXBuilder sxb = new SAXBuilder();
00126 List<String> listAxes = new ArrayList <String>() ;
00127 try
00128 {
00129 documentConfig= sxb.build(new File("../data/config.xml"));
00130 rootConfig = documentConfig.getRootElement();
00131 Element axes = rootConfig.getChild("axes");
00132 if (axes != null)
00133 {
00134 Iterator it = axes.getChildren().iterator();
00135 while (it.hasNext())
00136 {
00137 Element current = (Element)it.next();
00138 listAxes.add(current.getValue());
00139 }
00140 }
00141 else
00142 listAxes = null;
00143 }
00144 catch(Exception e){ e.printStackTrace(); }
00145 return listAxes;
00146 }
00147
00148
00149
00150
00168 public static List<String> getFields(String fields)
00169 {
00170 Element rootConfig;
00171 org.jdom.Document documentConfig;
00172 List<String> listFields = new ArrayList <String>() ;
00173 if ( (fields.equals("SQLfield")) || (fields.equals("SQLtype")) || (fields.equals("CSVfield")) )
00174 {
00175 SAXBuilder sxb = new SAXBuilder();
00176 try
00177 {
00178 documentConfig= sxb.build(new File("../data/config.xml"));
00179 rootConfig = documentConfig.getRootElement();
00180 Element HDfields = rootConfig.getChild("hardDataFields");
00181 Iterator it = HDfields.getChildren().iterator();
00182 while (it.hasNext())
00183 {
00184 Element current = (Element)it.next();
00185 String entry = current.getChild(fields).getValue() ;
00186 if (entry.length() > 1)
00187 listFields.add(entry);
00188 }
00189 }
00190 catch(Exception e){ e.printStackTrace(); }
00191 }
00192 else
00193 listFields = null;
00194 return listFields;
00195 }
00196
00197
00222 public static Boolean checkFile(LogWriter log)
00223 {
00224 Boolean fine = true;
00225 log.append("------- Checking config.xml");
00226
00227
00228 Map<String,String> configContent = getConfig() ;
00229 String[] necessaryConstants = { "mediaPath", "sqlBase", "sqlUser", "antechamberUrl", "cfKey", "cfJobNumber" };
00230 for (String constant : necessaryConstants)
00231 if (configContent.keySet().contains(constant))
00232 log.append(constant+" constant found");
00233 else
00234 {
00235 log.append("ERROR : " + constant + " NOT FOUND. Interrupting.");
00236 fine = false;
00237 }
00238
00239
00240 List<String> axes = getAxes() ;
00241 if (axes == null)
00242 {
00243 fine = false;
00244 log.append("ERROR : no axis found. Interrupting.");
00245 }
00246 else
00247 log.append(axes.size() + " axes found.");
00248
00249
00250 List<String> fields = getFields("SQLfield");
00251 String[] necessaryFields = { "media1", "media2", "greater", "axis" };
00252 if (fields == null)
00253 log.append("ERROR : no 'hardDataFields' section in the config.xml. Interrupting.");
00254 else
00255 for (String field : necessaryFields)
00256 if (fields.contains(field))
00257 log.append(field +" found in the hardDataFields section");
00258 else
00259 {
00260 log.append("ERROR : " + field + " NOT FOUND in the hardDataFields section. Interrupting.");
00261 fine = false;
00262 }
00263
00264 log.append("------- END check config.xml\n");
00265 return fine;
00266 }
00267
00268
00269
00270
00271
00272
00278 public static Integer getLastJob()
00279 {
00280 Integer id = 0;
00281 try{
00282 InputStream ips = new FileInputStream("../data/previously/lastJob");
00283 InputStreamReader ipsr = new InputStreamReader(ips);
00284 BufferedReader br = new BufferedReader(ipsr);
00285 id = Integer.parseInt(br.readLine());
00286 br.close();
00287 }
00288 catch (Exception e){ e.printStackTrace(); }
00289 return id;
00290 }
00291
00292
00298 public static void setLastJob(Integer id)
00299 {
00300 try
00301 {
00302 FileWriter fw = new FileWriter("../data/previously/lastJob", false);
00303 BufferedWriter output = new BufferedWriter(fw);
00304 output.write(id.toString());
00305 output.flush();
00306 output.close();
00307 } catch(Exception e) { e.printStackTrace(); }
00308 }
00309
00310
00321 private List<Integer[]> parsePrevious(String axis)
00322 {
00323 Element rootPrevious;
00324 org.jdom.Document documentPrevious;
00325 List<Integer[]> result = new ArrayList<Integer[]>();
00326 SAXBuilder sxb = new SAXBuilder();
00327 try
00328 {
00329 documentPrevious = sxb.build(new File("../data/previously/" + axis + ".xml"));
00330 rootPrevious = documentPrevious.getRootElement();
00331 Iterator it = rootPrevious.getChildren().iterator();
00332 while (it.hasNext())
00333 {
00334 Element array = (Element)it.next();
00335 List<Integer> arrayContent = new ArrayList<Integer>();
00336 Iterator jt = array.getChildren().iterator();
00337 while (jt.hasNext())
00338 {
00339 Element entry = (Element)jt.next();
00340 arrayContent.add (Integer.parseInt(entry.getValue()) );
00341 }
00342 result.add(arrayContent.toArray(new Integer[0]));
00343 }
00344 }
00345 catch(Exception e){ e.printStackTrace(); }
00346
00347 return result;
00348 }
00349
00350
00367 public List<Integer[]> getCurrentState(LogWriter log, String axis)
00368 {
00369 List<Integer[]> lst;
00370 File previouslyInCPS = new File("../data/previously/");
00371
00372 Integer count = 0;
00373 for (String file : previouslyInCPS.list())
00374 if (file.equals(axis + ".xml"))
00375 break;
00376 else
00377 count ++;
00378 if (count == previouslyInCPS.list().length)
00379 {
00380 lst = new ArrayList<Integer[]>();
00381 lst = this.mediaBase.getAllMedia();
00382 log.append("[DONE] Reading media to sort.\n ... Note: no previous results found for " + axis +
00383 ": starting from the beginning.");
00384 }
00385 else
00386 {
00387 lst = parsePrevious(axis);
00388 log.append("[DONE] Reading media to sort.\n ... Note: previous results found for " + axis +
00389 ": starting where we left.");
00390 }
00391 return lst;
00392 }
00393
00394
00395
00396
00397
00398
00399
00411 public void generateIntermediaryXMLs(String xmlFile, List<Integer[]> content)
00412 {
00413
00414 Element rootResults = new Element("results");
00415 org.jdom.Document documentResults = new Document(rootResults);
00416
00417 Map<Integer, Map<String,String>> mediaCharac= this.mediaBase.getContent();
00418 Map<String,String> media ;
00419 Element charac;
00420
00421 for (Integer[] array : content)
00422 {
00423 Element arrayElement = new Element("array");
00424 for (Integer mediaID : array)
00425 {
00426 Element identifier = new Element("id");
00427 identifier.setText(String.valueOf(mediaID));
00428 arrayElement.addContent(identifier);
00429 }
00430 rootResults.addContent(arrayElement);
00431 }
00432 try
00433 {
00434 XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
00435 sortie.output(documentResults, new FileOutputStream(xmlFile));
00436 }
00437 catch (Exception e){ e.printStackTrace(); }
00438 }
00439
00440
00452 private Double rank2value(Integer rank)
00453 {
00454 Double value = ( (2.0*rank)/(this.numberOfMedia-1) ) - 1.0;
00455 return value;
00456 }
00457
00458
00475 private List< Map<String,Integer> > preformating(Map<String,Integer[]> sortResults)
00476 {
00477 List< Map<String,Integer> > newMap = new ArrayList< Map<String,Integer> >();
00478
00479 for (int media=0; media<this.numberOfMedia; media++)
00480 {
00481 Map<String,Integer> mediaRanks = new Hashtable<String,Integer>();
00482
00483 for ( String axis : sortResults.keySet() )
00484 {
00485 int rank = 0;
00486
00487 while (rank<sortResults.get(axis).length)
00488 if (sortResults.get(axis)[rank] == media)
00489 break;
00490 else
00491 rank++ ;
00492 mediaRanks.put(axis,rank);
00493 }
00494 newMap.add(mediaRanks);
00495 }
00496 return newMap;
00497 }
00498
00499
00516 public String generateFinalXML(String xmlFile, Map<String,Integer[]> sortResults)
00517 {
00518 Element rootResults = new Element("results");
00519 org.jdom.Document documentResults = new Document(rootResults);
00520 Map<Integer, Map<String,String>> mediaCharac= this.mediaBase.getContent();
00521 Map<String,String> media ;
00522 List< Map<String,Integer> > results = this.preformating(sortResults) ;
00523 Element charac;
00524 String xmlString = "A problem was encoutered!";
00525 for (Integer mediaID : mediaCharac.keySet())
00526 {
00527
00528 media = mediaCharac.get(mediaID);
00529
00530 Element mediaElement = new Element("media");
00531 Attribute identifier = new Attribute("id",String.valueOf(mediaID));
00532 mediaElement.setAttribute(identifier);
00533 for (String entry : media.keySet())
00534 {
00535 charac = new Element(entry);
00536 charac.setText(media.get(entry));
00537 mediaElement.addContent(charac);
00538 }
00539
00540 Map<String,Integer> resultsForThisMedia = results.get(mediaID);
00541 for ( Iterator<String> emotion = resultsForThisMedia.keySet().iterator(); emotion.hasNext(); )
00542 {
00543 String axis = emotion.next();
00544 Integer rank = resultsForThisMedia.get(axis);
00545 Element coordinate = new Element(axis);
00546 coordinate.setText(this.rank2value(rank).toString());
00547 mediaElement.addContent(coordinate);
00548 }
00549 rootResults.addContent(mediaElement);
00550 }
00551
00552 try
00553 {
00554 XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
00555 sortie.output(documentResults, new FileOutputStream(xmlFile));
00556 xmlString = sortie.outputString(documentResults);
00557 }
00558 catch (Exception e){ e.printStackTrace(); }
00559 return xmlString;
00560 }
00561
00562 }