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

it.unibz.inf.ontop.model.term.functionsymbol.impl.AbstractBinaryComparisonSPARQLFunctionSymbol Maven / Gradle / Ivy

package it.unibz.inf.ontop.model.term.functionsymbol.impl;

import com.google.common.collect.ImmutableList;
import it.unibz.inf.ontop.iq.node.VariableNullability;
import it.unibz.inf.ontop.model.term.Constant;
import it.unibz.inf.ontop.model.term.ImmutableTerm;
import it.unibz.inf.ontop.model.term.TermFactory;
import it.unibz.inf.ontop.model.type.*;
import it.unibz.inf.ontop.utils.ImmutableCollectors;
import org.apache.commons.rdf.api.IRI;

import javax.annotation.Nonnull;
import java.util.Optional;


public abstract class AbstractBinaryComparisonSPARQLFunctionSymbol extends SPARQLFunctionSymbolImpl {

    private final RDFDatatype xsdBooleanType;

    protected AbstractBinaryComparisonSPARQLFunctionSymbol(@Nonnull String functionSymbolName, @Nonnull IRI functionIRI,
                                                           @Nonnull RDFTermType rdfRootType, RDFDatatype xsdBooleanType) {
        super(functionSymbolName, functionIRI, ImmutableList.of(rdfRootType, rdfRootType));
        this.xsdBooleanType = xsdBooleanType;
    }

    protected AbstractBinaryComparisonSPARQLFunctionSymbol(@Nonnull String functionSymbolName, @Nonnull String officialName,
                                                           @Nonnull RDFTermType rdfRootType, RDFDatatype xsdBooleanType) {
        super(functionSymbolName, officialName, ImmutableList.of(rdfRootType, rdfRootType));
        this.xsdBooleanType = xsdBooleanType;
    }

    @Override
    protected boolean tolerateNulls() {
        return false;
    }

    @Override
    public boolean isAlwaysInjectiveInTheAbsenceOfNonInjectiveFunctionalTerms() {
        return false;
    }

    @Override
    public Optional inferType(ImmutableList terms) {
        return Optional.of(TermTypeInference.declareTermType(xsdBooleanType));
    }

    @Override
    public boolean canBePostProcessed(ImmutableList arguments) {
        return false;
    }

    @Override
    protected final ImmutableTerm buildTermAfterEvaluation(ImmutableList newTerms,
                                                           TermFactory termFactory, VariableNullability variableNullability) {
        if (newTerms.stream()
                .allMatch(t -> isRDFFunctionalTerm(t) || (t instanceof Constant))) {
            ImmutableList typeTerms = newTerms.stream()
                    .map(t -> extractRDFTermTypeTerm(t, termFactory))
                    .collect(ImmutableCollectors.toList());

            ImmutableList subLexicalTerms = newTerms.stream()
                    .map(t -> extractLexicalTerm(t, termFactory))
                    .collect(ImmutableCollectors.toList());

            ImmutableTerm lexicalTerm = computeLexicalTerm(subLexicalTerms, typeTerms, termFactory)
                    .simplify(variableNullability);

            // NB: Only there for improving the performance
            if (lexicalTerm.equals(termFactory.getNullConstant()))
                return lexicalTerm;

            ImmutableTerm typeTerm = termFactory.getIfElseNull(
                    termFactory.getDBIsNotNull(lexicalTerm),
                    termFactory.getRDFTermTypeConstant(xsdBooleanType))
                    .simplify(variableNullability);

            return termFactory.getRDFFunctionalTerm(lexicalTerm, typeTerm);
        }
        else
            return termFactory.getImmutableFunctionalTerm(this, newTerms);
    }

    protected abstract ImmutableTerm computeLexicalTerm(ImmutableList subLexicalTerms,
                                                        ImmutableList typeTerms, TermFactory termFactory);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy