The PrologExpr class is the building brick of Prolog expressions. It can represent both a clause, and any subexpression (long integer, float, string, word, or list).
PrologExpr::PrologExpr
PrologExpr::~PrologExpr
PrologExpr::AddAttributeValue
PrologExpr::AddAttributeValueString
PrologExpr::AddAttributeValueStringList
PrologExpr::AddAttributeValueWord
PrologExpr::Append
PrologExpr::Arg
PrologExpr::Insert
PrologExpr::AssignAttributeValue
PrologExpr::AssignAttributeValueStringList
PrologExpr::AttributeValue
PrologExpr::Copy
PrologExpr::DeleteAttributeValue
PrologExpr::Functor
PrologExpr::GetClientData
PrologExpr::GetFirst
PrologExpr::GetLast
PrologExpr::GetNext
PrologExpr::IntegerValue
PrologExpr::Nth
PrologExpr::RealValue
PrologExpr::SetClientData
PrologExpr::StringValue
PrologExpr::Type
PrologExpr::WordValue
PrologExpr::WriteClipsClause
PrologExpr::WriteLispExpr
PrologExpr::WritePrologClause
PrologExpr::WritePrologExpr
void PrologExpr(char *functor)
Construct a new clause with this form, supplying the functor name.
void PrologExpr(PrologType type, char *word_or_string,
Bool allocate = TRUE)
Construct a new empty list, word (will be output with no quotes) or a string, depending on the value of type (PrologList, PrologWord, PrologString).
If a word or string, the allocate parameter determines whether a new copy of the value is allocated, the default being TRUE.
void PrologExpr(long the_int)
Construct an integer expression.
void PrologExpr(float the_float)
Construct a floating point expression.
void PrologExpr(wxList *the_list)
Construct a list expression. The list's nodes' data should themselves be PrologExprs.
The current version of this library no longer uses the wxList internally, so this constructor turns the list into its internal format (assuming a non-nested list) and then deletes the supplied list.
void ~PrologExpr(void)
Destructor.
Use these on clauses ONLY. Note that the functions for adding strings and words must be differentiated by function name which is why they are missing from this group (see AddAttributeValueString and AddAttributeValueWord).
void AddAttributeValue(char *attribute, float value)
Adds an attribute and floating point value pair to the clause.
void AddAttributeValue(char *attribute, long value)
Adds an attribute and long integer value pair to the clause.
void AddAttributeValue(char *attribute, wxList *value)
Adds an attribute and list value pair to the clause, converting the list into internal form and then deleting value. Note that the list should not contain nested lists (except if in internal PrologExpr form.)
void AddAttributeValue(char *attribute, PrologExpr **value)
Adds an attribute and PrologExpr value pair to the clause. Do not delete value once this function has been called.
void AddAttributeValueString(char *attribute, char *value)
Adds an attribute and string value pair to the clause.
void AddAttributeValueStringList(char *attribute, wxList *value)
Adds an attribute and string list value pair to the clause.
Note that the list passed to this function is a list of strings, NOT a list of PrologExprs; it gets turned into a list of PrologExprs automatically. This is a convenience function, since lists of strings are often manipulated in C++.
void AddAttributeValueWord(char *attribute, char *value)
Adds an attribute and word value pair to the clause.
void Append(PrologExpr *value)
Append the value to the end of the list. The PrologExpr must be a list.
PrologExpr * Arg(PrologType typ, int n)
Get nth arg of the given clause (starting from 1). NULL is returned if the expression is not a clause, or n is invalid, or the given type does not match the actual type. See also Nth.
void Insert(PrologExpr *value)
Insert the value at the start of the list. The PrologExpr must be a list.
These functions are the easiest way to retrieve attribute values, by passing a pointer to variable. If the attribute is present, the variable will be filled with the appropriate value. If not, the existing value is left alone. This style of retrieving attributes makes it easy to set variables to default values before calling these functions; no code is necessary to check whether the attribute is present or not.
void AssignAttributeValue(char *attribute, char **value)
Retrieve a string (or word) value.
void AssignAttributeValue(char *attribute, float *value)
Retrieve a floating point value.
void AssignAttributeValue(char *attribute, int *value)
Retrieve an integer value.
void AssignAttributeValue(char *attribute, long *value)
Retrieve a long integer value.
void AssignAttributeValue(char *attribute, PrologExpr **value)
Retrieve a PrologExpr pointer.
void AssignAttributeValueStringList(char *attribute, wxList *value)
Use this on clauses ONLY. See above for comments on this style of attribute value retrieval. This function expects to receive a pointer to a new list (created by the calling application); it will append strings to the list if the attribute is present in the clause.
PrologExpr * AttributeValue(char *word)
Use this on clauses ONLY. Searches the clause for an attribute matching word, and returns the value associated with it.
PrologExpr * Copy(void)
Recursively copies the expression, allocating new storage space.
void DeleteAttributeValue(char *attribute)
Use this on clauses ONLY. Deletes the attribute and its value (if any) from the clause.
char * Functor(void)
Use this on clauses ONLY. Returns the clause's functor (object name).
wxObject * GetClientData(void)
Retrieve arbitrary data stored with this clause. This can be useful when reading in data for storing a pointer to the C++ object, so when another clause makes a reference to this clause, its C++ object can be retrieved. See SetClientData.
PrologExpr * GetFirst(void)
If this is a list expression (or clause), gets the first element in the list.
See also GetLast, GetNext, Nth.
PrologExpr * GetLast(void)
If this is a list expression (or clause), gets the last element in the list.
See also GetFirst, GetNext, Nth.
PrologExpr * GetNext(void)
If this is a node in a list (any PrologExpr may be a node in a list), gets the next element in the list.
See also GetFirst, GetLast, Nth.
long IntegerValue(void)
Returns the integer value of the expression.
PrologExpr * Nth(int n)
Get nth arg of the given list expression (starting from 0). NULL is returned if the expression is not a list expression, or n is invalid. See also Arg.
Normally, you would use attribute-value pairs to add and retrieve data from objects (clauses) in a data file. However, if the data gets complex, you may need to store attribute values as lists, and pick them apart yourself.
Here is an example of using lists.
int regionNo = 1; char regionNameBuf[20]; PrologExpr *regionExpr = NULL; sprintf(regionNameBuf, "region%d", regionNo); // Keep getting attribute values until no more regions. while (regionExpr = clause->AttributeValue(regionNameBuf)) { /* * Get the region information * */ char *regionName = NULL; char *formatString = NULL; Bool formatMode = FORMAT_NONE; int fontSize = 10; int fontFamily = wxSWISS; int fontStyle = wxNORMAL; int fontWeight = wxNORMAL; char *textColour = NULL; if (regionExpr->Type() == PrologList) { PrologExpr *nameExpr = regionExpr->Nth(0); PrologExpr *stringExpr = regionExpr->Nth(1); PrologExpr *formatExpr = regionExpr->Nth(2); PrologExpr *sizeExpr = regionExpr->Nth(3); PrologExpr *familyExpr = regionExpr->Nth(4); PrologExpr *styleExpr = regionExpr->Nth(5); PrologExpr *weightExpr = regionExpr->Nth(6); PrologExpr *colourExpr = regionExpr->Nth(7); regionName = copystring(nameExpr->StringValue()); formatString = copystring(stringExpr->StringValue()); formatMode = (int)formatExpr->IntegerValue(); fontSize = (int)sizeExpr->IntegerValue(); fontFamily = (int)familyExpr->IntegerValue(); fontStyle = (int)styleExpr->IntegerValue(); fontWeight = (int)weightExpr->IntegerValue(); textColour = copystring(colourExpr->StringValue()); } wxFont *font = wxTheFontList->FindOrCreateFont(fontSize, fontFamily, fontStyle, fontWeight); ObjectRegion *region = new ObjectRegion; region->regionName = regionName; region->formatString = formatString; region->formatMode = formatMode; region->regionFont = font; region->textColour = textColour; regions.Append(region); regionNo ++; sprintf(regionNameBuf, "region%d", regionNo); }
float RealValue(void)
Returns the floating point value of the expression.
void SetClientData(wxObject *data)
Associate arbitrary data with this clause. This can be useful when reading in data for storing a pointer to the C++ object, so when another clause makes a reference to this clause, its C++ object can be retrieved. See GetClientData.
char * StringValue(void)
Returns the string value of the expression.
PrologType Type(void)
Returns the type of the expression. PrologType is defined as follows:
typedef enum { PrologNull, PrologInteger, PrologReal, PrologWord, PrologString, PrologList } PrologType;
char * WordValue(void)
Returns the word value of the expression.
void WriteClipsClause(ostream& stream, Bool filtering=FALSE, PrologDatabase *database = NULL)
Writes the clause to the given stream in CLIPS 'deffacts' format. If filtering is TRUE, the deftemplates associated with database are used to filter out slots which exist in the CLIPS facts but not in the deftemplates (see PrologDatabase::AddTemplate).
void WriteLispExpr(ostream& stream)
Writes the expression or clause to the given stream in LISP format. Not normally needed, since the whole PrologDatabase will usually be written at once. Lists are enclosed in parentheses will no commas.
void WritePrologClause(ostream& stream)
Writes the clause to the given stream in Prolog format. Not normally needed, since the whole PrologDatabase will usually be written at once. The format is: functor, open parenthesis, list of comma-separated expressions, close parenthesis, full stop.
void WritePrologExpr(ostream& stream)
Writes the expression (not clause) to the given stream in Prolog format. Not normally needed, since the whole PrologDatabase will usually be written at once. Lists are written in square bracketed, comma-delimited format.