Programming Assignment #2

CSE 452: Fall 2004

Due Date: 11/11/04

Instructions: This programming assignment is designed to develop your Prolog programming skills and to familiarize you with methods of establishing facts and rules via the use of a logical programming language. The deliverables for this assignment include a Prolog program called “<your name>-order.pl”, a data file containing Prolog “facts” called “<your name>-facts.pl”, and an additional file that is to be produced using word processing software like MS Word that will contain a step-by-step walkthrough demonstrating the capabilities of your program. In addition, a “README” file should be provided if there are any special instructions that are required to run your program (i.e., other than using the “load” command in the Prolog interpreter. All files are to be handed-in by email to by midnight on the due date shown.

Overview:

As stated in lecture, Prolog programs consist of three primary components; facts, rules, and queries. The goal of this assignment is to construct a customer order system that can be used to verify the validity of a customer’s order, to adjust inventory quantities, provide inventory reports, and generate lists of customer information. In order for this system to work, you will need to build a small “database” in a separate Prolog file that will consist of a series of facts on customers and inventory items. Rules should be encapsulated inside of functions whenever possible to provide the required functionality and access to these facts.

Quick-Start Guide to Prolog:

The Gnu Prolog interpreter is loaded on the CSE Linux machine “adriatic.” In order to run the interpreter SSH into “adriatic.cse.msu.edu” by logging in with your CSE username and password. No additional path information should be required in your .personal file.

At the command prompt simply type “gprolog” and the interpreter will start. The Prolog interpreter has its own series of prompts on which queries may be executed directly. External files may also be loaded and consulted as described in the lecture notes. Prolog queries always end with a period (“.”).

To exit Prolog press CNTRL-C and enter “e’ at the interpreter prompt.

Since your assignment requires you to produce 2 distinct files: one for rules and one for facts; you need to be aware of how to load and “consult” multiple Prolog files. At the interpreter prompt type in “[‘file1’, ‘file2’, …’filen’]” (without the outside double quotes).

Programming Assignment:

1.  Begin by populating your Prolog data file with the necessary facts. You will need to incorporate at least three distinct record types. These include:
a). customer/3 – where the 3 arguments are: customer name, city, and credit rating. Credit ratings should be in the form of aaa, aa, a, bbb, bb, b, ccc, cc, c, d, e; from highest to lowest. You need not use all credit ratings, just provide enough to demonstrate correct system capabilities. Assume that customers with credit ratings of “ccc” or higher are eligible to purchase products.
b). item/3 – where the three arguments are: item identification number, item name, and reorder point for inventory (i.e., when at or below this level it is time to reorder).
c). inventory/2 – where the two arguments are: item identification number (same as in item record above), and quantity in stock.
Add as many customers, items, and associated inventory records as you need to demonstrate your system.

2.  Next, create the following rules and place them in a separate file:
a). item_quantity/2 – this rule should permit the user to discover the number of items in stock knowing only the item name. Knowledge of the item identification number on the part of the user should not be necessary.
b). inventory_report – should produce an inventory of the items available for sale that displays the name of the item and the quantity in stock.
c). good_customer/1 – should take the name of the customer and determine whether or not he/she is a good customer based on credit rating (assume “ccc” or higher). Use the cut operator (!) to prevent the search for other cases once one has been found.
d). valid_order/3 – checks the validity of a customer order. A valid order is one where the customer name is in the system, his/her credit rating is “ccc” or higher, the item is a valid one listed in inventory, the ordered item is in stock, and the quantity of the item ordered does not exceed the quantity in stock. Your valid_order rule should produce meaningful responses that indicate whether or not the order is valid, and if not, what the reason is for the invalid order (e.g., customer not on file, item not in stock, etc.). It is only necessary to provide one message per invalid order. For instance, if you check whether the customer is in the system and he/she is not, it is sufficient to produce only this message. So if the item ordered was also not in the system, it would not be necessary to produce this result as well.
e). reorder/1 – this should check inventory levels against reorder quantities to determine if it is time to reorder. If it is, an appropriate message should be displayed.
f). update_inventory/2 – this rule should take the item name and order quantity as input and update the in stock quantity accordingly.
g). order/0 – this rule should make use of several of the other rules above. Entering “order” should prompt for the customer name, the item name, and quantity. The system should then use the other rules to validate the order, make sure the customer is in good standing, update inventory quantities, and display appropriate messages (example, “order successful”, “item not in stock”, “time to reorder”, etc..).

3.  Finally, write a series of queries to demonstrate each of the rules above. These should be handed-in in a MS Word or Tex file as part of your “walkthrough” of the system. The grader should be able to use your walkthrough to test all system components.