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.core;
020
021import com.google.common.collect.ComparisonChain;
022import org.apache.commons.codec.digest.DigestUtils;
023import org.dllearner.learningproblems.AxiomScore;
024import org.dllearner.utilities.EnrichmentVocabulary;
025import org.semanticweb.owlapi.manchestersyntax.renderer.ManchesterOWLSyntaxObjectRenderer;
026import org.semanticweb.owlapi.manchestersyntax.renderer.ManchesterOWLSyntaxPrefixNameShortFormProvider;
027import org.semanticweb.owlapi.model.*;
028import org.semanticweb.owlapi.util.DefaultPrefixManager;
029import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;
030
031import java.io.PrintWriter;
032import java.io.StringWriter;
033import java.util.*;
034
035public class EvaluatedAxiom<T extends OWLAxiom> extends EvaluatedHypothesis<T, AxiomScore>{
036        
037        private boolean asserted = false;
038        
039        public EvaluatedAxiom(T axiom, AxiomScore score) {
040                super(axiom, score);
041        }
042        
043        public EvaluatedAxiom(T axiom, AxiomScore score, boolean asserted) {
044                this(axiom, score);
045                this.asserted = asserted;
046        }
047
048        public T getAxiom() {
049                return getDescription();
050        }
051        
052        public boolean isAsserted() {
053                return asserted;
054        }
055
056        public void setAsserted(boolean asserted) {
057                this.asserted = asserted;
058        }
059
060        @Override
061        public String toString() {
062                return hypothesis + "(" + score.getAccuracy()+ ")";
063        }
064
065        public Map<OWLIndividual, List<OWLAxiom>> toRDF(String defaultNamespace){
066                Map<OWLIndividual, List<OWLAxiom>> ind2Axioms = new HashMap<>();
067                OWLDataFactory f = new OWLDataFactoryImpl();
068                
069                String id = DigestUtils.md5Hex(hypothesis.toString()) + score.getAccuracy();
070                OWLNamedIndividual ind = f.getOWLNamedIndividual(IRI.create(defaultNamespace + id));
071                
072        
073                StringWriter sw = new StringWriter();
074                PrintWriter pw = new PrintWriter(sw);
075                ManchesterOWLSyntaxObjectRenderer r = new ManchesterOWLSyntaxObjectRenderer(pw, new ManchesterOWLSyntaxPrefixNameShortFormProvider(new DefaultPrefixManager()));
076                hypothesis.accept(r);
077
078                OWLAxiom ax1 = f.getOWLClassAssertionAxiom(EnrichmentVocabulary.AddSuggestion, ind);
079                OWLAxiom ax2 = f.getOWLDataPropertyAssertionAxiom(EnrichmentVocabulary.hasAxiom, ind, sw.toString());
080                OWLAnnotation anno = f.getOWLAnnotation(EnrichmentVocabulary.belongsTo, ind.getIRI());
081//              OWLAxiom ax2 = ax.getAnnotatedAxiom(Collections.singleton(anno));
082                OWLAxiom ax3 = f.getOWLDataPropertyAssertionAxiom(EnrichmentVocabulary.confidence, ind, score.getAccuracy());
083                
084                List<OWLAxiom> axioms = new ArrayList<>();
085                axioms.add(ax1);
086                axioms.add(ax2);
087                axioms.add(ax3);
088                
089                ind2Axioms.put(ind, axioms);
090                
091                return ind2Axioms;
092        }
093
094        public static <T extends OWLAxiom> String prettyPrint(List<EvaluatedAxiom<T>> learnedAxioms) {
095                String str = "suggested axioms and their score in percent:\n";
096                if(learnedAxioms.isEmpty()) {
097                        return "  no axiom suggested\n";
098                } else {
099                        for (EvaluatedAxiom<T> learnedAxiom : learnedAxioms) {
100                                str += " " + prettyPrint(learnedAxiom) + "\n";
101                        }               
102                }
103                return str;
104        }
105        
106        public static <T extends OWLAxiom> String prettyPrint(EvaluatedAxiom<T> axiom) {
107                double acc = axiom.getScore().getAccuracy() * 100;
108                String accs = dfPercent.format(acc);
109                if(accs.length()==3) { accs = "  " + accs; }
110                if(accs.length()==4) { accs = " " + accs; }
111                String str =  accs + "%\t" + axiom.getAxiom();
112//              String str =  accs + "%\t" + axiom.getAxiom().toManchesterSyntaxString(null, PrefixCCMap.getInstance());
113                //TODO fix rendering
114                return str;
115        }
116        
117        public static <T extends OWLAxiom> List<EvaluatedAxiom<T>> getBestEvaluatedAxioms(Set<EvaluatedAxiom<T>> evaluatedAxioms, int nrOfAxioms) {
118                return getBestEvaluatedAxioms(evaluatedAxioms, nrOfAxioms, 0.0);
119        }
120        
121        public static <T extends OWLAxiom> List<EvaluatedAxiom<T>> getBestEvaluatedAxioms(Set<EvaluatedAxiom<T>> evaluatedAxioms, double accuracyThreshold) {
122                return getBestEvaluatedAxioms(evaluatedAxioms, Integer.MAX_VALUE, accuracyThreshold);
123        }
124
125        public static <T extends OWLAxiom> List<EvaluatedAxiom<T>> getBestEvaluatedAxioms(Set<EvaluatedAxiom<T>> evaluatedAxioms, int nrOfAxioms,
126                        double accuracyThreshold) {
127                List<EvaluatedAxiom<T>> returnList = new ArrayList<>();
128                
129                //get the currently best evaluated axioms
130                Set<EvaluatedAxiom<T>> orderedEvaluatedAxioms = new TreeSet<>(evaluatedAxioms);
131                
132                for(EvaluatedAxiom<T> evAx : orderedEvaluatedAxioms){
133                        if(evAx.getScore().getAccuracy() >= accuracyThreshold && returnList.size() < nrOfAxioms){
134                                returnList.add(evAx);
135                        }
136                }
137                
138                return returnList;
139        }
140
141}