This question builds on the program you prepared for Labs 10-12. You will be able to continue working with the program you submitted before, or with the solution posted on the course website. You can find the necessary background, as well as the requirements of the previous lab questions on the previous page. The following assumes you are familiar with Labs 10 to 12.
In the question, the term factbase refers to the set of facts that your program has stored in memory (either facts read from the input file or facts it created itself).
For your convenience, you can download the sample database used in the previous labs here.
lt2.c
in your favourite editor
so that it implements the functionality described in the Requirements
section below.
submit 2031A LT2 lt2.c
submit
command can be viewed by
typing man submit
. In addition to the commands introduced in Labs 10 to 12, your script must implement the following two commands:
addfact rel ent1 ent2
The expected behaviour is the following:
Your program can assume that there will always be exactly three arguments when this command is used. Your submission will not be tested with addfact commands that have a number of arguments different than 3.
Your program must create a new fact that corresponds to the provided arguments, and add it to the factbase. For example, if the following command is given
addfact use class2 class4
then the fact
use class2 class4
must be added at the end of the factbase.
Your program must produce no output, other than a new prompt. The successful insertion of the fact will be tested by giving a printdb command immediately after. The new fact must appear as the last fact.
Your program can assume that a successful setdb command has already taken place before an addfact command is issued.
See the sample run at the end of this question for an example of using this command.
delfact rel ent1 ent2
The expected behaviour is the following:
Your program can assume that there will always be exactly three arguments when this command is used. Your submission will not be tested with delfact commands that have a number of arguments different than 3.
Your program must search the factbase for facts that correspond to the provided arguments, and delete them from the factbase. For example, if the following command is given after example.txt has been set as the factbase
delfact inherit class3 class4
then this fact is deleted, i.e. the output of printdb immediately after will be
inherit class1 class2 implement class3 interface1 aggregate class3 class5
If the fact to be deleted does not exist in the factbase, your program should not make any changes to the factbase, nor print any warning.
The way your program implements deletion is up to you, but you must ensure no memory leaks.
In addition, if any of the arguments is a single question mark, then any fact that matches the remaining arguments must be deleted. For example, if the following command is given after example.txt has been set as the factbase
delfact ? class3 ?
then all facts where the first entity is class3 must be deleted. As a result, the output of printdb immediately after will be
inherit class1 class2
The above implies that the following command
delfact ? ? ?
deletes all the facts in the database.
Your program must produce no output, other than a new prompt. Successful fact deletion will be tested by giving a printdb command immediately after. The remaining facts must appear in the same order as before the deletion.
Your program can assume that a successful setdb command has already taken place before a delfact command is issued.
Following is a sample run that shows an example of the expected behaviour of the program.
% gcc lt2.c % a.out Grock- setdb example.txt Read 4 facts from example.txt Grock- addfact use class2 class4 Grock- printdb inherit class1 class2 inherit class3 class4 implement class3 interface1 aggregate class3 class5 use class2 class4 Grock- delfact aggregate class3 class5 Grock- printdb inherit class1 class2 inherit class3 class4 implement class3 interface1 use class2 class4 Grock- delfact ? ? class4 Grock- printdb inherit class1 class2 implement class3 interface1 Grock- quit %