00001 // -*- mode: c++; indent-tabs-mode: nil; tab-width:2 -*- 00002 #pragma once 00003 00004 #include <boost/smart_ptr/shared_ptr.hpp> 00005 #include "moses/ThreadPool.h" 00006 #include "moses/Manager.h" 00007 #include "moses/HypergraphOutput.h" 00008 #include "moses/IOWrapper.h" 00009 #include "moses/Manager.h" 00010 #include "moses/ChartManager.h" 00011 #include "moses/ContextScope.h" 00012 00013 #include "moses/Syntax/F2S/Manager.h" 00014 #include "moses/Syntax/S2T/Manager.h" 00015 #include "moses/Syntax/T2S/Manager.h" 00016 00017 #include <boost/shared_ptr.hpp> 00018 #include <boost/weak_ptr.hpp> 00019 #include <boost/make_shared.hpp> 00020 00021 #ifdef WITH_THREADS 00022 #include <boost/thread/shared_mutex.hpp> 00023 #include <boost/thread/locks.hpp> 00024 #endif 00025 00026 namespace Moses 00027 { 00028 class InputType; 00029 class OutputCollector; 00030 00031 00037 class TranslationTask : public Moses::Task 00038 { 00039 // no copying, no assignment 00040 TranslationTask(TranslationTask const& other) { } 00041 00042 TranslationTask const& 00043 operator=(TranslationTask const& other) { 00044 return *this; 00045 } 00046 protected: 00047 AllOptions::ptr m_options; 00048 boost::weak_ptr<TranslationTask> m_self; // weak ptr to myself 00049 boost::shared_ptr<ContextScope> m_scope; // sores local info 00050 // pointer to ContextScope, which stores context-specific information 00051 TranslationTask() { } ; 00052 TranslationTask(boost::shared_ptr<Moses::InputType> const& source, 00053 boost::shared_ptr<Moses::IOWrapper> const& ioWrapper); 00054 // Yes, the constructor is protected. 00055 // 00056 // TranslationTasks can only be created through the creator 00057 // functions create(...). The creator functions set m_self to a 00058 // weak_pointer s.t m_self.get() == this. The public member function 00059 // self() can then be used to get a shared_ptr to the Task that 00060 // guarantees the existence of the Task while that pointer is live. 00061 // Depending on the use, case, that shared pointer can be kept alive 00062 // or copied into a weak pointer that can then be used e.g. as a 00063 // hash key for caching context-dependent information in feature 00064 // functions. When it is time to clean up the cache, the feature 00065 // function can determine (via a check on the weak pointer) if the 00066 // task is still live or not, or maintain a shared_ptr to ensure the 00067 // task stays alive till it's done with it. 00068 00069 boost::shared_ptr<std::vector<std::string> > m_context; 00070 // SPTR<std::map<std::string, float> const> m_context_weights; 00071 public: 00072 00073 boost::shared_ptr<TranslationTask> 00074 self() { 00075 return m_self.lock(); 00076 } 00077 00078 virtual 00079 boost::shared_ptr<TranslationTask const> 00080 self() const { 00081 return m_self.lock(); 00082 } 00083 00084 // creator functions 00085 static boost::shared_ptr<TranslationTask> create(); 00086 00087 static 00088 boost::shared_ptr<TranslationTask> 00089 create(boost::shared_ptr<Moses::InputType> const& source); 00090 00091 static 00092 boost::shared_ptr<TranslationTask> 00093 create(boost::shared_ptr<Moses::InputType> const& source, 00094 boost::shared_ptr<Moses::IOWrapper> const& ioWrapper); 00095 00096 static 00097 boost::shared_ptr<TranslationTask> 00098 create(boost::shared_ptr<Moses::InputType> const& source, 00099 boost::shared_ptr<Moses::IOWrapper> const& ioWrapper, 00100 boost::shared_ptr<ContextScope> const& scope); 00101 00102 ~TranslationTask(); 00105 virtual void Run(); 00106 00107 boost::shared_ptr<Moses::InputType> 00108 GetSource() const { 00109 return m_source; 00110 } 00111 00112 boost::shared_ptr<Moses::IOWrapper const> 00113 GetIOWrapper() const { 00114 return m_ioWrapper; 00115 } 00116 00117 boost::shared_ptr<BaseManager> 00118 SetupManager(SearchAlgorithm algo); // = DefaultSearchAlgorithm); 00119 00120 00121 boost::shared_ptr<ContextScope> const& 00122 GetScope() const { 00123 UTIL_THROW_IF2(m_scope == NULL, "No context scope!"); 00124 return m_scope; 00125 } 00126 00127 boost::shared_ptr<std::vector<std::string> > 00128 GetContextWindow() const; 00129 00130 void 00131 SetContextWindow(boost::shared_ptr<std::vector<std::string> > const& cw); 00132 00133 // SPTR<std::map<std::string, float> const> GetContextWeights() const; 00134 // void SetContextWeights(std::string const& context_weights); 00135 // void ReSetContextWeights(std::map<std::string, float> const& new_weights); 00136 00137 AllOptions::ptr const& options() const; 00138 00139 protected: 00140 boost::shared_ptr<Moses::InputType> m_source; 00141 boost::shared_ptr<Moses::IOWrapper> m_ioWrapper; 00142 00143 void interpret_dlt(); 00144 }; 00145 00146 00147 } //namespace