From: <ku...@us...> - 2008-09-25 19:27:17
|
Revision: 1260 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=1260&view=rev Author: kurzum Date: 2008-09-25 19:26:59 +0000 (Thu, 25 Sep 2008) Log Message: ----------- - added constructor in gui.config - new Classes for collecting data and writing a latex table Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/gui/Config.java trunk/src/dl-learner/org/dllearner/scripts/NewSample.java trunk/src/dl-learner/org/dllearner/scripts/SemanticBibleComparison.java trunk/src/dl-learner/org/dllearner/utilities/Files.java Added Paths: ----------- trunk/src/dl-learner/org/dllearner/utilities/statistics/Table.java trunk/src/dl-learner/org/dllearner/utilities/statistics/TableColumn.java Modified: trunk/src/dl-learner/org/dllearner/gui/Config.java =================================================================== --- trunk/src/dl-learner/org/dllearner/gui/Config.java 2008-09-25 16:34:53 UTC (rev 1259) +++ trunk/src/dl-learner/org/dllearner/gui/Config.java 2008-09-25 19:26:59 UTC (rev 1260) @@ -76,6 +76,30 @@ private StartGUI gui; /** + * This constructor can be used systemwide to save configurations in conf files. + * Of course it should not really belong here, but either in core or utilities. + * Consider refactoring using a subclass of Config for the GUI. + * Nevertheless it still works. + * + * + * @param cm + * @param source + * @param reasoner + * @param rs + * @param lp + * @param la + */ + public Config(ComponentManager cm, KnowledgeSource source, ReasonerComponent reasoner, ReasoningService rs, LearningProblem lp, LearningAlgorithm la) { + super(); + this.cm = cm; + this.source = source; + this.reasoner = reasoner; + this.rs = rs; + this.lp = lp; + this.la = la; + } + + /** * Create central configuration object. * * @param gui Modified: trunk/src/dl-learner/org/dllearner/scripts/NewSample.java =================================================================== --- trunk/src/dl-learner/org/dllearner/scripts/NewSample.java 2008-09-25 16:34:53 UTC (rev 1259) +++ trunk/src/dl-learner/org/dllearner/scripts/NewSample.java 2008-09-25 19:26:59 UTC (rev 1260) @@ -41,6 +41,9 @@ import org.dllearner.core.LearningProblemUnsupportedException; import org.dllearner.core.ReasoningService; import org.dllearner.core.configurators.ComponentFactory; +import org.dllearner.gui.Config; +import org.dllearner.gui.ConfigSave; +import org.dllearner.gui.StartGUI; import org.dllearner.kb.OWLFile; import org.dllearner.learningproblems.PosNegDefinitionLP; import org.dllearner.reasoning.FastInstanceChecker; Modified: trunk/src/dl-learner/org/dllearner/scripts/SemanticBibleComparison.java =================================================================== --- trunk/src/dl-learner/org/dllearner/scripts/SemanticBibleComparison.java 2008-09-25 16:34:53 UTC (rev 1259) +++ trunk/src/dl-learner/org/dllearner/scripts/SemanticBibleComparison.java 2008-09-25 19:26:59 UTC (rev 1260) @@ -114,19 +114,16 @@ logger.warn("Start"); File tmpFile = new File(tmpFilename); - - List<File> confs = getFilesContaining(useSPARQL,"ten","all", "99+"); - //analyzeFiles(confs); - - - + List<String> confs = getFiles(useSPARQL); + reasoningService = ReasoningServiceFactory.getReasoningService(ontologyPath, AvailableReasoners.OWLAPIREASONERPELLET); ComponentManager cm =ComponentManager.getInstance(); + try{ - for (File f : confs) { - + for (String filename : confs) { + File f = new File(filename); Cache.getDefaultCache().clearCache(); String fileContent = Files.readFile(f); @@ -300,6 +297,16 @@ return null; } + + public static List<String> getFiles(boolean sparql){ + String actualDir = (sparql)?sparqldir:normaldir; + logger.warn(actualDir); + File f = new File(actualDir); + String[] files = f.list(); + Arrays.sort(files); + return Arrays.asList(files); + } + public static List<File> getFilesContaining(boolean sparql, String numExamples, String allOrEx, String acc) { List<File> ret = new ArrayList<File>(); //SortedSet<File> ret = new TreeSet<File>(); Modified: trunk/src/dl-learner/org/dllearner/utilities/Files.java =================================================================== --- trunk/src/dl-learner/org/dllearner/utilities/Files.java 2008-09-25 16:34:53 UTC (rev 1259) +++ trunk/src/dl-learner/org/dllearner/utilities/Files.java 2008-09-25 19:26:59 UTC (rev 1260) @@ -19,13 +19,22 @@ */ package org.dllearner.utilities; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.ArrayList; +import java.util.List; +import com.ibm.icu.util.StringTokenizer; + /** * @author Jens Lehmann * @@ -41,8 +50,7 @@ * @return Content of the file. */ public static String readFile(File file) throws FileNotFoundException, IOException { - - + BufferedReader br = new BufferedReader(new FileReader(file)); StringBuffer content = new StringBuffer(); try{ @@ -56,6 +64,74 @@ return content.toString(); } + + /** + * Reads in a file as Array + * + * @param file + * The file to read. + * @return StringArray with lines + */ + public static String[] readFileAsArray(File file) throws FileNotFoundException, IOException { + String content = readFile(file); + StringTokenizer st = new StringTokenizer(content, System.getProperty("line.separator")); + List<String> l = new ArrayList<String>(); + while (st.hasMoreTokens()) { + l.add((String) st.nextToken()); + + } + + return l.toArray(new String[l.size()]); + + } + + /** + * writes a serializable Object to a File. + * @param obj + * @param file + */ + public static void writeObjectToFile(Object obj, File file){ + + ObjectOutputStream oos = null; + try{ + FileOutputStream fos = new FileOutputStream(file); + BufferedOutputStream bos = new BufferedOutputStream(fos); + oos = new ObjectOutputStream(bos); + + oos.writeObject(obj); + oos.flush(); + oos.close(); + }catch (Exception e) { + e.printStackTrace(); + }finally{ + try{ + oos.close(); + }catch (Exception e) { + e.printStackTrace(); + } + } + } + + public static Object readObjectfromFile( File file){ + ObjectInputStream ois = null; + try{ + FileInputStream fis = new FileInputStream(file); + BufferedInputStream bis = new BufferedInputStream(fis); + ois = new ObjectInputStream(bis); + return ois.readObject(); + }catch (Exception e) { + e.printStackTrace(); + }finally{ + try { + ois.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + return null; + } + /** * Creates a new file with the given content or replaces the content of a @@ -185,5 +261,7 @@ e.printStackTrace(); } } + + } Added: trunk/src/dl-learner/org/dllearner/utilities/statistics/Table.java =================================================================== --- trunk/src/dl-learner/org/dllearner/utilities/statistics/Table.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/utilities/statistics/Table.java 2008-09-25 19:26:59 UTC (rev 1260) @@ -0,0 +1,205 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +package org.dllearner.utilities.statistics; + +import java.io.File; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import org.dllearner.utilities.Files; + + +/** + * Class to collect results and output them as a latex table or other formats. + * + * @author Sebastian Hellmann + * + */ +public class Table implements Serializable{ + + private static final long serialVersionUID = 0l; + + //used to give a good percentage output + //private DecimalFormat df = new DecimalFormat( ".00%" ); + private List<TableColumn> columns = new ArrayList<TableColumn>(); + + private String tableName = ""; + private String caption = ""; + private String label = ""; + + public Table(String tableName){ + this.tableName = tableName; + } + + public static void main(String[] args) { + Table t = new Table("myTable"); + String tableFile = "results/table/myTable"; + TableColumn c1 = new TableColumn("col1", new String[]{"a","b"}); + TableColumn c2 = new TableColumn("col2", new String[]{"c","d"}); + t.addColumn(c1); + System.out.println(t.getLatexString()); + + serializeColumns(t, "results/table",tableFile ); + + t = createTableFromSerializedColums("myTable", tableFile); + System.out.println(t.getLatexString()); + + t.addColumn(c2); + serializeColumns(t, "results/table",tableFile ); + t = createTableFromSerializedColums("myTable", tableFile); + System.out.println(t.getLatexString()); + + + + } + + public String getLatexString(){ + String tabular = ""; + for (int i = 0; i < columns.size(); i++) { + tabular+="l"; + } + + String headers = latexRow(getColumnHeaders()); + + String table=""; + + table += "\\begin{table*}\n"; + table += "\t\\centering\n"; + table += "\t\t\\begin{tabular}{"+tabular+"}\n"; + table += "\\hline\n"; + table += headers; + table += "\\hline\n"; + // add here + for (int i = 0; i < getNumberOfRows(); i++) { + table +=getRowInLatex(i); + } + table += "\\end{tabular}\n"; + table += "\t\\caption{"+caption+"}\n"; + table += "\t\\label{"+label+"}\n"; + table += "\\end{table*}\n\n"; + + //List<String> myList = new ArrayList<String>({""}); + + //List<String> list = Arrays.asList( "","" ); + return table; + + } + + public String getRowInLatex(int index){ + List<String> l = new ArrayList<String>(); + for(TableColumn c: columns){ + l.add(c.getEntry(index)); + } + return latexRow(l); + } + + public int getNumberOfRows(){ + if(columns.isEmpty())return 0; + else return columns.get(0).getSize(); + } + + + public List<String> getColumnHeaders(){ + List<String> entries = new ArrayList<String>(); + for (TableColumn c : columns) { + entries.add(c.getHeader()); + } + return entries; + } + + public String latexRow(List<String> entries){ + String ret=""; + for (String one : entries) { + ret+=" "+one+"\t& "; + } + ret = ret.substring(0,ret.length()-3); + ret+="\t\\\\\n"; + return ret; + } + + public void addColumn(TableColumn c){ + if(columns.isEmpty()){ + columns.add(c); + }else{ + if(getNumberOfRows()!=c.getSize()){ + System.out.println("ERROR: size of columns doesn't match"); + System.exit(0); + }else{ + columns.add(c); + } + } + } + + public static Table createTableFromSerializedColums(String tableName, String tableFile){ + Table ret = new Table(tableName); + try{ + + String[] columnFiles = Files.readFileAsArray(new File(tableFile)); + for (String filename : columnFiles) { + TableColumn col = TableColumn.deSerialize(new File(filename)); + //TableColumn col = (TableColumn) Files.readObjectfromFile(new File(filename)); + ret.addColumn(col); + } + // FileWriter fw = new FileWriter (); + }catch (Exception e) { + e.printStackTrace(); + } + return ret; + } + + public static void serializeColumns(Table t, String dir, String tableFile){ + String column = "column"; + String content = ""; + Files.mkdir(dir); + try{ + int i=0; + for(TableColumn c:t.getColumns()){ + String filename = dir+File.separator+t.getTableName()+column+(i++); + c.serialize(new File(filename)); + //Files.writeObjectToFile(c, new File(filename)); + content += filename+System.getProperty("line.separator"); + } + Files.createFile(new File(tableFile), content); + // + //FileWriter fw = new FileWriter (); + }catch (Exception e) { + e.printStackTrace(); + } + } + + + public List<TableColumn> getColumns() { + return columns; + } + + + public String getTableName() { + return tableName; + } + + + public void setTableName(String tableName) { + this.tableName = tableName; + } + + +} Added: trunk/src/dl-learner/org/dllearner/utilities/statistics/TableColumn.java =================================================================== --- trunk/src/dl-learner/org/dllearner/utilities/statistics/TableColumn.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/utilities/statistics/TableColumn.java 2008-09-25 19:26:59 UTC (rev 1260) @@ -0,0 +1,120 @@ +/** + * Copyright (C) 2007, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +package org.dllearner.utilities.statistics; + +import java.io.File; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.dllearner.utilities.Files; + + +public class TableColumn implements Serializable { + + private static final long serialVersionUID = 1l; + private String header; + private List<String> entries = new ArrayList<String>(); + +public TableColumn() { + super(); + +} + +public TableColumn(String header) { + super(); + this.header = header; +} + + +public TableColumn( String[] entries) { + this.entries = Arrays.asList(entries); +} + + +public TableColumn(String header, String[] entries) { + this(header); + this.entries = Arrays.asList(entries); +} + +public TableColumn( List<String> entries) { + this.entries = entries; +} + + +public String getHeader() { + return header; +} + +public void setHeader(String header) { + this.header = header; +} + +/** + * entires should be in Latex, if the target is latex + * @param entry + */ +public void addEntry(String entry){ + entries.add(entry); +} + +public int getSize(){ + return entries.size(); +} + +public String getEntry(int index){ + return entries.get(index); +} + +public void serialize(File file){ + String content = header+System.getProperty("line.separator"); + for (String entry : entries) { + content += entry+System.getProperty("line.separator"); + } + Files.createFile(file, content); +} + +public static TableColumn deSerialize(File f){ + TableColumn ret = null; + try{ + String[] c = Files.readFileAsArray(f); + ret = new TableColumn(); + boolean first = true; + for (String line : c) { + if(first){ + first = false; + ret.setHeader(line); + + }else{ + ret.addEntry(line); + } + + } + }catch (Exception e) { + e.printStackTrace(); + } + + return ret; +} + + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |