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.math.RoundingMode; 010import java.util.ArrayList; 011import java.util.Collections; 012import java.util.HashMap; 013import java.util.HashSet; 014import java.util.List; 015import java.util.Map; 016import java.util.Random; 017import java.util.Set; 018import java.util.SortedSet; 019import org.apache.log4j.Logger; 020import org.dllearner.core.AbstractReasonerComponent; 021import org.dllearner.core.ComponentAnn; 022import org.dllearner.core.ComponentInitException; 023import org.dllearner.core.KnowledgeSource; 024import org.dllearner.core.ReasoningMethodUnsupportedException; 025import org.dllearner.core.config.ConfigOption; 026import org.dllearner.core.probabilistic.unife.ParameterLearningException; 027import org.dllearner.kb.OWLAPIOntology; 028import org.dllearner.learningproblems.ClassLearningProblem; 029import org.dllearner.reasoning.ClosedWorldReasoner; 030import org.dllearner.reasoning.OWLAPIReasoner; 031import org.mindswap.pellet.utils.Timer; 032import org.mindswap.pellet.utils.Timers; 033import org.semanticweb.owlapi.model.IRI; 034import org.semanticweb.owlapi.model.OWLAnnotation; 035import org.semanticweb.owlapi.model.OWLAxiom; 036import org.semanticweb.owlapi.model.OWLDataFactory; 037import org.semanticweb.owlapi.model.OWLOntology; 038import org.semanticweb.owlapi.model.OWLOntologyCreationException; 039import org.semanticweb.owlapi.model.OWLOntologyManager; 040import unife.bundle.utilities.BundleUtilities; 041import static unife.bundle.utilities.BundleUtilities.getManchesterSyntaxString; 042import unife.edge.utilities.EDGEUtilities; 043import unife.exception.IllegalValueException; 044import unife.math.utilities.MathUtilities; 045import unife.utilities.GeneralUtils; 046 047/** 048 * 049 * @author Giuseppe Cota <giuseppe.cota@unife.it> 050 */ 051@ComponentAnn(name = "DummyParameterLearner", shortName = "dummypl", version = 1.0) 052public class DummyParameterLearner extends AbstractEDGE { 053 054 @ConfigOption(defaultValue = "0.4", 055 required = false, 056 description = "Value of the fixed probability. All the probabilistic " 057 + "axioms will have the same probability") 058 protected double fixedProbability = 0.4; 059 060 private static Logger logger 061 = Logger.getLogger(DummyParameterLearner.class.getName()); 062 063 Map<OWLAxiom, BigDecimal> pMap; 064 065 public DummyParameterLearner() { 066 067 } 068 069 public DummyParameterLearner(ClassLearningProblem lp, Set<OWLAxiom> targetAxioms) { 070 super(lp, targetAxioms); 071 } 072 073 /** 074 * Get the Log-Likelihood of all the examples/queries. 075 * 076 * @return the log-likelihood of all the examples/queries 077 */ 078 public BigDecimal getLL() { 079 return new BigDecimal(0); 080 } 081 082 @Override 083 public Map<String, Long> getTimeMap() { 084 Map<String, Long> dummyTimers = new HashMap<>(); 085 dummyTimers.put("DummyParameterLearner", 0L); 086 return dummyTimers; 087 } 088 089 @Override 090 public void init() throws ComponentInitException { 091 AbstractReasonerComponent rc = learningProblem.getReasoner(); 092 if (rc instanceof ClosedWorldReasoner) { 093 sourcesOntology = ((ClosedWorldReasoner) rc).getReasonerComponent().getOntology(); 094 } else if (rc instanceof OWLAPIReasoner) { 095 sourcesOntology = ((OWLAPIReasoner) rc).getOntology(); 096 } else { 097 throw new ComponentInitException("Unsupported Reasoning: " 098 + rc.getClass()); 099 } 100 } 101 102 @Override 103 public BigDecimal getParameter(OWLAxiom ax) throws ParameterLearningException { 104 105 BigDecimal parameter; 106 for (OWLAxiom axiom : pMap.keySet()) { 107 if (axiom.equalsIgnoreAnnotations(ax)) { 108 parameter = pMap.get(axiom); 109 return parameter; 110 } 111 } 112 return null; 113 } 114 115 public OWLOntology getLearnedOntology() { 116 Timers timers = new Timers(); 117 Timer timer = timers.createTimer("OntologyCreation"); 118 119 timer.start(); 120 OWLOntologyManager owlManager = sourcesOntology.getOWLOntologyManager(); 121 OWLDataFactory owlFactory = owlManager.getOWLDataFactory(); 122 123 for (OWLAxiom ax : sourcesOntology.getLogicalAxioms()) { 124 125 for (OWLAxiom pax : GeneralUtils.safe(pMap).keySet()) { 126 127 if (pax.equalsIgnoreAnnotations(ax)) { 128 Set<OWLAnnotation> axAnnotations = new HashSet(ax.getAnnotations()); 129 if (ax.getAnnotations(BundleUtilities.PROBABILISTIC_ANNOTATION_PROPERTY).size() > 0) { 130 for (OWLAnnotation ann : axAnnotations) { 131 if (ann.getProperty().equals(BundleUtilities.PROBABILISTIC_ANNOTATION_PROPERTY)) { 132 axAnnotations.remove(ann); 133 break; 134 } 135 } 136 } 137 138 owlManager.removeAxiom(sourcesOntology, ax); 139 140 axAnnotations.add(owlFactory.getOWLAnnotation(BundleUtilities.PROBABILISTIC_ANNOTATION_PROPERTY, owlFactory.getOWLLiteral(pMap.get(pax).doubleValue()))); 141 owlManager.addAxiom(sourcesOntology, pax.getAnnotatedAxiom(axAnnotations)); 142 //ontologyAxioms.remove(pax); 143 break; 144 } 145 } 146 } 147 148 timer.stop(); 149 150 logger.info("Successful creation of the learned ontology"); 151 logger.info("Ontology created in " + timer.getAverage() + " (ms)"); 152 153 return sourcesOntology; 154 155 } 156 157 public void changeSourcesOntology(OWLOntology ontology) { 158 sourcesOntology = ontology; 159 learningProblem.getReasoner().changeSources(Collections.singleton((KnowledgeSource) new OWLAPIOntology(ontology))); 160// learningProblem.getReasoner().changeSources(Collections.singleton((KnowledgeSource) ontology)); 161 } 162 163 public void reset() { 164 isRunning = false; 165 stop = false; 166 } 167 168 public BigDecimal getLOGZERO() { 169 return new BigDecimal(Math.log(0.000001)).setScale(accuracy, RoundingMode.HALF_UP); 170 } 171 172 @Override 173 public void start() { 174 isRunning = true; 175 stop = false; 176 177 SortedSet<OWLAxiom> axioms = EDGEUtilities.get_ax_filtered(sourcesOntology); 178 179 pMap = new HashMap<>(); 180 181 Random probGenerator = new Random(); 182 183 if (randomize) { 184 probGenerator.setSeed(seed); 185 logger.debug("Random Seed set to: " + seed); 186 } 187 188 for (OWLAxiom axiom : axioms) { 189 List<BigDecimal> probList = new ArrayList<>(); 190 String axiomName = getManchesterSyntaxString(axiom); 191 192 if (probabilizeAll) { 193 double probValue; 194 if (randomize) { 195 probValue = probGenerator.nextDouble(); 196 } else { 197 probValue = fixedProbability; 198 } 199 probList.add(MathUtilities.getBigDecimal(probValue, accuracy)); 200 201 } else { 202 203 for (OWLAnnotation annotation : axiom.getAnnotations(BundleUtilities.PROBABILISTIC_ANNOTATION_PROPERTY)) { 204 205 // metodo per aggiungere coppia assioma/probabilita alla Map 206 if (annotation.getValue() != null) { 207 208 BigDecimal annProbability; 209 if (randomize) { 210 annProbability = MathUtilities.getBigDecimal(probGenerator.nextDouble(), accuracy); 211 probList.add(annProbability); 212 } else { 213 annProbability = MathUtilities.getBigDecimal(fixedProbability, accuracy); 214 probList.add(annProbability); 215 } 216 if (showAll) { 217// System.out.print(axiomName); 218 String str = " => " + annProbability; 219// System.out.println(str); 220 logger.info(axiomName + str); 221 } 222 } 223 } 224 } 225 if (probList.size() > 0) { 226 OWLAxiom pMapAxiom = axiom.getAxiomWithoutAnnotations(); 227 BigDecimal varProbTemp = MathUtilities.getBigDecimal("0", accuracy); 228 if (pMap.containsKey(pMapAxiom)) { 229 varProbTemp = pMap.get(pMapAxiom); 230 } 231 for (BigDecimal ithProb : probList) { 232 BigDecimal mul = varProbTemp.multiply(ithProb).setScale(accuracy, RoundingMode.HALF_UP); 233 varProbTemp = varProbTemp.add(ithProb).subtract(mul); 234 } 235 pMap.put(pMapAxiom, varProbTemp); 236 } 237 238 } 239 240 if (probabilizeAll) { 241// System.out.println("Created " + axioms.size() + " probabilistic axiom"); 242 logger.info("Created " + axioms.size() + " probabilistic axiom"); 243 } 244 245 if (pMap.size() > 0) { 246// System.out.println("Probability Map computed. Size: " + pMap.size()); 247 logger.info("Probability Map computed. Size: " + pMap.size()); 248 } else { 249// System.out.println("Probability Map computed. Size: " + pMap.size()); 250 logger.info("Probability Map is empty"); 251 } 252 253 isRunning = false; 254 stop = true; 255 } 256 257}