📄 texpr.tex
字号:
\section{wxExpr overview}\label{exproverview}wxExpr is a C++ class reading and writing a subset of Prolog-like syntax,supporting objects attribute/value pairs.wxExpr can be used to develop programs with readable androbust data files. Within wxWidgets itself, it is used to parsethe {\tt .wxr} dialog resource files.{\bf History of wxExpr}During the development of the tool Hardy within the AIAI, a need arosefor a data file format for C++ that was easy for both humans andprograms to read, was robust in the face of fast-moving softwaredevelopment, and that provided some compatibility with AI languagessuch as Prolog and LISP.The result was the wxExpr library (formerly called PrologIO), which is able to read and write aProlog-like attribute-value syntax, and is additionally capable ofwriting LISP syntax for no extra programming effort. The advantages ofsuch a library are as follows:\begin{enumerate}\itemsep=0pt\item The data files are readable by humans;\item I/O routines are easier to write and debug compared with using binary files;\item the files are robust: unrecognised data will just be ignored by the application\item Inbuilt hashing gives a random access capability, useful for when linkingup C++ objects as data is read in;\item Prolog and LISP programs can load the files using a single command.\end{enumerate}The library was extended to use the ability to read and writeProlog-like structures for remote procedure call (RPC) communication.The next two sections outline the two main ways the library can be used.\subsection{wxExpr for data file manipulation}\itemsep=0ptThe fact that the output is in Prolog syntax is irrelevant for mostprogrammers, who just need a reasonable I/O facility. Typical outputlooks like this:\begin{verbatim}diagram_definition(type = "Spirit Belief Network").node_definition(type = "Model", image_type = "Diamond", attribute_for_label = "name", attribute_for_status_line = "label", colour = "CYAN", default_width = 120, default_height = 80, text_size = 10, can_resize = 1, has_hypertext_item = 1, attributes = ["name", "combining_function", "level_of_belief"]).arc_definition(type = "Potentially Confirming", image_type = "Spline", arrow_type = "End", line_style = "Solid", width = 1, segmentable = 0, attribute_for_label = "label", attribute_for_status_line = "label", colour = "BLACK", text_size = 10, has_hypertext_item = 1, can_connect_to = ["Evidence", "Cluster", "Model", "Evidence", "Evidence", "Cluster"], can_connect_from = ["Data", "Evidence", "Cluster", "Evidence", "Data", "Cluster"]).\end{verbatim}This is substantially easier to read and debug than a series of numbers andstrings.Note the object-oriented style: a file comprises a series of {\it clauses}.Each clause is an object with a {\it functor}\/ or object name, followedby a list of attribute-value pairs enclosed in parentheses, and finishedwith a full stop. Each attribute value may be a string, a word (no quotes),an integer, a real number, or a list with potentially recursive elements.The way that the facility is used by an application to read in a file isas follows:\begin{enumerate}\itemsep=0pt\item The application creates a wxExprDatabase instance.\item The application tells the database to read in the entire file.\item The application searches the database for objects it requires,decomposing the objects using the wxExpr API. The database may be hashed,allowing rapid linking-up of application data.\item The application deletes or clears the wxExprDatabase.\end{enumerate}Writing a file is just as easy:\begin{enumerate}\itemsep=0pt\item The application creates a wxExprDatabase instance.\item The application adds objects to the database using the API.\item The application tells the database to write out the entire database,in Prolog or LISP notation.\item The application deletes or clears the wxExprDatabase.\end{enumerate}To use the library, include "wxexpr.h".\subsection{wxExpr compilation}For UNIX compilation, ensure that YACC and LEX or FLEX are on your system. Check thatthe makefile uses the correct programs: a common error is to compiley\_tab.c with a C++ compiler. Edit the CCLEX variable in make.envto specify a C compiler. Also, do not attempt to compile lex\_yy.csince it is included by y\_tab.c.For DOS compilation, the simplest thing is to copy dosyacc.c to y\_tab.c, and doslex.c tolex\_yy.c. It is y\_tab.c that must be compiled (lex\_yy.c is included byy\_tab.c) so if adding source files to a project file, ONLY add y\_tab.cplus the .cc files. If you wish to alter the parser, you will need YACCand FLEX on DOS.The DOS tools are available at the AIAI ftp site, in the tools directory. Note thatfor FLEX installation, you need to copy flex.skl into the directoryc:/lib.If you are using Borland C++ and wish to regenerate lex\_yy.c and y\_tab.cyou need to generate lex\_yy.c with FLEX and then comment out the `malloc' and `free'prototypes in lex\_yy.c. It will compile with lots of warnings. If youget an undefined \_PROIO\_YYWRAP symbol when you link, you need to removeUSE\_DEFINE from the makefile and recompile. This is because the parser.yfile has a choice of defining this symbol as a function or as a define,depending on what the version of FLEX expects. See the bottom ofparser.y, and if necessary edit it to make it compile in the oppositeway to the current compilation.%To test out wxExpr compile the test program (samples/wxexpr/wxexpr.exe),%and try loading test.exp into the test%program. Then save it to another file. If the second is identical to the%first, wxExpr is in a working state.\subsection{Bugs}These are the known bugs:\begin{enumerate}\itemsep=0pt\item Functors are permissible only in the main clause (object).Therefore nesting of structures must be done using lists, not predicatesas in Prolog.\item There is a limit to the size of strings read in (about 5000 bytes).\end{enumerate}\subsection{Using wxExpr}This section is a brief introduction to using the wxExpr package.First, some terminology. A {\it wxExprDatabase}\/ is a list of {\it clauses},each of which represents an object or record which needs to be saved to a file.A clause has a {\it functor}\/ (name), and a list of attributes, each of whichhas a value. Attributes may take the following types of value: string, word,integer, floating point number, and list. A list can itself contain anytype, allowing for nested data structures.Consider the following code.\begin{verbatim}wxExprDatabase db;wxExpr *my_clause = new wxExpr("object");my_clause->AddAttributeValue("id", (long)1);my_clause->AddAttributeValueString("name", "Julian Smart");db.Append(my_clause);ofstream file("my_file");db.Write(file);\end{verbatim}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 likethis:\begin{verbatim}object(id = 1, name = "Julian Smart").\end{verbatim}To read the database back in, the following will work:\begin{verbatim}wxExprDatabase db;db.Read("my_file");db.BeginFind();wxExpr *my_clause = db.FindClauseByFunctor("object");int id = 0;wxString name = "None found";my_clause->GetAttributeValue("id", id);my_clause->GetAttributeValue("name", name);cout << "Id is " << id << ", name is " << name << "\n";\end{verbatim}Note the setting of defaults before attempting to retrieve attribute values,since they may not be found.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -