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

it.unibz.inf.ontop.iq.request.FunctionalDependencies Maven / Gradle / Ivy

package it.unibz.inf.ontop.iq.request;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.collect.Streams;
import it.unibz.inf.ontop.iq.request.impl.FunctionalDependenciesImpl;
import it.unibz.inf.ontop.model.term.Variable;
import it.unibz.inf.ontop.substitution.InjectiveSubstitution;
import it.unibz.inf.ontop.substitution.SubstitutionFactory;
import it.unibz.inf.ontop.utils.ImmutableCollectors;

import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public interface FunctionalDependencies {

    boolean isEmpty();

    Stream, ImmutableSet>> stream();

    FunctionalDependencies rename(InjectiveSubstitution renamingSubstitution, SubstitutionFactory substitutionFactory);
    FunctionalDependencies concat(FunctionalDependencies other);

    /**
     * "Merges" two sets of determinants in the sense of a union: The dependents become the intersection of any
     * two dependent sets of the two FD sets that are not empty, while the determinants become their union.
     */
    FunctionalDependencies merge(FunctionalDependencies other);

    boolean contains(ImmutableSet determinants, ImmutableSet dependents);

    ImmutableSet> getDeterminantsOf(Variable variable);

    static FunctionalDependencies of(ImmutableSet... dependencies) {
        if(dependencies.length % 2 != 0)
            throw new IllegalArgumentException("FunctionalDependency must be built of 2n ImmutableSets.");
        var determinants = IntStream.range(0, dependencies.length)
                .filter(i -> i % 2 == 0)
                .mapToObj(i -> dependencies[i]);
        var dependents = IntStream.range(0, dependencies.length)
                .filter(i -> i % 2 == 1)
                .mapToObj(i -> dependencies[i]);
        return new FunctionalDependenciesImpl(Streams.zip(determinants, dependents, (a, b) -> Maps.immutableEntry(a, b))
                .collect(ImmutableCollectors.toSet())
            );
    }

    static FunctionalDependencies empty() {
        return new FunctionalDependenciesImpl(ImmutableSet.of());
    }

    static Collector, ImmutableSet>, ?, FunctionalDependencies> toFunctionalDependencies() {
        return FunctionalDependenciesImpl.getCollector();
    }

    static FunctionalDependencies fromUniqueConstraints(ImmutableSet> uniqueConstraints, ImmutableSet allVariables) {
        return uniqueConstraints.stream()
                .map(uc -> Maps.immutableEntry(uc, Sets.difference(allVariables, uc).immutableCopy()))
                .filter(fd -> !fd.getValue().isEmpty())
                .collect(FunctionalDependencies.toFunctionalDependencies());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy