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.cli.unife; 007 008import java.io.FileNotFoundException; 009import java.io.IOException; 010import java.io.PrintWriter; 011import java.io.UnsupportedEncodingException; 012import java.util.HashSet; 013import java.util.Iterator; 014import java.util.Set; 015import org.dllearner.Constants.State; 016import org.dllearner.cli.CLIBase2; 017import org.dllearner.core.ComponentAnn; 018import org.dllearner.core.LearningProblem; 019import org.dllearner.core.config.ConfigOption; 020import org.dllearner.core.probabilistic.unife.OWLProbReasonerResult; 021import org.dllearner.core.probabilistic.unife.OWLProbabilisticReasoner; 022import org.dllearner.learningproblems.PosNegLP; 023import org.dllearner.learningproblems.PosOnlyLP; 024import org.dllearner.utils.unife.OWLUtils; 025import org.semanticweb.owlapi.model.OWLAxiom; 026import org.semanticweb.owlapi.model.OWLClass; 027import org.semanticweb.owlapi.model.OWLIndividual; 028import org.semanticweb.owlapi.model.OWLException; 029import org.slf4j.LoggerFactory; 030import org.springframework.beans.factory.annotation.Autowired; 031import static unife.utilities.GeneralUtils.safe; 032 033/** 034 * 035 * @author Giuseppe Cota <giuseppe.cota@unife.it> 036 */ 037/** 038 * Evaluate a Probabilistic Knowledge Base (Ontology). It runs test queries over 039 * the learned ontology. 040 * 041 * @author Giuseppe Cota <giuseppe.cota@unife.it> 042 */ 043@ComponentAnn(name = "Ontology validator", version = 0, shortName = "") 044public class OntologyValidation extends CLIBase2 { 045 046 private static final org.slf4j.Logger logger = LoggerFactory.getLogger(OntologyValidation.class); 047 048 //private ParameterLearningAlgorithm pla; 049 //private OWLOntology learnedOntology; 050 //private OWLOntology initialOntology; 051 @Autowired 052 private LearningProblem lp; 053 054 @Autowired 055 private OWLProbabilisticReasoner reasoner; 056 057 @ConfigOption(description = "learned class", required = true) 058 private OWLClass classExpression; 059 060 private String outputFile; 061 062 @Override 063 public void init() throws IOException { 064 super.init(); 065 } 066 067 @Override 068 public void run() { 069 Set<OWLIndividual> posIndividuals = null; 070 Set<OWLIndividual> negIndividuals = null; 071 Set<OWLAxiom> posTestQueries; 072 Set<OWLAxiom> negTestQueries; 073 if (lp instanceof PosNegLP) { 074 posIndividuals = ((PosNegLP) lp).getPositiveExamples(); 075 negIndividuals = ((PosNegLP) lp).getNegativeExamples(); 076 } else if (lp instanceof PosOnlyLP) { 077 posIndividuals = ((PosOnlyLP) lp).getPositiveExamples(); 078 } else { 079 throw new UnsupportedOperationException("Unsupported learning problem: " + lp.getClass()); 080 } 081 // convert the individuals into assertional axioms 082 posTestQueries = OWLUtils.convertIndividualsToAssertionalAxioms(posIndividuals, classExpression); 083 negTestQueries = OWLUtils.convertIndividualsToAssertionalAxioms(safe(negIndividuals), classExpression); 084 try { 085 // run the test queries over the initial ontology 086 Set<OWLProbReasonerResult> posTestResults = computeQueries(posTestQueries); 087 Set<OWLProbReasonerResult> negTestResults = computeQueries(negTestQueries); 088 // write result on output test configuration file 089 PrintWriter outFile = new PrintWriter(outputFile, "UTF-8"); 090 outFile.println("pos: " + posTestResults.size()); 091 outFile.println("neg: " + negTestResults.size()); 092 outFile.print("values: "); 093 for (OWLProbReasonerResult q : posTestResults) { 094 outFile.print(q.getProbability() + "-pos"); 095 outFile.print(","); 096 } 097 Iterator<OWLProbReasonerResult> it = negTestResults.iterator(); 098 while (it.hasNext()) { 099 OWLProbReasonerResult q = it.next(); 100 outFile.print(q.getProbability() + "-neg"); 101 if (it.hasNext()) { 102 outFile.print(","); 103 } 104 } 105 outFile.println(); 106 outFile.close(); 107 } catch (OWLException owle) { 108 logger.error("Error while computing the probabilities of the test set"); 109 System.exit(State.FAILURE.ordinal()); 110 } catch (FileNotFoundException | UnsupportedEncodingException ex) { 111 logger.error("Impossible to write output file " + outputFile + ". " 112 + "Reason: " + ex.getMessage()); 113 System.exit(State.FAILURE.ordinal()); 114 } 115 116 } 117 118 /** 119 * It computes a set of probabilistic queries 120 */ 121 private Set<OWLProbReasonerResult> computeQueries(Set<OWLAxiom> queries) throws OWLException { 122 Set<OWLProbReasonerResult> results = new HashSet<>(); 123 for (OWLAxiom query : queries) { 124 results.add(reasoner.computeQuery(query)); 125 } 126 return results; 127 } 128 129 /** 130 * @param pla the pla to set 131 */ 132// @Autowired 133// public void setPla(ParameterLearningAlgorithm pla) { 134// this.pla = pla; 135// } 136 /** 137 * @param learnedOntology the learnedOntology to set 138 */ 139// public void setLearnedOntology(OWLOntology learnedOntology) { 140// this.learnedOntology = learnedOntology; 141// } 142// /** 143// * @param initialOntology the initialOntology to set 144// */ 145// public void setInitialOntology(OWLOntology initialOntology) { 146// this.initialOntology = initialOntology; 147// } 148 /** 149 * @param outputFile the outputFile to set 150 */ 151 public void setOutputFile(String outputFile) { 152 this.outputFile = outputFile; 153 } 154 155 /** 156 * @param classExpression the classExpression to set 157 */ 158 public void setClassExpression(OWLClass classExpression) { 159 this.classExpression = classExpression; 160 } 161 162}