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

📄 clipodbc.cpp

📁 clips的odbc应用
💻 CPP
字号:
/*
--------------------------------------------------------------------
-                                                                  -
- CLIPODBC.cpp                                                     -
-                                                                  -
-    This program demonstrates the CLIPS/ODBC interface.           -
-                                                                  -
--------------------------------------------------------------------

*/

#include <time.h>
#include <stdio.h>
#include <string.h>
#include <direct.h>
#include "c:\clips\wrapper\clipsmfc.h"

int main(int argc, char *argv[])
{
	CCLIPSWrap CLIPSEng(1); //Create an instance of CLIPS
	CString strCLIPSScript, strAssert, strODBCInfo;
	CString strQuery, strDeftemplateName, strOutFile = "clpdrb.txt";
	char szPathBuffer[OFS_MAXPATHNAME], *buf;
	int iErrCode, i; FILE *fp;
	unsigned long lFileSize, lNumBytesRead;
	time_t   dbStart, dbFinish, CLIPSStart, CLIPSFinish;
	double   elapsed_time;
	int iODBCParmFound = 0, iPathParmFound = 0;

#ifdef _DEBUG
	strODBCInfo = "DSN=CLIPSODBC;";
	getcwd(szPathBuffer, OFS_MAXPATHNAME);
	strCLIPSScript = szPathBuffer;
	strCLIPSScript += "\\relation.clp";
#else
	//See if any command line parameters are set
	for(i = 0; i < argc; i++)
	{
		//Obtain ODBC connect information from command line
		if(strcmp(argv[i], "-O") == 0)
			iODBCParmFound = i;

		if(strcmp(argv[i], "-P") == 0)
			iPathParmFound = i;
	}

	//If ODBC command line override found, use it
	if(iODBCParmFound != 0)
	{
		strODBCInfo = "DSN=";
		strODBCInfo += argv[iODBCParmFound + 1];
		printf("Found at %d %s\n", iODBCParmFound, LPCSTR(strODBCInfo));
	}
	else
		strODBCInfo = "DSN=CLIPSODBC;";

	//If there is no path sepcified on the command, then assume current
	//   directory.
	if(iPathParmFound != 0)
	{
		strcpy(szPathBuffer, argv[iPathParmFound + 1]);
	}
	else
	{
		getcwd(szPathBuffer, OFS_MAXPATHNAME);
	}
	
	if(strlen(szPathBuffer) != 3) //Not installed on root
		strcat(szPathBuffer, "\\");

	strCLIPSScript = szPathBuffer;
	strCLIPSScript += "relation.clp";
#endif

	//Initialize the CLIPS Engine -- DLL Loaded here!
	//If Initialization fails, display message to browser.
	if(!CLIPSEng.CLIPSInit())
	{
		printf("CLIPS failed during initialization. Make sure the DLL is either in the current directory or somewhere in your PATH\n");
		return(0);
	}

	//Load the CLIPS script(facts/rules etc.)
	//If Load fails, display message.
	iErrCode = CLIPSEng.CLIPSLoad(strCLIPSScript);
	if(iErrCode != CCLIPSWrap::READ_OK)
	{
		printf("CLIPS failed while loading the script %s\n", LPCSTR(strCLIPSScript));
		switch (iErrCode)
		{
			case CCLIPSWrap::READ_FAIL :
				printf("Read Failure\n");
				break;
			case CCLIPSWrap::PARSE_FAIL :
				printf("Parse Failure\n");
				break;
			case CCLIPSWrap::BAD_LOAD_NAME :
				printf("Bad Load Name\n");
				break;
			case CCLIPSWrap::READ_NOT_INIT :
				printf("Read Not Initialized\n");
		}

		CLIPSEng.CLIPSExit(0);
		return(0);
	}

	//Issue CLIPS 'reset' Command
	CLIPSEng.CLIPSReset();

	// -- Execute any ODBC Queries -- //
	//Record the starting time for database access
	time( &dbStart );

	//Assert 'father-of' facts
	strDeftemplateName = "father-of";
	strQuery = "SELECT father, child FROM FATHEROF";
	CLIPSEng.CLIPSODBCQuery(strQuery, 
							strODBCInfo,
							strDeftemplateName,
							TRUE);

	//Assert 'mother-of' facts
	strDeftemplateName = "mother-of";
	strQuery = "SELECT mother, child FROM MOTHEROF";
	CLIPSEng.CLIPSODBCQuery(strQuery, 
							strODBCInfo,
							strDeftemplateName,
							TRUE);

	//Assert 'wife-of' facts, husband of automatic
	strDeftemplateName = "wife-of";
	strQuery = "SELECT wife, husband FROM WIFEOF";
	CLIPSEng.CLIPSODBCQuery(strQuery, 
							strODBCInfo,
							strDeftemplateName,
							TRUE);

	//Assert 'male' facts, husband of automatic
	strDeftemplateName = "male";
	strQuery = "SELECT person FROM male";
	CLIPSEng.CLIPSODBCQuery(strQuery, 
							strODBCInfo,
							strDeftemplateName,
							TRUE);

	//Assert 'female' facts, husband of automatic
	strDeftemplateName = "female";
	strQuery = "SELECT person FROM female";
	CLIPSEng.CLIPSODBCQuery(strQuery, 
							strODBCInfo,
							strDeftemplateName,
							TRUE);

	//Record the ending time for database access
	time( &dbFinish );

	//Assert Print-Phase fact, start the rules firing.
	strAssert = "(Print-Phase (phase Print-Brother))";
	if(!CLIPSEng.CLIPSAssert(strAssert))
	{
		return(-255);
	}

	//Dribble the output to a file to be displayed later
	CLIPSEng.CLIPSDribble(strOutFile, TRUE);

	//Record the start time for the CLIPS 'Run'
	time( &CLIPSStart );

	//Run the CLIPS Engine
	CLIPSEng.CLIPSRun();

	//Record the end time for the CLIPS 'Run'
	time( &CLIPSFinish );

	//Close dribble output
	CLIPSEng.CLIPSDribble(strOutFile, FALSE);

	//Open the file
	if((fp = fopen(LPCSTR(strOutFile), "r+b")) == NULL)
	{
		fprintf(stderr, "\nFile %s could not be opened\n", LPCSTR(strOutFile));
		exit(1);
	}

	//Set the file pointer to the end
	fseek(fp, 0L, SEEK_END);

	//Get the file size
	lFileSize = ftell(fp);

	//Set the pointer back to the beginning
	fseek(fp, 0L, SEEK_SET);

	buf = NULL;
	buf = (char *)malloc(lFileSize + 1);
	if(!buf)
	{
		fprintf(stderr, "Buffer Allocation failed\n");
		exit(2);
	}

	buf[lFileSize] = '\0'; //Terminating NULL

	lNumBytesRead = fread(buf, sizeof(char), lFileSize, fp);
	if(lNumBytesRead != lFileSize)
	{
		fprintf(stderr, "Unable to read file %s\n", LPCSTR(strOutFile));
		free(buf);
		exit(3);
	}
	
	//Close the file
	fclose(fp);

	//Print the output and free memory
	printf("%s\n", buf);
	free(buf);

	//Exit CLIPS and close any open files
	CLIPSEng.CLIPSExit(0);

	//For the terminally curious
	//if /stat or -stat is entered on the command, elapsed times will show
	for(i = 0; i < argc; i++)
	{
		if(!strcmp(argv[i], "/stat"))
		{
			elapsed_time = difftime( dbFinish, dbStart );
			printf( "\nDatabase access and fact assertion took %6.0f seconds.\n", elapsed_time );

			elapsed_time = difftime( CLIPSFinish, CLIPSStart );
			printf( "\nCLIPS 'Run' time took %6.0f seconds.\n", elapsed_time );
		}

		if(!strcmp(argv[i], "-stat"))
		{
			elapsed_time = difftime( dbFinish, dbStart );
			printf( "\nDatabase access and fact assertion took %6.0f seconds.\n", elapsed_time );

			elapsed_time = difftime( CLIPSFinish, CLIPSStart );
			printf( "\nCLIPS 'Run' time took %6.0f seconds.\n", elapsed_time );
		}
	}

	return(0);
}

⌨️ 快捷键说明

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