static void ClearDefrules() { struct defmodule *theModule; theModule = (struct defmodule *) FindDefmodule("MAIN"); Focus((void *) theModule); }
static void ResetDefrules() { struct defmodule *theModule; CurrentEntityTimeTag = 0L; ClearFocusStack(); theModule = (struct defmodule *) FindDefmodule("MAIN"); Focus((void *) theModule); }
void SetCurrentModuleCommand( Environment *theEnv, UDFContext *context, UDFValue *returnValue) { UDFValue theArg; const char *argument; Defmodule *theModule; CLIPSLexeme *oldModuleName; /*=======================*/ /* Set the return value. */ /*=======================*/ theModule = GetCurrentModule(theEnv); if (theModule == NULL) { returnValue->lexemeValue = FalseSymbol(theEnv); return; } oldModuleName = theModule->header.name; returnValue->value = oldModuleName; /*=====================================================*/ /* Check for the correct number and type of arguments. */ /*=====================================================*/ if (! UDFFirstArgument(context,SYMBOL_BIT,&theArg)) { return; } argument = theArg.lexemeValue->contents; /*================================================*/ /* Set the current module to the specified value. */ /*================================================*/ theModule = FindDefmodule(theEnv,argument); if (theModule == NULL) { CantFindItemErrorMessage(theEnv,"defmodule",argument,true); return; } SetCurrentModule(theEnv,theModule); }
Defmodule *GetModuleName( UDFContext *context, unsigned int whichArgument, bool *error) { UDFValue returnValue; Defmodule *theModule; Environment *theEnv = context->environment; const char *functionName = UDFContextFunctionName(context); *error = false; /*========================*/ /* Retrieve the argument. */ /*========================*/ if (! UDFNthArgument(context,1,SYMBOL_BIT,&returnValue)) { *error = true; return NULL; } /*=======================================*/ /* Check to see that the symbol actually */ /* corresponds to a defined module. */ /*=======================================*/ if ((theModule = FindDefmodule(theEnv,returnValue.lexemeValue->contents)) == NULL) { if (strcmp("*",returnValue.lexemeValue->contents) != 0) { ExpectedTypeError1(theEnv,functionName,whichArgument,"'defmodule name'"); *error = true; } return NULL; } /*=================================*/ /* Return a pointer to the module. */ /*=================================*/ return(theModule); }
globle void Reset() { struct callFunctionItem *resetPtr; /*=====================================*/ /* The reset command can't be executed */ /* while a reset is in progress. */ /*=====================================*/ if (ResetInProgress) return; ResetInProgress = TRUE; ResetReadyInProgress = TRUE; /*================================================*/ /* If the reset is performed from the top level */ /* command prompt, reset the halt execution flag. */ /*================================================*/ if (CurrentEvaluationDepth == 0) SetHaltExecution(FALSE); /*=======================================================*/ /* Call the before reset function to determine if the */ /* reset should continue. [Used by the some of the */ /* windowed interfaces to query the user whether a */ /* reset should proceed with activations on the agenda.] */ /*=======================================================*/ if ((BeforeResetFunction != NULL) ? ((*BeforeResetFunction)() == FALSE) : FALSE) { ResetReadyInProgress = FALSE; ResetInProgress = FALSE; return; } ResetReadyInProgress = FALSE; /*===========================*/ /* Call each reset function. */ /*===========================*/ for (resetPtr = ListOfResetFunctions; (resetPtr != NULL) && (GetHaltExecution() == FALSE); resetPtr = resetPtr->next) { (*resetPtr->func)(); } /*============================================*/ /* Set the current module to the MAIN module. */ /*============================================*/ SetCurrentModule((void *) FindDefmodule("MAIN")); /*===========================================*/ /* Perform periodic cleanup if the reset was */ /* issued from an embedded controller. */ /*===========================================*/ if ((CurrentEvaluationDepth == 0) && (! EvaluatingTopLevelCommand) && (CurrentExpression == NULL)) { PeriodicCleanup(TRUE,FALSE); } /*===================================*/ /* A reset is no longer in progress. */ /*===================================*/ ResetInProgress = FALSE; }
void Reset( Environment *theEnv) { struct voidCallFunctionItem *resetPtr; GCBlock gcb; /*=====================================*/ /* The reset command can't be executed */ /* while a reset is in progress. */ /*=====================================*/ if (ConstructData(theEnv)->ResetInProgress) return; ConstructData(theEnv)->ResetInProgress = true; ConstructData(theEnv)->ResetReadyInProgress = true; /*=====================================*/ /* If embedded, clear the error flags. */ /*=====================================*/ if (EvaluationData(theEnv)->CurrentExpression == NULL) { ResetErrorFlags(theEnv); } SetErrorValue(theEnv,NULL); /*========================================*/ /* Set up the frame for tracking garbage. */ /*========================================*/ GCBlockStart(theEnv,&gcb); /*=======================================================*/ /* Call the before reset function to determine if the */ /* reset should continue. [Used by the some of the */ /* windowed interfaces to query the user whether a */ /* reset should proceed with activations on the agenda.] */ /*=======================================================*/ if ((ConstructData(theEnv)->BeforeResetCallback != NULL) ? ((*ConstructData(theEnv)->BeforeResetCallback)(theEnv) == false) : false) { ConstructData(theEnv)->ResetReadyInProgress = false; ConstructData(theEnv)->ResetInProgress = false; return; } ConstructData(theEnv)->ResetReadyInProgress = false; /*===========================*/ /* Call each reset function. */ /*===========================*/ for (resetPtr = ConstructData(theEnv)->ListOfResetFunctions; (resetPtr != NULL) && (GetHaltExecution(theEnv) == false); resetPtr = resetPtr->next) { (*resetPtr->func)(theEnv,resetPtr->context); } /*============================================*/ /* Set the current module to the MAIN module. */ /*============================================*/ SetCurrentModule(theEnv,FindDefmodule(theEnv,"MAIN")); /*===========================================*/ /* Perform periodic cleanup if the reset was */ /* issued from an embedded controller. */ /*===========================================*/ GCBlockEnd(theEnv,&gcb); CallPeriodicTasks(theEnv); /*===================================*/ /* A reset is no longer in progress. */ /*===================================*/ ConstructData(theEnv)->ResetInProgress = false; }
int main(int argc, char **argv) { InitializeEnvironment(); IncrementGCLocks(); Load("src/main.clp"); void * module = FindDefmodule("Hypnotoad"); SetCurrentModule(module); zmq = zmq_init(1); pub = zmq_socket(zmq, ZMQ_PUB); zmq_bind(pub, "tcp://*:5554"); void * sub = zmq_socket(zmq, ZMQ_SUB); zmq_bind(sub, "tcp://*:5555"); zmq_setsockopt(sub, ZMQ_SUBSCRIBE, "", 0); while (1) { char * msg, * topic; int topic_size; char * remote_filename; zmq_msg_t request; zmq_msg_init (&request); zmq_recv (sub, &request, 0); topic = zmq_msg_data (&request); topic_size = zmq_msg_size(&request); int fact = !strncmp(topic, "fact", 4); if (!fact) { remote_filename = (char *) malloc(topic_size + 1); strncpy(remote_filename, topic, topic_size); } zmq_msg_close (&request); zmq_msg_init (&request); zmq_recv (sub, &request, 0); msg = zmq_msg_data (&request); if (fact) { LoadFactsFromString(msg, zmq_msg_size(&request)); Focus(module); Run(-1); } else { char * filename = tmpnam(0); /* dump file */ FILE * f = fopen(filename,"w+"); fwrite(msg, zmq_msg_size(&request), 1, f); fclose(f); char * fact = (char *) malloc(1024); sprintf(fact, "(remote-file \"%s\" \"%s\")\n",remote_filename,filename); /* load file mapping */ Focus(module); LoadFactsFromString(fact, -1); Focus(module); Run(-1); #if 0 DATA_OBJECT res; Eval("(facts)",&res); fflush(0); #endif /* cleanup */ free(fact); free(remote_filename); unlink(filename); free(filename); } zmq_msg_close (&request); } zmq_close(pub); zmq_close(sub); zmq_term(zmq); DecrementGCLocks(); }
static int ParseImportSpec( char *readSource, struct token *theToken, struct defmodule *newModule) { struct defmodule *theModule; struct portItem *thePort, *oldImportSpec; int found, count; /*===========================*/ /* Look for the module name. */ /*===========================*/ SavePPBuffer(" "); GetToken(readSource,theToken); if (theToken->type != SYMBOL) { SyntaxErrorMessage("defmodule import specification"); return(TRUE); } /*=====================================*/ /* Verify the existence of the module. */ /*=====================================*/ if ((theModule = (struct defmodule *) FindDefmodule(ValueToString(theToken->value))) == NULL) { CantFindItemErrorMessage("defmodule",ValueToString(theToken->value)); return(TRUE); } /*========================================*/ /* If the specified module doesn't export */ /* any constructs, then the import */ /* specification is meaningless. */ /*========================================*/ if (theModule->exportList == NULL) { NotExportedErrorMessage(GetDefmoduleName(theModule),NULL,NULL); return(TRUE); } /*==============================================*/ /* Parse the remaining portion of the import */ /* specification and return if an error occurs. */ /*==============================================*/ oldImportSpec = newModule->importList; if (ParseExportSpec(readSource,theToken,newModule,theModule)) return(TRUE); /*========================================================*/ /* If the ?NONE keyword was used with the import spec, */ /* then no constructs were actually imported and the */ /* import spec does not need to be checked for conflicts. */ /*========================================================*/ if (newModule->importList == oldImportSpec) return(FALSE); /*======================================================*/ /* Check to see if the construct being imported can be */ /* by the specified module. This check exported doesn't */ /* guarantee that a specific named construct actually */ /* exists. It just checks that it could be exported if */ /* it does exists. */ /*======================================================*/ if (newModule->importList->constructType != NULL) { /*=============================*/ /* Look for the construct in */ /* the module that exports it. */ /*=============================*/ found = FALSE; for (thePort = theModule->exportList; (thePort != NULL) && (! found); thePort = thePort->next) { if (thePort->constructType == NULL) found = TRUE; else if (thePort->constructType == newModule->importList->constructType) { if (newModule->importList->constructName == NULL) found = TRUE; else if (thePort->constructName == NULL) found = TRUE; else if (thePort->constructName == newModule->importList->constructName) { found = TRUE; } } } /*=======================================*/ /* If it's not exported by the specified */ /* module, print an error message. */ /*=======================================*/ if (! found) { if (newModule->importList->constructName == NULL) { NotExportedErrorMessage(GetDefmoduleName(theModule), ValueToString(newModule->importList->constructType), NULL); } else { NotExportedErrorMessage(GetDefmoduleName(theModule), ValueToString(newModule->importList->constructType), ValueToString(newModule->importList->constructName)); } return(TRUE); } } /*======================================================*/ /* Verify that specific named constructs actually exist */ /* and can be seen from the module importing them. */ /*======================================================*/ SaveCurrentModule(); SetCurrentModule((void *) newModule); for (thePort = newModule->importList; thePort != NULL; thePort = thePort->next) { if ((thePort->constructType == NULL) || (thePort->constructName == NULL)) { continue; } theModule = (struct defmodule *) FindDefmodule(ValueToString(thePort->moduleName)); SetCurrentModule(theModule); if (FindImportedConstruct(ValueToString(thePort->constructType),NULL, ValueToString(thePort->constructName),&count, TRUE,FALSE) == NULL) { NotExportedErrorMessage(GetDefmoduleName(theModule), ValueToString(thePort->constructType), ValueToString(thePort->constructName)); RestoreCurrentModule(); return(TRUE); } } RestoreCurrentModule(); /*===============================================*/ /* The import list has been successfully parsed. */ /*===============================================*/ return(FALSE); }
globle int ParseDefmodule( char *readSource) { SYMBOL_HN *defmoduleName; struct defmodule *newDefmodule; struct token inputToken; int i; struct moduleItem *theItem; struct portItem *portSpecs, *nextSpec; struct defmoduleItemHeader *theHeader; struct callFunctionItem *defineFunctions; struct defmodule *redefiningMainModule = NULL; int parseError; struct portItem *oldImportList, *oldExportList; short overwrite = FALSE; /*================================================*/ /* Flush the buffer which stores the pretty print */ /* representation for a module. Add the already */ /* parsed keyword defmodule to this buffer. */ /*================================================*/ SetPPBufferStatus(ON); FlushPPBuffer(); SetIndentDepth(3); SavePPBuffer("(defmodule "); /*===============================*/ /* Modules cannot be loaded when */ /* a binary load is in effect. */ /*===============================*/ #if BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE if ((Bloaded() == TRUE) && (! CheckSyntaxMode)) { CannotLoadWithBloadMessage("defmodule"); return(TRUE); } #endif /*=====================================================*/ /* Parse the name and comment fields of the defmodule. */ /* Remove the defmodule if it already exists. */ /*=====================================================*/ defmoduleName = GetConstructNameAndComment(readSource,&inputToken,"defmodule", FindDefmodule,DeleteDefmodule,"+", TRUE,TRUE,FALSE); if (defmoduleName == NULL) { return(TRUE); } if (strcmp(ValueToString(defmoduleName),"MAIN") == 0) { redefiningMainModule = (struct defmodule *) FindDefmodule("MAIN"); } /*==============================================*/ /* Create the defmodule structure if necessary. */ /*==============================================*/ if (redefiningMainModule == NULL) { newDefmodule = (struct defmodule *) FindDefmodule(ValueToString(defmoduleName)); if (newDefmodule) { overwrite = TRUE; } else { newDefmodule = get_struct(defmodule); newDefmodule->name = defmoduleName; newDefmodule->usrData = NULL; newDefmodule->next = NULL; } } else { overwrite = TRUE; newDefmodule = redefiningMainModule; } if (overwrite) { oldImportList = newDefmodule->importList; oldExportList = newDefmodule->exportList; } newDefmodule->importList = NULL; newDefmodule->exportList = NULL; /*===================================*/ /* Finish parsing the defmodule (its */ /* import/export specifications). */ /*===================================*/ parseError = ParsePortSpecifications(readSource,&inputToken,newDefmodule); /*====================================*/ /* Check for import/export conflicts. */ /*====================================*/ if (! parseError) parseError = FindMultiImportConflict(newDefmodule); /*======================================================*/ /* If an error occured in parsing or an import conflict */ /* was detected, abort the definition of the defmodule. */ /* If we're only checking syntax, then we want to exit */ /* at this point as well. */ /*======================================================*/ if (parseError || CheckSyntaxMode) { while (newDefmodule->importList != NULL) { nextSpec = newDefmodule->importList->next; rtn_struct(portItem,newDefmodule->importList); newDefmodule->importList = nextSpec; } while (newDefmodule->exportList != NULL) { nextSpec = newDefmodule->exportList->next; rtn_struct(portItem,newDefmodule->exportList); newDefmodule->exportList = nextSpec; } if ((redefiningMainModule == NULL) && (! overwrite)) { rtn_struct(defmodule,newDefmodule); } if (overwrite) { newDefmodule->importList = oldImportList; newDefmodule->exportList = oldExportList; } if (parseError) return(TRUE); return(FALSE); } /*===============================================*/ /* Increment the symbol table counts for symbols */ /* used in the defmodule data structures. */ /*===============================================*/ if (redefiningMainModule == NULL) { IncrementSymbolCount(newDefmodule->name); } else { if ((newDefmodule->importList != NULL) || (newDefmodule->exportList != NULL)) { MainModuleRedefinable = FALSE; } } for (portSpecs = newDefmodule->importList; portSpecs != NULL; portSpecs = portSpecs->next) { if (portSpecs->moduleName != NULL) IncrementSymbolCount(portSpecs->moduleName); if (portSpecs->constructType != NULL) IncrementSymbolCount(portSpecs->constructType); if (portSpecs->constructName != NULL) IncrementSymbolCount(portSpecs->constructName); } for (portSpecs = newDefmodule->exportList; portSpecs != NULL; portSpecs = portSpecs->next) { if (portSpecs->moduleName != NULL) IncrementSymbolCount(portSpecs->moduleName); if (portSpecs->constructType != NULL) IncrementSymbolCount(portSpecs->constructType); if (portSpecs->constructName != NULL) IncrementSymbolCount(portSpecs->constructName); } /*====================================================*/ /* Allocate storage for the module's construct lists. */ /*====================================================*/ if (redefiningMainModule != NULL) { /* Do nothing */ } else if (NumberOfModuleItems == 0) newDefmodule->itemsArray = NULL; else { newDefmodule->itemsArray = (struct defmoduleItemHeader **) gm2((int) sizeof(void *) * NumberOfModuleItems); for (i = 0, theItem = ListOfModuleItems; (i < NumberOfModuleItems) && (theItem != NULL); i++, theItem = theItem->next) { if (theItem->allocateFunction == NULL) { newDefmodule->itemsArray[i] = NULL; } else { newDefmodule->itemsArray[i] = (struct defmoduleItemHeader *) (*theItem->allocateFunction)(); theHeader = (struct defmoduleItemHeader *) newDefmodule->itemsArray[i]; theHeader->theModule = newDefmodule; theHeader->firstItem = NULL; theHeader->lastItem = NULL; } } } /*=======================================*/ /* Save the pretty print representation. */ /*=======================================*/ SavePPBuffer("\n"); if (GetConserveMemory() == TRUE) { newDefmodule->ppForm = NULL; } else { newDefmodule->ppForm = CopyPPBuffer(); } /*==============================================*/ /* Add the defmodule to the list of defmodules. */ /*==============================================*/ if (redefiningMainModule == NULL) { if (LastDefmodule == NULL) ListOfDefmodules = newDefmodule; else LastDefmodule->next = newDefmodule; LastDefmodule = newDefmodule; newDefmodule->bsaveID = NumberOfDefmodules++; } SetCurrentModule((void *) newDefmodule); /*=========================================*/ /* Call any functions required by other */ /* constructs when a new module is defined */ /*=========================================*/ for (defineFunctions = AfterModuleDefinedFunctions; defineFunctions != NULL; defineFunctions = defineFunctions->next) { (* (void (*)(void)) defineFunctions->func)(); } /*===============================================*/ /* Defmodule successfully parsed with no errors. */ /*===============================================*/ return(FALSE); }