source: src/dgp/grammar.hh @ a15e59b

Last change on this file since a15e59b was a15e59b, checked in by Tomasz Obrebski <to@…>, 12 years ago

dodana opcja --time w dgp, poprawione przesy�anie b�belk�w, obsluga &LEFT, &RIGHT

  • Property mode set to 100644
File size: 8.1 KB
Line 
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"
12#include "boubble.hh"
13
14using namespace std;
15
16//enum PROP { INIT=0, FIN=1 };
17//typedef bitset<16> PropSet;
18
19const PropSet EmptyPropSet = PropSet();
20
21const FlagSet EmptyFlagSet = FlagSet();
22
23//====================================================================================================
24// class Link
25//====================================================================================================
26
27struct Link
28{
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
34  Role role;
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
52};
53
54typedef set<Link> Links;
55
56//====================================================================================================
57// class Grammar
58//====================================================================================================
59
60class Grammar
61{
62
63 public:
64
65  static const int RESIZE_DELTA=16;
66
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]; };
83 
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
93private:
94
95  RoleSet                      sgl;
96  vector< RoleSet >            obl;      //[Cat]
97  RoleSet                      left;
98  RoleSet                      right;
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]
110
111  vector< vector< Roles > >    connect;  //[Cat][Cat]
112
113  vector< vector< Links > >    connect1; //[Cat][Cat]
114
115  vector< RoleSet >            include;  //[Role]
116  vector< RoleSet >            exclude;  //[Role]
117
118  vector< vector< LongRels > > longrel;  //[Cat][Cat]
119
120  list< Boubble* >              boubbles;
121 
122  vector< vector< list<Boubble*> > >   uptrigger;//[Cat][Role]
123  vector< vector< list<Boubble*> > >   dntrigger;//[Cat][Role]
124
125  void add_category(const char* s);
126  void add_type(const char* s);
127  void add_flag(const char* s)                   { Flag::add(s); }
128  void add_long(const char* l, const char* p)    { LongRel::add(l); boubbles.push_back( new Boubble(p,l) ); }
129  void add_triggers(Cat h, Cat d, LongRel l);
130
131  void set_sgl(Role r)                     { sgl.set(r); }
132  void set_obl(Cat c, Role r)              { obl[c].set(r); }
133  void set_left(Role r)                    { left.set(r); }
134  void set_right(Role r)                   { right.set(r); }
135  void set_init(Role r)                    { init.set(r); }
136  void set_fin(Role r)                     { fin.set(r); }
137  void set_initf(Flag f)                   { initf.set(f); }
138  void set_finf(Flag f)                    { finf.set(f); }
139  void set_order(Role r, Role s)           { lt[s].set(r); }
140
141  //  void set_connect(Cat c, Cat d, Role r)   { connect[c][d].insert(r); }
142  //  void set_connect(Cat c, Cat d, Flag f, Role r)   { connect1[c][d].insert(Link(r,f)); }
143
144  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)); }
145
146  void set_include(Role r, Role s)         { include[r].set(s); }
147  void set_exclude(Role r, Role s)         { exclude[r].set(s); }
148  void set_longrel(Cat c, Cat d, LongRel l){ longrel[c][d].insert(l); }
149  void set_set(Cat c, Flag f)              { set[c].set(f); }
150  void set_pass(Role r, Flag f)            { pass[r].set(f); }
151  void set_lt(Role r, Role s);
152  void compute_gt();
153  void compute_triggers();
154  bool contains_boubble(const list<Boubble*> boubble_list, Boubble* bp) const;
155
156};
157
158//----------------------------------------------------------------------------------------------------
159
160inline
161Roles&  Grammar::connectable(Cat h, Cat d)
162{
163  return connect[h][d];
164}
165
166//----------------------------------------------------------------------------------------------------
167
168inline
169Roles Grammar::connectable(Cat h, Cat d, FlagSet hfs, FlagSet dfs)  // ZBYT WOLNE!!!!!!!!!!!!!!!!!!!!!!!!!! (-> Roles&)
170{
171  Roles ret;
172  for(Links::const_iterator l = connect1[h][d].begin(); l != connect1[h][d].end(); l++)
173    if( (l->hflagplus==0 || hfs[l->hflagplus]) && (l->hflagminus==0 || !hfs[l->hflagminus]) )
174      if( (l->dflagplus==0 || dfs[l->dflagplus]) && (l->dflagminus==0 || !dfs[l->dflagminus]) )
175        ret.insert(l->role);
176  return ret;
177}
178
179//----------------------------------------------------------------------------------------------------
180
181inline
182list<const Link*> Grammar::connectable2(Cat h, Cat d, FlagSet hfs, FlagSet dfs)  // ZBYT WOLNE!!!!!!!!!!!!!!!!!!!!!!!!!! (-> Roles&)
183{
184  list<const Link*> ret;
185  for(Links::const_iterator l = connect1[h][d].begin(); l != connect1[h][d].end(); l++)
186    if( (l->hflagplus==0 || hfs[l->hflagplus]) && (l->hflagminus==0 || !hfs[l->hflagminus]) )
187      if( (l->dflagplus==0 || dfs[l->dflagplus]) && (l->dflagminus==0 || !dfs[l->dflagminus]) )
188        ret.push_back(&(*l));
189  return ret;
190}
191
192//----------------------------------------------------------------------------------------------------
193
194// inline
195// bool Grammar::check_constr(NodeProp& hprop, NodeProp& dprop, int dir, Role role)    // dir: 0-left 1-right
196// {
197//   return
198//     !hprop.forbidden[role] &&
199//     ( dir==1 || !right[role] ) &&
200//     ( dir==0 || !left[role]  ) &&
201//     ( dir==1 || (hprop.attached&init).none() ) &&
202//     ( dir==0 || (hprop.attached&fin).none() )
203//     ;
204// }
205
206//----------------------------------------------------------------------------------------------------
207
208inline
209bool Grammar::check_constr2(NodeProp& hprop, NodeProp& dprop, int dir, const Link& link)    // dir: 0-left 1-right
210{
211  return 
212    !hprop.forbidden[link.role] &&
213    ( dir==1 || (!right[link.role] && !link.props[Prop("RIGHT")]) ) &&  // ZREZYGNOWAÆ Z TABLICY right[<role>]
214    ( dir==0 || (!left[link.role] && !link.props[Prop("LEFT")]) ) &&
215    ( dir!=0 || !hprop.init_attached ) &&
216    ( dir!=1 || !hprop.fin_attached )
217    ;
218}
219
220//----------------------------------------------------------------------------------------------------
221
222inline
223bool Grammar::check_longrel(Cat hcat, Cat dcat, LongRel rel)
224{
225  return longrel[hcat][dcat].find(rel) != longrel[hcat][dcat].end();
226}
227
228//----------------------------------------------------------------------------------------------------
229
230inline bool Grammar::is_sgl(Role r)
231{
232  return sgl[r];
233}
234
235//----------------------------------------------------------------------------------------------------
236
237inline RoleSet Grammar::is_obl(Cat c)
238{
239  return obl[c];
240}
241
242//====================================================================================================
243
244#endif
Note: See TracBrowser for help on using the repository browser.