|
|
|
Due date: Tuesday Feb. 16 by 11:59PM.
Instructions: Upload your homework using the
second homework turnin page.
Please put all of your code in
a directory called queries and upload
queries.zip or queries.tar.gz.
Weight: The homework will count approximately 5% of your final grade.
Datalog
Download DES, the executable distribution for Windows (or Linux if you like).
Alternatively you may use
the XSB datalog engine,
or some other Datalog system, to develop your code.
The following instructions are for DES on Windows.
-
Download and unzip the DES zip file, extracting the
des folder.
-
The manual is in the
doc subfolder for easy reference.
-
Save the file
datalog.txt to
the des folder.
-
Start
des.exe in the des folder.
-
At the DES command prompt, enter the following to read the facts
into DES.
DES-Datalog> /consult datalog.txt
-
Now you are ready to write rules. At the command prompt enter
something like.
DES-Datalog> p(X) :- supplier(X, _).
Enter return and it will evaluate p(X).
-
To create more elaborate queries,
create a file
query.pl in the des folder.
-
Add your queries to the file.
-
At the DES command prompt, enter the following to read the facts
and queries into DES.
DES-Datalog> /consult datalog.txt, query.pl
Each time you consult it will overwrite all the data previously present.
You can list all of the facts and rules as follows.
DES-Datalog> /listing
You can list a specific rule, such as for p,
or fact as follows.
DES-Datalog> /listing p
-
Once you have consulted the rules and facts, you can evaluate a
rule, e.g.,
p, as follows.
DES-Datalog> p(X).
-
Enjoy!
Consider a supplier-parts database with the following schema.
supplier (SupplierName, City)
part (PartName, Color, Weight)
subpart (PartName, SubPartName)
shipment (SupplierName, PartName, Date)
An example collection of Datalog ground facts for a data instance in this
schema is
available in the file datalog.txt.
Formulate the following predicates in Datalog. Each rule should go into
the file query.pl and follow the conventions
listed in the query. You may define
any helper predicates that you like.
-
inLoganShipsGrey(SupplierName) - Computes the names of
suppliers located in logan that ship grey parts.
-
threeSuppliers(PartName) - Computes the names of
parts that were shipped by at least three different suppliers.
-
allSubparts(PartName, SubPartName) - Recursively computes the
names of all of the subparts (where a subpart could be a subpart of a subpart)
of any given part.
-
relatedSuppliers(SupplierName1, SupplierName2) - A supplier
is related to another supplier if it ships the same part as another
supplier, or it is located in the same city as another supplier, or both
suppliers are related to the same supplier.
XQuery
Download Qizx or
the eXist XQuery
engine or some other XQuery implementation (there are many) to
develop your code.
Assume that there is an XML document with the following structure.
<supplier name="ACME">
<city>Paris</city>
<part name="Engine">
<color>red</color>
<subpart name="...">
<subpart name="...">...
Could be many levels of subparts
</subpart>
...
</subpart>
</part>
</supplier>
An example document is given in data.xml.
Load this data set into eXist and
formulate the following queries in XQuery.
Make each query a separate file.
-
names.xq -
What are the names of suppliers located in logan that ship red parts?
The answer should be in the following form.
<names>
<name>acme</name>
...
</names>
-
shippedTwice.xq -
Which parts have been shipped by at least two different suppliers?
The answer should be in the following form.
<shippedTwice>
<name>engine
<supplier>acme</supplier>
<supplier>crumbs</supplier>
</name>
...
</shippedTwice>
-
subparts.xq -
What are all the subparts of each part?
The answer should be in the following form.
<subparts>
<part>engine
<subpart>cylinder</subpart>
<subpart>piston</subpart>
...
</part>
...
</subparts>
-
cities.xq -
Which cities are related by the fact that they both ship an
engine subpart?
The answer should be in the following form.
<cities>
<cityPair>
<subpart>cylinder</subpart>
<city>Logan</city>
<city>Salt Lake City</city>
</cityPair>
...
</cities>
|