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.kb.extraction;
020
021import java.net.MalformedURLException;
022import java.net.URL;
023import java.util.ArrayList;
024import java.util.List;
025import java.util.Set;
026import java.util.SortedSet;
027import java.util.TreeSet;
028
029import javax.swing.ProgressMonitor;
030
031import org.apache.log4j.Logger;
032import org.dllearner.utilities.JamonMonitorLogger;
033import org.semanticweb.owlapi.model.OWLOntology;
034
035import com.jamonapi.Monitor;
036
037/**
038 * An object of this class encapsulates everything.
039 * 
040 * @author Sebastian Hellmann
041 * 
042 */
043public class Manager {
044
045        private Configuration configuration;
046        private ExtractionAlgorithm extractionAlgorithm;
047        private int nrOfExtractedTriples = 0;
048        private List<Node> seedNodes = new ArrayList<>();
049        private boolean stop = false;
050        
051        private ProgressMonitor mon;
052        
053        private static Logger logger = Logger
054                .getLogger(Manager.class);
055        
056        
057        public void useConfiguration(Configuration configuration) {
058                this.configuration = configuration;
059                this.extractionAlgorithm = new ExtractionAlgorithm(configuration);
060        }
061
062//      public Node extractOneURI(String uri) {
063//              
064//              //logger.info("Start extracting: "+uri);
065//              Node n = extractionAlgorithm.expandNode(uri, configuration.getTupelAquisitor());
066//              //logger.info("Finished extracting: "+uri );
067//              seedNodes.add(n);
068//              return n;
069//      }
070        
071        /**
072         * Stops the algorithm...
073         * meaning only the remaining sparql queries will not be processed anymore
074         */
075        public void stop(){
076                stop = true;
077                extractionAlgorithm.stop();
078        }
079        
080        private boolean stopCondition(){
081                return stop;
082        }
083        
084        private void reset(){
085                stop = false;
086                extractionAlgorithm.reset();
087        }
088        
089        
090
091        public List<Node> extract(Set<String> instances) {
092                List<Node> allExtractedNodes = new ArrayList<>();
093                logger.info("Start extracting "+instances.size() + " instances ");
094                if(mon != null){
095                        mon.setNote("Start extracting "+instances.size() + " instances ");
096                        mon.setMaximum(instances.size());
097                }
098                int progress=0;
099                for (String one : instances) {
100                        progress++;
101                        if(mon != null){
102                                mon.setProgress(progress);
103                        }
104                        logger.info("Progress: "+progress+" of "+instances.size()+" finished: "+one);
105                        if(stopCondition()){
106                                break;
107                        }
108                        
109                        try {
110                                Node n = extractionAlgorithm.expandNode(one, configuration.getTupelAquisitor());
111                                seedNodes.add(n);
112                                allExtractedNodes.add(n);
113                        } catch (Exception e) {
114                                logger.warn("extraction failed for: "+one);
115                                e.printStackTrace();
116                                
117                        }
118                }
119                //((SparqlTupleAquisitor) configuration.getTupelAquisitor()).printHM();
120                //System.exit(0);
121                reset();
122                logger.info("Finished extraction");
123                return allExtractedNodes;
124                
125        }
126        
127        public OWLOntology getOWLAPIOntologyForNodes(List<Node> nodes, boolean saveOntology){
128                Monitor m1 = JamonMonitorLogger.getTimeMonitor(Manager.class, "Time conversion to OWL Ontology").start();
129                for (Node n : nodes) {
130                        n.toOWLOntology(configuration.getOwlAPIOntologyCollector());
131                }
132                m1.stop();
133                
134                if(saveOntology){
135                        Monitor m2 = JamonMonitorLogger.getTimeMonitor(Manager.class, "Time saving Ontology").start();
136                        configuration.getOwlAPIOntologyCollector().saveOntology();
137                        m2.stop();
138                }
139                return configuration.getOwlAPIOntologyCollector().getCurrentOntology();
140                
141        }
142        
143        public URL getPhysicalOntologyURL()throws MalformedURLException{
144                return configuration.getOwlAPIOntologyCollector().getPhysicalIRI().toURI().toURL();
145                
146        }
147        
148        public String getNTripleForAllExtractedNodes(){
149                return getNTripleForNodes(seedNodes);
150        }
151        
152        public String getNTripleForNodes(List<Node> nodes){
153                SortedSet<String> tripleCollector = new TreeSet<>();
154                for (Node n : nodes) {
155                        tripleCollector.addAll(n.toNTriple());
156                }
157                logger.info("Converting to NTriple");
158                StringBuffer nt = new StringBuffer(100000);
159                Object[] arr = tripleCollector.toArray();
160                nrOfExtractedTriples = arr.length;
161                for (int i = 0; i < arr.length; i++) {
162                        nt.append((String) arr[i]).append("\n");
163                        if (i % 1000 == 0)
164                                logger.info(i + " of  " + arr.length + " triples done");
165                }
166                logger.info(arr.length + " of  " + arr.length + " triples done");
167                logger.info("Ontology String size = " + nt.length());
168                return nt.toString();
169        }
170
171
172        public Configuration getConfiguration() {
173                return configuration;
174        }
175
176        @Deprecated
177        public int getNrOfExtractedTriples() {
178                return nrOfExtractedTriples;
179        }
180
181        public void addProgressMonitor(ProgressMonitor mon){
182                this.mon = mon;
183        }
184}