This chapter is a brief introduction to using the PROLOGIO package.
First, some terminology. A Prolog database is a list of clauses, each of which represents an object or record which needs to be saved to a file. A clause has a functor (name), and a list of attributes, each of which has a value. Attributes may take the following types of value: string, word, integer, floating point number, and list. A list can itself contain any type, allowing for nested data structures.
Consider the following code.
PrologDatabase db;
PrologExpr *my_clause = new PrologExpr("object");
my_clause->AddAttributeValue("id", (long)1);
my_clause->AddAttributeValueString("name", "Julian Smart");
db.Append(my_clause);
ofstream file("my_file");
db.WriteProlog(file);
This creates a database, constructs a clause, adds it to the database,
and writes the whole database to a file. The file it produces looks like
this:
object(id = 1, name = "Julian Smart").To read the database back in, the following will work:
PrologDatabase db;
db.ReadProlog("my_file");
db.BeginFind();
PrologExpr *my_clause = db.FindClauseByFunctor("object");
int id = 0;
char *name = copystring("None found");
my_clause->AssignAttributeValue("id", &id);
my_clause->AssignAttributeValue("name", &name);
cout << "Id is " << id << ", name is " << name << "\n";
Note the setting of defaults before attempting to retrieve attribute values,
since they may not be found.