private static boolean extendHomomorphism(Map<Variable, VariableOrGroundTerm> map, DataAtom from, DataAtom to) { return from.getPredicate().equals(to.getPredicate()) && extendHomomorphism(map, from.getArguments(), to.getArguments()); }
@Override public boolean hasSamePredicateAndArity(DataAtom otherAtom) { if (!predicate.equals(otherAtom.getPredicate())) return false; if (getEffectiveArity() != otherAtom.getEffectiveArity()) return false; return true; }
/** * Checks if the atom is unary or not. */ private static boolean isUnary(DataAtom atom) { return atom.getArity() == 1; }
/** * Returns the list of the terms from atom corresponding * to the positions * TODO: explain */ protected static ImmutableList<VariableOrGroundTerm> extractArguments(DataAtom atom, ImmutableList<Integer> positions) { ImmutableList.Builder<VariableOrGroundTerm> listBuilder = ImmutableList.builder(); int atomLength = atom.getArguments().size(); for (Integer keyIndex : positions) { if (keyIndex > atomLength) { // TODO: find another exception throw new RuntimeException("The key index does not respect the arity of the atom " + atom); } else { listBuilder.add(atom.getTerm(keyIndex - 1)); } } return listBuilder.build(); }
protected static boolean hasDuplicates(DataAtom atom) { ImmutableList<? extends VariableOrGroundTerm> termList = atom.getArguments(); Set<VariableOrGroundTerm> termSet = new HashSet<>(termList); return termSet.size() < termList.size(); } }
private static ImmutableSubstitution<VariableOrGroundTerm> extractSubstitution(DistinctVariableOnlyDataAtom sourceAtom, DataAtom targetAtom) { if (!sourceAtom.getPredicate().equals(targetAtom.getPredicate())) { throw new IllegalStateException("Incompatible predicates"); } else if (sourceAtom.getEffectiveArity() != targetAtom.getEffectiveArity()) { throw new IllegalStateException("Different arities"); } ImmutableMap<Variable, VariableOrGroundTerm> newMap = FunctionalTools.zip( sourceAtom.getArguments(), (ImmutableList<VariableOrGroundTerm>) targetAtom.getArguments()).stream() .collect(ImmutableCollectors.toMap()); return SUBSTITUTION_FACTORY.getSubstitution(newMap); }
@Override protected Optional<IQ> getDefinition(IntensionalDataNode dataNode) { return Optional.ofNullable(map.get(dataNode.getProjectionAtom().getPredicate())); }
@Override public DistinctVariableDataAtom applyToDistinctVariableDataAtom(DistinctVariableDataAtom dataAtom) throws ConversionException { DataAtom newDataAtom = applyToDataAtom(dataAtom); if (newDataAtom instanceof DistinctVariableDataAtom) { return (DistinctVariableDataAtom) newDataAtom; } /** * Checks if new data atom can be converted into a DistinctVariableDataAtom */ if (newDataAtom.getArguments().size() == newDataAtom.getVariables().size()) { return ATOM_FACTORY.getDistinctVariableDataAtom(newDataAtom.getPredicate(), (ImmutableList<Variable>)newDataAtom.getArguments()); } else { throw new ConversionException("The substitution has transformed a DistinctVariableDataAtom into" + "a non-DistinctVariableDataAtom: " + newDataAtom); } }
private Optional<GroundTermRemovalFromDataNodeProposal> makeProposal(IntermediateQuery query) { ImmutableList.Builder<DataNode> dataNodesToSimplifyBuilder = ImmutableList.builder(); for (QueryNode node : query.getNodesInTopDownOrder()) { if (node instanceof DataNode) { DataNode dataNode = (DataNode) node; if (dataNode.getProjectionAtom().containsGroundTerms()) { dataNodesToSimplifyBuilder.add(dataNode); } } } ImmutableList<DataNode> dataNodesToSimplify = dataNodesToSimplifyBuilder.build(); if (dataNodesToSimplify.isEmpty()) { return Optional.empty(); } else { GroundTermRemovalFromDataNodeProposal proposal = new GroundTermRemovalFromDataNodeProposalImpl( dataNodesToSimplify); return Optional.of(proposal); } }
protected static boolean hasDuplicates(DataAtom atom) { ImmutableList<? extends VariableOrGroundTerm> termList = atom.getArguments(); Set<VariableOrGroundTerm> termSet = new HashSet<>(termList); return termSet.size() < termList.size(); }
private ImmutableSubstitution<VariableOrGroundTerm> extractSubstitution(DistinctVariableOnlyDataAtom sourceAtom, DataAtom targetAtom) { if (!sourceAtom.getPredicate().equals(targetAtom.getPredicate())) { throw new IllegalStateException("Incompatible predicates"); } else if (sourceAtom.getEffectiveArity() != targetAtom.getEffectiveArity()) { throw new IllegalStateException("Different arities"); } ImmutableMap<Variable, VariableOrGroundTerm> newMap = FunctionalTools.zip( sourceAtom.getArguments(), (ImmutableList<VariableOrGroundTerm>) targetAtom.getArguments()).stream() .collect(ImmutableCollectors.toMap()); return substitutionFactory.getSubstitution(newMap); } }
private RDFAtomPredicate getRDFAtomPredicate(DataAtom atom){ return Optional.of(atom.getPredicate()) .filter(p -> p instanceof RDFAtomPredicate) .map(p -> (RDFAtomPredicate) p) .orElseThrow(() -> new CanonicalTransformerException(RDFAtomPredicate.class.getName() + " expected")); }
/** * Returns the list of the terms from atom corresponding * to the positions * TODO: explain */ protected static ImmutableList<VariableOrGroundTerm> extractArguments(DataAtom atom, ImmutableList<Integer> positions) { ImmutableList.Builder<VariableOrGroundTerm> listBuilder = ImmutableList.builder(); int atomLength = atom.getArguments().size(); for (Integer keyIndex : positions) { if (keyIndex > atomLength) { // TODO: find another exception throw new RuntimeException("The key index does not respect the arity of the atom " + atom); } else { listBuilder.add(atom.getTerm(keyIndex - 1)); } } return listBuilder.build(); }
public Function convertToMutableFunction(DataAtom dataAtom) { return convertToMutableFunction(dataAtom.getPredicate(), dataAtom.getArguments()); }
private ImmutableList<VariableOrGroundTerm> extractDeterminantArguments(DataAtom dataAtom, ImmutableList<Integer> determinantIndexes) { ImmutableList<? extends VariableOrGroundTerm> arguments = dataAtom.getArguments(); return determinantIndexes.stream() .map(i -> arguments.get(i - 1)) .collect(ImmutableCollectors.toList()); }
protected static ImmutableMultimap<RelationPredicate, ExtensionalDataNode> extractDataNodes(ImmutableList<QueryNode> siblings) { ImmutableMultimap.Builder<RelationPredicate, ExtensionalDataNode> mapBuilder = ImmutableMultimap.builder(); for (QueryNode node : siblings) { if (node instanceof ExtensionalDataNode) { ExtensionalDataNode dataNode = (ExtensionalDataNode) node; mapBuilder.put(dataNode.getProjectionAtom().getPredicate(), dataNode); } } return mapBuilder.build(); }
private Optional<IRI> getPropertyIRI(DataAtom atom) { AtomPredicate atomPredicate = atom.getPredicate(); return Optional.of(atomPredicate) .filter(p -> p instanceof RDFAtomPredicate) .map(p -> (RDFAtomPredicate) p) .flatMap(p -> p.getPropertyIRI(atom.getArguments())); }
@Override public ImmutableSet<Variable> getLocalVariables() { return atom.getArguments() .stream() .filter(Variable.class::isInstance) .map(Variable.class::cast) .collect(ImmutableCollectors.toSet()); }