Cadabra
Computer algebra system for field theory problems
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Props.hh
Go to the documentation of this file.
1 /*
2 
3  Cadabra: a field-theory motivated computer algebra system.
4  Copyright (C) 2001-2014 Kasper Peeters <kasper.peeters@phi-sci.com>
5 
6  This program is free software: you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation, either version 3 of the
9  License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 // Classes handling storage of property information. Actual property
22 // implementations are in the properties directory in separate files.
23 
24 #pragma once
25 
26 #include <map>
27 #include <list>
28 #include "Storage.hh"
29 
30 namespace cadabra {
31 
32 class Properties;
33 class Kernel;
34 
35 class pattern {
36  public:
37  pattern();
38  pattern(const Ex&);
39 
40  bool match(const Properties&, const Ex::iterator&, bool ignore_parent_rel=false) const;
41  bool children_wildcard() const;
42 
44 };
45 
47 
48 class keyval_t {
49  public:
50  typedef std::pair<std::string, Ex::iterator> kvpair_t;
51  typedef std::list<kvpair_t> kvlist_t;
52 
53  typedef kvlist_t::const_iterator const_iterator;
54  typedef kvlist_t::iterator iterator;
56 
57  const_iterator find(const std::string&) const;
58  iterator find(const std::string&);
59  const_iterator begin() const;
60  const_iterator end() const;
61  void push_back(const kvpair_t&);
62  void erase(iterator);
63 
64  private:
66 };
67 
83 // default implementation returns true for any pattern.
105 
106 
107 class property {
108  public:
109  property(bool hidden=false);
110  virtual ~property() {};
111 
112  // Parse the argument tree into key-value pairs. Returns false on error.
113  bool parse_to_keyvals(const Ex&, keyval_t&);
114 
115  // Use the pre-parsed arguments in key/value form to set parameters.
116  // Parses universal arguments by default. Will be called once for
117  // every property; assigning a non-list property to multiple patterns
118  // still calls this only once.
119  // FIXME: failure to call
120  // into superclass may lead to problems for labelled properties.
121  virtual bool parse(const Kernel&, keyval_t& keyvals);
122 
123  // Check whether the property can be associated with the pattern.
124  // Throw an error if validation fails. Needs access to all other
125  // declared properties so that it can understand what the pattern
126  // means (which objects are indices etc.).
127  virtual void validate(const Kernel&, const Ex&) const;
128 
130 // virtual void display(std::ostream&) const;
131 
134  virtual void latex(std::ostream&) const;
135 
136  virtual std::string name() const=0;
137  virtual std::string unnamed_argument() const;
138 
139  // To compare properties we sometimes need to compare their variables, not only
140  // their type. The following function needs to be overridden in all properties
141  // for which comparison by type is not sufficient to establish equality.
142  //
143  // id_match: only one of these properties can be registered, but their data is not the same
144  // exact_match: these properties are exactly identical
146  virtual match_t equals(const property *) const;
147 
151  void hidden(bool h);
152  bool hidden(void) const;
153 
154  private:
155  bool parse_one_argument(Ex::iterator arg, keyval_t& keyvals);
156  bool hidden_;
157 };
158 
159 class labelled_property : virtual public property {
160  public:
161  virtual bool parse(const Kernel&, keyval_t&) override;
162  std::string label;
163 };
164 
167 
168 class list_property : public property {
169  public:
170 };
171 
177 
178 template<class T>
179 class Inherit {
180  public:
181  virtual ~Inherit() {};
182  virtual std::string name() const { return std::string("Stay Away"); };
183 };
184 
187 
188 class PropertyInherit : virtual public property {
189  public:
190  virtual std::string name() const { return std::string("PropertyInherit"); };
191 };
192 
202 
203 class Properties {
204  public:
205  // Registering property types.
207  public:
209 
210  typedef std::map<std::string, property* (*)()> internal_property_map_t;
211  typedef internal_property_map_t::iterator iterator;
212 
214  };
215 
219 
220  void register_property(property* (*)(), const std::string& name);
222  typedef std::pair<pattern *, const property *> pat_prop_pair_t;
223 
231  typedef std::multimap<nset_t::iterator, pat_prop_pair_t, nset_it_less> property_map_t;
232  typedef std::multimap<const property *, pattern *> pattern_map_t;
233 
237  std::string master_insert(Ex proptree, property *thepropbase);
238 
239  void clear();
240 
245  property_map_t props; // pattern -> property
246  pattern_map_t pats; // property -> pattern; for list properties, patterns are stored here in order
247 
248  // Normal search: given a pattern, get its property if any.
249  template<class T> const T* get(Ex::iterator, bool ignore_parent_rel=false) const; // Shorthand for get_composite
250  template<class T> const T* get() const;
251  template<class T> const T* get_composite(Ex::iterator, bool ignore_parent_rel=false) const;
252  template<class T> const T* get_composite(Ex::iterator, int& serialnum, bool doserial=true, bool ignore_parent_rel=false) const;
253  // Ditto for labelled properties
254  template<class T> const T* get_composite(Ex::iterator, const std::string& label) const;
255  template<class T> const T* get_composite(Ex::iterator, int& serialnum, const std::string& label, bool doserial=true) const;
256  // For list properties: given two patterns, get a common property.
257  template<class T> const T* get_composite(Ex::iterator, Ex::iterator, bool ignore_parent_rel=false) const;
258  template<class T> const T* get_composite(Ex::iterator, Ex::iterator, int&, int&, bool ignore_parent_rel=false) const;
259 
260  // Get the outermost node which has the given property attached, i.e. go down through
261  // all (if any) nodes which have just inherited the property.
262  template<class T> Ex::iterator head(Ex::iterator, bool ignore_parent_rel=false) const;
263 
264  // Search through pointers
265  bool has(const property *, Ex::iterator);
266  // Find serial number of a pattern in a given list property
267  int serial_number(const property *, const pattern *) const;
268 
269  // Inverse search: given a property type, get a pattern which has this property.
270  // When given an iterator, it starts to search in the property
271  // map from this particular point. Note: this searches on property type, not exact property.
272 // template<class T>
273 // property_map_t::iterator get_pattern(property_map_t::iterator=props.begin());
274 
275  // Equivalent search: given a node, get a pattern of equivalents.
276 // property_map_t::iterator get_equivalent(Ex::iterator,
277 // property_map_t::iterator=props.begin());
278 
279  private:
280  void insert_prop(const Ex&, const property *);
281  void insert_list_prop(const std::vector<Ex>&, const list_property *);
282 };
283 
284 template<class T>
285 const T* Properties::get(Ex::iterator it, bool ignore_parent_rel) const
286  {
287  return get_composite<T>(it, ignore_parent_rel);
288  }
289 
290 template<class T>
291 const T* Properties::get_composite(Ex::iterator it, bool ignore_parent_rel) const
292  {
293  int tmp;
294  return get_composite<T>(it, tmp, false, ignore_parent_rel);
295  }
296 
297 template<class T>
298 const T* Properties::get_composite(Ex::iterator it, int& serialnum, bool doserial, bool ignore_parent_rel) const
299  {
300  const T* ret=0;
301  bool inherits=false;
302 
303  //std::cerr << *it->name_only() << std::endl;
304 // std::cerr << props.size() << std::endl;
305  std::pair<property_map_t::const_iterator, property_map_t::const_iterator> pit=props.equal_range(it->name_only());
306 
307  // First look for properties of the node itself. Go through the loop twice:
308  // once looking for patterns which do not have wildcards, and then looking
309  // for wildcard patterns.
310  bool wildcards=false;
311  for(;;) {
312  property_map_t::const_iterator walk=pit.first;
313  while(walk!=pit.second) {
314  if(wildcards==(*walk).second.first->children_wildcard()) {
315  // First check property type; a dynamic cast is much faster than a pattern match.
316  ret=dynamic_cast<const T *>((*walk).second.second);
317  if(ret) {
318  if((*walk).second.first->match(*this, it, ignore_parent_rel)) {
319  if(doserial) {
320  std::pair<pattern_map_t::const_iterator, pattern_map_t::const_iterator>
321  pm=pats.equal_range((*walk).second.second);
322  serialnum=0;
323  while(pm.first!=pm.second) {
324  if((*pm.first).second==(*walk).second.first)
325  break;
326  ++serialnum;
327  ++pm.first;
328  }
329  }
330  break;
331  }
332  }
333  ret=0;
334  if(dynamic_cast<const PropertyInherit *>((*walk).second.second))
335  inherits=true;
336  else if(dynamic_cast<const Inherit<T> *>((*walk).second.second))
337  inherits=true;
338  }
339  ++walk;
340  }
341  if(!wildcards && !ret) {
342 // std::cout << "not yet found, switching to wildcards" << std::endl;
343  wildcards=true;
344  }
345  else {
346 // std::cout << "all searches done" << std::endl;
347  break;
348  }
349  }
350 
351  // If no property was found, figure out whether a property is inherited from a child node.
352  if(!ret && inherits) {
353 // std::cout << "no match but perhaps inheritance?" << std::endl;
354  Ex::sibling_iterator sib=it.begin();
355  while(sib!=it.end()) {
356  const T* tmp=get_composite<T>((Ex::iterator)(sib), serialnum, doserial);
357  if(tmp) {
358  ret=tmp;
359  break;
360  }
361  ++sib;
362  }
363  }
364 
365 // std::cout << ret << std::endl;
366  return ret;
367  }
368 
369 template<class T>
370 const T* Properties::get_composite(Ex::iterator it, const std::string& label) const
371  {
372  int tmp;
373  return get_composite<T>(it, tmp, label, false);
374  }
375 
376 template<class T>
377 const T* Properties::get_composite(Ex::iterator it, int& serialnum, const std::string& label, bool doserial) const
378  {
379  const T* ret=0;
380  bool inherits=false;
381  std::pair<property_map_t::const_iterator, property_map_t::const_iterator> pit=props.equal_range(it->name);
382 
383  // First look for properties of the node itself. Go through the loop twice:
384  // once looking for patterns which do not have wildcards, and then looking
385  // for wildcard patterns.
386  bool wildcards=false;
387  for(;;) {
388  property_map_t::const_iterator walk=pit.first;
389  while(walk!=pit.second) {
390  if(wildcards==(*walk).second.first->children_wildcard()) {
391  if((*walk).second.first->match(*this, it)) { // match found
392  ret=dynamic_cast<const T *>((*walk).second.second);
393  if(ret) { // found! determine serial number
394  // std::cerr << "found weight for " << ret->label << " vs " << label << std::endl;
395  if(ret->label!=label && ret->label!="all")
396  ret=0;
397  else {
398  if(doserial)
399  serialnum=serial_number( (*walk).second.second, (*walk).second.first );
400  break;
401  }
402  }
403  if(dynamic_cast<const PropertyInherit *>((*walk).second.second))
404  inherits=true;
405  else if(dynamic_cast<const Inherit<T> *>((*walk).second.second))
406  inherits=true;
407  }
408  }
409  ++walk;
410  }
411  if(!wildcards) wildcards=true;
412  else break;
413  }
414 
415  // If no property was found, figure out whether a property is inherited from a child node.
416  if(!ret && inherits) {
417  Ex::sibling_iterator sib=it.begin();
418  while(sib!=it.end()) {
419  const T* tmp=get_composite<T>((Ex::iterator)(sib), serialnum, label, doserial);
420  if(tmp) {
421  ret=tmp;
422  break;
423  }
424  ++sib;
425  }
426  }
427  return ret;
428  }
429 
430 template<class T>
431 const T* Properties::get_composite(Ex::iterator it1, Ex::iterator it2, bool ignore_parent_rel) const
432  {
433  int tmp1, tmp2;
434  return get_composite<T>(it1,it2,tmp1,tmp2, ignore_parent_rel);
435  }
436 
437 template<class T>
438 const T* Properties::get_composite(Ex::iterator it1, Ex::iterator it2, int& serialnum1, int& serialnum2, bool ignore_parent_rel) const
439  {
440  const T* ret1=0;
441  const T* ret2=0;
442  bool found=false;
443 
444  bool inherits1=false, inherits2=false;
445  std::pair<property_map_t::const_iterator, property_map_t::const_iterator> pit1=props.equal_range(it1->name);
446  std::pair<property_map_t::const_iterator, property_map_t::const_iterator> pit2=props.equal_range(it2->name);
447 
448  property_map_t::const_iterator walk1=pit1.first;
449  while(walk1!=pit1.second) {
450  if((*walk1).second.first->match(*this, it1, ignore_parent_rel)) { // match for object 1 found
451  ret1=dynamic_cast<const T *>((*walk1).second.second);
452  if(ret1) { // property of the right type found for object 1
453  property_map_t::const_iterator walk2=pit2.first;
454  while(walk2!=pit2.second) {
455  if((*walk2).second.first->match(*this, it2, ignore_parent_rel)) { // match for object 1 found
456  ret2=dynamic_cast<const T *>((*walk2).second.second);
457  if(ret2) { // property of the right type found for object 2
458  if(ret1==ret2 && walk1!=walk2) { // accept if properties are the same and patterns are not
459  serialnum1=serial_number( (*walk1).second.second, (*walk1).second.first );
460  serialnum2=serial_number( (*walk2).second.second, (*walk2).second.first );
461  found=true;
462  goto done;
463  }
464  }
465  }
466  if(dynamic_cast<const PropertyInherit *>((*walk2).second.second))
467  inherits2=true;
468  ++walk2;
469  }
470  }
471  if(dynamic_cast<const PropertyInherit *>((*walk1).second.second))
472  inherits1=true;
473  }
474  ++walk1;
475  }
476 
477  // If no property was found, figure out whether a property is inherited from a child node.
478  if(!found && (inherits1 || inherits2)) {
479  Ex::sibling_iterator sib1, sib2;
480  if(inherits1) sib1=it1.begin();
481  else sib1=it1;
482  bool keepgoing1=true;
483  do { // 1
484  bool keepgoing2=true;
485  if(inherits2) sib2=it2.begin();
486  else sib2=it2;
487  do { // 2
488  const T* tmp=get_composite<T>((Ex::iterator)(sib1), (Ex::iterator)(sib2), serialnum1, serialnum2, ignore_parent_rel);
489  if(tmp) {
490  ret1=tmp;
491  found=true;
492  goto done;
493  }
494  if(!inherits2 || ++sib2==it2.end())
495  keepgoing2=false;
496  } while(keepgoing2);
497  if(!inherits1 || ++sib1==it1.end())
498  keepgoing1=false;
499  } while(keepgoing1);
500  }
501 
502  done:
503  if(!found) ret1=0;
504  return ret1;
505  }
506 
507 template<class T>
508 const T* Properties::get() const
509  {
510  const T* ret=0;
511  // FIXME: hack
512  nset_t::iterator nit=name_set.insert(std::string("")).first;
513  std::pair<property_map_t::const_iterator, property_map_t::const_iterator> pit=
514  props.equal_range(nit);
515  while(pit.first!=pit.second) {
516  ret=dynamic_cast<const T *>((*pit.first).second.second);
517  if(ret) break;
518  ++pit.first;
519  }
520  return ret;
521  }
522 
523 template<class T>
524 Ex::iterator Properties::head(Ex::iterator it, bool ignore_parent_rel) const
525  {
526  Ex::iterator dn=it;
527  for(;;) {
528  if(get<PropertyInherit>(dn, ignore_parent_rel)) {
529  dn=dn.begin();
530  }
531  else {
532  assert(get<T>(dn));
533  break;
534  }
535  }
536  return dn;
537  }
538 
539 }
const_iterator find(const std::string &) const
Definition: Props.cc:170
bool hidden_
Definition: Props.hh:156
void register_property(property *(*)(), const std::string &name)
Registering properties.
Definition: Props.cc:165
bool parse_to_keyvals(const Ex &, keyval_t &)
Definition: Props.cc:261
virtual void latex(std::ostream &) const
Display the property on the stream.
Definition: Props.cc:287
match_t
Definition: Props.hh:145
bool has(const property *, Ex::iterator)
Definition: Props.cc:125
Basic storage class for symbolic mathemematical expressions.
Definition: Storage.hh:130
Ex obj
Definition: Props.hh:43
PropertyInherit is like Inherit<T> for all properties.
Definition: Props.hh:188
virtual ~property()
Definition: Props.hh:110
const_iterator begin() const
Definition: Props.cc:192
void insert_prop(const Ex &, const property *)
Definition: Props.cc:331
virtual bool parse(const Kernel &, keyval_t &) override
Definition: Props.cc:302
void erase(iterator)
Definition: Props.cc:207
Definition: Props.hh:145
virtual std::string name() const
Definition: Props.hh:182
virtual std::string unnamed_argument() const
Definition: Props.cc:292
Ex::iterator head(Ex::iterator, bool ignore_parent_rel=false) const
Definition: Props.hh:524
bool hidden(void) const
Definition: Props.cc:223
std::pair< pattern *, const property * > pat_prop_pair_t
Definition: Props.hh:222
void insert_list_prop(const std::vector< Ex > &, const list_property *)
Definition: Props.cc:417
void clear()
Definition: Props.cc:141
virtual void validate(const Kernel &, const Ex &) const
Definition: Props.cc:237
const T * get() const
Definition: Props.hh:508
std::string label
Definition: Props.hh:162
kvlist_t keyvals
Definition: Props.hh:65
Arguments to properties get parsed into a keyval_t structure.
Definition: Props.hh:48
bool parse_one_argument(Ex::iterator arg, keyval_t &keyvals)
Definition: Props.cc:241
pattern_map_t pats
Definition: Props.hh:246
virtual match_t equals(const property *) const
Definition: Props.cc:297
registered_property_map_t registered_properties
Definition: Props.hh:221
bool children_wildcard() const
Definition: Props.cc:117
Definition: Props.hh:159
const_iterator end() const
Definition: Props.cc:197
Base class for all properties, handling argument parsing and defining the interface.
Definition: Props.hh:107
kvlist_t::iterator iterator
Definition: Props.hh:54
int serial_number(const property *, const pattern *) const
Definition: Props.cc:512
Definition: Props.hh:145
std::multimap< const property *, pattern * > pattern_map_t
Definition: Props.hh:232
virtual std::string name() const
Definition: Props.hh:190
std::multimap< nset_t::iterator, pat_prop_pair_t, nset_it_less > property_map_t
We keep two multi-maps: one from the pattern to the property (roughly) and one from the property to t...
Definition: Props.hh:231
~registered_property_map_t()
Definition: Props.cc:160
void push_back(const kvpair_t &)
Definition: Props.cc:202
property_map_t props
The following two maps own the pointers to the properties and patterns stored in them; use clear() to...
Definition: Props.hh:245
const T * get_composite(Ex::iterator, bool ignore_parent_rel=false) const
Definition: Props.hh:291
pattern()
Definition: Props.cc:32
std::map< std::string, property *(*)()> internal_property_map_t
Definition: Props.hh:210
Something cannot be both a list property and a normal property at the same time, so we can safely inh...
Definition: Props.hh:168
nset_t name_set
Definition: Storage.cc:31
virtual std::string name() const =0
internal_property_map_t store
Definition: Props.hh:213
kvpair_t value_type
Definition: Props.hh:55
If a property X derives from Inherit<Y>, and get<Y> is called on an object which has an X property (b...
Definition: Props.hh:179
kvlist_t::const_iterator const_iterator
Definition: Props.hh:53
Definition: Kernel.hh:15
virtual ~Inherit()
Definition: Props.hh:181
Definition: Props.hh:35
std::list< kvpair_t > kvlist_t
Definition: Props.hh:51
std::string master_insert(Ex proptree, property *thepropbase)
Register a property for the indicated Ex.
Definition: Props.cc:562
internal_property_map_t::iterator iterator
Definition: Props.hh:211
Class holding a collection of properties attached to expressions.
Definition: Props.hh:203
property(bool hidden=false)
Definition: Props.cc:213
virtual bool parse(const Kernel &, keyval_t &keyvals)
Definition: Props.cc:228
ProgressMonitor * pm
Definition: PythonCdb.cc:965
bool match(const Properties &, const Ex::iterator &, bool ignore_parent_rel=false) const
Definition: Props.cc:41
std::pair< std::string, Ex::iterator > kvpair_t
Definition: Props.hh:50
Definition: Props.hh:145