| 1 | #include "global.hh" | 
|---|
| 2 | #include "sgraph.hh" | 
|---|
| 3 | #include "mgraph.hh" | 
|---|
| 4 | #include "grammar.hh" | 
|---|
| 5 | #include "const.hh" | 
|---|
| 6 | #include <stdio.h> | 
|---|
| 7 |  | 
|---|
| 8 |  | 
|---|
| 9 | int SGraph::add_base_snode(MNode* mn) | 
|---|
| 10 | { | 
|---|
| 11 | int nodeind=n; | 
|---|
| 12 | SNode &node=nodes[n]; | 
|---|
| 13 |  | 
|---|
| 14 | node.clear(); | 
|---|
| 15 |  | 
|---|
| 16 | node.mnode=mn; | 
|---|
| 17 |  | 
|---|
| 18 | for(vector<MNode*>::iterator pm=node.mnode->pred.begin(); pm!=node.mnode->pred.end(); ++pm) | 
|---|
| 19 | for(vector<int>::iterator ps=(*pm)->snodes.begin(); ps!=(*pm)->snodes.end(); ++ps) | 
|---|
| 20 | if(nodes[*ps].in_LH) | 
|---|
| 21 | { | 
|---|
| 22 | node.LV.set(*ps); | 
|---|
| 23 | if(nodes[*ps].saturated()) node.LV |= nodes[*ps].LH; | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 | mn->snodes.push_back(nodeind); | 
|---|
| 27 | ++n; | 
|---|
| 28 |  | 
|---|
| 29 | node.in_LH=true; | 
|---|
| 30 |  | 
|---|
| 31 | return nodeind; | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | void SGraph::update_left(int headind, int depind) | 
|---|
| 36 | { | 
|---|
| 37 | SNode &head=nodes[headind], &dep=nodes[depind]; | 
|---|
| 38 |  | 
|---|
| 39 | if(dep.saturated()) head.LV |= dep.LV, head.LD |= dep.LD; | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 | void SGraph::update_right(int headind, int depind) | 
|---|
| 44 | { | 
|---|
| 45 | SNode &head=nodes[headind], &dep=nodes[depind]; | 
|---|
| 46 |  | 
|---|
| 47 | dep.LH.set(headind); | 
|---|
| 48 | if(head.saturated()) | 
|---|
| 49 | dep.LH |= head.LH; | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 |  | 
|---|
| 53 | int SGraph::clone(int ancind, NodeProp newprop) | 
|---|
| 54 | { | 
|---|
| 55 | int newind = n++; | 
|---|
| 56 | SNode &newnode=nodes[newind]; | 
|---|
| 57 | SNode &ancnode = nodes[ancind]; | 
|---|
| 58 |  | 
|---|
| 59 | newnode.clear(); | 
|---|
| 60 | newnode.prop=newprop; | 
|---|
| 61 | newnode.mnode=ancnode.mnode; | 
|---|
| 62 | newnode.mnode->snodes.push_back(newind); | 
|---|
| 63 | return newind; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 67 | //------------------------------------------------------------------------- | 
|---|
| 68 | //------------------------------------------------------------------------- | 
|---|
| 69 |  | 
|---|
| 70 |  | 
|---|
| 71 | int SGraph::print_node(FILE* f, int n, unsigned int info) | 
|---|
| 72 | { | 
|---|
| 73 | char buf[1000]; | 
|---|
| 74 | sprint_node(buf,n,info); | 
|---|
| 75 | fputs(buf,f); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | int SGraph::sprint_node(char* buf, int nodeind, unsigned int info) | 
|---|
| 79 | { | 
|---|
| 80 | char* buf0=buf; | 
|---|
| 81 | char descr[256]; | 
|---|
| 82 | char nodeinfo[16]; | 
|---|
| 83 |  | 
|---|
| 84 | SNode &node=nodes[nodeind]; | 
|---|
| 85 |  | 
|---|
| 86 | buf+=sprintf(buf," dgp:%d",nodeind); | 
|---|
| 87 | buf+=sprintf(buf, saturated(nodeind) ? ";s" : ";u"); | 
|---|
| 88 |  | 
|---|
| 89 | bool cont=false; | 
|---|
| 90 | if (info&HEADS) | 
|---|
| 91 | { | 
|---|
| 92 | buf+=sprintf(buf,";"); | 
|---|
| 93 | for(vector<Arc>::iterator h=node.heads.begin(); h!=node.heads.end(); ++h) | 
|---|
| 94 | { | 
|---|
| 95 | if(cont) buf+=sprintf(buf,","); else cont=true; | 
|---|
| 96 | buf+=sprintf(buf,"++%s-%d/%d",h->role.str(),h->dst,h->anc); | 
|---|
| 97 | } | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | if (info&DEPS) | 
|---|
| 101 | { | 
|---|
| 102 | buf+=sprintf(buf,";"); | 
|---|
| 103 | for(vector<Arc>::iterator d=node.deps.begin(); d!=node.deps.end(); ++d) | 
|---|
| 104 | { | 
|---|
| 105 | //      if(! nodes[d->dst].saturated()) continue; // NIE DRUKUJ NIENASYCONYCH PODRZEDNIKOW | 
|---|
| 106 | if(cont) buf+=sprintf(buf,","); else cont=true; | 
|---|
| 107 | buf+=sprintf(buf,"--%s-%d/%d",d->role.str(),d->dst,d->anc); | 
|---|
| 108 | } | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | if (info&SETS) | 
|---|
| 112 | { | 
|---|
| 113 | int ord=0; | 
|---|
| 114 | buf+=sprintf(buf,";{"); | 
|---|
| 115 | for(vector<MNode*>::iterator pm=node.mnode->pred.begin(); pm!=node.mnode->pred.end(); ++pm) | 
|---|
| 116 | for(vector<int>::iterator ps=(*pm)->snodes.begin(); ps!=(*pm)->snodes.end(); ++ps) | 
|---|
| 117 | buf+=sprintf(buf, ord++ ? ",%d" : "%d", *ps); | 
|---|
| 118 | buf+=sprintf(buf,"};{"); | 
|---|
| 119 | ord=0;for(int j=0; j<=n; ++j) if(node.LV[j]) buf+=sprintf(buf, ord++ ? ",%d" : "%d", j); | 
|---|
| 120 | buf+=sprintf(buf,"};{"); | 
|---|
| 121 | ord=0;for(int j=0; j<=n; ++j) if(node.LH[j]) buf+=sprintf(buf, ord++ ? ",%d" : "%d", j); | 
|---|
| 122 | buf+=sprintf(buf,"};{"); | 
|---|
| 123 | ord=0;for(int j=0; j<=n; ++j) if(node.LD[j]) buf+=sprintf(buf, ord++ ? ",%d" : "%d", j); | 
|---|
| 124 | buf+=sprintf(buf,"}"); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | if (info&CONSTRAINTS)//  buf+=sprint_node_constraints(buf,n); | 
|---|
| 128 | { | 
|---|
| 129 | buf+=sprintf(buf,";"); | 
|---|
| 130 | int cont=0; | 
|---|
| 131 | for(Role i=1; i<=Role::count(); ++i) | 
|---|
| 132 | if(node.prop.forbidden[i]) buf+=sprintf(buf,"%s!%s",(cont++)?",":"",i.str()); | 
|---|
| 133 | for(Role i=1; i<=Role::count(); ++i) | 
|---|
| 134 | if(node.prop.required[i]) buf+=sprintf(buf,"%s&%s",(cont++)?",":"",i.str()); | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | //   buf+=sprintf(buf,"\n"); | 
|---|
| 138 |  | 
|---|
| 139 | return buf-buf0; | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 |  | 
|---|
| 143 | int SGraph::sprint_node_debug(char* buf, const char* pref, int n) | 
|---|
| 144 | { | 
|---|
| 145 | char *buf0 = buf; | 
|---|
| 146 | buf+=sprintf(buf,"#%s",pref); | 
|---|
| 147 | buf+=sprint_node(buf,n,HEADS|DEPS|SETS|CONSTRAINTS); | 
|---|
| 148 | buf+=sprintf(buf,"\n"); | 
|---|
| 149 | return buf-buf0; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | int SGraph::print_node_debug(FILE* f, const char* pref, int n) | 
|---|
| 153 | { | 
|---|
| 154 | char buf[1000]; | 
|---|
| 155 | sprint_node_debug(buf,pref,n); | 
|---|
| 156 | fputs(buf,f); | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | void SGraph::print_arc(FILE* f, int left, int right, Role role, int dir) // 0 - left, 1 - right | 
|---|
| 160 | { | 
|---|
| 161 | fprintf(f,"#   %s:%s.%02d %s %s.%02d\n", | 
|---|
| 162 | role.str(),nodes[left].mnode->type,left, | 
|---|
| 163 | dir ? "-->" : "<--", | 
|---|
| 164 | nodes[right].mnode->type,right); | 
|---|
| 165 | } | 
|---|