| [5f4d9c3] | 1 | #ifndef _GRAMMAR_HH |
|---|
| 2 | #define _GRAMMAR_HH |
|---|
| 3 | |
|---|
| 4 | #include <bitset> |
|---|
| 5 | #include <vector> |
|---|
| 6 | #include <list> |
|---|
| 7 | #include <set> |
|---|
| 8 | |
|---|
| 9 | #include "const.hh" |
|---|
| 10 | #include "thesymbols.hh" |
|---|
| 11 | #include "sgraph.hh" |
|---|
| [e7de6cc] | 12 | #include "boubble.hh" |
|---|
| [5f4d9c3] | 13 | |
|---|
| [e7de6cc] | 14 | using namespace std; |
|---|
| [5f4d9c3] | 15 | |
|---|
| [e7de6cc] | 16 | //enum PROP { INIT=0, FIN=1 }; |
|---|
| 17 | //typedef bitset<16> PropSet; |
|---|
| 18 | |
|---|
| 19 | const PropSet EmptyPropSet = PropSet(); |
|---|
| 20 | |
|---|
| 21 | const FlagSet EmptyFlagSet = FlagSet(); |
|---|
| 22 | |
|---|
| 23 | //==================================================================================================== |
|---|
| 24 | // class Link |
|---|
| 25 | //==================================================================================================== |
|---|
| 26 | |
|---|
| 27 | struct Link |
|---|
| [5f4d9c3] | 28 | { |
|---|
| [e7de6cc] | 29 | Link(Role r, Flag dfplus="NULL", Flag dfminus="NULL") : role(r), dflagplus(dfplus), dflagminus(dfminus) { } |
|---|
| 30 | Link(Role r, PropSet ps=EmptyPropSet, Flag hfp="NULL", Flag hfm="NULL", Flag dfp="NULL", Flag dfm="NULL") |
|---|
| 31 | : role(r), props(ps), hflagplus(hfp), hflagminus(hfm), dflagplus(dfp), dflagminus(dfm) { } |
|---|
| 32 | //Link(Role r) : role(r), dflagplus("NULL") { } |
|---|
| 33 | |
|---|
| [5f4d9c3] | 34 | Role role; |
|---|
| [e7de6cc] | 35 | Flag hflagplus; |
|---|
| 36 | Flag hflagminus; |
|---|
| 37 | Flag dflagplus; |
|---|
| 38 | Flag dflagminus; |
|---|
| 39 | PropSet props; |
|---|
| 40 | |
|---|
| 41 | bool operator<(const Link& l) const |
|---|
| 42 | { |
|---|
| 43 | if(role < l.role) return true; |
|---|
| 44 | if(hflagplus < l.hflagplus) return true; |
|---|
| 45 | if(hflagminus < l.hflagminus) return true; |
|---|
| 46 | if(dflagplus < l.dflagplus) return true; |
|---|
| 47 | if(dflagminus < l.dflagminus) return true; |
|---|
| 48 | if(props.to_ulong() < l.props.to_ulong()) return true; |
|---|
| 49 | return false; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| [5f4d9c3] | 52 | }; |
|---|
| 53 | |
|---|
| [e7de6cc] | 54 | typedef set<Link> Links; |
|---|
| 55 | |
|---|
| 56 | //==================================================================================================== |
|---|
| 57 | // class Grammar |
|---|
| 58 | //==================================================================================================== |
|---|
| [5f4d9c3] | 59 | |
|---|
| 60 | class Grammar |
|---|
| 61 | { |
|---|
| 62 | |
|---|
| 63 | public: |
|---|
| 64 | |
|---|
| [e7de6cc] | 65 | static const int RESIZE_DELTA=16; |
|---|
| [5f4d9c3] | 66 | |
|---|
| [e7de6cc] | 67 | Grammar() {} ; |
|---|
| 68 | |
|---|
| 69 | Roles& connectable(Cat h, Cat d); |
|---|
| 70 | Roles connectable(Cat h, Cat d, FlagSet f, FlagSet df); |
|---|
| 71 | |
|---|
| 72 | list<const Link*> connectable2(Cat h, Cat d, FlagSet hfs, FlagSet dfs); |
|---|
| 73 | |
|---|
| 74 | bool check_constr(NodeProp& hprop, NodeProp& dprop, int dir, Role role); |
|---|
| 75 | bool check_constr2(NodeProp& hprop, NodeProp& dprop, int dir, const Link& link); |
|---|
| 76 | |
|---|
| 77 | bool check_longrel(Cat hcat, Cat dcat, LongRel rel); |
|---|
| 78 | bool is_sgl(Role r); |
|---|
| 79 | RoleSet is_obl(Cat c); |
|---|
| 80 | |
|---|
| 81 | RoleSet& constr_include(Role r) { return include[r]; }; |
|---|
| 82 | RoleSet& constr_exclude(Role r) { return exclude[r]; }; |
|---|
| [5f4d9c3] | 83 | |
|---|
| [e7de6cc] | 84 | FlagSet initial_flags(Cat c) { return set[c]; } |
|---|
| 85 | FlagSet pass_flags(Role r) { return pass[r]; } |
|---|
| 86 | |
|---|
| 87 | list<Boubble*> trigger_boubbles(Cat c, Role r, Dir d); |
|---|
| 88 | |
|---|
| 89 | bool read(FILE* f); |
|---|
| 90 | void write(ostream& os); |
|---|
| 91 | void write(FILE* f); |
|---|
| 92 | |
|---|
| 93 | private: |
|---|
| [5f4d9c3] | 94 | |
|---|
| 95 | RoleSet sgl; |
|---|
| [e7de6cc] | 96 | vector< RoleSet > obl; //[Cat] |
|---|
| [5f4d9c3] | 97 | RoleSet left; |
|---|
| 98 | RoleSet right; |
|---|
| [e7de6cc] | 99 | RoleSet init; |
|---|
| 100 | RoleSet fin; |
|---|
| 101 | FlagSet initf; |
|---|
| 102 | FlagSet finf; |
|---|
| 103 | |
|---|
| 104 | vector< RoleSet > lt; //[Role] |
|---|
| 105 | vector< RoleSet > gt; //[Role] |
|---|
| 106 | |
|---|
| 107 | vector< FlagSet > set; //[Cat] |
|---|
| 108 | // vector< FlagSet > rset; //[Role] |
|---|
| 109 | vector< FlagSet > pass; //[Role] |
|---|
| [5f4d9c3] | 110 | |
|---|
| [e7de6cc] | 111 | vector< vector< Roles > > connect; //[Cat][Cat] |
|---|
| [5f4d9c3] | 112 | |
|---|
| [e7de6cc] | 113 | vector< vector< Links > > connect1; //[Cat][Cat] |
|---|
| [5f4d9c3] | 114 | |
|---|
| [e7de6cc] | 115 | vector< RoleSet > include; //[Role] |
|---|
| 116 | vector< RoleSet > exclude; //[Role] |
|---|
| 117 | |
|---|
| 118 | vector< vector< LongRels > > longrel; //[Cat][Cat] |
|---|
| 119 | |
|---|
| [519eaf5] | 120 | list< Boubble* > boubbles; |
|---|
| [e7de6cc] | 121 | |
|---|
| 122 | vector< vector< list<Boubble*> > > uptrigger;//[Cat][Role] |
|---|
| 123 | vector< vector< list<Boubble*> > > dntrigger;//[Cat][Role] |
|---|
| [5f4d9c3] | 124 | |
|---|
| 125 | void add_category(const char* s); |
|---|
| 126 | void add_type(const char* s); |
|---|
| [e7de6cc] | 127 | void add_flag(const char* s) { Flag::add(s); } |
|---|
| [519eaf5] | 128 | void add_long(const char* l, const char* p) { LongRel::add(l); boubbles.push_back( new Boubble(p,l) ); |
|---|
| 129 | boubbles.push_back( (new Boubble(p,l))->reversed() ); } |
|---|
| [e7de6cc] | 130 | void add_triggers(Cat h, Cat d, LongRel l); |
|---|
| 131 | |
|---|
| 132 | void set_sgl(Role r) { sgl.set(r); } |
|---|
| 133 | void set_obl(Cat c, Role r) { obl[c].set(r); } |
|---|
| 134 | void set_left(Role r) { left.set(r); } |
|---|
| 135 | void set_right(Role r) { right.set(r); } |
|---|
| 136 | void set_init(Role r) { init.set(r); } |
|---|
| 137 | void set_fin(Role r) { fin.set(r); } |
|---|
| 138 | void set_initf(Flag f) { initf.set(f); } |
|---|
| 139 | void set_finf(Flag f) { finf.set(f); } |
|---|
| 140 | void set_order(Role r, Role s) { lt[s].set(r); } |
|---|
| 141 | |
|---|
| 142 | // void set_connect(Cat c, Cat d, Role r) { connect[c][d].insert(r); } |
|---|
| 143 | // void set_connect(Cat c, Cat d, Flag f, Role r) { connect1[c][d].insert(Link(r,f)); } |
|---|
| 144 | |
|---|
| 145 | void set_connect(Cat h, Flag hfp, Flag hfm, Cat d, Flag dfp, Flag dfm, Role r, PropSet ps ) { connect1[h][d].insert(Link(r,ps,hfp,hfm,dfp,dfm)); } |
|---|
| 146 | |
|---|
| 147 | void set_include(Role r, Role s) { include[r].set(s); } |
|---|
| 148 | void set_exclude(Role r, Role s) { exclude[r].set(s); } |
|---|
| 149 | void set_longrel(Cat c, Cat d, LongRel l){ longrel[c][d].insert(l); } |
|---|
| 150 | void set_set(Cat c, Flag f) { set[c].set(f); } |
|---|
| 151 | void set_pass(Role r, Flag f) { pass[r].set(f); } |
|---|
| [5f4d9c3] | 152 | void set_lt(Role r, Role s); |
|---|
| 153 | void compute_gt(); |
|---|
| [e7de6cc] | 154 | void compute_triggers(); |
|---|
| 155 | bool contains_boubble(const list<Boubble*> boubble_list, Boubble* bp) const; |
|---|
| [5f4d9c3] | 156 | |
|---|
| [e7de6cc] | 157 | }; |
|---|
| [5f4d9c3] | 158 | |
|---|
| [e7de6cc] | 159 | //---------------------------------------------------------------------------------------------------- |
|---|
| [5f4d9c3] | 160 | |
|---|
| [e7de6cc] | 161 | inline |
|---|
| 162 | Roles& Grammar::connectable(Cat h, Cat d) |
|---|
| 163 | { |
|---|
| 164 | return connect[h][d]; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | //---------------------------------------------------------------------------------------------------- |
|---|
| 168 | |
|---|
| 169 | inline |
|---|
| 170 | Roles Grammar::connectable(Cat h, Cat d, FlagSet hfs, FlagSet dfs) // ZBYT WOLNE!!!!!!!!!!!!!!!!!!!!!!!!!! (-> Roles&) |
|---|
| 171 | { |
|---|
| 172 | Roles ret; |
|---|
| 173 | for(Links::const_iterator l = connect1[h][d].begin(); l != connect1[h][d].end(); l++) |
|---|
| 174 | if( (l->hflagplus==0 || hfs[l->hflagplus]) && (l->hflagminus==0 || !hfs[l->hflagminus]) ) |
|---|
| 175 | if( (l->dflagplus==0 || dfs[l->dflagplus]) && (l->dflagminus==0 || !dfs[l->dflagminus]) ) |
|---|
| 176 | ret.insert(l->role); |
|---|
| 177 | return ret; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | //---------------------------------------------------------------------------------------------------- |
|---|
| 181 | |
|---|
| 182 | inline |
|---|
| 183 | list<const Link*> Grammar::connectable2(Cat h, Cat d, FlagSet hfs, FlagSet dfs) // ZBYT WOLNE!!!!!!!!!!!!!!!!!!!!!!!!!! (-> Roles&) |
|---|
| 184 | { |
|---|
| 185 | list<const Link*> ret; |
|---|
| 186 | for(Links::const_iterator l = connect1[h][d].begin(); l != connect1[h][d].end(); l++) |
|---|
| 187 | if( (l->hflagplus==0 || hfs[l->hflagplus]) && (l->hflagminus==0 || !hfs[l->hflagminus]) ) |
|---|
| 188 | if( (l->dflagplus==0 || dfs[l->dflagplus]) && (l->dflagminus==0 || !dfs[l->dflagminus]) ) |
|---|
| 189 | ret.push_back(&(*l)); |
|---|
| 190 | return ret; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | //---------------------------------------------------------------------------------------------------- |
|---|
| [5f4d9c3] | 194 | |
|---|
| [a15e59b] | 195 | // inline |
|---|
| 196 | // bool Grammar::check_constr(NodeProp& hprop, NodeProp& dprop, int dir, Role role) // dir: 0-left 1-right |
|---|
| 197 | // { |
|---|
| 198 | // return |
|---|
| 199 | // !hprop.forbidden[role] && |
|---|
| 200 | // ( dir==1 || !right[role] ) && |
|---|
| 201 | // ( dir==0 || !left[role] ) && |
|---|
| 202 | // ( dir==1 || (hprop.attached&init).none() ) && |
|---|
| 203 | // ( dir==0 || (hprop.attached&fin).none() ) |
|---|
| 204 | // ; |
|---|
| 205 | // } |
|---|
| [e7de6cc] | 206 | |
|---|
| 207 | //---------------------------------------------------------------------------------------------------- |
|---|
| 208 | |
|---|
| 209 | inline |
|---|
| 210 | bool Grammar::check_constr2(NodeProp& hprop, NodeProp& dprop, int dir, const Link& link) // dir: 0-left 1-right |
|---|
| 211 | { |
|---|
| 212 | return |
|---|
| 213 | !hprop.forbidden[link.role] && |
|---|
| [a15e59b] | 214 | ( dir==1 || (!right[link.role] && !link.props[Prop("RIGHT")]) ) && // ZREZYGNOWAÆ Z TABLICY right[<role>] |
|---|
| 215 | ( dir==0 || (!left[link.role] && !link.props[Prop("LEFT")]) ) && |
|---|
| [e7de6cc] | 216 | ( dir!=0 || !hprop.init_attached ) && |
|---|
| 217 | ( dir!=1 || !hprop.fin_attached ) |
|---|
| [5f4d9c3] | 218 | ; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| [e7de6cc] | 221 | //---------------------------------------------------------------------------------------------------- |
|---|
| 222 | |
|---|
| 223 | inline |
|---|
| 224 | bool Grammar::check_longrel(Cat hcat, Cat dcat, LongRel rel) |
|---|
| 225 | { |
|---|
| 226 | return longrel[hcat][dcat].find(rel) != longrel[hcat][dcat].end(); |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | //---------------------------------------------------------------------------------------------------- |
|---|
| 230 | |
|---|
| 231 | inline bool Grammar::is_sgl(Role r) |
|---|
| 232 | { |
|---|
| 233 | return sgl[r]; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | //---------------------------------------------------------------------------------------------------- |
|---|
| 237 | |
|---|
| 238 | inline RoleSet Grammar::is_obl(Cat c) |
|---|
| 239 | { |
|---|
| 240 | return obl[c]; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | //==================================================================================================== |
|---|
| [5f4d9c3] | 244 | |
|---|
| 245 | #endif |
|---|