⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clips_bc.cpp

📁 clips专家系统内核打包类,很有参考性.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*
clipsBCPP.cpp : implementation file for a CLIPS Wrapper class using Borland C++

Notes:			Allows encapsulation of the 'C' interface for CLIPS. Also adds
					enhanced parameter/error checking, and simplifies the
					housekeeping that is needed in the main application as a lot of
					this is handled by class instance variables. It should also be
					relatively easy to expand this class to cover more of the
					CLIPS interface than I initially used. This class depends upon
					the ansi cstring class and the StringArray class I made. Although 
					these can be easily replaced. This class does not use virtual functions,
					(I did not intend for this to be a base class).

Author(s):	Mark Tomlinson

Version:		1.0
To Do:	I want to add a messagepump callback to prevent CLIPS from
			stalling out windows during long processing loops.

Revision		WHO		Date			WHY
-------------------------------------
	0.9		MDT		1/21/95	 initial
	1.0		MDT		3/26/95	 added file route support and cleanup
										 of other routes. added GetAllFacts()
										 added initial Defglobal routines these
										 are not yet tested!
	1.1		MDT		4/23/95	 added a msgpump on a PeriodicFunction and
										 added a cbfunct for doing eye-candy in here too
										 also added OutOfMemoryFunction() for CLIPS and
										 Batch() command.
	2.0	 	MDT	   7/1/95	 replaced cstring.h with mstring.h, added ability
										 to override this and use cstring.h by definition
										 of USE_BORLAND_STRING. Many additional CLIPS functions
										 added to wrapper. Initial Defglobal functions added,
										 however these do not appear to work in all cases.
	2.1		MDT		11/24/95  clean up code for route buffers, added all instance
										 handling code - also re-looked at defglobal code
										 this should work now, (sure as PPDefglobal works!).
   2.5      MDT       5/7/96   this should be last version - updated to synch
   										with MFC version, changes include: instance slot
                                 functions working, fact slot functions working
                                 dynamically load/unload the DLL instead of 
                                 an implicit load. Modify capturePrint to put output
                                 file into Windows directory.
*/
#pragma hdrstop
#include <windows.h>
//stream i/o
#include <iostream.h> 	//ios
#include <fstream.h> 	//ofstream                  
#include <stdio.h>
#include <dir.h>			//dir & path
//use these for advanced logging
//#include <classlib/date.h>
//#include <classlib/time.h>
//#include "log.h"
#ifndef __StringAr_H
	#include "stringar.h"
#endif

#define __defc
//default loop count
int	Defcount;
BOOL	Running;


//macro used to check GPA() return values
#define CHKFNPTR(p,s)	if (p == NULL) { \
						::MessageBox(NULL,"Unable to locate CLIPS function Entry point",s,MB_OK); \
						return FALSE; }
//macro to load a given proc address from CLIPS DLL
#define GPA(p,t,n)	p = (t) GetProcAddress(m_hLib,n); \
					CHKFNPTR(p,n)

#include "clips_BC.h"	//class definition
/////////////////////////////////////////////////////////////////////////////
// exported functions for DLL suppport/callbacks

extern "C"	{
/*	some global variables */
//array of pointer to the string buffers for memory routes
StringArray*	pRoutes[NUMROUTES];
//string arrays to manage lists of file routes
StringArray*	FileRoutes;
StringArray*	FileNames;
}

//prototype for generic CB function which can be set to fire periodically
void far       (*CBfunctPtr)(BOOL);

/*
This function handles out of memory conditions for CLIPS, this is a callback
from CLIPS.
*/ 
#pragma argsused
extern "C" int _export ReportMemoryProblems(unsigned long p)
{
	::MessageBox(NULL,"CLIPS has run out of memory","CLIPSDLL",MB_ICONSTOP|MB_OK);
	return 0;
}

/*
This function captures which routes we will handle, this is a callback
from CLIPS.
*/
extern "C" int _export captureQuery(char *router)
{
	/*
	If the router comes from stdout, stdin, wclips, wdisplay, ...,
  then we will accept the output.  Otherwise, pass the buck onto
  someone else. This could be done using a string container class.
	*/
	//return TRUE if we handle this route, and FALSE if not.
	return (GetRouteNum(router) != -1);
}

/*
This function actually handles what to do with the routed
information. This is a callback from CLIPS.
*/
extern "C" int _export capturePrint(char far* router, char far* str)
{
	String		hold;
   String		Route;
	int			rnum;
   char			filename[256];

   //get Windows directory - this is where we want to put it
   memset(filename,0,256);
	GetWindowsDirectory(filename,255);

	// Dump all routes to the file CLIPSDLL.ERR
	// if not handled in another way, in this code
	// the "other way" is to copy the string into
	// an array which was initialized elsewhere.
	// since CLIPS does not actually format the route
	// data you could add a routine to collect all the
	// output data on a given route until a CRLF was recieved
	// thus building a single string of output.
	//(ex-) (str-cat ?item1 ?item2 ?item3 ... "some text" ...)
	//      (printout wwhatever ..........)
	strcat(filename,"\\CLIPSDLL.ERR");

	ofstream ofError(filename,ios::out | ios::app | ios::binary);
	BOOL		fHandled = FALSE;

	hold.assign(str);
	rnum = GetRouteNum(router);
	if(rnum != -1)	{
		if(pRoutes[rnum] != NULL)	{
			pRoutes[rnum]->Add(str);
			fHandled = TRUE;
         }
		}
   Route.assign(router);
	if (FileRoutes!=NULL)	{
		if(FileRoutes->IsMember(router)!=-1L)	{
			long		i = 0;
			String   tstring;

			tstring.assign(router);
			i = FileRoutes->GetIndex(tstring);
			if(i != -1L)	{
				ofstream ofError((const char *)(*FileNames)[i].c_str(),ios::out | ios::app | ios::binary);
				ofError << hold;
				ofError << "\r\n";
				fHandled = TRUE;
				}
			}
		}

	if(!fHandled)	{
		//no need to stream just a CRLF - CLIPS has a tendancy to
		//'belch' these out. We are also not formatting the output
		//in any way.
		if((_fstrncmp(hold.c_str(),"\r\n",2)!= 0)	&&
			(_fstrncmp(hold.c_str(),"\r",1)!= 0) 	&&
			(_fstrncmp(hold.c_str(),"\n",1)!= 0))	{
			hold.append("\r\n",0);
			ofError << "Msg from ";
			ofError << (char *)router;
			ofError << "\r\n";
			ofError << hold;
			ofError << "\r\n";
			}
		}
	return TRUE;
}	//end of extern 'c' declaration block

//return ordinal number of specified route
int GetRouteNum(const char far* route)
{
	int rnum = -1;

	if(strcmp(route, "stdout")==0)
		rnum = 0;
	if(strcmp(route, "werror")==0)
		rnum = 1;
	if(strcmp(route, "wclips")==0)
		rnum = 2;
	if(strcmp(route, "wdisplay")==0)
		rnum = 3;
	if(strcmp(route, "wdialog")==0)
		rnum = 4;
	if(strcmp(route, "wtrace")==0)
		rnum = 5;
	if(strcmp(route, "wagenda")==0)
		rnum = 6;
	if(strcmp(route, "wwarning")==0)
		rnum = 7;
	return rnum;
}

/////////////////////////////////////////////////////////////////////////////
// CCLIPSWrap - all class implementation functions

//constructor
//note: construction does NOT load the DLL
CCLIPSWrap::CCLIPSWrap(int count)
	: factPtr (NULL), rulePtr (NULL), modulePtr (NULL), globalPtr (NULL),
	  instancePtr (NULL), classPtr (NULL), m_hLib (NULL)
{
	Defcount = count;
	Running = FALSE;
	m_fClipsInit = FALSE;
	CLEARMBUF
	for(int i = 0; i < NUMROUTES; i++)	{
		pRoutes[i] = NULL;
		}
	FileRoutes	= NULL;
	FileNames	= NULL;       
	CBfunctPtr  = NULL;
}

//destructor
//cleanup memory and unload the DLL
CCLIPSWrap::~CCLIPSWrap()
{
	factPtr = NULL;
	rulePtr = NULL;
	modulePtr = NULL;
	globalPtr = NULL;
	instancePtr = NULL;
	classPtr = NULL;
	if(FileRoutes != NULL)	{
		delete FileRoutes;
		FileRoutes = NULL;
		}
	if(FileNames != NULL)	{
		delete FileNames;
		FileNames = NULL;
		}
	if(m_hLib != NULL)		{
		FreeLibrary(m_hLib);
		m_hLib = NULL;
      }
//the route buffers in memory will not be deleted
//since we did not allocate them in this class
}

/////////////////////////////////////////////////////////////////////////////
// virtual functions

/***************************************************************************
		Function	:	CCLIPSWrap::SetMsgLoopCBfn(void far (*func_ptr)(void))
		Author		:	Mark Tomlinson
		Desc.		:	set callback function which be invoked during msgpump loops
		Returns		:	none
****************************************************************************/
void CCLIPSWrap::SetMsgLoopCBfn(void far (*func_ptr)(BOOL))
{
	CBfunctPtr = func_ptr;
}


/***************************************************************************
		Function	:	CCLIPSWrap::SetFactPtr(void* Fact)
		Author		:	Mark Tomlinson
		Desc.		:	set factPtr to Fact
		Returns		:	none
****************************************************************************/
void CCLIPSWrap::SetFactPtr(void* Fact)
{
	factPtr = Fact;
}

/***************************************************************************
		Function	:	CCLIPSWrap::SetRulePtr(void* Rule)
		Author		:	Mark Tomlinson
		Desc.		:	set rulePtr to Rule
		Returns		:	none
****************************************************************************/
void CCLIPSWrap::SetRulePtr(void* Rule)
{
	rulePtr = Rule;
}

/***************************************************************************
		Function	:	CCLIPSWrap::SetModulePtr(void* Module)
		Author		:	Mark Tomlinson
		Desc.		:	set modulePtr to Module
		Returns		:	none
****************************************************************************/
void CCLIPSWrap::SetModulePtr(void* Module)
{
	modulePtr = Module;
}

/***************************************************************************
		Function	:	CCLIPSWrap::SetGlobalPtr(void* Global)
		Author		:	Mark Tomlinson
		Desc.		:	set globalPtr to Global
		Returns		:	none
****************************************************************************/
void CCLIPSWrap::SetGlobalPtr(void* Global)
{
	globalPtr = Global;
}

/***************************************************************************
		Function	:	CCLIPSWrap::SetInstancePtr(void* Instance)
		Author		:	Mark Tomlinson
		Desc.		:	set instancePtr to Instance
		Returns		:	none
****************************************************************************/
void CCLIPSWrap::SetInstancePtr(void* Instance)
{
	instancePtr = Instance;
}

/***************************************************************************
		Function	:	CCLIPSWrap::SetClassPtr(void* Class)
		Author		:	Mark Tomlinson
		Desc.		:	set classPtr to Class
		Returns		:	none
****************************************************************************/
void CCLIPSWrap::SetClassPtr(void* Class)
{
	classPtr = Class;
}

/***************************************************************************
		Function	:	CCLIPSWrap::SetAgendaPtr(void* Agenda)
		Author		:	Mark Tomlinson
		Desc.		:	set AgendaPtr to Agenda
		Returns		:	none
****************************************************************************/
void CCLIPSWrap::SetAgendaPtr(void* Agenda)
{
	agendaPtr = Agenda;
}

/***************************************************************************
		Function	:	CCLIPSWrap::SetActivationPtr(void* Activation)
		Author		:	Mark Tomlinson
		Desc.		:	set activationPtr to Activation
		Returns		:	none
****************************************************************************/
void CCLIPSWrap::SetActivationPtr(void* Activation)
{
	activationPtr = Activation;
}

/***************************************************************************
		Function	:	CCLIPSWrap::GetFactPtr(void)
		Author		:	Mark Tomlinson
		Desc.		:	access factPtr
		Returns		:	factPtr
****************************************************************************/
void* CCLIPSWrap::GetFactPtr(void)
{
	return factPtr;
}

/***************************************************************************
		Function	:	CCLIPSWrap::GetRulePtr(void)
		Author		:	Mark Tomlinson
		Desc.		:	access rulePtr
		Returns		:	rulePtr
****************************************************************************/
void* CCLIPSWrap::GetRulePtr(void)
{
	return rulePtr;
}

/***************************************************************************
		Function	:	CCLIPSWrap::GetModulePtr(void)
		Author		:	Mark Tomlinson
		Desc.		:	access modulePtr
		Returns		:	modulePtr
****************************************************************************/
void* CCLIPSWrap::GetModulePtr(void)
{
	return modulePtr;
}

/***************************************************************************
		Function	:	CCLIPSWrap::GetGlobalPtr(void)
		Author		:	Mark Tomlinson
		Desc.		:	access globalPtr
		Returns		:	modulePtr
****************************************************************************/
void* CCLIPSWrap::GetGlobalPtr(void)
{
	return globalPtr;
}

/***************************************************************************
		Function	:	CCLIPSWrap::GetInstancePtr(void)
		Author		:	Mark Tomlinson
		Desc.		:	access instancePtr
		Returns		:	instancePtr
****************************************************************************/
void* CCLIPSWrap::GetInstancePtr(void)
{
	return instancePtr;
}

/***************************************************************************
		Function	:	CCLIPSWrap::GetClassPtr(void)
		Author		:	Mark Tomlinson
		Desc.		:	access classPtr
		Returns		:	classPtr
****************************************************************************/
void* CCLIPSWrap::GetClassPtr(void)
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -