001/* 002 * To change this license header, choose License Headers in Project Properties. 003 * To change this template file, choose Tools | Templates 004 * and open the template in the editor. 005 */ 006package org.dllearner.algorithms.probabilistic.parameter.unife.edge; 007 008import java.math.BigDecimal; 009import java.util.Collections; 010import java.util.Map; 011import java.util.Set; 012import org.dllearner.core.AbstractReasonerComponent; 013import org.dllearner.core.ComponentInitException; 014import org.dllearner.core.KnowledgeSource; 015import org.dllearner.core.ReasoningMethodUnsupportedException; 016import org.dllearner.core.config.ConfigOption; 017import org.dllearner.kb.OWLAPIOntology; 018import org.dllearner.reasoning.ClosedWorldReasoner; 019import org.dllearner.reasoning.OWLAPIReasoner; 020import org.dllearner.core.probabilistic.unife.AbstractParameterLearningAlgorithm; 021import org.dllearner.core.probabilistic.unife.ParameterLearningException; 022import static org.dllearner.algorithms.probabilistic.parameter.unife.edge.AbstractEDGE.PossibleOutputFormat.*; 023import org.dllearner.learningproblems.ClassLearningProblem; 024import org.mindswap.pellet.utils.Timers; 025import org.semanticweb.owlapi.model.OWLAxiom; 026import org.semanticweb.owlapi.model.OWLOntology; 027import unife.edge.EDGE; 028import unife.edge.EDGEStat; 029import unife.exception.IllegalValueException; 030 031/** 032 * 033 * @author Giuseppe Cota <giuseppe.cota@unife.it>, Riccardo Zese 034 * <riccardo.zese@unife.it> 035 */ 036public abstract class AbstractEDGE extends AbstractParameterLearningAlgorithm { 037 038 protected EDGE edge; 039 040 public static enum PossibleOutputFormat { 041 042 OWLXML, 043 OWLFUNCTIONAL 044 } 045 046 @ConfigOption(description = "seed for random generation", defaultValue = "0") 047 protected int seed = 0; 048 049 @ConfigOption(description = "randomize the starting probabilities of the probabilistic axioms", defaultValue = "false") 050 protected boolean randomize = false; 051 052 // se randomize All è true allora anche randomize è true 053 @ConfigOption(description = "make probabilistic all the axioms in the starting probabilistic " 054 + "ontology (including non probabilistic ones)", defaultValue = "false") 055 protected boolean probabilizeAll = false; 056 057 @ConfigOption(description = "stop difference between log-likelihood of two consecutive EM cycles", defaultValue = "0.000000000028") 058 protected double differenceLL = 0.00028; 059 060 @ConfigOption(description = "stop ratio between log-likelihood of two consecutive EM cycles", defaultValue = "0.000000000028") 061 protected double ratioLL = 0.00028; 062 063 @ConfigOption(description = "maximum number of cycles", defaultValue = "" + Long.MAX_VALUE) 064 protected long maxIterations = Long.MAX_VALUE; 065 066 @ConfigOption(description = "the maximum number of explanations to find for each query", defaultValue = "" + Integer.MAX_VALUE) 067 protected int maxExplanations = Integer.MAX_VALUE; 068 // ATTENZIONE EDGE USA IL FORMATO STRING PER TIMEOUT!!! 069 @ConfigOption(description = "max time allowed for the inference (format: [0-9]h[0-9]m[0-9]s)", defaultValue = "0s (infinite timeout)") 070 protected String timeout = "0s"; 071 072 @ConfigOption(description = "force the visualization of all results", defaultValue = "false") 073 protected boolean showAll = false; 074 075 @ConfigOption(description = "format of the output file", defaultValue = "OWLXML") 076 protected PossibleOutputFormat outputformat = OWLXML; 077 078 @ConfigOption(description = "max number of positive examples that edge must handle when a class learning problem is given", defaultValue = "0 (infinite)") 079 protected int maxPositiveExamples = 0; 080 081 @ConfigOption(description = "max number of negative examples that edge must handle when a class learning problem is given", defaultValue = "0 (infinite)") 082 protected int maxNegativeExamples = 0; 083 084 @ConfigOption(description = "accuracy used during the computation of the probabilistic values (number of digital places)", defaultValue = "5") 085 protected int accuracy = 5; 086 087 @ConfigOption(description = "If true EDGE keeps the old parameter values of all the probabilistic axioms and it does not relearn them", defaultValue = "false") 088 protected boolean keepParameters = false; 089 090 protected Set<OWLAxiom> positiveExampleAxioms; 091 protected Set<OWLAxiom> negativeExampleAxioms; 092 093 protected EDGEStat results; 094 095 // ontology obtained by merging all the sources 096 protected OWLOntology sourcesOntology; 097 098 public AbstractEDGE() { 099 100 } 101 102 public AbstractEDGE(ClassLearningProblem lp, Set<OWLAxiom> targetAxioms) { 103 super(lp, targetAxioms); 104 } 105 106 /** 107 * Get the Log-Likelihood of all the examples/queries. 108 * 109 * @return the log-likelihood of all the examples/queries 110 */ 111 public BigDecimal getLL() { 112 if (results != null) { 113 return results.getLL(); 114 } else { 115// return new BigDecimal("-500.04"); // stub 116 throw new NullPointerException("EDGE results are NULL"); 117 } 118 } 119 120 @Override 121 public Map<String, Long> getTimeMap() { 122 if (results != null) { 123 return results.getTimers(); 124 } else { 125 throw new NullPointerException("EDGE results are NULL"); 126 } 127 } 128 129 @Override 130 public void init() throws ComponentInitException { 131 try { 132 edge.setAccuracy(accuracy); 133 edge.setDiffLL("" + differenceLL); 134 edge.setMaxExplanations(maxExplanations); 135 edge.setMaxIterations(maxIterations); 136 edge.setMerge(true); 137 edge.setRandomize(randomize); 138 edge.setProbabilizeAll(probabilizeAll); 139 edge.setRatioLL("" + ratioLL); 140 edge.setSeed(seed); 141 edge.setShowAll(showAll); 142 edge.setTimeOut(timeout); 143 //edge.setTimers(new Timers()); 144 AbstractReasonerComponent rc = learningProblem.getReasoner(); 145 if (rc instanceof ClosedWorldReasoner) { 146 sourcesOntology = ((ClosedWorldReasoner) rc).getReasonerComponent().getOntology(); 147 } else if (rc instanceof OWLAPIReasoner) { 148 sourcesOntology = ((OWLAPIReasoner) rc).getOntology(); 149 } else { 150 throw new ReasoningMethodUnsupportedException("Unsupported Reasoning: " 151 + rc.getClass()); 152 } 153 edge.setOntologies(sourcesOntology); 154 } catch (IllegalValueException ilve) { 155 throw new ComponentInitException(ilve); 156 } catch (ReasoningMethodUnsupportedException rmue) { 157 throw new ComponentInitException(rmue); 158 } 159 } 160 161 @Override 162 public BigDecimal getParameter(OWLAxiom ax) throws ParameterLearningException { 163 Map<OWLAxiom, BigDecimal> pMap = edge.getPMap(); 164 BigDecimal parameter; 165 for (OWLAxiom axiom : pMap.keySet()) { 166 if (axiom.equalsIgnoreAnnotations(ax)) { 167 parameter = pMap.get(axiom); 168 return parameter; 169 } 170 } 171 172 return null; 173 } 174 175 /** 176 * @return the seed 177 */ 178 public int getSeed() { 179 return seed; 180 } 181 182 /** 183 * @param seed the seed to set 184 */ 185 public void setSeed(int seed) { 186 this.seed = seed; 187 } 188 189 /** 190 * @return the randomize 191 */ 192 public boolean isRandomize() { 193 return randomize; 194 } 195 196 /** 197 * @param randomize the randomize to set 198 */ 199 public void setRandomize(boolean randomize) { 200 this.randomize = randomize; 201 } 202 203 /** 204 * @return the randomizeAll 205 */ 206 public boolean isRandomizeAll() { 207 return probabilizeAll; 208 } 209 210 /** 211 * @param probabilizeAll the randomizeAll to set 212 */ 213 public void setProbabilizeAll(boolean probabilizeAll) { 214 this.probabilizeAll = probabilizeAll; 215 } 216 217 /** 218 * @return the differenceLL 219 */ 220 public double getDifferenceLL() { 221 return differenceLL; 222 } 223 224 /** 225 * @param differenceLL the differenceLL to set 226 */ 227 public void setDifferenceLL(double differenceLL) { 228 this.differenceLL = differenceLL; 229 } 230 231 /** 232 * @return the ratioLL 233 */ 234 public double getRatioLL() { 235 return ratioLL; 236 } 237 238 /** 239 * @param ratioLL the ratioLL to set 240 */ 241 public void setRatioLL(double ratioLL) { 242 this.ratioLL = ratioLL; 243 } 244 245 /** 246 * @return the maxIterations 247 */ 248 public long getMaxIterations() { 249 return maxIterations; 250 } 251 252 /** 253 * @param maxIterations the maxIterations to set 254 */ 255 public void setMaxIterations(long maxIterations) { 256 this.maxIterations = maxIterations; 257 } 258 259 /** 260 * @return the maxExplanations 261 */ 262 public int getMaxExplanations() { 263 return maxExplanations; 264 } 265 266 /** 267 * @param maxExplanations the maxExplanations to set 268 */ 269 public void setMaxExplanations(int maxExplanations) { 270 this.maxExplanations = maxExplanations; 271 } 272 273 /** 274 * @return the timeout 275 */ 276 public String getTimeout() { 277 return timeout; 278 } 279 280 /** 281 * @param timeout the timeout to set 282 */ 283 public void setTimeout(String timeout) { 284 this.timeout = timeout; 285 } 286 287 /** 288 * @return the showAll 289 */ 290 public boolean isShowAll() { 291 return showAll; 292 } 293 294 /** 295 * @param showAll the showAll to set 296 */ 297 public void setShowAll(boolean showAll) { 298 this.showAll = showAll; 299 } 300 301 /** 302 * @return the outputformat 303 */ 304 public PossibleOutputFormat getOutputformat() { 305 return outputformat; 306 } 307 308 /** 309 * @param outputformat the outputformat to set 310 */ 311 public void setOutputformat(PossibleOutputFormat outputformat) { 312 this.outputformat = outputformat; 313 } 314 315 /** 316 * @return the maxPositiveExamples 317 */ 318 public int getMaxPositiveExamples() { 319 return maxPositiveExamples; 320 } 321 322 /** 323 * @param maxPositiveExamples the maxPositiveExamples to set 324 */ 325 public void setMaxPositiveExamples(int maxPositiveExamples) { 326 this.maxPositiveExamples = maxPositiveExamples; 327 } 328 329 /** 330 * @return the maxNegativeExamples 331 */ 332 public int getMaxNegativeExamples() { 333 return maxNegativeExamples; 334 } 335 336 /** 337 * @param maxNegativeExamples the maxNegativeExamples to set 338 */ 339 public void setMaxNegativeExamples(int maxNegativeExamples) { 340 this.maxNegativeExamples = maxNegativeExamples; 341 } 342 343 /** 344 * @return the positiveExamples 345 */ 346 public Set<OWLAxiom> getPositiveExampleAxioms() { 347 return positiveExampleAxioms; 348 } 349 350 /** 351 * @param positiveExampleAxioms the positiveExamples to set 352 */ 353 public void setPositiveExampleAxioms(Set<OWLAxiom> positiveExampleAxioms) { 354 this.positiveExampleAxioms = positiveExampleAxioms; 355 } 356 357 /** 358 * @return the negativeExamples 359 */ 360 public Set<OWLAxiom> getNegativeExampleAxioms() { 361 return negativeExampleAxioms; 362 } 363 364 /** 365 * @param negativeExampleAxioms the negativeExamples to set 366 */ 367 public void setNegativeExampleAxioms(Set<OWLAxiom> negativeExampleAxioms) { 368 this.negativeExampleAxioms = negativeExampleAxioms; 369 } 370 371 /** 372 * @return the accuracy 373 */ 374 public int getAccuracy() { 375 return accuracy; 376 } 377 378 /** 379 * @param accuracy the accuracy to set 380 */ 381 public void setAccuracy(int accuracy) { 382 this.accuracy = accuracy; 383 } 384 385 public OWLOntology getLearnedOntology() { 386 return edge.getLearnedOntology(); 387 } 388 389 /** 390 * @return the ontologySources 391 */ 392 public OWLOntology getSourcesOntology() { 393 return sourcesOntology; 394 } 395 396 public void changeSourcesOntology(OWLOntology ontology) { 397 sourcesOntology = ontology; 398 edge.setOntologies(ontology); 399 learningProblem.getReasoner().changeSources(Collections.singleton((KnowledgeSource) new OWLAPIOntology(ontology))); 400// learningProblem.getReasoner().changeSources(Collections.singleton((KnowledgeSource) ontology)); 401 } 402 403 public void reset() { 404 edge.reset(); 405 isRunning = false; 406 stop = false; 407 } 408 409 public BigDecimal getLOGZERO() { 410 return edge.LOGZERO; 411 } 412 413}