Skip to content

Commit

Permalink
SelfJoinLikeExecutor: now lightens the substitution to "unify".
Browse files Browse the repository at this point in the history
  • Loading branch information
bcogrel committed Nov 6, 2019
1 parent 7197c83 commit 3fd1d25
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public Optional<ImmutableSubstitution<NonFunctionalTerm>> computeMGUS2(Immutable
* Computes one Most General Unifier (MGU) of (two) substitutions.
*/
public Optional<ImmutableSubstitution<ImmutableTerm>> computeMGUS(ImmutableSubstitution<? extends ImmutableTerm> substitution1,
ImmutableSubstitution<? extends ImmutableTerm> substitution2) {
ImmutableSubstitution<? extends ImmutableTerm> substitution2) {

ImmutableList.Builder<ImmutableTerm> firstArgListBuilder = ImmutableList.builder();
ImmutableList.Builder<ImmutableTerm> secondArgListBuilder = ImmutableList.builder();
Expand All @@ -214,13 +214,8 @@ public Optional<ImmutableSubstitution<VariableOrGroundTerm>> computeAtomMGUS(
ImmutableSubstitution<VariableOrGroundTerm> substitution1,
ImmutableSubstitution<VariableOrGroundTerm> substitution2) {
Optional<ImmutableSubstitution<ImmutableTerm>> optionalMGUS = computeMGUS(substitution1, substitution2);
if (optionalMGUS.isPresent()) {
return Optional.of(substitutionTools.convertIntoVariableOrGroundTermSubstitution(
optionalMGUS.get()));
}
else {
return Optional.empty();
}
return optionalMGUS
.map(substitutionTools::convertIntoVariableOrGroundTermSubstitution);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.google.common.collect.ImmutableMap;
import it.unibz.inf.ontop.model.term.Constant;
import it.unibz.inf.ontop.model.term.ImmutableTerm;
import it.unibz.inf.ontop.model.term.NonConstantTerm;
import it.unibz.inf.ontop.model.term.Variable;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.unibz.inf.ontop.iq.executor.join;

import com.google.common.collect.*;
import it.unibz.inf.ontop.exception.MinorOntopInternalBugException;
import it.unibz.inf.ontop.iq.exception.EmptyQueryException;
import it.unibz.inf.ontop.iq.node.*;
import it.unibz.inf.ontop.model.atom.DataAtom;
Expand All @@ -20,6 +21,8 @@
import it.unibz.inf.ontop.model.term.VariableOrGroundTerm;
import it.unibz.inf.ontop.substitution.ImmutableSubstitution;
import it.unibz.inf.ontop.utils.ImmutableCollectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Stream;
Expand All @@ -28,6 +31,8 @@

public class SelfJoinLikeExecutor {

private static final Logger LOGGER = LoggerFactory.getLogger(SelfJoinLikeExecutor.class);

/**
* TODO: explain
*
Expand Down Expand Up @@ -207,6 +212,7 @@ protected PredicateLevelProposal proposeForGroupingMap(
/*
* Collection of unifying substitutions
*/
long beforeUnifying = System.currentTimeMillis();
ImmutableSet<ImmutableSubstitution<VariableOrGroundTerm>> unifyingSubstitutions =
dataNodeGroups.stream()
.filter(g -> g.size() > 1)
Expand All @@ -219,6 +225,8 @@ protected PredicateLevelProposal proposeForGroupingMap(
})
.filter(s -> !s.isEmpty())
.collect(ImmutableCollectors.toSet());
LOGGER.debug(String.format("Self-join-like unification took %d ms", System.currentTimeMillis() - beforeUnifying));

/*
* All the nodes that have been at least once dominated (--> could thus be removed).
*
Expand Down Expand Up @@ -291,17 +299,57 @@ protected ImmutableSubstitution<VariableOrGroundTerm> unifyRedundantNodes(
return accumulatedSubstitution;
}

ImmutableMap<Variable, Collection<ExtensionalDataNode>> occurrenceVariableMap = redundantNodes.stream()
.flatMap(n -> n.getVariables().stream()
.map(v -> Maps.immutableEntry(v, n)))
.collect(ImmutableCollectors.toMultimap()).asMap();

// Variables used in more than one data node
ImmutableSet<Variable> sharedVariables = occurrenceVariableMap.entrySet().stream()
.filter(e -> ImmutableSet.copyOf(e.getValue()).size() > 1)
.map(Map.Entry::getKey)
.collect(ImmutableCollectors.toSet());
ImmutableSet<Variable> nonSharedVariables = Sets.difference(occurrenceVariableMap.keySet(), sharedVariables)
.immutableCopy();

Iterator<ExtensionalDataNode> nodeIterator = redundantNodes.iterator();

/*
* For performance purposes, we can detach some fragments from the substitution to be "unified" with the following atom.
*/
ImmutableList.Builder<ImmutableSubstitution<VariableOrGroundTerm>> nonSharedSubstitutionListBuilder = ImmutableList.builder();

// Non-final
DataAtom accumulatedAtom = nodeIterator.next().getProjectionAtom();

while (nodeIterator.hasNext()) {
DataAtom newAtom = nodeIterator.next().getProjectionAtom();

/*
* Before the following unification, we detach a fragment about non-shared variables from the accumulated substitution
*
* Particularly useful when dealing with tables with a large number of columns (e.g. views after collapsing some JSON objects)
*
*/
ImmutableSubstitution<VariableOrGroundTerm> nonSharedSubstitution = accumulatedSubstitution.reduceDomainToIntersectionWith(nonSharedVariables);
if (!nonSharedSubstitution.isEmpty())
nonSharedSubstitutionListBuilder.add(nonSharedSubstitution);

ImmutableSubstitution<VariableOrGroundTerm> substitutionToUnify = nonSharedSubstitution.isEmpty()
? accumulatedSubstitution
: accumulatedSubstitution.reduceDomainToIntersectionWith(sharedVariables);

// May throw an exception
accumulatedSubstitution = updateSubstitution(accumulatedSubstitution, accumulatedAtom, newAtom);
accumulatedSubstitution = updateSubstitution(substitutionToUnify, accumulatedAtom, newAtom);

accumulatedAtom = accumulatedSubstitution.applyToDataAtom(accumulatedAtom);
}
return accumulatedSubstitution;

return Stream.concat(
nonSharedSubstitutionListBuilder.build().stream(),
Stream.of(accumulatedSubstitution))
.reduce((v1, v2) -> v2.composeWith2(v1))
.orElseThrow(() -> new MinorOntopInternalBugException("At least one substitution was expected"));
}

protected Optional<ImmutableSubstitution<VariableOrGroundTerm>> mergeSubstitutions(
Expand Down

0 comments on commit 3fd1d25

Please sign in to comment.