001    /**
002     * Copyright (C) 2007-2011, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     *
006     * DL-Learner is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * DL-Learner is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     */
019    
020    package org.dllearner.utilities.learn;
021    
022    import java.io.File;
023    import java.io.FileOutputStream;
024    import java.io.IOException;
025    import java.util.SortedSet;
026    import java.util.TreeSet;
027    
028    import org.dllearner.core.owl.Individual;
029    
030    public class ConfWriter {
031    
032            StringBuffer stats = new StringBuffer("/**STATS:\n");
033    
034            public String workingDir = "examples/stest/";
035    
036            public void writeROLLearnerOWLFileInd(String filename, SortedSet<Individual> pos,
037                            SortedSet<Individual> neg, String owlFile, SortedSet<String> ignoredConcepts) {
038    
039                    writeROLLearnerOWLFile(filename, toString(pos), toString(neg), owlFile, ignoredConcepts);
040            }
041    
042            public void writeROLLearnerOWLFile(String filename, SortedSet<String> pos,
043                            SortedSet<String> neg, String owlFile, SortedSet<String> ignoredConcepts) {
044    
045                    String prefix = "refexamples";
046                    StringBuffer buf = new StringBuffer();
047                    buf.append("import(\"" + owlFile + "\");\n\n");
048    
049                    buf.append(getIgnoredConcepts(ignoredConcepts, prefix));
050    
051                    buf.append(getStandard(prefix));
052    
053                    buf.append(getPosAndNeg(pos, neg));
054    
055                    stats.append(ignoredConcepts + "\n");
056                    stats.append("No of positive Examples: " + pos.size() + "\n");
057                    stats.append("No of negative Examples: " + neg.size() + "\n");
058                    stats.append("\n**/\n\n");
059    
060                    writeToFile(filename, stats.toString() + buf.toString());
061    
062            }
063    
064            public void writeSPARQL(String filename, SortedSet<String> pos, SortedSet<String> neg,
065                            String uri, SortedSet<String> ignoredConcepts, String standardSettings, String prefixAlgortihm) {
066    
067            
068                    String prefixSparql = "sparql";
069    
070                    
071    
072    
073                    // "sparql.format = \"KB\";\n\n";
074    
075                    StringBuffer buf = new StringBuffer();
076                    buf.append("import(\"" + uri + "\",\"SPARQL\");\n\n");
077    
078                    buf.append(standardSettings);
079                    buf.append(getIgnoredConcepts(ignoredConcepts, prefixAlgortihm));
080                    buf.append(getStandard(prefixAlgortihm));
081                    buf.append(sparqlInstances(pos, neg, prefixSparql));
082                    buf.append(getPosAndNeg(pos, neg));
083    
084                    stats.append(ignoredConcepts + "\n");
085                    stats.append("No of positive Examples: " + pos.size() + "\n");
086                    stats.append("No of negative Examples: " + neg.size() + "\n");
087                    stats.append("\n**/\n\n");
088    
089                    writeToFile(filename, stats.toString() + buf.toString());
090    
091            }
092    
093            public String sparqlInstances(SortedSet<String> pos, SortedSet<String> neg, String prefix) {
094                    SortedSet<String> ret = new TreeSet<String>();
095                    ret.addAll(pos);
096                    ret.addAll(neg);
097    
098                    return getStringSet(ret, prefix, "instances");
099    
100            }
101    
102            public String getPosAndNeg(SortedSet<String> pos, SortedSet<String> neg) {
103                    StringBuffer buf = new StringBuffer();
104                    buf.append("\n\n");
105                    for (String individuals : pos) {
106                            buf.append("+\"" + individuals + "\"\n");
107    
108                    }
109                    buf.append("\n\n");
110                    for (String individuals : neg) {
111    
112                            buf.append("-\"" + individuals + "\"\n");
113                    }
114    
115                    return buf.toString();
116            }
117    
118            public String getIgnoredConcepts(SortedSet<String> ignoredConcepts, String prefix) {
119    
120                    return getStringSet(ignoredConcepts, prefix, "ignoredConcepts");
121            }
122    
123            public String getStringSet(SortedSet<String> set, String prefix, String type) {
124                    if (set.size() == 0)
125                            return "\n";
126                    String ret = prefix + "." + type + "={\n";
127                    int x = 0;
128                    for (String string : set) {
129                            if (x > 0)
130                                    ret += ",";
131                            ret += "\"" + string + "\"\n";
132                            x++;
133                    }
134                    ret += "};\n";
135                    return ret;
136            }
137    
138            public String getStandard(String prefix) {
139                    String ret = "algorithm = " + prefix + ";\n" + "reasoner=fastInstanceChecker;\n\n" +
140    
141                    prefix + ".useAllConstructor = false;\n" + prefix + ".useExistsConstructor = true;\n"
142                                    + prefix + ".useCardinalityRestrictions = false;\n" + prefix
143                                    + ".useNegation = false;\n";
144    
145                    return ret;
146    
147            }
148    
149            protected void writeToFile(String filename, String content) {
150                    // create the file we want to use
151                    File file = new File(workingDir + filename);
152    
153                    try {
154                            file.createNewFile();
155                            FileOutputStream fos = new FileOutputStream(workingDir + filename, false);
156                            // ObjectOutputStream o = new ObjectOutputStream(fos);
157                            fos.write(content.getBytes());
158                            fos.flush();
159                            fos.close();
160                    } catch (IOException e) {
161                            e.printStackTrace();
162                    }
163            }
164    
165            protected SortedSet<String> toString(SortedSet<Individual> set) {
166                    SortedSet<String> ret = new TreeSet<String>();
167                    for (Individual ind : set) {
168                            ret.add(ind.toString());
169                    }
170                    return ret;
171            }
172            
173            
174            public static String listExamples (boolean posOrNeg, SortedSet<Individual> s ){
175                    StringBuffer sbuf = new StringBuffer();
176                    String sign = (posOrNeg)?"+":"-";
177                    for (Individual individual : s) {
178                            sbuf.append(sign+"\""+individual+"\"\n");
179                    }
180                    
181                    return sbuf.toString();
182            }
183            
184    
185            public void addToStats(String add) {
186                    stats.append(add);
187            }
188    
189    }