001package org.dllearner.utilities.examples;
002
003import java.util.Set;
004
005import org.dllearner.reasoning.OWLAPIReasoner;
006import org.semanticweb.owlapi.apibinding.OWLManager;
007import org.semanticweb.owlapi.expression.OWLEntityChecker;
008import org.semanticweb.owlapi.expression.ShortFormEntityChecker;
009import org.semanticweb.owlapi.model.OWLClassExpression;
010import org.semanticweb.owlapi.model.OWLIndividual;
011import org.semanticweb.owlapi.util.BidirectionalShortFormProvider;
012import org.semanticweb.owlapi.util.BidirectionalShortFormProviderAdapter;
013import org.semanticweb.owlapi.util.SimpleShortFormProvider;
014import org.semanticweb.owlapi.util.mansyntax.ManchesterOWLSyntaxParser;
015
016/**
017 * @author Lorenz Buehmann
018 */
019public class DLQueryBasedExamplesProvider implements ExamplesProvider{
020
021    private final OWLAPIReasoner rc;
022    private final OWLClassExpression posExamplesCE;
023    private final OWLClassExpression negExamplesCE;
024
025    private Set<OWLIndividual> posExamples;
026    private Set<OWLIndividual> negExamples;
027
028    public DLQueryBasedExamplesProvider(OWLAPIReasoner rc,
029                                        OWLClassExpression posExamplesCE,
030                                        OWLClassExpression negExamplesCE) {
031        this.rc = rc;
032        this.posExamplesCE = posExamplesCE;
033        this.negExamplesCE = negExamplesCE;
034    }
035
036    public DLQueryBasedExamplesProvider(OWLAPIReasoner rc,
037                                        String posExamplesClassExpressionString,
038                                        String negExamplesClassExpressionString) {
039        this.rc = rc;
040        this.posExamplesCE = parseClassExpression(posExamplesClassExpressionString);
041        this.negExamplesCE = parseClassExpression(negExamplesClassExpressionString);
042    }
043
044    /**
045     * Parses a class expression string to obtain a class expression.
046     *
047     * @param classExpressionString
048     *        The class expression string
049     * @return The corresponding class expression if the class expression string
050     *         is malformed or contains unknown entity names.
051     */
052    private OWLClassExpression parseClassExpression(String classExpressionString) {
053        BidirectionalShortFormProvider bidiShortFormProvider = new BidirectionalShortFormProviderAdapter(
054                rc.getManager(),
055                rc.getOntology().getImportsClosure(),
056                new SimpleShortFormProvider());
057        // Set up the real parser
058        ManchesterOWLSyntaxParser parser = OWLManager.createManchesterParser();
059        parser.setDefaultOntology(rc.getOntology());
060        // Specify an entity checker that wil be used to check a class
061        // expression contains the correct names.
062        OWLEntityChecker entityChecker = new ShortFormEntityChecker(bidiShortFormProvider);
063        parser.setOWLEntityChecker(entityChecker);
064        // Do the actual parsing
065        return parser.parseClassExpression();
066    }
067
068    @Override
069    public Set<OWLIndividual> getPosExamples() {
070        if(posExamples == null) {
071            posExamples = rc.getIndividuals();
072        }
073        return posExamples;
074    }
075
076    @Override
077    public Set<OWLIndividual> getNegExamples() {
078        if(negExamples == null) {
079            negExamples = rc.getIndividuals();
080        }
081        return negExamples;
082    }
083}