KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Semantic_elaboration.h
Go to the documentation of this file.
1 #ifndef SEMANTIC_OBJECT_H_
2 #define SEMANTIC_OBJECT_H_
3 
11 #include "semantics/Semantic_db.h"
12 #include "semantics/SemanticIO.h"
13 #include "l_cpp/l_exception.h"
14 #include <map>
15 #include <deque>
16 #include <algorithm>
17 #include <boost/tuple/tuple.hpp>
18 #include <boost/shared_ptr.hpp>
19 #include <boost/make_shared.hpp>
20 #include <boost/array.hpp>
21 #include <boost/functional/hash.hpp>
22 #include <boost/algorithm/string/join.hpp>
23 
24 namespace semantics
25 {
26 
27 /*------------------------------------------------------------
28  * FORWARD DECLARATIONS
29  *------------------------------------------------------------*/
30 
34 class Semantic_data_base;
35 
36 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
37 
45 class Semantic_elaboration;
46 
47 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
48 
52 template<class T> class Semantic_traits;
53 
54 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
55 
62 template<class T, class Traits = Semantic_traits<T> > class Semantic_data;
63 
64 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
65 
69 template<class T, class Traits = Semantic_traits<T> >
71 
72 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
73 
77 template<class T, class Traits = Semantic_traits<T> >
79 
80 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
81 
82 template<class T, class Traits>
83 std::ostream& operator<<(std::ostream&, Semantic_data<T, Traits>);
84 
85 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
86 
87 class Null_primitive;
88 
90 
91 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
92 
95 const boost::shared_ptr<Null_semantic_terminal>&
97 
98 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
99 
100 /*------------------------------------------------------------
101  * CLASS DEFINITIONS
102  *------------------------------------------------------------*/
103 
105 {
106 public:
107  /*------------------------------------------------------------
108  * TYPEDEFS
109  *------------------------------------------------------------*/
110 
113  typedef boost::shared_ptr<Semantic_data_base> Base_ptr;
114  typedef boost::tuple<size_t, size_t> Hash_pair;
115  typedef std::map<size_t, std::string> Map;
116  typedef std::pair<size_t, std::string> Map_entry;
117 
118  friend class Semantic_elaboration;
119 public:
120  /*------------------------------------------------------------
121  * CONSTRUCTORS/DESTRUCTOR
122  *------------------------------------------------------------*/
123 
125 
126  /*------------------------------------------------------------
127  * ACCESSORS
128  *------------------------------------------------------------*/
129 
130  virtual const Val_type& head() const = 0;
131  virtual const size_t& head_code() const = 0;
132  virtual const size_t& args_code() const = 0;
133  virtual const Hash_pair& hash_pair() const = 0;
134 
135  /*------------------------------------------------------------
136  * STATIC ACCESSORS
137  *------------------------------------------------------------*/
138 
140  {
141  static boost::shared_ptr<Map> ghm(new Map());
142  return *ghm;
143  }
144 
145  static Map& global_arg_map()
146  {
147  static boost::shared_ptr<Map> gam(new Map());
148  return *gam;
149  }
150 };
151 
152 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
153 
154 template<class T, class Traits>
155 class Semantic_data : public Semantic_data_base
156 {
157 public:
158  /*------------------------------------------------------------
159  * TYPEDEFS
160  *------------------------------------------------------------*/
161 
163  typedef boost::shared_ptr<Semantic_data<T, Traits> > Self_ptr;
164  typedef boost::array<Val_type, Traits::n_args> Arg_list;
165  typedef boost::array<Key_type, Traits::n_args> Arg_list_s;
166  typedef boost::array<Token_map*, Traits::n_args> Arg_map_list;
167 
168  friend class Nonterminal_elaboration<T, Traits>;
169 public:
170  /*------------------------------------------------------------
171  * CONSTRUCTORS/DESTRUCTOR
172  *------------------------------------------------------------*/
173 
176  Semantic_data(const Val_type& head, const Arg_list& args);
177 
180  Semantic_data(const Key_type& head, const Arg_list_s& args);
181 
184  Semantic_data(const Val_type& head);
185 
188  Semantic_data(const Key_type& head);
189 
190  /*------------------------------------------------------------
191  * ACCESSORS
192  *------------------------------------------------------------*/
193 
196  const Arg_list& args() const {return args_;}
197 
200  const Val_type& head() const {return head_;}
201 
204  const Key_type& head_s() const {return head_map().decode(head_);}
205 
208  const size_t& head_code() const {return hash_pair_.get<0>();}
209 
212  const size_t& args_code() const {return hash_pair_.get<1>();}
213 
216  const Hash_pair& hash_pair() const {return hash_pair_;}
217 
218  /*------------------------------------------------------------
219  * FRIEND FUNCTIONS
220  *------------------------------------------------------------*/
221 
224  friend std::ostream& operator<< <T,Traits>(std::ostream& os, Self_type d);
225 public:
226 
227  /*------------------------------------------------------------
228  * STATIC ACCESSORS
229  *------------------------------------------------------------*/
230 
232  {
233  static boost::shared_ptr<Semantic_db> hm(new Semantic_db());
234  return *hm;
235  }
237  {
238  static boost::shared_ptr<Arg_map_list> aml(new Arg_map_list);
239  return *aml;
240  }
241 
242 private:
243  const Val_type head_;
244  const Arg_list args_;
245  Hash_pair hash_pair_;
246 
247  void initialize_codes();
248 };
249 
250 
251 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
252 
258 {
259 public:
260  /*------------------------------------------------------------
261  * TYPEDEFS
262  *------------------------------------------------------------*/
263 
267  typedef boost::shared_ptr<Semantic_elaboration> Self_ptr;
268  typedef boost::shared_ptr<const Semantic_elaboration> Self_ptr_const;
269  typedef std::deque<Self_ptr_const> Self_ptrs_read;
271  typedef boost::tuple<Self_ptr_const, Semantic_data_base::Hash_pair> Step_result;
272  typedef int Referent_code;
273  typedef std::vector<Referent_code> Referent_list;
275 public:
276  /*------------------------------------------------------------
277  * CONSTRUCTORS/DESTRUCTOR
278  *------------------------------------------------------------*/
279 
281 
282  /*------------------------------------------------------------
283  * ACCESSORS
284  *------------------------------------------------------------*/
285 
288  virtual const Val_type& head() const = 0;
289 
292  virtual const Key_type& head_s() const = 0;
293 
296  virtual const Val_type& arg(const size_t& pos) const = 0;
297 
300  virtual const Key_type& arg_s(const size_t& pos) const = 0;
301 
304  virtual const size_t& head_code() const = 0;
305 
308  virtual const size_t& args_code() const = 0;
309 
312  virtual size_t type_code() const = 0;
313 
316  virtual const Data_base_ptr data() const = 0;
317 
320  virtual const Step_code::Weights& step_filter() const = 0;
321 
324  virtual Self_ptr child(const size_t&) const = 0;
325 
328  virtual Self_ptrs_read children() const = 0;
329 
332  virtual const Referent_code& head_referent() const {return NULL_REFERENT;}
333 
336  virtual const Referent_code& arg_referent(const size_t&) const
337  {
338  return NULL_REFERENT;
339  }
340 
343  virtual Step_result take_a_step(const Step_code_t& step_code) const = 0;
344 
345  /*------------------------------------------------------------
346  * MANIPULATORS
347  *------------------------------------------------------------*/
348 
351  virtual void add_child(const size_t&, const Self_ptr) = 0;
352 
355  virtual void insert_child(const size_t&, const Self_ptr, const size_t&) = 0;
356 
357  /*------------------------------------------------------------
358  * QUERIES
359  *------------------------------------------------------------*/
360 
363  virtual bool is_terminal() const = 0;
364 
365  /*------------------------------------------------------------
366  * DISPLAY FUNCTIONS
367  *------------------------------------------------------------*/
368 
371  virtual void print(std::ostream& os) const = 0;
372 
375  virtual void print_subtree(std::ostream&, int indent_level = 0) const = 0;
376 
377  /*------------------------------------------------------------
378  * FRIEND FUNCTIONS
379  *------------------------------------------------------------*/
380 
381  friend std::ostream& operator<<(std::ostream& os, Self_ptr);
382 };
383 
384 
385 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
386 
387 template<class T, class Traits>
388 class Terminal_elaboration : public Semantic_elaboration
389 {
390 public:
391  /*------------------------------------------------------------
392  * TYPEDEFS
393  *------------------------------------------------------------*/
394 
396  typedef Traits Traits_t;
397  typedef boost::shared_ptr<Semantic_data<T> > Data_ptr;
398 public:
399  /*------------------------------------------------------------
400  * CONSTRUCTORS/DESTRUCTOR
401  *------------------------------------------------------------*/
402 
406 
410 
414 
415  /*------------------------------------------------------------
416  * ACCESSORS
417  *------------------------------------------------------------*/
418 
421  const Val_type& head() const {return data_ -> head();};
422 
425  const Key_type& head_s() const {return data_ -> head_s();};
426 
429  const Val_type& arg(const size_t&) const
430  {
431  KJB_THROW_2(
433  "Attempted to access the argument of a Terminal_elaboration"
434  );
435  };
436 
439  const Key_type& arg_s(const size_t&) const
440  {
441  KJB_THROW_2(
443  "Attempted to access the argument of a Terminal_elaboration"
444  );
445  };
446 
449  const size_t& head_code() const {return data_ -> head_code();}
450 
453  const size_t& args_code() const {return data_ -> args_code();}
454 
457  size_t type_code() const {return Traits::type_code;}
458 
461  const Data_base_ptr data() const {return data_;}
462 
466  {
467  return Traits::step_filter();
468  }
469 
472  Base_ptr child(const size_t&) const {return Base_ptr();}
473 
477  {
478  return Self_ptrs_read();
479  }
480 
483  Step_result take_a_step(const Step_code_t& step_code) const;
484 
485  /*------------------------------------------------------------
486  * MANIPULATORS
487  *------------------------------------------------------------*/
488 
491  void add_child(const size_t&, const Self_ptr) {}
492 
495  void insert_child(const size_t&, const Self_ptr, const size_t&) {}
496 
497  /*------------------------------------------------------------
498  * QUERIES
499  *------------------------------------------------------------*/
500 
503  bool is_terminal() const {return true;}
504 
505  /*------------------------------------------------------------
506  * DISPLAY FUNCTIONS
507  *------------------------------------------------------------*/
508 
511  void print(std::ostream& os) const {os << *data_;}
512 
515  void print_subtree(std::ostream& os, int indent_level) const;
516 private:
517  Data_ptr data_;
518 };
519 
520 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
521 
522 template<class T, class Traits>
523 class Nonterminal_elaboration : public Semantic_elaboration
524 {
525 public:
526  /*------------------------------------------------------------
527  * TYPEDEFS
528  *------------------------------------------------------------*/
529 
532  typedef Traits Traits_t;
535  typedef boost::shared_ptr<Semantic_data<T> > Data_ptr;
536  typedef typename Data_type::Arg_list Arg_list;
538  typedef boost::array<Base_ptr, Traits::n_args> Child_array;
539 public:
540  /*------------------------------------------------------------
541  * CONSTRUCTORS/DESTRUCTOR
542  *------------------------------------------------------------*/
543 
551  (
552  const Val_type& head,
553  const Arg_list& args,
554  const Referent_list& referent_list =
555  Referent_list(Traits::n_args + 1, NULL_REFERENT)
556  );
557 
565  (
566  const Key_type& head,
567  const Arg_list_s& args,
568  const Referent_list& referent_list =
569  Referent_list(Traits::n_args + 1, NULL_REFERENT)
570  );
571 
572  /*------------------------------------------------------------
573  * ACCESSORS
574  *------------------------------------------------------------*/
575 
578  const Val_type& head() const {return data_ -> head();};
579 
582  const Key_type& head_s() const
583  {
584  return data_->head_s();
585  };
586 
589  const Val_type& arg(const size_t& pos) const
590  {
591  IFTD(pos < Traits::n_args, kjb::Index_out_of_bounds,
592  "Attempted to access argument %d in a Nonterminal_elaboration"
593  " with only %d arguments", (pos)(Traits::n_args));
594  return args()[pos];
595  };
596 
599  const Key_type& arg_s(const size_t& pos) const
600  {
601  IFTD(pos < Traits::n_args, kjb::Index_out_of_bounds,
602  "Attempted to access argument %d in a Nonterminal_elaboration"
603  " with only %d arguments", (pos)(Traits::n_args));
604  return Data_type::arg_map_list()[pos]->decode(arg(pos));
605  };
606 
609  const size_t& head_code() const {return data_ -> head_code();}
610 
613  const size_t& args_code() const {return data_ -> args_code();}
614 
617  size_t type_code() const {return Traits::type_code;}
618 
621  const Data_base_ptr data() const {return data_;}
622 
625  const Arg_list& args() const {return data_ -> args();}
626 
629  Base_ptr child(const size_t& index) const;
630 
634  {
635  return Self_ptrs_read(children_.begin(), children_.end());
636  }
637 
638  const Referent_code& head_referent() const {return referent_list_[0];}
639 
640  const Referent_code& arg_referent(const size_t& pos) const
641  {
642  IFTD(pos < Traits::n_args, kjb::Illegal_argument,
643  "Attempted to access referent corresponding to nonexistent "
644  "argument %d in a Nonterminal_elaboration with only "
645  "%d arguments", (pos)(Traits::n_args));
646  return referent_list_[pos + 1];
647  }
648 
652  {
653  return Traits::step_filter();
654  }
655 
662  Step_result take_a_step(const Step_code_t& step_code) const;
663 
664  /*------------------------------------------------------------
665  * MANIPULATORS
666  *------------------------------------------------------------*/
667 
671  void set_data_args(const Arg_list& args);
672 
675  void add_child(const size_t& pos, const Base_ptr child);
676 
679  void insert_child(
680  const size_t& pos,
681  const Base_ptr new_child,
682  const size_t& pos_for_old
683  );
684 
685  /*------------------------------------------------------------
686  * QUERIES
687  *------------------------------------------------------------*/
688 
692  bool is_terminal() const {return false;}
693 
694  /*------------------------------------------------------------
695  * DISPLAY FUNCTIONS
696  *------------------------------------------------------------*/
697 
700  void print(std::ostream& os) const
701  {
702  os << *data_;
703  if(head_referent() != NULL_REFERENT) os << " " << head_referent();
704  os << "(";
705  for(Referent_list::const_iterator it = ++(referent_list_.begin());
706  it != referent_list_.end(); ++it)
707  {
708  if(*it != NULL_REFERENT)
709  {
710  os << *it << ",";
711  }
712  }
713  os << ")";
714  os << std::endl;
715  }
716 
719  void print_subtree(std::ostream& os, int indent_level = 0) const;
720 
721 private:
722  Data_ptr data_;
723  Base_ptr head_child_;
724  Child_array children_;
725  Referent_list referent_list_;
726 };
727 
728 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
729 
730 /*------------------------------------------------------------
731  * MEMBER FUNCTION IMPLEMENTATION (Semantic_data)
732  *------------------------------------------------------------*/
733 
734 template<class T, class Traits>
736  const Val_type& head,
737  const Arg_list& args
738  ) : head_(head),
739  args_(args),
740  hash_pair_(boost::make_tuple(0UL, 0UL))
741 {
742  initialize_codes();
743  // Map hash codes to readable strings
744  std::string hstring = head_map().decode(head);
745  Arg_list_s args_s = multi_map_decode<Arg_list_s>(arg_map_list(), args);
746  global_head_map().insert(Map_entry(hash_pair_.get<0>(), hstring));
747  std::string argstring =
748  "(" + boost::algorithm::join(args_s, ",") + ")";
749  global_arg_map().insert(Map_entry(hash_pair_.get<1>(), argstring));
750 }
751 
752 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
753 
754 template<class T, class Traits>
756  const Key_type& head,
757  const Arg_list_s& args
758  ) : Semantic_data_base(),
759  head_(head_map().encode(head)),
760  args_(multi_map_encode<Arg_list>(args, arg_map_list())),
761  hash_pair_(boost::make_tuple(0UL, 0UL))
762 {
763  initialize_codes();
764  global_head_map().insert(Map_entry(hash_pair_.get<0>(), head));
765  std::string argstring =
766  "(" + boost::algorithm::join(args, ",") + ")";
767  global_arg_map().insert(Map_entry(hash_pair_.get<1>(), argstring));
768 }
769 
770 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
771 
772 template<class T, class Traits>
774 : head_(head), args_(), hash_pair_(boost::make_tuple(0UL, 0UL))
775 {
776  initialize_codes();
777  std::string hstring = head_map().decode(head);
778  global_head_map().insert(Map_entry(hash_pair_.get<0>(), hstring));
779  global_arg_map().insert(Map_entry(hash_pair_.get<1>(), ""));
780 }
781 
782 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
783 
784 template<class T, class Traits>
786 : head_(head_map().encode(head)), args_(), hash_pair_(boost::make_tuple(0UL, 0UL))
787 {
788  initialize_codes();
789  global_head_map().insert(Map_entry(hash_pair_.get<0>(), head));
790  global_arg_map().insert(Map_entry(hash_pair_.get<1>(), ""));
791 }
792 
793 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
794 
795 template<class T, class Traits>
797 {
798  boost::hash_combine<int>(hash_pair_.get<0>(), head_);
799  boost::hash_combine<int>(hash_pair_.get<0>(), Traits::type_code);
800  boost::hash_range(hash_pair_.get<1>(), args_.begin(), args_.end());
801  boost::hash_combine<int>(hash_pair_.get<1>(), Traits::type_code);
802 }
803 
804 /*------------------------------------------------------------
805  * MEMBER FUNCTION IMPLEMENTATION (Terminal_elaboration)
806  *------------------------------------------------------------*/
807 
808 template<class T, class Traits>
810  : Semantic_elaboration(), data_(boost::make_shared<Semantic_data<T> >(0))
811 {
812 }
813 
814 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
815 
816 template<class T, class Traits>
819  data_(boost::make_shared<Semantic_data<T> >(head))
820 {
821 }
822 
823 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
824 
825 template<class T, class Traits>
828  data_(boost::make_shared<Semantic_data<T> >(head))
829 {
830 }
831 
832 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
833 
834 template<class T, class Traits>
837 {
838  switch(step_code)
839  {
841  case Step_code::IDENTITY:
842  case Step_code::HEAD:
843  case Step_code::LEFT_ARG:
845  case Step_code::ATT0:
846  case Step_code::ATT1:
848  default:
849  return boost::make_tuple(
851  boost::make_tuple(0UL, 0UL)
852  );
853  }
854 }
855 
856 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
857 
858 template<class T, class Traits>
860  std::ostream& os,
861  int indent_level
862  ) const
863 {
864  std::string lead(4*indent_level, ' ');
865  os << lead << *data_ << std::endl;
866 };
867 
868 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
869 
870 /*------------------------------------------------------------
871  * MEMBER FUNCTION IMPLEMENTATION (Nonterminal_elaboration)
872  *------------------------------------------------------------*/
873 
874 template<class T, class Traits>
876  const Val_type& head,
877  const Arg_list& args,
878  const Referent_list& referent_list
879  ) : Semantic_elaboration(),
880  data_(boost::make_shared<Semantic_data<T> >(head, args)),
881  head_child_(
882  boost::make_shared<
883  Terminal_elaboration<typename Traits::Head_type>
884  >(head)
885  ),
886  children_(),
887  referent_list_(referent_list)
888 {
889  for(typename Child_array::iterator it = children_.begin();
890  it != children_.end(); ++it)
891  {
892  *it = Base_ptr();
893  }
894 }
895 
896 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
897 
898 template<class T, class Traits>
900  const Key_type& head,
901  const Arg_list_s& args,
902  const Referent_list& referent_list
903  ) : Semantic_elaboration(),
904  data_(boost::make_shared<Semantic_data<T> >(head, args)),
905  head_child_(
906  boost::make_shared<
907  Terminal_elaboration<typename Traits::Head_type>
908  >(Semantic_data<T>::head_map().encode(head))
909  ),
910  children_(),
911  referent_list_(referent_list)
912 {
913  for(typename Child_array::iterator it = children_.begin();
914  it != children_.end(); ++it)
915  {
916  *it = Base_ptr();
917  }
918 }
919 
920 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
921 
922 template<class T, class Traits>
924 {
925  Val_type h = data_ -> head();
926  data_ = boost::make_shared<Semantic_data<T> >(h, args);
927 }
928 
929 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
930 
931 template<class T, class Traits>
934 {
935  IFTD(index < Traits::n_args, kjb::Illegal_argument,
936  "Attempted to access child in nonexistent slot %d in"
937  " Nonterminal_elaboration with only %d slots",
938  (index)(Traits::n_args));
939  return children_[index];
940 }
941 
942 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
943 
944 template<class T, class Traits>
946  const size_t& pos,
947  const Base_ptr child
948  )
949 {
950  IFTD(pos < Traits::n_args, kjb::Illegal_argument,
951  "Attempted to add child to nonexistent slot %d in Nonterminal_elaboration"
952  "which has only %d slots", (pos)(Traits::n_args));
953  children_[pos] = child;
954 }
955 
956 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
957 
958 template<class T, class Traits>
960  const size_t& pos,
961  const Base_ptr new_child,
962  const size_t& pos_for_old
963  )
964 {
965  IFTD(pos < Traits::n_args, kjb::Illegal_argument,
966  "Attempted to insert child to nonexistent slot %d in Nonterminal_elaboration"
967  "which has only %d slots", (pos)(Traits::n_args));
968  Base_ptr old_child = children_[pos];
969  add_child(pos, new_child);
970  children_[pos] -> add_child(pos_for_old, old_child);
971 }
972 
973 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
974 
975 template<class T, class Traits>
978  const Step_code_t& step_code
979  ) const
980 {
981  Base_ptr p0 = Traits::n_args > 0 ? children_[0] : Base_ptr();
982  Base_ptr p1 = Traits::n_args > 1 ? children_[1] : Base_ptr();
983  switch(step_code)
984  {
985  case Step_code::HEAD:
986  return head_child_ == NULL ?
987  Step_result(
989  boost::make_tuple(0UL, 0UL)) :
990  Step_result(
991  head_child_,
992  boost::make_tuple(
993  head_child_ -> head_code(),
994  head_child_ -> args_code()
995  )
996  );
997  case Step_code::LEFT_ARG:
998  case Step_code::ATT0:
999  return p0 == NULL ?
1000  Step_result(
1002  boost::make_tuple(0UL, 0UL)) :
1003  Step_result(
1004  p0,
1005  boost::make_tuple(p0 -> head_code(), p0 -> args_code())
1006  );
1007  case Step_code::RIGHT_ARG:
1008  case Step_code::ATT1:
1009  return p1 == NULL ?
1010  Step_result(
1012  boost::make_tuple(0UL, 0UL)) :
1013  Step_result(
1014  p1,
1015  boost::make_tuple(p1 -> head_code(), p1 -> args_code())
1016  );
1017  case Step_code::IDENTITY:
1018  case Step_code::NULL_STEP:
1019  case Step_code::NUM_STEPS:
1020  default:
1021  return Step_result(
1023  boost::make_tuple(0UL, 0UL)
1024  );
1025  }
1026 }
1027 
1028 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1029 
1030 template<class T, class Traits>
1032  std::ostream& os,
1033  int indent_level
1034  ) const
1035 {
1036  std::string lead(4*indent_level, ' ');
1037  os << lead;
1038  print(os);
1039  head_child_ -> print_subtree(os, indent_level + 1);
1040  for(typename Child_array::const_iterator it = children_.begin();
1041  it != children_.end(); it++)
1042  {
1043  if(*it != NULL) (*it) -> print_subtree(os, indent_level + 1);
1044  }
1045 }
1046 
1047 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1048 
1049 /*------------------------------------------------------------
1050  * FREE FUNCTIONS
1051  *------------------------------------------------------------*/
1052 
1054 
1055 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1056 
1057 std::ostream& operator<<(std::ostream& os, Semantic_elaboration::Self_ptr elab);
1058 
1059 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1060 
1061 std::ostream& operator<<(std::ostream& os, Semantic_data_base::Hash_pair);
1062 
1063 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1064 
1065 template<class T, class Traits>
1066 std::ostream& operator<<(
1067  std::ostream& os,
1069  )
1070 {
1071  typedef Semantic_data<T, Traits> D;
1072  typename D::Arg_list_s keys = multi_map_decode<typename D::Arg_list_s>(
1073  D::arg_map_list(), d.args_);
1074  os << D::head_map().decode(d.head_);
1075  if(Traits::n_args != 0)
1076  {
1077  os << "(";
1078  for(typename D::Arg_list_s::const_iterator it = keys.begin();
1079  it != keys.end(); it++)
1080  {
1081  os << *it << ",";
1082  }
1083  os << ")";
1084  }
1085  // os << " -- " << d.hash_pair_;
1086  return os;
1087 }
1088 
1089 
1090 };
1091 
1092 #endif
T multi_map_encode(M maps, U keys)
Definition: Token_map.h:74
boost::array< Key_type, Traits::n_args > Arg_list_s
Definition: Semantic_elaboration.h:165
const Val_type & arg(const size_t &pos) const
return underlying hash code for head value
Definition: Semantic_elaboration.h:589
virtual const Hash_pair & hash_pair() const =0
Semantic_step_proposal::Step_result Step_result
Definition: Semantic_step_proposal.cpp:40
Semantic_data< T, Traits > Self_type
Definition: Semantic_elaboration.h:162
const Key_type & head_s() const
return code for head value
Definition: Semantic_elaboration.h:582
boost::shared_ptr< Semantic_data< T > > Data_ptr
Definition: Semantic_elaboration.h:397
boost::tuple< size_t, size_t > Hash_pair
Definition: Semantic_elaboration.h:114
const size_t & args_code() const
return underlying hash code for args
Definition: Semantic_elaboration.h:453
Self_ptrs_read children() const
return pointer to child in a particular position
Definition: Semantic_elaboration.h:476
virtual Self_ptr child(const size_t &) const =0
return pointer to child in a particular position
Step_result take_a_step(const Step_code_t &step_code) const
takes a step down the semantic tree, returning data
Definition: Semantic_elaboration.h:977
virtual const size_t & args_code() const =0
return underlying hash code for args
const Step_code::Weights & step_filter() const
return binary filter array indicating allowable steps
Definition: Semantic_elaboration.h:651
const Key_type & head_s() const
return code for head value
Definition: Semantic_elaboration.h:425
const Key_type & arg_s(const size_t &pos) const
return code for head value
Definition: Semantic_elaboration.h:599
Definition: SemanticIO.h:62
Terminal_elaboration()
construct with null head value
Definition: Semantic_elaboration.h:809
boost::shared_ptr< const Semantic_elaboration > Self_ptr_const
Definition: Semantic_elaboration.h:268
Object thrown when an index argument exceeds the size of a container.
Definition: l_exception.h:399
Traits Traits_t
Definition: Semantic_elaboration.h:532
Definition: SemanticIO.h:60
static Semantic_db & head_map()
Definition: Semantic_elaboration.h:231
const Data_base_ptr data() const
return pointer to underlying data node
Definition: Semantic_elaboration.h:461
const Arg_list & args() const
return args (pass-through to data_)
Definition: Semantic_elaboration.h:625
virtual const size_t & head_code() const =0
virtual const Referent_code & arg_referent(const size_t &) const
get the integral code for the arg in position pos (if it exists)
Definition: Semantic_elaboration.h:336
std::ostream & operator<<(std::ostream &os, const Categorical_event_base &e)
display key in human readable form
Definition: Categorical_event.cpp:13
Definition: SemanticIO.h:63
virtual const Data_base_ptr data() const =0
return pointer to underlying data node
const Key_type & arg_s(const size_t &) const
return underlying hash code for head value
Definition: Semantic_elaboration.h:439
const Arg_list & args() const
return the actual arg tuple
Definition: Semantic_elaboration.h:196
Semantic_data(const Val_type &head, const Arg_list &args)
standard constructor
Definition: Semantic_elaboration.h:735
const Referent_code & arg_referent(const size_t &pos) const
get the integral code for the arg in position pos (if it exists)
Definition: Semantic_elaboration.h:640
virtual size_t type_code() const =0
return the code corresponding to the type of elaboration
virtual const size_t & args_code() const =0
Definition: Semantic_elaboration.h:78
virtual void print_subtree(std::ostream &, int indent_level=0) const =0
recursively print elaboration tree contents
Structure for returning results of a single sampling move.
Definition: sample_base.h:55
boost::shared_ptr< Semantic_elaboration > Self_ptr
Definition: Semantic_elaboration.h:267
static Map & global_arg_map()
Definition: Semantic_elaboration.h:145
Semantic_data_base::Hash_pair Hash_pair
Definition: Semantic_elaboration.h:266
Semantic_data_base::Val_type Val_type
Definition: Semantic_elaboration.h:265
const size_t & head_code() const
return hash code for head
Definition: Semantic_elaboration.h:208
std::vector< Referent_code > Referent_list
Definition: Semantic_elaboration.h:273
void add_child(const size_t &, const Self_ptr)
put a child in a particular position
Definition: Semantic_elaboration.h:491
Data_type::Arg_list_s Arg_list_s
Definition: Semantic_elaboration.h:537
void print_subtree(std::ostream &os, int indent_level) const
recursively print elaboration tree contents
Definition: Semantic_elaboration.h:859
void add_child(const size_t &pos, const Base_ptr child)
add a new elaboration of argument "index", discarding any existing
Definition: Semantic_elaboration.h:945
std::string Key_type
Definition: Token_map.h:23
virtual Step_result take_a_step(const Step_code_t &step_code) const =0
step down the tree according to step_code
std::map< size_t, std::string > Map
Definition: Semantic_elaboration.h:115
size_t type_code() const
return type code
Definition: Semantic_elaboration.h:617
Semantic_db::Key_type Key_type
Definition: Semantic_elaboration.h:112
void insert_child(const size_t &pos, const Base_ptr new_child, const size_t &pos_for_old)
add child and have it adopt existing one
Definition: Semantic_elaboration.h:959
static Arg_map_list & arg_map_list()
Definition: Semantic_elaboration.h:236
virtual const Val_type & head() const =0
void print_subtree(std::ostream &os, int indent_level=0) const
recursively print the full elaboration tree
Definition: Semantic_elaboration.h:1031
Traits class storing information specific to given elaboration types.
Definition: Semantic_elaboration.h:52
Semantic_elaboration::Self_ptr Base_ptr
Definition: Semantic_elaboration.h:395
boost::shared_ptr< Semantic_data< T > > Data_ptr
Definition: Semantic_elaboration.h:535
const size_t & head_code() const
return underlying hash code for head value
Definition: Semantic_elaboration.h:449
Semantic_data_base::Base_ptr Data_base_ptr
Definition: Semantic_elaboration.h:270
virtual void print(std::ostream &os) const =0
print contents at this node
Self_ptrs_read children() const
access the container of children by const reference
Definition: Semantic_elaboration.h:633
Base_ptr child(const size_t &index) const
get pointer to child elaboration in position "index"
Definition: Semantic_elaboration.h:933
const Data_base_ptr data() const
return pointer to data node
Definition: Semantic_elaboration.h:621
virtual bool is_terminal() const =0
is this a terminal node?
Definition: SemanticIO.h:57
void initialize_global_semantic_maps()
Definition: Semantic_elaboration.cpp:13
const Referent_code & head_referent() const
get the integral code for the referent (if it exists)
Definition: Semantic_elaboration.h:638
Class representing semantic nodes at the leaves of trees.
Definition: Semantic_elaboration.h:70
virtual const size_t & head_code() const =0
return underlying hash code for head value
Semantic_data_base::Key_type Key_type
Definition: Semantic_elaboration.h:264
std::pair< size_t, std::string > Map_entry
Definition: Semantic_elaboration.h:116
const Val_type & head() const
return code for head value
Definition: Semantic_elaboration.h:421
const Hash_pair & hash_pair() const
return hash pair for head and args
Definition: Semantic_elaboration.h:216
Definition: SemanticIO.h:56
#define encode(triedge)
Definition: triangle.c:819
size_t Val_type
Definition: Token_map.h:24
Nonterminal_elaboration(const Val_type &head, const Arg_list &args, const Referent_list &referent_list=Referent_list(Traits::n_args+1, NULL_REFERENT))
construct an elaboration from numeric codes for head and args
Definition: Semantic_elaboration.h:875
virtual void add_child(const size_t &, const Self_ptr)=0
put a child in a particular position
Step_result take_a_step(const Step_code_t &step_code) const
take a step down the semantic tree of type step_code
Definition: Semantic_elaboration.h:836
void insert_child(const size_t &, const Self_ptr, const size_t &)
add child and have existing child "adopt" the old one
Definition: Semantic_elaboration.h:495
void set_data_args(const Arg_list &args)
set new argument values for underyling data
Definition: Semantic_elaboration.h:923
Abstract base class of Semantic Data template.
Definition: Semantic_elaboration.h:104
virtual void insert_child(const size_t &, const Self_ptr, const size_t &)=0
add child and have existing child adopt the old one
virtual ~Semantic_data_base()
Definition: Semantic_elaboration.h:124
bool is_terminal() const
is this a terminal node?
Definition: Semantic_elaboration.h:503
Semantic_elaboration::Self_ptr_const Base_ptr_const
Definition: Semantic_elaboration.h:534
virtual const Val_type & head() const =0
return underlying hash code for head value
boost::tuple< Self_ptr_const, Semantic_data_base::Hash_pair > Step_result
Definition: Semantic_elaboration.h:271
virtual const Key_type & head_s() const =0
return underlying hash code for head value
virtual const Key_type & arg_s(const size_t &pos) const =0
return underlying hash code for head value
Definition: SemanticIO.h:61
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
virtual const Val_type & arg(const size_t &pos) const =0
return underlying hash code for head value
virtual ~Semantic_elaboration()
Definition: Semantic_elaboration.h:280
Definition: SemanticIO.h:58
boost::array< Base_ptr, Traits::n_args > Child_array
Definition: Semantic_elaboration.h:538
bool is_terminal() const
query whether this elaboration is a terminal or not
Definition: Semantic_elaboration.h:692
int Referent_code
Definition: Semantic_elaboration.h:272
virtual const Step_code::Weights & step_filter() const =0
return a binary filter indicating what steps are allowed
Forward declarations.
Definition: prob_weight_array.h:28
const size_t & head_code() const
return underlying hash code for head value
Definition: Semantic_elaboration.h:609
void print(std::ostream &os) const
print contents at this node to ostream os
Definition: Semantic_elaboration.h:700
virtual Self_ptrs_read children() const =0
return pointer to child in a particular position
Base class for semantic elaborations.
Definition: Semantic_elaboration.h:257
Object thrown when the program does something thought impossible.
Definition: l_exception.h:358
boost::array< Token_map *, Traits::n_args > Arg_map_list
Definition: Semantic_elaboration.h:166
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
Terminal_elaboration< Null_primitive > Null_semantic_terminal
Definition: Semantic_elaboration.h:87
void print(std::ostream &os) const
print contents at this node
Definition: Semantic_elaboration.h:511
Semantic_data< T > Data_type
Definition: Semantic_elaboration.h:531
Definition: SemanticIO.h:59
Traits Traits_t
Definition: Semantic_elaboration.h:396
boost::shared_ptr< Semantic_data< T, Traits > > Self_ptr
Definition: Semantic_elaboration.h:163
virtual const Referent_code & head_referent() const
get the integral code for the referent (if it exists)
Definition: Semantic_elaboration.h:332
D
Definition: APPgetLargeConnectedEdges.m:106
boost::array< Val_type, Traits::n_args > Arg_list
Definition: Semantic_elaboration.h:164
const Val_type & arg(const size_t &) const
return underlying hash code for head value
Definition: Semantic_elaboration.h:429
size_t type_code() const
return type code
Definition: Semantic_elaboration.h:457
Support for error handling exception classes in libKJB.
const size_t & args_code() const
return underlying hash code for args
Definition: Semantic_elaboration.h:613
#define IFTD(a, ex, msg, params)
Definition: l_exception.h:112
friend std::ostream & operator<<(std::ostream &os, Self_ptr)
Definition: Semantic_elaboration.cpp:34
static const Referent_code NULL_REFERENT
Definition: Semantic_elaboration.h:274
Semantic_elaboration::Self_ptr Base_ptr
Definition: Semantic_elaboration.h:533
Concrete semantic data class for storing hash codes about nodes.
Definition: Semantic_elaboration.h:62
virtual const Key_type & decode(const Val_type &val)
look up key associated with code
Definition: Token_map.cpp:33
const size_t & args_code() const
return hash code for arg list
Definition: Semantic_elaboration.h:212
const Key_type & head_s() const
return the actual head value
Definition: Semantic_elaboration.h:204
Code
Definition: SemanticIO.h:54
const Val_type & head() const
return code for head value
Definition: Semantic_elaboration.h:578
Base_ptr child(const size_t &) const
return pointer to child in a particular position
Definition: Semantic_elaboration.h:472
const Val_type & head() const
return the actual head value
Definition: Semantic_elaboration.h:200
boost::shared_ptr< Semantic_data_base > Base_ptr
Definition: Semantic_elaboration.h:113
Nonterminal_elaboration< T, Traits > Self_type
Definition: Semantic_elaboration.h:530
Data_type::Arg_list Arg_list
Definition: Semantic_elaboration.h:536
Definition: Semantic_db.h:16
Semantic_db::Val_type Val_type
Definition: Semantic_elaboration.h:111
const boost::shared_ptr< Null_semantic_terminal > & null_semantic_terminal()
creates a new "null" terminal node (w/ no semantic content)
Definition: Semantic_traits.cpp:46
static Map & global_head_map()
Definition: Semantic_elaboration.h:139
std::deque< Self_ptr_const > Self_ptrs_read
Definition: Semantic_elaboration.h:269
const Step_code::Weights & step_filter() const
return binary filter array indicating allowable steps
Definition: Semantic_elaboration.h:465