java.lang.Object
edu.harvard.seas.pl.abcdatalog.engine.topdown.Relation
All Implemented Interfaces:
Iterable<Tuple>
Direct Known Subclasses:
QsqSupRelation

public class Relation extends Object implements Iterable<Tuple>
A relation, i.e., a set of tuples of a fixed arity with an associated attribute schema of the same arity.
  • Field Details

    • tuples

      protected Set<Tuple> tuples
      The tuples of this relation.
    • attributes

      protected TermSchema attributes
      The attribute schema of this relation.
    • arity

      public final int arity
      The fixed arity of this relation.
  • Constructor Details

    • Relation

      public Relation(Relation other)
      Constructs a relation from another relation.
      Parameters:
      other - the other relation
    • Relation

      public Relation(TermSchema attributes)
      Constructs an empty relation with the given attribute schema.
      Parameters:
      attributes - the attribute schema
    • Relation

      public Relation(int arity)
      Constructs an empty relation of the given arity. It has a default attribute schema.
      Parameters:
      arity - the arity
  • Method Details

    • add

      public boolean add(Tuple x)
      Add a tuple of the proper arity to this relation.
      Parameters:
      x - the tuple
      Returns:
      whether this relation has changed
    • addAll

      public boolean addAll(Relation other)
      Add all the tuples of another relation to this relation. The two relations must have the same arity.
      Parameters:
      other - the other relation
      Returns:
      whether this relation has changed
    • removeAll

      public boolean removeAll(Relation other)
      Remove all the tuples in other relation from this relation. The two relations must have the same arity.
      Parameters:
      other - the other relation
      Returns:
      whether this relation has changed
    • filter

      public Relation filter(Function<Tuple,Boolean> f)
      Returns a new relation consisting of those tuples that meet the supplied predicate.
      Parameters:
      f - the predicate
      Returns:
      the new relation
    • size

      public int size()
      Returns the number of tuples in this relation.
      Returns:
      the number of tuples
    • isEmpty

      public boolean isEmpty()
      Returns whether the relation has any tuples.
      Returns:
      whether the relation is empty
    • joinAndProject

      public Relation joinAndProject(Relation other, TermSchema schema)
      Creates a new relation by joining this relation with the other relation and projecting onto the supplied attribute schema. If the schema has attributes not in either relation, those terms are null.
      Parameters:
      other - the other relation
      schema - the attribute schema
      Returns:
      the new relation
    • applyTuplesAsSubstitutions

      public Relation applyTuplesAsSubstitutions(Tuple x)
      For each tuple t in this relation, creates a substitution by mapping each attribute in the schema to the corresponding element in t, and then creates a new relation by applying each substitution to the input tuple x.
      Parameters:
      x - the input tuple
      Returns:
      the new relation
    • project

      public Relation project(List<Boolean> colsToKeep)
      Creates a new relation that results from the projection of this relation. The projection is described by a list of booleans, where a true value denotes that the column should be retained.
      Parameters:
      colsToKeep - the description of the projection
      Returns:
      the new relation
    • contains

      public boolean contains(Tuple x)
      Returns whether this relation contains the input tuple x.
      Parameters:
      x - the input tuple
      Returns:
      whether this relation contains x
    • getAttributes

      public TermSchema getAttributes()
      Returns the attribute schema for this relation.
      Returns:
      the attribute schema
    • renameAttributes

      public void renameAttributes(TermSchema schema)
      Change the attribute schema of this relation to the supplied one, which must be of the same arity as this relation.
      Parameters:
      schema - the new schema
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • iterator

      public Iterator<Tuple> iterator()
      Specified by:
      iterator in interface Iterable<Tuple>