Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

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,v 1.16 2004/07/21 16:30:55 chrish42 Exp $ 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 00048 namespace PLearn { 00049 using namespace std; 00050 00051 /* The VMatLanguage object contains a VPL bytecode program that can be applied to a row of a VMat. 00052 00053 The VPL Language is described in /u/jkeable/visu/help/vpl.html. 00054 00055 By the way, the define statements in the VPL language can be recursive. Cool! 00056 00057 */ 00058 00059 class VMatLanguage: public Object 00060 { 00061 typedef Object inherited; 00062 00063 VMat vmsource; 00064 TVec<int> program; 00065 TVec<RealMapping> mappings; 00066 mutable Vec pstack; 00067 mutable Vec myvec; 00068 mutable Vec mem; 00069 00070 // maps opcodes strings to opcodes numbers 00071 static map<string, int> opcodes; 00072 00074 static void build_opcodes_map(); 00075 00076 // generates bytecode from a preprocessed text sourcecode 00077 void generateCode(const string& processed_sourcecode); 00078 void generateCode(istream& processed_sourcecode); 00079 00080 // this function takes raw VPL code and returns the preprocessed sourcecode 00081 // along with the defines and the fieldnames it generated 00082 void preprocess(istream& in, map<string, string>& defines, string& processed_sourcecode, 00083 vector<string>& fieldnames); 00084 00085 public: 00086 VMatLanguage():vmsource(Mat()) { build_(); } 00087 VMatLanguage(VMat vmsrc):vmsource(vmsrc) { build_(); } 00088 00089 PLEARN_DECLARE_OBJECT(VMatLanguage); 00090 static void declareOptions(OptionList &ol); 00091 00092 virtual void build(); 00093 00096 void run(const Vec& srcvec, const Vec& result, int rowindex=-1) const; 00097 00100 void run(int rowindex, const Vec& result) const; 00101 00102 inline void setSource(VMat the_source) 00103 { 00104 vmsource = the_source; 00105 // program must be compiled with the right source in place... 00106 program.resize(0); 00107 } 00108 00109 // from the outside, use the next 3 high-level functions 00111 00115 void compileStream(istream &in, vector<string>& fieldnames); 00116 void compileString(const string & code, vector<string>& fieldnames); 00117 void compileFile(const string & filename, vector<string>& fieldnames); 00118 00119 int pstackSize() const {return pstack.size();} 00120 00121 // set this to true when debugging. Will dump the preprocessed code when you call compilexxxx 00122 static bool output_preproc; 00123 private: 00124 void build_(); 00125 }; 00126 00127 DECLARE_OBJECT_PTR(VMatLanguage); 00128 00129 /* 00130 PreprocessingVMatrix : a VMatLanguage derivated product, a la sauce VMat. 00131 Construct a PreprocessingVMatrix by specifying the source VMat and a string containing the VPL code to process each row 00132 */ 00133 00134 class PreprocessingVMatrix: public RowBufferedVMatrix 00135 { 00136 typedef RowBufferedVMatrix inherited; 00137 00138 protected: 00139 VMat source; 00140 VMatLanguage program; 00141 Vec sourcevec; 00142 vector<string> fieldnames; 00143 00144 public: 00145 PreprocessingVMatrix(){} 00146 PreprocessingVMatrix(VMat the_source, const string& program_string); 00147 00148 PLEARN_DECLARE_OBJECT(PreprocessingVMatrix); 00149 00150 virtual void build(); 00151 00152 protected: 00153 00154 virtual void getNewRow(int i, const Vec& v) const; 00155 static void declareOptions(OptionList &ol); 00156 00157 private: 00158 00159 void build_(); 00160 }; 00161 00162 DECLARE_OBJECT_PTR(PreprocessingVMatrix); 00163 00164 time_t getDateOfCode(const string& codefile); 00165 00166 } // end of namespace PLearn 00167 00168 #endif 00169 00170

Generated on Tue Aug 17 16:10:35 2004 for PLearn by doxygen 1.3.7