logo
 
Homework Two - More Fun with Queries
CS 6800 - Advanced Database Management Systems
Utah State University
Home
Calendar
Homework   
Syllabus
Resources
People
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.

  1. Download and unzip the DES zip file, extracting the des folder.
  2. The manual is in the doc subfolder for easy reference.
  3. Save the file datalog.txt to the des folder.
  4. Start des.exe in the des folder.
  5. At the DES command prompt, enter the following to read the facts into DES.
      DES-Datalog> /consult datalog.txt
    
  6. 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).
  7. To create more elaborate queries, create a file query.pl in the des folder.
  8. Add your queries to the file.
  9. 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
    
  10. Once you have consulted the rules and facts, you can evaluate a rule, e.g., p, as follows.
      DES-Datalog> p(X).
    
  11. 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.

  1. inLoganShipsGrey(SupplierName) - Computes the names of suppliers located in logan that ship grey parts.
  2. threeSuppliers(PartName) - Computes the names of parts that were shipped by at least three different suppliers.
  3. 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.
  4. 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.
  1. 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>
    
  2. 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>
    
  3. 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>
    
  4. 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>
    

                                                                                                                                                                                                                                                                                                                                             
  E-mail questions or comments to Curtis.Dyreson at usu dot edu