Re: Re: Re: Orocos Program

Peter Soetens <[email protected]>
Newsgroups gmane.science.robotics.orocos.user
Organization KU Leuven
Message-ID <[email protected]>
On Tuesday 18 May 2004 09:58, Andrea Consonni wrote:
> >>> you want to introduce a constructor like :
> >>>
> >>> var Double6D d6 = Double6D(0.,1.,2.,3.,4.,5.)
> >>
> >> Exactly!

Ok. I have found the bug. The parser only allows keywords which are fully 
lowercase. Thus it must be : double6d(...) instead. I could soften this 
requirement, but I think it is better to leave it this way.

I have successfully tested the double6d(1.0) constructor... So everything you 
did was correct. While I was "at it", i Also added the 
double6d(1.0,2.0,3.0,4.0,5.0,6.0) constructor, that's what is in the second 
patch. The second patch also includes the first patch.

I also added my test program (which is very limited :-).

'Hope this helps you a lot...

Peter

PS : do :
cd orocos-0.14.2/packages/execution/program_parser/0.14.2/
patch -p4 < fix-xxx.patch

-- 
------------------------------------------------------------------------
Peter Soetens, Research Assistent                  http://www.orocos.org
Katholieke Universiteit Leuven
Division Production Engineering,                      tel. +32 16 322773
Machine Design and Automation                         fax. +32 16 322987
Celestijnenlaan 300B                   [email protected]
B-3001 Leuven Belgium                 http://www.mech.kuleuven.ac.be/pma
------------------------------------------------------------------------
fix-double6d.patch (text/x-diff, 1.9 KB)
Index: packages/execution/program_parser/current/include/parser-common.hpp
===================================================================
--- packages/execution/program_parser/current/include/parser-common.hpp	(revision 638)
+++ packages/execution/program_parser/current/include/parser-common.hpp	(working copy)
@@ -134,7 +134,7 @@
           "do", "until", "next", "done", "or", "and", "not", "include", "if",
           "define", "end", "then", "else", "for", "foreach", "while", "true",
           "false", "stop", "async", "time", "const", "frame", "double",
-          "int", "bool", "char", "string", "vector", "double6D", "rotation", "twist",
+          "int", "bool", "char", "string", "vector", "double6d", "rotation", "twist",
           "wrench", "nothing", "var", "set", "let", "alias", "to", "sync",
           "return", "call";
 
Index: packages/execution/program_parser/current/src/Types.cxx
===================================================================
--- packages/execution/program_parser/current/src/Types.cxx	(revision 638)
+++ packages/execution/program_parser/current/src/Types.cxx	(working copy)
@@ -102,6 +102,6 @@
     data["double"] = new TemplateTypeInfo<double>();
     data["bool"] = new TemplateTypeInfo<bool>();
     // Cappellini Consonni Extension
-    data["double6D"] = new TemplateTypeInfo<Double6D>();
+    data["double6d"] = new TemplateTypeInfo<Double6D>();
   };
 }
Index: packages/execution/program_parser/current/src/ExpressionParser.cxx
===================================================================
--- packages/execution/program_parser/current/src/ExpressionParser.cxx	(revision 638)
+++ packages/execution/program_parser/current/src/ExpressionParser.cxx	(working copy)
@@ -300,7 +300,7 @@
 
     // Cappellini Consonni Extension
     double6Dctor = (
-        str_p( "double6D" )
+        str_p( "double6d" )
       >> '('
       >> expression
       >> ')' )[ bind( &ExpressionParser::seen_unary, this, "double6Dd" ) ];
fix-double6d_and_add_6dctor.patch (text/x-diff, 15.9 KB)
Index: packages/execution/program_parser/current/include/DataSource.hpp
===================================================================
--- packages/execution/program_parser/current/include/DataSource.hpp	(revision 637)
+++ packages/execution/program_parser/current/include/DataSource.hpp	(working copy)
@@ -283,6 +283,72 @@
   };
 
   /**
+   * The extension of BinaryDataSource to sixary functions..
+   */
+  template<typename function>
+  class SixaryDataSource
+    : public DataSource<typename function::result_type>
+  {
+    typedef typename function::result_type value_t;
+    typedef typename remove_cr<typename function::first_argument_type>::type first_arg_t;
+    typedef typename remove_cr<typename function::second_argument_type>::type second_arg_t;
+    typedef typename remove_cr<typename function::third_argument_type>::type third_arg_t;
+    typedef typename remove_cr<typename function::forth_argument_type>::type forth_arg_t;
+    typedef typename remove_cr<typename function::fifth_argument_type>::type fifth_arg_t;
+    typedef typename remove_cr<typename function::sixth_argument_type>::type sixth_arg_t;
+    typename DataSource<first_arg_t>::shared_ptr ma;
+    typename DataSource<second_arg_t>::shared_ptr mb;
+    typename DataSource<third_arg_t>::shared_ptr mc;
+    typename DataSource<forth_arg_t>::shared_ptr md;
+    typename DataSource<fifth_arg_t>::shared_ptr me;
+    typename DataSource<sixth_arg_t>::shared_ptr mf;
+    function fun;
+  public:
+    typedef boost::intrusive_ptr<SixaryDataSource<function> > shared_ptr;
+
+    SixaryDataSource(
+                     DataSource<first_arg_t>* a,
+                     DataSource<second_arg_t>* b,
+                     DataSource<third_arg_t>* c,
+                     DataSource<forth_arg_t>* d,
+                     DataSource<fifth_arg_t>* e,
+                     DataSource<sixth_arg_t>* f,
+                       function _fun )
+      : ma( a ), mb( b ), mc( c ),md( d ), me( e ), mf( f ),
+        fun( _fun )
+      {
+      };
+
+    virtual value_t get() const
+      {
+        first_arg_t a = ma->get();
+        second_arg_t b = mb->get();
+        third_arg_t c = mc->get();
+        forth_arg_t d = md->get();
+        fifth_arg_t e = me->get();
+        sixth_arg_t f = mf->get();
+        return fun( a, b, c, d, e, f );
+      }
+
+    virtual void reset()
+      {
+        ma->reset();
+        mb->reset();
+        mc->reset();
+        md->reset();
+        me->reset();
+        mf->reset();
+      }
+
+      virtual DataSource<typename function::result_type>* clone() const
+      {
+          return new SixaryDataSource<function>(ma->clone(), mb->clone(), mc->clone(),
+                                                md->clone(), me->clone(), mf->clone(),
+                                                fun);
+      }
+  };
+
+  /**
    * The extension of BinaryDataSource to unary functions..
    */
   template <typename function>
Index: packages/execution/program_parser/current/include/parser-common.hpp
===================================================================
--- packages/execution/program_parser/current/include/parser-common.hpp	(revision 638)
+++ packages/execution/program_parser/current/include/parser-common.hpp	(working copy)
@@ -134,7 +134,7 @@
           "do", "until", "next", "done", "or", "and", "not", "include", "if",
           "define", "end", "then", "else", "for", "foreach", "while", "true",
           "false", "stop", "async", "time", "const", "frame", "double",
-          "int", "bool", "char", "string", "vector", "double6D", "rotation", "twist",
+          "int", "bool", "char", "string", "vector", "double6d", "rotation", "twist",
           "wrench", "nothing", "var", "set", "let", "alias", "to", "sync",
           "return", "call";
 
Index: packages/execution/program_parser/current/include/ExpressionParser.hpp
===================================================================
--- packages/execution/program_parser/current/include/ExpressionParser.hpp	(revision 638)
+++ packages/execution/program_parser/current/include/ExpressionParser.hpp	(working copy)
@@ -92,7 +92,7 @@
       divexp, modexp, plusexp, minusexp, smallereqexp, smallerexp,
       greatereqexp, greaterexp, equalexp, notequalexp, orexp, andexp,
       ifthenelseexp, frameexp, vectorexp, double6Dexp, rotexp, groupexp, atomicexpression,
-      constructorexp, framector, vectorctor, double6Dctor, rotationctor, time_expression,
+      constructorexp, framector, vectorctor, double6Dctor, double6Dctor6, rotationctor, time_expression,
       time_spec;
 
     /**
@@ -117,6 +117,7 @@
     void seen_unary( const std::string& op );
     void seen_binary( const std::string& op );
     void seen_ternary( const std::string& op );
+    void seen_sixary( const std::string& op );
     void seenvalue();
     void seendatacall();
     void seentimespec( int n );
Index: packages/execution/program_parser/current/include/Operators.hpp
===================================================================
--- packages/execution/program_parser/current/include/Operators.hpp	(revision 637)
+++ packages/execution/program_parser/current/include/Operators.hpp	(working copy)
@@ -99,17 +99,37 @@
       DataSourceBase* c ) = 0;
   };
 
+  class SixaryOp
+  {
+  public:
+    virtual ~SixaryOp();
+    /**
+     * If op is the operator you are responsible for, and if the
+     * argument DataSource's are of the correct type, then return an
+     * appropriate DataSource ( i.e. a DataSource that will apply a
+     * certain operation on the values it gets from its argument
+     * DataSources, and will return that value ).
+     * Otherwise, return 0.
+     */
+    virtual DataSourceBase* build(
+      const std::string& op,
+      DataSourceBase* a, DataSourceBase* b, DataSourceBase* c,
+      DataSourceBase* d, DataSourceBase* e, DataSourceBase* f) = 0;
+  };
+
   class OperatorRegistry
   {
     std::vector<UnaryOp*> unaryops;
     std::vector<BinaryOp*> binaryops;
     std::vector<TernaryOp*> ternaryops;
+    std::vector<SixaryOp*> sixaryops;
     OperatorRegistry();
     OperatorRegistry( const OperatorRegistry& );
     ~OperatorRegistry();
     void add( UnaryOp* o );
     void add( BinaryOp* o );
     void add( TernaryOp* o );
+    void add( SixaryOp* o );
   public:
     static OperatorRegistry& instance();
     DataSourceBase* applyUnary( const std::string& op, DataSourceBase* a );
@@ -118,6 +138,10 @@
     DataSourceBase* applyTernary(
       const std::string& op, DataSourceBase* a, DataSourceBase* b,
       DataSourceBase* c );
+    DataSourceBase* applySixary(
+      const std::string& op,
+      DataSourceBase* a, DataSourceBase* b, DataSourceBase* c,
+      DataSourceBase* d, DataSourceBase* e, DataSourceBase* f);
   };
 }
 
Index: packages/execution/program_parser/current/src/Operators.cxx
===================================================================
--- packages/execution/program_parser/current/src/Operators.cxx	(revision 638)
+++ packages/execution/program_parser/current/src/Operators.cxx	(working copy)
@@ -77,6 +77,36 @@
     return pointer_to_ternary_function<ResultT, Arg1T, Arg2T, Arg3T>( fun );
   }
 
+  template<typename ResultT, typename Arg1T, typename Arg2T, typename Arg3T,
+           typename Arg4T, typename Arg5T, typename Arg6T >
+  struct pointer_to_sixary_function
+  {
+    ResultT (*fun)( Arg1T, Arg2T, Arg3T, Arg4T, Arg5T, Arg6T );
+    typedef ResultT result_type;
+    typedef Arg1T first_argument_type;
+    typedef Arg2T second_argument_type;
+    typedef Arg3T third_argument_type;
+    typedef Arg4T forth_argument_type;
+    typedef Arg5T fifth_argument_type;
+    typedef Arg6T sixth_argument_type;
+    pointer_to_sixary_function( ResultT (*f)(Arg1T, Arg2T, Arg3T, Arg4T, Arg5T, Arg6T ) )
+      : fun( f )
+      {
+      }
+    ResultT operator()( Arg1T a, Arg2T b, Arg3T c, Arg4T d, Arg5T e, Arg6T f ) const
+      {
+        return (*fun)( a, b, c, d, e, f );
+      }
+  };
+
+    template<typename ResultT, typename Arg1T, typename Arg2T, typename Arg3T,
+           typename Arg4T, typename Arg5T, typename Arg6T >
+  pointer_to_sixary_function<ResultT, Arg1T, Arg2T, Arg3T, Arg4T, Arg5T, Arg6T>
+  ptr_fun( ResultT (*fun)( Arg1T, Arg2T, Arg3T, Arg4T, Arg5T, Arg6T ) )
+  {
+    return pointer_to_sixary_function<ResultT, Arg1T, Arg2T, Arg3T, Arg4T, Arg5T, Arg6T>( fun );
+  }
+
   // combines boost::remove_reference and boost::remove_const
   template<typename T>
   struct remove_cr
@@ -224,6 +254,53 @@
     return new TernaryOperator<function>( op, f );
   }
 
+  template<typename function>
+  class SixaryOperator
+    : public SixaryOp
+  {
+    typedef typename mystl::remove_cr<typename function::first_argument_type>::type arg1_t;
+    typedef typename mystl::remove_cr<typename function::second_argument_type>::type arg2_t;
+    typedef typename mystl::remove_cr<typename function::third_argument_type>::type arg3_t;
+    typedef typename mystl::remove_cr<typename function::forth_argument_type>::type arg4_t;
+    typedef typename mystl::remove_cr<typename function::fifth_argument_type>::type arg5_t;
+    typedef typename mystl::remove_cr<typename function::sixth_argument_type>::type arg6_t;
+    typedef typename function::result_type result_t;
+    const char* mop;
+    function fun;
+  public:
+    SixaryOperator( const char* op, function f )
+      : mop( op ), fun( f )
+      {
+      };
+    DataSource<result_t>* build( const std::string& op,
+                                 DataSourceBase* a, DataSourceBase* b, DataSourceBase* c,
+                                 DataSourceBase* d, DataSourceBase* e, DataSourceBase* f )
+      {
+        if ( op != mop ) return 0;
+        DataSource<arg1_t>* arg1 =
+          dynamic_cast<DataSource<arg1_t>*>( a );
+        DataSource<arg2_t>* arg2 =
+          dynamic_cast<DataSource<arg2_t>*>( b );
+        DataSource<arg3_t>* arg3 =
+          dynamic_cast<DataSource<arg3_t>*>( c );
+        DataSource<arg4_t>* arg4 =
+          dynamic_cast<DataSource<arg4_t>*>( d );
+        DataSource<arg5_t>* arg5 =
+          dynamic_cast<DataSource<arg5_t>*>( e );
+        DataSource<arg6_t>* arg6 =
+          dynamic_cast<DataSource<arg6_t>*>( f );
+        if ( !arg1 || ! arg2 || !arg3 || !arg4 || !arg5 || !arg6 ) return 0;
+        return new SixaryDataSource<function>( arg1, arg2, arg3, arg4, arg5, arg6, fun );
+      };
+  };
+
+  template<typename function>
+  SixaryOperator<function>*
+  newSixaryOperator( const char* op, function f )
+  {
+    return new SixaryOperator<function>( op, f );
+  }
+
   OperatorRegistry& OperatorRegistry::instance()
   {
     static OperatorRegistry reg;
@@ -251,6 +328,17 @@
     return d6d;
   }
 
+  Double6D double6D6d( double a, double b, double c, double d, double e, double f )
+  {
+    Double6D d6d;
+    d6d[0] = a;
+    d6d[1] = b;
+    d6d[2] = c;
+    d6d[3] = d;
+    d6d[4] = e;
+    d6d[5] = f;
+    return d6d;
+  }
 
 
   OperatorRegistry::OperatorRegistry()
@@ -316,10 +404,18 @@
     add( newTernaryOperator( "rotationeuler",
                              mystl::ptr_fun( Rotation::EulerZYZ ) ) );
     add( newBinaryOperator( "framevr", std::ptr_fun( &framevr ) ) );
+#endif
 
     // Cappellini Consonni Extension
     add( newUnaryOperator( "double6Dd", std::ptr_fun( &double6Dd ) ) );
-#endif
+    add( newSixaryOperator( "double6D6d", mystl::ptr_fun( &double6D6d ) ) );
+
+    //add( newUnaryOperator( "-", std::negate<Double6D>() ) );
+    add( newBinaryOperator( "*", std::multiplies<Double6D>() ) );
+    add( newBinaryOperator( "+", std::plus<Double6D>() ) );
+    add( newBinaryOperator( "-", std::minus<Double6D>() ) );
+    //add( newBinaryOperator( "*", mystl::multiplies<double, Double6D>() ) );
+    add( newBinaryOperator( "*", mystl::multiplies<Double6D, double>() ) );
   };
 
   void OperatorRegistry::add( UnaryOp* a )
@@ -337,12 +433,18 @@
     ternaryops.push_back( b );
   };
 
+  void OperatorRegistry::add( SixaryOp* b )
+  {
+    sixaryops.push_back( b );
+  };
+
   OperatorRegistry::~OperatorRegistry()
   {
     mystl::delete_all( unaryops.begin(), unaryops.end() );
     mystl::delete_all( binaryops.begin(), binaryops.end() );
     mystl::delete_all( ternaryops.begin(), ternaryops.end() );
-  };
+    mystl::delete_all( sixaryops.begin(), sixaryops.end() ); 
+ };
 
   DataSourceBase* OperatorRegistry::applyUnary(
     const std::string& op, DataSourceBase* a )
@@ -384,6 +486,22 @@
     return 0;
   }
 
+  DataSourceBase* OperatorRegistry::applySixary(
+    const std::string& op,
+    DataSourceBase* a, DataSourceBase* b,
+    DataSourceBase* c, DataSourceBase* d,
+    DataSourceBase* e, DataSourceBase* f )
+  {
+    typedef std::vector<SixaryOp*> vec;
+    typedef vec::iterator iter;
+    for ( iter i = sixaryops.begin(); i != sixaryops.end(); ++i )
+    {
+      DataSourceBase* ret = (*i)->build( op, a, b, c, d, e, f );
+      if ( ret ) return ret;
+    };
+    return 0;
+  }
+
   UnaryOp::~UnaryOp()
   {
   }
@@ -395,4 +513,8 @@
   TernaryOp::~TernaryOp()
   {
   }
+
+  SixaryOp::~SixaryOp()
+  {
+  }
 }
Index: packages/execution/program_parser/current/src/Types.cxx
===================================================================
--- packages/execution/program_parser/current/src/Types.cxx	(revision 638)
+++ packages/execution/program_parser/current/src/Types.cxx	(working copy)
@@ -102,6 +102,6 @@
     data["double"] = new TemplateTypeInfo<double>();
     data["bool"] = new TemplateTypeInfo<bool>();
     // Cappellini Consonni Extension
-    data["double6D"] = new TemplateTypeInfo<Double6D>();
+    data["double6d"] = new TemplateTypeInfo<Double6D>();
   };
 }
Index: packages/execution/program_parser/current/src/ExpressionParser.cxx
===================================================================
--- packages/execution/program_parser/current/src/ExpressionParser.cxx	(revision 638)
+++ packages/execution/program_parser/current/src/ExpressionParser.cxx	(working copy)
@@ -278,6 +278,7 @@
         framector
       | vectorctor
       | double6Dctor
+      | double6Dctor6
       | rotationctor;
 
     framector = (
@@ -300,11 +301,27 @@
 
     // Cappellini Consonni Extension
     double6Dctor = (
-        str_p( "double6D" )
+        str_p( "double6d" )
       >> '('
       >> expression
       >> ')' )[ bind( &ExpressionParser::seen_unary, this, "double6Dd" ) ];
 
+    double6Dctor6 = (
+        str_p( "double6d" )
+      >> '('
+      >> expression
+      >> ','
+      >> expression
+      >> ','
+      >> expression
+      >> ','
+      >> expression
+      >> ','
+      >> expression
+      >> ','
+      >> expression
+      >> ')' )[ bind( &ExpressionParser::seen_sixary, this, "double6D6d" ) ];
+
     rotationctor = (
          str_p( "rotation" )
       >> '('
@@ -462,6 +479,43 @@
     parsestack.push( ret );
   };
 
+  void ExpressionParser::seen_sixary( const std::string& op )
+  {
+    DataSourceBase::shared_ptr arg1( parsestack.top() );
+    parsestack.pop();
+    DataSourceBase::shared_ptr arg2( parsestack.top() );
+    parsestack.pop();
+    DataSourceBase::shared_ptr arg3( parsestack.top() );
+    parsestack.pop();
+    DataSourceBase::shared_ptr arg4( parsestack.top() );
+    parsestack.pop();
+    DataSourceBase::shared_ptr arg5( parsestack.top() );
+    parsestack.pop();
+    DataSourceBase::shared_ptr arg6( parsestack.top() );
+    parsestack.pop();
+
+    // we still have a reference to these, that we took when
+    // we pushed it up the parsestack..
+    arg1->deref();
+    arg2->deref();
+    arg3->deref();
+    arg4->deref();
+    arg5->deref();
+    arg6->deref();
+
+    // Arg6 is the first (!) argument, as it was pushed on the stack
+    // first.
+    DataSourceBase* ret =
+        OperatorRegistry::instance().applySixary( op,
+                                                  arg6.get(), arg5.get(), arg4.get(),
+                                                  arg3.get(), arg2.get(), arg1.get() );
+    if ( ! ret )
+      throw parse_exception( "Cannot apply sixary operator \"" + op +
+                             "\" to value." );
+    ret->ref();
+    parsestack.push( ret );
+  };
+
   void ExpressionParser::dropResult()
   {
     parsestack.top()->deref();
program.ops (text/plain, 283 B)
/**
 * This very simple program demonstrates built-in features
 * of the Program Parser.
 */
program {
	var vector v    = vector(1.0,1.0,1.0)
	var double6d d6 = double6d(1.0)
	var double6d d62 = double6d(1.0,2.0,3.0,4.0,5.0, 6.0)

	var double6d res = d6 + d62

	set res = res * 2.0
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.