PLearn 0.1
VMatLanguage.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn ("A C++ Machine Learning Library")
00004 // Copyright (C) 2002 Pascal Vincent and Julien Keable
00005 //
00006 
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 //
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 //
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 //
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 //
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 /* *******************************************************
00036  * $Id: VMatLanguage.h 7042 2007-05-09 23:44:20Z saintmlx $
00037  * This file is part of the PLearn library.
00038  ******************************************************* */
00039 
00040 #ifndef VMatLanguage_INC
00041 #define VMatLanguage_INC
00042 
00043 #include "RowBufferedVMatrix.h"
00044 #include "VMat.h"
00045 #include <plearn/base/RealMapping.h>
00046 
00047 #ifdef __INTEL_COMPILER
00048 #pragma warning(disable:1125) // Get rid of ICC compiler warning.
00049 #endif
00050 
00051 namespace PLearn {
00052 using namespace std;
00053 
00054 /* The VMatLanguage object contains a VPL bytecode program that can be applied to a row of a VMat.
00055 
00056 The VPL Language is described in /u/jkeable/visu/help/vpl.html.
00057 
00058 By the way, the define statements in the VPL language can be recursive. Cool!
00059 
00060 */
00061 
00062 class VMatLanguage: public Object
00063 {
00064     typedef Object inherited;
00065 
00066     VMat vmsource;
00067     TVec<string> srcfieldnames;
00068     TVec<string> outputfieldnames;
00069     TVec<int> program;
00070     TVec<RealMapping> mappings;
00071     mutable Vec pstack;
00072     mutable Vec myvec;
00073     mutable Vec mem;
00074 
00075     // maps opcodes strings to opcodes numbers
00076     static map<string, int> opcodes;
00077 
00079     static void build_opcodes_map();
00080 
00081     // generates bytecode from a preprocessed text sourcecode
00082     void generateCode(const string& processed_sourcecode);
00083     void generateCode(PStream& processed_sourcecode);
00084 
00085     // This function takes raw VPL code and returns the preprocessed sourcecode
00086     // along with the defines and fieldnames it generated.
00087     void preprocess(PStream& in,                   map<string, string>& defines,
00088                     string&  processed_sourcecode, vector<string>&      fieldnames );
00089 
00090 public:
00091     string sourcecode;
00092 
00093     VMatLanguage():vmsource(Mat()) { build_(); }
00094     VMatLanguage(VMat vmsrc);
00095 
00096     PLEARN_DECLARE_OBJECT(VMatLanguage);
00097     static void declareOptions(OptionList &ol);
00098 
00099     virtual void build();
00100 
00103     virtual void run(const Vec& srcvec, const Vec& result, int rowindex=-1) const;
00104 
00107     void run(int rowindex, const Vec& result) const;
00108 
00109     void setSource(VMat the_source);
00110     void setSourceFieldNames(TVec<string> the_srcfieldnames);
00111 
00112     inline TVec<string> getOutputFieldNames() const
00113     { return outputfieldnames; }
00114 
00115     inline int inputsize() const
00116     { return srcfieldnames.length(); }
00117 
00118     inline int outputsize() const
00119     { return outputfieldnames.length(); }
00120 
00121     // from the outside, use the next 3 high-level functions
00123 
00128     void compileStream(PStream &in, vector<string>& fieldnames);
00129     void compileString(const string & code, vector<string>& fieldnames);
00130     void compileFile(const PPath& filename, vector<string>& fieldnames);
00131     void compileString(const string & code, TVec<string>& fieldnames);
00132 
00133     static void getOutputFieldNamesFromString(const string & code, vector<string>& fieldnames);
00134     static void getOutputFieldNamesFromString(const string & code, TVec<string>& fieldnames);
00135     static void getOutputFieldNamesFromStream(PStream &in, vector<string>& fieldnames);
00136     static void staticPreprocess(PStream& in, map<string, string>& defines,
00137                                  string&  processed_sourcecode, vector<string>& fieldnames );
00138 
00139     inline operator bool() const
00140     { return program.length()>0; }
00141 
00143     void clear();
00144 
00145     int pstackSize() const {return pstack.size();}
00146 
00147 
00149     Vec getMemory() const { return mem; }
00150 
00152     void setMemory(const Vec& new_mem) const;
00153 
00154 
00155     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00156 
00157     // set this to true when debugging. Will dump the preprocessed code when you call compilexxxx
00158     static bool output_preproc;
00159 private:
00160     void build_();
00161 };
00162 
00163 DECLARE_OBJECT_PTR(VMatLanguage);
00164 
00165 /*
00166   PreprocessingVMatrix : a VMatLanguage derivated product, a la sauce VMat.
00167   Construct a PreprocessingVMatrix by specifying the source VMat and a string containing the VPL code to process each row
00168 */
00169 
00170 class PreprocessingVMatrix: public RowBufferedVMatrix
00171 {
00172     typedef RowBufferedVMatrix inherited;
00173 
00174 protected:
00175     VMat source;
00176     PP<VMatLanguage> program;
00177     Vec sourcevec;
00178     vector<string> fieldnames;
00179 
00180 public:
00181     PreprocessingVMatrix(){}
00182     PreprocessingVMatrix(VMat the_source, const string& program_string);
00183 
00184     PLEARN_DECLARE_OBJECT(PreprocessingVMatrix);
00185 
00186     virtual void build();
00187 
00188 protected:
00189 
00190     virtual void getNewRow(int i, const Vec& v) const;
00191     static void declareOptions(OptionList &ol);
00192 
00193 private:
00194 
00195     void build_();
00196 };
00197 
00198 DECLARE_OBJECT_PTR(PreprocessingVMatrix);
00199 
00200 time_t getDateOfCode(const string& codefile);
00201 
00202 } // end of namespace PLearn
00203 
00204 #ifdef __INTEL_COMPILER
00205 #pragma warning(default:1125)
00206 #endif
00207 
00208 #endif
00209 
00210 
00211 /*
00212   Local Variables:
00213   mode:c++
00214   c-basic-offset:4
00215   c-file-style:"stroustrup"
00216   c-file-offsets:((innamespace . 0)(inline-open . 0))
00217   indent-tabs-mode:nil
00218   fill-column:79
00219   End:
00220 */
00221 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines