001/**
002 * Copyright (C) 2007 - 2016, 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 */
019package org.dllearner.utilities.examples;
020
021import org.dllearner.algorithms.ocel.OCEL;
022import org.dllearner.core.KnowledgeSource;
023import org.dllearner.kb.OWLFile;
024import org.dllearner.learningproblems.PosNegLPStandard;
025import org.dllearner.reasoning.ClosedWorldReasoner;
026import org.dllearner.utilities.Files;
027import org.dllearner.utilities.Helper;
028import org.dllearner.utilities.URLencodeUTF8;
029
030import java.io.File;
031import java.io.FileWriter;
032import java.io.IOException;
033import java.net.MalformedURLException;
034import java.net.URL;
035import java.util.*;
036
037/**
038 * Creates a temporary file with all the data collected for examples.
039 * e.g.:
040 * basedir = tiger/  
041 * one Example file with nt data = tiger/uri
042 * uris will be urlencoded
043 * 
044 *  
045 * @author Sebastian Hellmann <hellmann@informatik.uni-leipzig.de>
046 *
047 */
048public class ExampleDataCollector {
049
050        SortedSet<String> exampleURIs;
051        private String baseDir;
052        
053        public ExampleDataCollector(String baseDir, SortedSet<String> exampleURIs, List<File> additionalSources){
054                this.exampleURIs=exampleURIs;
055                this.baseDir = baseDir(baseDir);
056        }
057        
058        public static void main(String[] args) {
059                String b = "http://nlp2rdf.org/ontology/s";
060                String baseDir = "examples/nlp2rdf/tiger/";
061                
062                SortedSet<String> pos = new TreeSet<>(Arrays.asList(new String[]{b + "197", b + "2013", b + "2704"}));
063                SortedSet<String> neg = new TreeSet<>(Arrays.asList(new String[]{b + "1", b + "2", b + "3"}));
064                List<URL> urls = new ArrayList<>();
065                urls.addAll (convert(baseDir, pos));
066                urls.addAll (convert(baseDir, neg));
067                
068                Set<KnowledgeSource> tmp = new HashSet<>();
069                try {
070                        URL add = new File(baseDir+"tiger.rdf").toURI().toURL();
071//                       add = new File(baseDir+"new.rdf").toURI().toURL();
072                        urls.add(add);
073                        
074                        for(URL u: urls){
075                                OWLFile ks = new OWLFile();
076                                ks.setUrl(u);
077                                tmp.add(ks);
078                        }
079                        
080                        ClosedWorldReasoner rc = new ClosedWorldReasoner(tmp);
081                        PosNegLPStandard lp = new PosNegLPStandard(rc);
082                        lp.setPositiveExamples(Helper.getIndividualSet(pos));
083                        lp.setNegativeExamples(Helper.getIndividualSet(neg));
084                        OCEL la = new OCEL(lp, rc);
085                        for(KnowledgeSource ks: tmp){
086                                ks.init();
087                        }
088                        rc.init();
089                        lp.init();
090                        la.init();
091
092                        // start learning algorithm
093                        la.start();
094
095                } catch (Exception e) {
096                        e.printStackTrace();
097                }
098        }
099        
100        public static List<URL> convert(String baseDir, SortedSet<String> exampleURIs){
101                List<URL> u = new ArrayList<>();
102                for (String exampleURI : exampleURIs) {
103                        try {
104                                u.add(new File(toFileName(baseDir, exampleURI)).toURI().toURL());
105                        } catch (MalformedURLException e) {
106                                e.printStackTrace();
107                        }
108                
109                }
110                return u;
111                
112        }
113        
114        @SuppressWarnings("unused")
115        private File collect(){
116                String from = null;
117                File tmpFile = null;
118                FileWriter fw = null;
119                try{
120                        tmpFile = File.createTempFile(ExampleDataCollector.class.getSimpleName(), null);
121                        fw = new FileWriter(tmpFile, false);
122                        for(String one: exampleURIs){
123                                from = toFileName(baseDir, one) ;
124                                addToFile(fw, from);
125                        }
126                }catch (Exception e) {
127                        e.printStackTrace();
128                }finally{
129                                try {
130                                        fw.close();
131                                } catch (IOException e) {
132                                        e.printStackTrace();
133                                }
134                        }
135                
136                return tmpFile;
137        }
138        
139        private void addToFile(FileWriter fw, String fileName)throws Exception{
140                String tmp = Files.readFile(new File(fileName));
141                fw.write(tmp);
142                fw.flush();
143        }
144        
145        private static String toFileName(String baseDir, String exampleURI){
146                        return baseDir(baseDir)+URLencodeUTF8.encode(exampleURI);
147        }
148        
149        private static String baseDir(String baseDir){
150                return (baseDir.endsWith(File.separator))?baseDir:baseDir+File.separator;
151        }
152        
153}