📄 reorder.c
字号:
*newChange = TRUE; break; } /*==============================================================*/ /* Convert not/or CE combinations into and/not CE combinations. */ /*==============================================================*/ else if ((theLHS->type == NOT_CE) && (argPtr->type == OR_CE)) { change = TRUE; *newChange = TRUE; tempArg = argPtr->right; argPtr->right = NULL; argPtr->bottom = NULL; ReturnLHSParseNodes(theEnv,argPtr); theLHS->type = AND_CE; theLHS->right = tempArg; while (tempArg != NULL) { newNode = GetLHSParseNode(theEnv); CopyLHSParseNode(theEnv,newNode,tempArg,FALSE); newNode->right = tempArg->right; newNode->bottom = NULL; tempArg->type = NOT_CE; tempArg->negated = FALSE; tempArg->exists = FALSE; tempArg->existsNand = FALSE; tempArg->logical = FALSE; tempArg->value = NULL; tempArg->expression = NULL; tempArg->right = newNode; tempArg = tempArg->bottom; } break; } /*=====================================*/ /* Remove duplication of or CEs within */ /* or CEs and and CEs within and CEs. */ /*=====================================*/ else if (((theLHS->type == OR_CE) && (argPtr->type == OR_CE)) || ((theLHS->type == AND_CE) && (argPtr->type == AND_CE))) { if (argPtr->logical) theLHS->logical = TRUE; change = TRUE; *newChange = TRUE; tempArg = argPtr->right; nextArg = argPtr->bottom; argPtr->right = NULL; argPtr->bottom = NULL; ReturnLHSParseNodes(theEnv,argPtr); if (lastArg == NULL) { theLHS->right = tempArg; } else { lastArg->bottom = tempArg; } argPtr = tempArg; while (tempArg->bottom != NULL) tempArg = tempArg->bottom; tempArg->bottom = nextArg; } /*===================================================*/ /* If no changes are needed, move on to the next CE. */ /*===================================================*/ else { count++; lastArg = argPtr; argPtr = argPtr->bottom; } } } /*===========================*/ /* Return the reordered LHS. */ /*===========================*/ return(theLHS); }/***********************************************************//* PerformReorder2: Reorders a group of CEs to accommodate *//* KB Rete topology. The second pass performs all other *//* transformations not associated with the or CE. *//***********************************************************/static struct lhsParseNode *PerformReorder2( void *theEnv, struct lhsParseNode *theLHS, int *newChange) { struct lhsParseNode *argPtr; int change; /*======================================================*/ /* Loop through the CEs as long as changes can be made. */ /*======================================================*/ change = TRUE; *newChange = FALSE; while (change) { change = FALSE; for (argPtr = theLHS->right; argPtr != NULL;) { /*=======================================================*/ /* A sequence of three not CEs grouped within each other */ /* can be replaced with a single not CE. For example, */ /* (not (not (not (a)))) can be replaced with (not (a)). */ /*=======================================================*/ if ((theLHS->type == NOT_CE) && (argPtr->type == NOT_CE) && (argPtr->right != NULL) && (argPtr->right->type == NOT_CE)) { change = TRUE; *newChange = TRUE; theLHS->right = argPtr->right->right; argPtr->right->right = NULL; ReturnLHSParseNodes(theEnv,argPtr); break; } /*==========================================*/ /* Replace two not CEs containing a pattern */ /* CE with an exists pattern CE. */ /*==========================================*/ else if ((theLHS->type == NOT_CE) && (argPtr->type == NOT_CE) && (argPtr->right != NULL) && (argPtr->right->type == PATTERN_CE)) { change = TRUE; *newChange = TRUE; CopyLHSParseNode(theEnv,theLHS,argPtr->right,FALSE); theLHS->negated = TRUE; theLHS->exists = TRUE; theLHS->existsNand = FALSE; theLHS->right = argPtr->right->right; argPtr->right->networkTest = NULL; argPtr->right->externalNetworkTest = NULL; argPtr->right->secondaryNetworkTest = NULL; argPtr->right->secondaryExternalNetworkTest = NULL; argPtr->right->externalRightHash = NULL; argPtr->right->externalLeftHash = NULL; argPtr->right->leftHash = NULL; argPtr->right->rightHash = NULL; argPtr->right->betaHash = NULL; argPtr->right->expression = NULL; argPtr->right->userData = NULL; argPtr->right->right = NULL; argPtr->right->bottom = NULL; ReturnLHSParseNodes(theEnv,argPtr); break; } /*======================================*/ /* Replace not CEs containing a pattern */ /* CE with a negated pattern CE. */ /*======================================*/ else if ((theLHS->type == NOT_CE) && (argPtr->type == PATTERN_CE)) { change = TRUE; *newChange = TRUE; CopyLHSParseNode(theEnv,theLHS,argPtr,FALSE); theLHS->negated = TRUE; theLHS->exists = FALSE; theLHS->existsNand = FALSE; theLHS->right = argPtr->right; argPtr->networkTest = NULL; argPtr->externalNetworkTest = NULL; argPtr->secondaryNetworkTest = NULL; argPtr->secondaryExternalNetworkTest = NULL; argPtr->externalRightHash = NULL; argPtr->externalLeftHash = NULL; argPtr->constantSelector = NULL; argPtr->constantValue = NULL; argPtr->leftHash = NULL; argPtr->rightHash = NULL; argPtr->betaHash = NULL; argPtr->expression = NULL; argPtr->userData = NULL; argPtr->right = NULL; argPtr->bottom = NULL; ReturnLHSParseNodes(theEnv,argPtr); break; } /*============================================================*/ /* Replace "and" and "not" CEs contained within a not CE with */ /* just the and CE, but increment the nand depths of the */ /* pattern contained within. */ /*============================================================*/ else if ((theLHS->type == NOT_CE) && ((argPtr->type == AND_CE) || (argPtr->type == NOT_CE))) { change = TRUE; *newChange = TRUE; theLHS->type = argPtr->type; theLHS->negated = argPtr->negated; theLHS->exists = argPtr->exists; theLHS->existsNand = argPtr->existsNand; theLHS->value = argPtr->value; theLHS->logical = argPtr->logical; theLHS->right = argPtr->right; argPtr->right = NULL; argPtr->bottom = NULL; ReturnLHSParseNodes(theEnv,argPtr); IncrementNandDepth(theEnv,theLHS->right,TRUE); break; } /*===================================================*/ /* If no changes are needed, move on to the next CE. */ /*===================================================*/ else { argPtr = argPtr->bottom; } } } /*===========================*/ /* Return the reordered LHS. */ /*===========================*/ return(theLHS); }/**************************************************//* ReverseAndOr: Switches and/or CEs into *//* equivalent or/and CEs. For example: *//* *//* (and (or a b) (or c d)) *//* *//* would be converted to *//* *//* (or (and a (or c d)) (and b (or c d))), *//* *//* if the "or" CE being expanded was (or a b). *//**************************************************/static struct lhsParseNode *ReverseAndOr( void *theEnv, struct lhsParseNode *listOfCEs, struct lhsParseNode *orCE, int orPosition) { int count; struct lhsParseNode *listOfExpandedOrCEs = NULL; struct lhsParseNode *lastExpandedOrCE = NULL; struct lhsParseNode *copyOfCEs, *replaceCE; /*========================================================*/ /* Loop through each of the CEs contained within the "or" */ /* CE that is being expanded into the enclosing "and" CE. */ /*========================================================*/ while (orCE != NULL) { /*===============================*/ /* Make a copy of the and/or CE. */ /*===============================*/ copyOfCEs = CopyLHSParseNodes(theEnv,listOfCEs); /*====================================================*/ /* Get a pointer to the "or" CE being expanded in the */ /* copy just made based on the position of the "or" */ /* CE in the original and/or CE (e.g., 1st, 2nd). */ /*====================================================*/ for (count = 1, replaceCE = copyOfCEs->right; count != orPosition; count++, replaceCE = replaceCE->bottom) { /* Do Nothing*/ } /*===================================================*/ /* Delete the contents of the "or" CE being expanded */ /* in the copy of the and/or CE. From the example */ /* above, (and (or a b) (or c d)) would be replaced */ /* with (and (or) (or c d)). Note that the "or" CE */ /* is still left as a placeholder. */ /*===================================================*/ ReturnLHSParseNodes(theEnv,replaceCE->right); /*======================================================*/ /* Copy the current CE being examined in the "or" CE to */ /* the placeholder left in the and/or CE. From the */ /* example above, (and (or) (or c d)) would be replaced */ /* with (and a (or c d)) if the "a" pattern from the */ /* "or" CE was being examined or (and b (or c d)) if */ /* the "b" pattern from the "or" CE was being examined. */ /*======================================================*/ CopyLHSParseNode(theEnv,replaceCE,orCE,TRUE); replaceCE->right = CopyLHSParseNodes(theEnv,orCE->right); /*====================================*/ /* Add the newly expanded "and" CE to */ /* the list of CEs already expanded. */ /*====================================*/ if (lastExpandedOrCE == NULL) { listOfExpandedOrCEs = copyOfCEs; copyOfCEs->bottom = NULL; lastExpandedOrCE = copyOfCEs; } else { lastExpandedOrCE->bottom = copyOfCEs; copyOfCEs->bottom = NULL; lastExpandedOrCE = copyOfCEs; } /*=======================================================*/ /* Move on to the next CE in the "or" CE being expanded. */ /*=======================================================*/ orCE = orCE->bottom; } /*=====================================================*/ /* Release the original and/or CE list to free memory. */ /*=====================================================*/ ReturnLHSParseNodes(theEnv,listOfCEs); /*================================================*/ /* Wrap an or CE around the list of expanded CEs. */ /*================================================*/ copyOfCEs = GetLHSParseNode(theEnv); copyOfCEs->type = OR_CE; copyOfCEs->right = listOfExpandedOrCEs; /*================================*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -