Writing Queries and Constraints

This document explains how to write queries and constraints in Aocl.  We use startrek as our running example: its CDSpec definition is given below along with its png image.  The database of tuples is not shown, but can be viewed hereMeta4 translated this specification into the package Allegory/startrek whose content is the bottom figure.

:::DocGen/Production/startrek.cdspec.pl:::



Here is the list of topics covered in this document:

Queries

---Q1---
---Q2---
---Q3---
---Q4---
---Q5---
---Q6---

Constraints

---C1---
---C2---

Operations on Aocl Tables

Here is the current list of operations on Aocl tables.  These operations are in addition to class diagram rightsemijoins that Meta4 generates.  The first set of operations are non-updating operations on Aocl tables.  T denotes a tuple type, and TBL denotes a table type of T tuples.  Note that some operations have multiple names.  Also, suppose "assoc" is the name of an association which connects TBL to table TBL<L> (where L is the type of the TBL<L>'s tuple class). A right-semijoin is implemented for each "assoc":

LinkedList<T>
tuples()
return list of all tuples of this table (and subtables)
stream<T>stream()produce a stream of all tuples of this table (and subtables)
boolean
contains(T t)
does this table contain tuple t
int
count()
size()
return number of tuples in this table (and subtables)
boolean
isEmpty()
notExists()
is table empty of tuples (same as count()==0)
boolean
isNotEmpty()
exists()
does table have tuples (same as count()>0)
TBL
unique()return table with unique tuples, deleting replicas
TBL
intersect(TBL x)
return in a table tuples common to table this and table x
T
getTupleEH(String id)
return tuple referenced by this identifier; throw exception if no such tuple exists
TBLselect(Predicate<T>)return table of tuples that satisfy the given predicate
TBL
reject(Predicate<T>)
return table of tuples that do NOT satisfy the given predicate
TBL
sort(Comparator<T>)
create a new table that sorts this table by the given comparator
TBL
duplicates()
create a new table with the duplicate tuples of this table; no duplicate is itself replicated
boolean
equals(TBL t)
return true if this table and table t have the same set of tuples
void
forEach(Consumer<T>)
apply consumer function to each tuple in this table
boolean
anyMatch(Predicate<T>)
return true if any tuple of this table satisfies given predicate
boolean
allMatch(Predicate<T>)
return true if all tuples of this table satisfy the given predicate.
boolean
noMatch(Predicate<T>)
return true if NO tuple of this table satisfies the given predicate.
void
print()
print to System.out the tuples of this table and all subtables
void
print(PrintStream)
print to PrintStream the tuples of this table and all subtables
void
printLocal(PrintStream)
print only tuples of this table and NOT subtables
void
groupBy(Function<T,String>,
   Consumer<TBL> action)
partition the tuples of this table into groups by the (grouper) function; apply action to each subtable (typically adding an aggregated tuple to a table).
void
error(ErrorReport, FormatString, Function<T,String>...)
add an error to ErrorReport object formatted by the FormatString whose arguments are provided by a sequence of functions to be applied to each tuple of 'this' table.
T
getFirst()
return first tuple of 'this' table
TBL<L>
assoc()
return table of L tuples that are connected to 'this' table via association 'assoc'

Here are operations to update a table:

void
add(T)
add tuple t to this table
void
addFirst(T)
add tuple to the head (top) of this table
void
addLast(T)
add tuple to the tail (bottom) of this table
void
deleteAll()
delete all tuples in this table
boolean
delete(T)
delete tuple from this table or a subtable; only one tuple is deleted. Returns true if a tuple was deleted; false otherwise
boolean
cascadingDelete(T)
delete this tuple and any tuples that reference it.  Returns true if the given tuple was deleted
void
deleteEH(T)
same as delete, but throw an exception if given tuple was not deleted
void
cascadingDeleteEH(T)
same as cascadingDelete, but throw an exception if given tuple was not deleted

Note: cascadingDelete maintains the structural integrity of a database, meaning that after it executes, no tuples should remain that point to (or reference) non-existent tuples as a result of a tuple deletion.  Now, if your database has constraints, you are responsible for ensuring that the resultant database satisfies them.  See examples above on how to use delete operations.


Operations on Aocl Tuples

Here are operations on tuples, in addition to their obvious constructors:

int
compareTo(T)
compare the id of this tuple with the given tuple
void
print(PrintStream)
print this tuple on given PrintStream
void
print()
print this tuple on System.out
void
println()
print this tuple with return on System.out
void
print(Object...)
print objects on single line separated by a space
void
println(Object...)
print objects on single line separated by a space, ending with new line
String
toString()
convert this tuple to a string
TBL<L>
assoc()
follow this tuple via association "assoc" to produce a table of tuples of type L, where L is the name of associated class)
T<L>
link2assoc()
if association "assoc" has a 1 cardinality, this operation retrieves the corresponding tuple in TBL<L> where L is the name of the associated class.

At the present time, updating a field of a tuple is perhaps a bit too easy. Look:
T t = (computation that returns a tuple of type T in table TBL);
t.s = "don"; // update string field s with value "don"
t.i = 15; // update int field i with value 15
t.f = 3.1415; // update float field f with value 3.1415
t.b = true; // update bool field f to true