📄 tgrecipe.c
字号:
pbufname = ++pqres; pbuf = PQparray(pbufname); ntups = PQntuplesGroup(pbuf, 0); if (ntups == 0) return; fromNode_attnum = PQfnumber(pbuf, 0, "fromNode"); fromPort_attnum = PQfnumber(pbuf, 0, "fromPort"); toNode_attnum = PQfnumber(pbuf, 0, "toNode"); toPort_attnum = PQfnumber(pbuf, 0, "toPort"); for (i = 0; i < ntups; i++) { fromNode = PQgetvalue(pbuf, i, fromNode_attnum); toNode = PQgetvalue(pbuf, i, toNode_attnum); fromPortStr = PQgetvalue(pbuf, i, fromPort_attnum); toPortStr = PQgetvalue(pbuf, i, toPort_attnum); if (!fromPortStr || fromPortStr[0] == '\0') { elog(NOTICE, "lookupEdges(): SANITY CHECK failed. Edge with invalid fromPort value!"); return; } if (!toPortStr || toPortStr[0] == '\0') { elog(NOTICE, "lookupEdges(): SANITY CHECK failed. Edge with invalid toPort value!!"); return; } fromPort = atoi(fromPortStr); toPort = atoi(toPortStr); fromNodePtr = findNodeInRecipe(r, fromNode); if (!fromNodePtr) { elog(NOTICE, "lookupEdges(): SANITY CHECK failed. Edge with bad fromNode value!"); return; } toNodePtr = findNodeInRecipe(r, toNode); if (!toNodePtr) { elog(NOTICE, "lookupEdges(): SANITY CHECK failed. Edge with bad toNode value!"); return; } /* * check to see if the from port is already connected. if it is, * then this means we should construct a Tee node */ if (fromNodePtr->outNodes->val[fromPort - 1] != NULL) { TgNodePtr tn; tn = connectTee(r, fromNodePtr, toNodePtr, fromPort, toPort); addArr_TgNodePtr(r->allNodes, &tn); } else { fromNodePtr->outNodes->val[fromPort - 1] = toNodePtr; toNodePtr->inNodes->val[toPort - 1] = fromNodePtr; } } PQclear(pbufname);}/* handle tee connections here Everytime an output port is connected multiply, we explicitly insert TgTeeNode returns the teeNode created*/static TgNode *connectTee(TgRecipe * r, TgNodePtr fromNode, TgNodePtr toNode, int fromPort, int toPort){ TgNodePtr origToNode; TgNodePtr tn; TgNodePtr BlankNodePtr; int origToPort; int i; /* the toNode formerly pointed to */ origToNode = fromNode->outNodes->val[fromPort - 1]; if (origToNode == NULL) { elog(NOTICE, "Internal Error: connectTee() called with a null origToNode"); return; } for (i = 0; i < origToNode->inNodes->num; i++) { if (origToNode->inNodes->val[i] == fromNode) break; } /* the inport of the former toNode */ /* ports start with 1, array indices start from 0 */ origToPort = i + 1; /* add a tee node now. */ tn = malloc(sizeof(TgNode)); /* generate a name for the tee node table */ tn->nodeName = malloc(50); sprintf(tn->nodeName, "tee_%u", newoid());/* tn->nodeName = NULL; */ tn->nodeType = TG_TEE_NODE; tn->nodeElem = NULL; tn->inNodes = newArr_TgNodePtr(); tn->outNodes = newArr_TgNodePtr(); BlankNodePtr = (TgNodePtr) NULL; /* each TgTeeNode has one input and two outputs, NULL them initiallly */ addArr_TgNodePtr(tn->inNodes, &BlankNodePtr); addArr_TgNodePtr(tn->outNodes, &BlankNodePtr); addArr_TgNodePtr(tn->outNodes, &BlankNodePtr); /* * make the old toNode the left parent of the tee node add the new * toNode as the right parent of the tee node */ tn->outNodes->val[0] = origToNode; origToNode->inNodes->val[origToPort - 1] = tn; tn->outNodes->val[1] = toNode; toNode->inNodes->val[toPort - 1] = tn; /* connect the fromNode to the new tee node */ fromNode->outNodes->val[fromPort - 1] = tn; tn->inNodes->val[0] = fromNode; return tn;}/* -------------------------------------fillAllNodes fill out the nodes of a recipe ------------------------------------ */voidfillAllNodes(TgRecipe * r, char *name){ char qbuf[MAX_QBUF_LENGTH]; int i; char *pqres; char *pbufname; PortalBuffer *pbuf; int ntups; TgElement *elem; TgNode *node; /* 1) fill out the elements that are in the recipe */ sprintf(qbuf, Q_RETRIEVE_ELEMENTS_IN_RECIPE, name); pqres = PQexec(qbuf); if (*pqres == 'R' || *pqres == 'E') { elog(NOTICE, "fillAllNodes(): Error while executing query : %s\n", qbuf); elog(NOTICE, "result = %s, error is %s\n", pqres, PQerrormsg); return; } pbufname = ++pqres; pbuf = PQparray(pbufname); ntups = PQntuplesGroup(pbuf, 0); for (i = 0; i < ntups; i++) { elem = malloc(sizeof(TgElement)); fillTgElement(elem, pbuf, i); addArr_TgElementPtr(r->elements, &elem); } PQclear(pbufname); sprintf(qbuf, Q_RETRIEVE_NODES_IN_RECIPE, name); pqres = PQexec(qbuf); if (*pqres == 'R' || *pqres == 'E') { elog(NOTICE, "fillAllNodes(): Error while executing query : %s\n", qbuf); elog(NOTICE, "result = %s, error is %s\n", pqres, PQerrormsg); return; } pbufname = ++pqres; pbuf = PQparray(pbufname); ntups = PQntuplesGroup(pbuf, 0); for (i = 0; i < ntups; i++) { node = malloc(sizeof(TgNode)); fillTgNode(r, node, pbuf, i); addArr_TgNodePtr(r->allNodes, &node); } PQclear(pbufname);}/* -------------------------------------fillAllElements fill out the elements of a recipe ------------------------------------ */voidfillAllElements(TgRecipe * r, char *name){ char qbuf[MAX_QBUF_LENGTH]; int i; char *pqres; char *pbufname; PortalBuffer *pbuf; int ntups; TgElement *elem; sprintf(qbuf, Q_RETRIEVE_ELEMENTS_IN_RECIPE, name); pqres = PQexec(qbuf); if (*pqres == 'R' || *pqres == 'E') { elog(NOTICE, "fillAllElements(): Error while executing query : %s\n", qbuf); elog(NOTICE, "result = %s, error is %s\n", pqres, PQerrormsg); return; } pbufname = ++pqres; pbuf = PQparray(pbufname); ntups = PQntuplesGroup(pbuf, 0); for (i = 0; i < ntups; i++) { elem = malloc(sizeof(TgElement)); fillTgElement(elem, pbuf, i); addArr_TgElementPtr(r->elements, &elem); } PQclear(pbufname);}/* -------------------------------------fillTgRecipe takes a query result in the PortalBuffer containing a Recipe and converts it to a C TgRecipe strcture ------------------------------------ */TgRecipe *fillTgRecipe(PortalBuffer *pbuf, int tupno){ TgRecipe *r; int i, j; /* 1) set up the recipe structure */ r = (TgRecipe *) malloc(sizeof(TgRecipe)); fillTgElement(&r->elmValue, pbuf, 0); r->elmValue.elemType = TG_RECIPE; r->allNodes = newArr_TgNodePtr(); r->rootNodes = newArr_TgNodePtr(); r->eyes = newArr_TgNodePtr(); r->tees = newArr_TgNodePtr(); r->elements = newArr_TgElementPtr(); /* * 2) find all the elements. There may be less elements than nodes * because you can have multiple instantiations of an element in a * recipe */ fillAllElements(r, r->elmValue.elemName); /* 3) find all the nodes in the recipe */ fillAllNodes(r, r->elmValue.elemName); /* * 4) find all the edges, and connect the nodes, may also add tee * nodes to the allNodes field */ lookupEdges(r, r->elmValue.elemName); /* 5) find all the rootNodes in the recipe */ /* * root nodes are nodes with no incoming nodes or whose incoming nodes * are all null */ /* 6) find all the eyes in the recipe */ /* eye nodes are nodes with the node type TG_EYE_NODE */ /* 7) find all the tee nodes in the recipe */ /* tee nodes are nodes with the node type TG_TEE_NODE */ for (i = 0; i < r->allNodes->num; i++) { TgNode *nptr = r->allNodes->val[i]; if (nptr->nodeType == TG_EYE_NODE) addArr_TgNodePtr(r->eyes, &nptr); else if (nptr->nodeType == TG_TEE_NODE) addArr_TgNodePtr(r->tees, &nptr); if (nptr->inNodes->num == 0) addArr_TgNodePtr(r->rootNodes, &nptr); else { for (j = 0; j < nptr->inNodes->num && (nptr->inNodes->val[j] == NULL); j++); if (j == nptr->inNodes->num) addArr_TgNodePtr(r->rootNodes, &nptr); } } return r;}/* -------------------------------------retrieveRecipe find the recipe with the given name ------------------------------------ */TgRecipe *retrieveRecipe(char *name){ char qbuf[MAX_QBUF_LENGTH]; TgRecipe *recipe; char *pqres; char *pbufname; PortalBuffer *pbuf; int ntups; sprintf(qbuf, Q_RETRIEVE_RECIPE_BYNAME, name); pqres = PQexec(qbuf); if (*pqres == 'R' || *pqres == 'E') { elog(NOTICE, "retrieveRecipe: Error while executing query : %s\n", qbuf); elog(NOTICE, "result = %s, error is %s\n", pqres, PQerrormsg); return NULL; } pbufname = ++pqres; pbuf = PQparray(pbufname); ntups = PQntuplesGroup(pbuf, 0); if (ntups == 0) { elog(NOTICE, "retrieveRecipe(): No recipe named %s exists\n", name); return NULL; } if (ntups != 1) { elog(NOTICE, "retrieveRecipe(): Multiple (%d) recipes named %s exists\n", ntups, name); return NULL; } recipe = fillTgRecipe(pbuf, 0); PQclear(pbufname); return recipe;}/* -------------------- copyXXX functions ----------------------- */voidcopyTgElementPtr(TgElementPtr * from, TgElementPtr * to){ *to = *from;}voidcopyTgNodePtr(TgNodePtr * from, TgNodePtr * to){ *to = *from;}voidcopyTgRecipePtr(TgRecipePtr * from, TgRecipePtr * to){ *to = *from;}voidcopyTgString(TgString * from, TgString * to){ TgString fromTgString = *from; TgString toTgString; toTgString = (TgString) malloc(strlen(fromTgString) + 1); strcpy(toTgString, fromTgString); *to = toTgString;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -