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