unsigned int ss_model::scan_references(char *input, gld_property *target[], unsigned max) { unsigned int n; char *p; char ref[1024]; for ( p=input,n=0 ; *p!='\0' && n<max ; ) { while ( *p!='\0' && isspace(*p) ) p++; // skip white space if ( *p!='\0' && sscanf(p,"%s",ref)==0 ) break; char objname[64], propname[64]; if ( sscanf(ref,"%[^.].%s",objname,propname)!=2 ) exception("'%s' is not a valid 'object.property' reference",ref); OBJECT *obj = gl_get_object(objname); if ( obj==NULL ) exception("object '%s' is not found", objname); target[n] = new gld_property(obj,propname); if ( !target[n]->is_valid() ) exception("property '%s.%s' is not found", objname, propname); if ( target[n]->get_type()!=PT_double && target[n]->get_type()!=PT_random ) exception("property '%s.%s' is not a real number", objname, propname); while ( *p!='\0' && !isspace(*p) ) p++; // skip non-space n++; } return n; }
void machine::send(char *to, void *str, size_t len) { if (len==0) len=strlen((char*)str); OBJECT *obj = gl_get_object(to); machine *dst = OBJECTDATA(obj,plc)->get_machine(); send(new Message(str,len,this,dst)); }
void sendx(machine *src, char *to, void *str, unsigned int len) { if (len==0) len=(unsigned int)strlen((char*)str); OBJECT *obj = gl_get_object(to); if (obj) { machine *dst = OBJECTDATA(obj,plc)->get_machine(); src->send(new Message(str,len,src,dst)); } else gl_error("sendx(machine=%x, to='%s', str='%-.8s%s', len=%d): message recipient '%s' not found", src, to, (char*)str, len>8?"...":"", len, to); }
EXPORT void submit_bid_state(char *from, char *to, char *function_name, char *function_class, void *bidding_buffer, size_t bid_len)//(char *obj, char *from, double quantity, double price, unsigned int is_on, KEY bid_id) { char biddername[64]; int rv; OBJECT *obj = NULL; BIDINFO *bidding_info = (BIDINFO *)bidding_buffer; if( strncmp(function_name, "submit_bid_state", 16)== 0){ obj = gl_get_object(to); if( obj == NULL){ gl_error("bid::submit_bid_state: No auction object exists with given name %s.", to); bidding_info->bid_accepted = false; } if (obj->oclass==auction::oclass) { if(bidding_info->state == BS_UNKNOWN) {// not a stateful bid gl_verbose("%s submits stateless bid for Q:%.2f at P:%.4f", from,bidding_info->quantity,bidding_info->price); auction *mkt = OBJECTDATA(obj,auction); rv = mkt->submit(from,bidding_info->quantity,bidding_info->price,bidding_info->bid_id,bidding_info->state,bidding_info->rebid, bidding_info->market_id); } else { gl_verbose("%s submits stateful (%s) bid for Q:%.2f at P:%.4f", from,bidding_info->state,bidding_info->quantity,bidding_info->price); auction *mkt = OBJECTDATA(obj,auction); rv = mkt->submit(from,bidding_info->quantity,bidding_info->price,bidding_info->bid_id,bidding_info->state,bidding_info->rebid, bidding_info->market_id); } if(rv == 0) { bidding_info->bid_accepted = false; } } else if(obj->oclass == stubauction::oclass){ gl_error("bid::submit_bid_state: market object is not an auction object."); bidding_info->bid_accepted = false; } else { gl_error("bid::submit_bid_state: market object is not an auction object."); bidding_info->bid_accepted = false; } } else { gl_error("bid::submit_bid_state: This function is not the intended function. %s was the intended function to call. Ignoring bid", function_name); } }
EXPORT int check() { /* check each link to make sure it has a node at either end */ FINDLIST *list = gl_find_objects(FL_NEW,FT_MODULE,SAME,"powerflow",NULL); OBJECT *obj=NULL; int *nodemap, /* nodemap marks where nodes are */ *linkmap, /* linkmap counts the number of links to/from a given node */ *tomap; /* counts the number of references to any given node */ int errcount = 0; int objct = 0; int queuef = 0, queueb = 0, queuect = 0; int islandct = 0; int i, j; GLOBALVAR *gvroot = NULL; PFLIST anchor, *tlist = NULL; link **linklist = NULL, **linkqueue = NULL; objct = gl_get_object_count(); anchor.ptr = NULL; anchor.next = NULL; nodemap = (int *)malloc((size_t)(objct*sizeof(int))); linkmap = (int *)malloc((size_t)(objct*sizeof(int))); tomap = (int *)malloc((size_t)(objct*sizeof(int))); linkqueue = (link **)malloc((size_t)(objct*sizeof(link *))); linklist = (link **)malloc((size_t)(objct*sizeof(link *))); memset(nodemap, 0, objct*sizeof(int)); memset(linkmap, 0, objct*sizeof(int)); memset(tomap, 0, objct*sizeof(int)); memset(linkqueue, 0, objct*sizeof(link *)); memset(linklist, 0, objct*sizeof(link *)); /* per-object checks */ /* check from/to info on links */ while (obj=gl_find_next(list,obj)) { if (gl_object_isa(obj,"node")) { /* add to node map */ nodemap[obj->id]+=1; /* if no parent, then add to anchor list */ if(obj->parent == NULL){ tlist = (PFLIST *)malloc(sizeof(PFLIST)); tlist->ptr = obj; tlist->next = anchor.next; anchor.next = tlist; tlist = NULL; } } else if (gl_object_isa(obj,"link")) { link *pLink = OBJECTDATA(obj,link); OBJECT *from = pLink->from; OBJECT *to = pLink->to; node *tNode = OBJECTDATA(to, node); node *fNode = OBJECTDATA(from, node); /* count 'to' reference */ tomap[to->id]++; /* check link connections */ if (from==NULL){ gl_error("link %s (%s:%d) from object is not specified", pLink->get_name(), pLink->oclass->name, pLink->get_id()); ++errcount; } else if (!gl_object_isa(from,"node")){ gl_error("link %s (%s:%d) from object is not a node", pLink->get_name(), pLink->oclass->name, pLink->get_id()); ++errcount; } else { /* is a "from" and it isa(node) */ linkmap[from->id]++; /* mark that this node has a link from it */ } if (to==NULL){ gl_error("link %s (%s:%d) to object is not specified", pLink->get_name(), pLink->oclass->name, pLink->get_id()); ++errcount; } else if (!gl_object_isa(to,"node")){ gl_error("link %s (%s:%d) to object is not a node", pLink->get_name(), pLink->oclass->name, pLink->get_id()); ++errcount; } else { /* is a "to" and it isa(node) */ linkmap[to->id]++; /* mark that this node has links to it */ } /* add link to heap? */ if((from != NULL) && (to != NULL) && (linkmap[from->id] > 0) && (linkmap[to->id] > 0)){ linklist[queuect] = pLink; queuect++; } // check phases /* this isn't cooperating with me. -MH */ /* if(tNode->get_phases(PHASE_A) == fNode->get_phases(PHASE_A)){ gl_error("link:%i: to, from nodes have mismatched A phase (%i vs %i)", obj->id, tNode->get_phases(PHASE_A), fNode->get_phases(PHASE_A)); ++errcount; } if(tNode->get_phases(PHASE_B) == fNode->get_phases(PHASE_B)){ gl_error("link:%i: to, from nodes have mismatched B phase (%i vs %i)", obj->id, tNode->get_phases(PHASE_B), fNode->get_phases(PHASE_B)); ++errcount; } if(tNode->get_phases(PHASE_C) == fNode->get_phases(PHASE_C)){ gl_error("link:%i: to, from nodes have mismatched C phase (%i vs %i)", obj->id, tNode->get_phases(PHASE_C), fNode->get_phases(PHASE_C)); ++errcount; } if(tNode->get_phases(PHASE_D) == fNode->get_phases(PHASE_D)){ gl_error("link:%i: to, from nodes have mismatched D phase (%i vs %i)", obj->id, tNode->get_phases(PHASE_D), fNode->get_phases(PHASE_D)); ++errcount; } if(tNode->get_phases(PHASE_N) == fNode->get_phases(PHASE_N)){ gl_error("link:%i: to, from nodes have mismatched N phase (%i vs %i)", obj->id, tNode->get_phases(PHASE_N), fNode->get_phases(PHASE_N)); ++errcount; }*/ } } for(i = 0; i < objct; ++i){ /* locate unlinked nodes */ if(nodemap[i] != 0){ if(linkmap[i] * nodemap[i] > 0){ /* there is a node at [i] and links to it*/ ; } else if(linkmap[i] == 1){ /* either a feeder or an endpoint */ ; } else { /* unattached node */ gl_error("node:%i: node with no links to or from it", i); nodemap[i] *= -1; /* mark as unlinked */ ++errcount; } } } for(i = 0; i < objct; ++i){ /* mark by islands*/ if(nodemap[i] > 0){ /* has linked node */ linkmap[i] = i; /* island until proven otherwise */ } else { linkmap[i] = -1; /* just making sure... */ } } queueb = 0; for(i = 0; i < queuect; ++i){ if(linklist[i] != NULL){ /* consume the next item */ linkqueue[queueb] = linklist[i]; linklist[i] = NULL; queueb++; } while(queuef < queueb){ /* expand this island */ linkmap[linkqueue[queuef]->to->id] = linkmap[linkqueue[queuef]->from->id]; /* capture the adjacent nodes */ for(j = 0; j < queuect; ++j){ if(linklist[j] != NULL){ if(linklist[j]->from->id == linkqueue[queuef]->to->id){ linkqueue[queueb] = linklist[j]; linklist[j] = NULL; ++queueb; } } } ++queuef; } /* we've consumed one island, grab another */ } for(i = 0; i < objct; ++i){ if(nodemap[i] != 0){ gl_testmsg("node:%i on island %i", i, linkmap[i]); if(linkmap[i] == i){ ++islandct; } } if(tomap[i] > 1){ FINDLIST *cow = gl_find_objects(FL_NEW,FT_ID,SAME,i,NULL); OBJECT *moo = gl_find_next(cow, NULL); char grass[64]; gl_output("object #%i, \'%s\', has more than one link feeding to it (this will diverge)", i, gl_name(moo, grass, 64)); } } gl_output("Found %i islands", islandct); tlist = anchor.next; while(tlist != NULL){ PFLIST *tptr = tlist; tlist = tptr->next; free(tptr); } /* An extra something to check link directionality, * if the root node has been defined on the command line. * -d3p988 */ gvroot = gl_global_find("powerflow::rootnode"); if(gvroot != NULL){ PFLIST *front=NULL, *back=NULL, *del=NULL; /* node queue */ OBJECT *_node = gl_get_object((char *)gvroot->prop->addr); OBJECT *_link = NULL; int *rankmap = (int *)malloc((size_t)(objct*sizeof(int))); int bct = 0; if(_node == NULL){ gl_error("powerflow check(): Unable to do directionality check, root node name not found."); } else { gl_testmsg("Powerflow Check ~ Backward Links:"); } for(int i = 0; i < objct; ++i){ rankmap[i] = objct; } rankmap[_node->id] = 0; front = (PFLIST *)malloc(sizeof(PFLIST)); front->next = NULL; front->ptr = _node; back = front; while(front != NULL){ // find all links from the node for(OBJECT *now=gl_find_next(list, NULL); now != NULL; now = gl_find_next(list, now)){ link *l; if(!gl_object_isa(now, "link")) continue; l = OBJECTDATA(now, link); if((l->from != front->ptr) && (l->to != front->ptr)){ continue; } else if(rankmap[l->from->id]<objct && rankmap[l->to->id]<objct){ continue; } else if(rankmap[l->from->id] < rankmap[l->to->id]){ /* mark */ rankmap[l->to->id] = rankmap[l->from->id]+1; } else if(rankmap[l->from->id] > rankmap[l->to->id]){ /* swap & mark */ OBJECT *t = l->from; gl_testmsg("reversed link: %s goes from %s to %s", now->name, l->from->name, l->to->name); l->from = l->to; l->to = t; rankmap[l->to->id] = rankmap[l->from->id]+1;; } // enqueue the "to" node back->next = (PFLIST *)malloc(sizeof(PFLIST)); back->next->next = NULL; //back->next->ptr = l->to; back = back->next; back->ptr = l->to; } del = front; front = front->next; free(del); } } free(nodemap); free(linkmap); free(linklist); free(linkqueue); return 0; }