All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.coode.oppl.search.OWLAxiomSearchTree Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
package org.coode.oppl.search;

import static org.coode.oppl.utils.ArgCheck.checkNotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.coode.oppl.PartialOWLObjectInstantiator;
import org.coode.oppl.Variable;
import org.coode.oppl.VariableVisitorEx;
import org.coode.oppl.bindingtree.Assignment;
import org.coode.oppl.bindingtree.BindingNode;
import org.coode.oppl.function.SimpleValueComputationParameters;
import org.coode.oppl.function.ValueComputationParameters;
import org.coode.oppl.generated.GeneratedVariable;
import org.coode.oppl.generated.RegexpGeneratedVariable;
import org.coode.oppl.rendering.ManchesterSyntaxRenderer;
import org.coode.oppl.utils.OWLObjectExtractor;
import org.coode.oppl.utils.VariableExtractor;
import org.coode.oppl.variabletypes.ANNOTATIONPROPERTYVariableType;
import org.coode.oppl.variabletypes.CLASSVariableType;
import org.coode.oppl.variabletypes.CONSTANTVariableType;
import org.coode.oppl.variabletypes.DATAPROPERTYVariableType;
import org.coode.oppl.variabletypes.INDIVIDUALVariableType;
import org.coode.oppl.variabletypes.InputVariable;
import org.coode.oppl.variabletypes.OBJECTPROPERTYVariableType;
import org.coode.oppl.variabletypes.VariableTypeVisitorEx;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObject;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLOntology;

/** @author Luigi Iannone */
public class OWLAxiomSearchTree extends SearchTree {
    private final ValueComputationParameters parameters;

    /** @param parameters
     *            parameters */
    public OWLAxiomSearchTree(ValueComputationParameters parameters) {
        this.parameters = checkNotNull(parameters, "parameters");
    }

    /** @return the parameters */
    public ValueComputationParameters getParameters() {
        return parameters;
    }

    @Override
    protected List getChildren(OWLAxiom node) {
        Set leaves = getParameters().getConstraintSystem().getLeaves();
        List toReturn = new ArrayList();
        VariableExtractor variableExtractor = new VariableExtractor(getParameters()
                .getConstraintSystem(), false);
        Set> variables = variableExtractor.extractVariables(node);
        if (!variables.isEmpty()) {
            Variable variable = variables.iterator().next();
            Collection values = new HashSet();
            if (leaves == null) {
                values.addAll(getAssignableValues(variable));
            } else {
                for (BindingNode bindingNode : leaves) {
                    SimpleValueComputationParameters pars = new SimpleValueComputationParameters(
                            getParameters().getConstraintSystem(), bindingNode,
                            getParameters().getRuntimeExceptionHandler());
                    if (bindingNode.containsAssignedVariable(variable)) {
                        values.add(getParameters().getBindingNode().getAssignmentValue(
                                variable, pars));
                    } else {
                        values.addAll(getAssignableValues(variable));
                    }
                }
            }
            for (OWLObject value : values) {
                Assignment assignment = new Assignment(variable, value);
                BindingNode bindingNode = new BindingNode(
                        Collections.singleton(assignment), variables);
                SimpleValueComputationParameters par = new SimpleValueComputationParameters(
                        getParameters().getConstraintSystem(), bindingNode,
                        getParameters().getRuntimeExceptionHandler());
                PartialOWLObjectInstantiator instantiator = new PartialOWLObjectInstantiator(
                        par);
                toReturn.add((OWLAxiom) node.accept(instantiator));
            }
        }
        return toReturn;
    }

    @Override
    protected boolean goalReached(OWLAxiom start) {
        boolean found = false;
        Iterator iterator = getParameters().getConstraintSystem()
                .getOntologyManager().getOntologies().iterator();
        while (!found && iterator.hasNext()) {
            OWLOntology ontology = iterator.next();
            found = ontology.containsAxiom(start);
        }
        return found;
    }

    protected Set getAllClasses() {
        Set toReturn = new HashSet();
        Set ontologies = getParameters().getConstraintSystem()
                .getOntologyManager().getOntologies();
        for (OWLOntology owlOntology : ontologies) {
            toReturn.addAll(owlOntology.getClassesInSignature());
        }
        return toReturn;
    }

    protected Set getAllConstants() {
        Set toReturn = new HashSet();
        for (OWLOntology ontology : getParameters().getConstraintSystem()
                .getOntologyManager().getOntologies()) {
            for (OWLAxiom axiom : ontology.getAxioms()) {
                toReturn.addAll(OWLObjectExtractor.getAllOWLLiterals(axiom));
            }
        }
        return toReturn;
    }

    protected Set getAllDataProperties() {
        Set toReturn = new HashSet();
        Set ontologies = getParameters().getConstraintSystem()
                .getOntologyManager().getOntologies();
        for (OWLOntology owlOntology : ontologies) {
            toReturn.addAll(owlOntology.getDataPropertiesInSignature());
        }
        return toReturn;
    }

    protected Set getAllAnnotationProperties() {
        Set toReturn = new HashSet();
        Set ontologies = getParameters().getConstraintSystem()
                .getOntologyManager().getOntologies();
        for (OWLOntology owlOntology : ontologies) {
            toReturn.addAll(owlOntology.getAnnotationPropertiesInSignature());
        }
        return toReturn;
    }

    protected Set getAllIndividuals() {
        Set toReturn = new HashSet();
        Set ontologies = getParameters().getConstraintSystem()
                .getOntologyManager().getOntologies();
        for (OWLOntology owlOntology : ontologies) {
            toReturn.addAll(owlOntology.getIndividualsInSignature());
        }
        return toReturn;
    }

    protected final VariableTypeVisitorEx> assignableValuesVisitor = new VariableTypeVisitorEx>() {
        @Override
        public Set visitCLASSVariableType(
                CLASSVariableType classVariableType) {
            return OWLAxiomSearchTree.this.getAllClasses();
        }

        @Override
        public Set visitOBJECTPROPERTYVariableType(
                OBJECTPROPERTYVariableType objectpropertyVariableType) {
            return OWLAxiomSearchTree.this.getObjectProperties();
        }

        @Override
        public Set visitDATAPROPERTYVariableType(
                DATAPROPERTYVariableType datapropertyVariableType) {
            return OWLAxiomSearchTree.this.getAllDataProperties();
        }

        @Override
        public Set visitANNOTATIONPROPERTYVariableType(
                ANNOTATIONPROPERTYVariableType annotationpropertyVariableType) {
            return OWLAxiomSearchTree.this.getAllAnnotationProperties();
        }

        @Override
        public Set visitINDIVIDUALVariableType(
                INDIVIDUALVariableType individualVariableType) {
            return OWLAxiomSearchTree.this.getAllIndividuals();
        }

        @Override
        public Set visitCONSTANTVariableType(
                CONSTANTVariableType constantVariableType) {
            return OWLAxiomSearchTree.this.getAllConstants();
        }
    };

    private Collection getAssignableValues(Variable variable) {
        Set toReturn = new HashSet();
        VariableVisitorEx> visitor = new VariableVisitorEx>() {
            @Override
            public  Set
                    visit(InputVariable v) {
                return v.getType().accept(assignableValuesVisitor);
            }

            @Override
            public  Set visit(
                    RegexpGeneratedVariable v) {
                Set result = v.getType().accept(
                        assignableValuesVisitor);
                Iterator iterator = result.iterator();
                while (iterator.hasNext()) {
                    OWLObject owlObject = iterator.next();
                    ManchesterSyntaxRenderer renderer = OWLAxiomSearchTree.this
                            .getParameters()
                            .getConstraintSystem()
                            .getOPPLFactory()
                            .getManchesterSyntaxRenderer(
                                    OWLAxiomSearchTree.this.getParameters()
                                            .getConstraintSystem());
                    owlObject.accept(renderer);
                    if (!v.getPatternGeneratingOPPLFunction()
                            .compute(OWLAxiomSearchTree.this.getParameters())
                            .matcher(renderer.toString()).matches()) {
                        iterator.remove();
                    }
                }
                return result;
            }

            @Override
            public  Set visit(
                    GeneratedVariable v) {
                return Collections.emptySet();
            }
        };
        toReturn.addAll(variable.accept(visitor));
        return toReturn;
    }

    protected Set getObjectProperties() {
        Set toReturn = new HashSet();
        Set ontologies = getParameters().getConstraintSystem()
                .getOntologyManager().getOntologies();
        for (OWLOntology owlOntology : ontologies) {
            toReturn.addAll(owlOntology.getObjectPropertiesInSignature());
        }
        return toReturn;
    }

    /** @return the assignableValuesVisitor */
    public VariableTypeVisitorEx> getAssignableValuesVisitor() {
        return assignableValuesVisitor;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy