1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #ifndef MOZC_ENGINE_ENGINE_H_
31 #define MOZC_ENGINE_ENGINE_H_
32 
33 #include <memory>
34 
35 #include "base/port.h"
36 #include "data_manager/data_manager_interface.h"
37 #include "dictionary/dictionary_interface.h"
38 #include "dictionary/pos_group.h"
39 #include "engine/engine_interface.h"
40 
41 namespace mozc {
42 
43 class Connector;
44 class ConverterInterface;
45 class ImmutableConverterInterface;
46 class PredictorInterface;
47 class RewriterInterface;
48 class Segmenter;
49 class SuggestionFilter;
50 class UserDataManagerInterface;
51 
52 namespace dictionary {
53 class POSMatcher;
54 class UserDictionary;
55 }  // namespace dictionary
56 
57 // Builds and manages a set of modules that are necessary for conversion engine.
58 class Engine : public EngineInterface {
59  public:
60   // There are two types of engine: desktop and mobile.  The differences are the
61   // underlying prediction engine (DesktopPredictor or MobilePredictor) and
62   // learning preference (to learn content word or not).  See Init() for the
63   // details of implementation.
64 
65   // Creates an instance with desktop configuration from a data manager.  The
66   // ownership of data manager is passed to the engine instance.
67   static std::unique_ptr<Engine> CreateDesktopEngine(
68       std::unique_ptr<const DataManagerInterface> data_manager);
69 
70   // Helper function for the above factory, where data manager is instantiated
71   // by a default constructor.  Intended to be used for OssDataManager etc.
72   template <typename DataManagerType>
CreateDesktopEngineHelper()73   static std::unique_ptr<Engine> CreateDesktopEngineHelper() {
74     return CreateDesktopEngine(
75         std::unique_ptr<const DataManagerType>(new DataManagerType()));
76   }
77 
78   // Creates an instance with mobile configuration from a data manager.  The
79   // ownership of data manager is passed to the engine instance.
80   static std::unique_ptr<Engine> CreateMobileEngine(
81       std::unique_ptr<const DataManagerInterface> data_manager);
82 
83   // Helper function for the above factory, where data manager is instantiated
84   // by a default constructor.  Intended to be used for OssDataManager etc.
85   template <typename DataManagerType>
CreateMobileEngineHelper()86   static std::unique_ptr<Engine> CreateMobileEngineHelper() {
87     return CreateMobileEngine(
88         std::unique_ptr<const DataManagerType>(new DataManagerType()));
89   }
90 
91   Engine();
92   ~Engine() override;
93 
GetConverter()94   ConverterInterface *GetConverter() const override { return converter_.get(); }
GetPredictor()95   PredictorInterface *GetPredictor() const override { return predictor_; }
GetSuppressionDictionary()96   dictionary::SuppressionDictionary *GetSuppressionDictionary() override {
97     return suppression_dictionary_.get();
98   }
99 
100   bool Reload() override;
101 
GetUserDataManager()102   UserDataManagerInterface *GetUserDataManager() override {
103     return user_data_manager_.get();
104   }
105 
GetDataVersion()106   StringPiece GetDataVersion() const override {
107     return data_manager_->GetDataVersion();
108   }
109 
GetDataManager()110   const DataManagerInterface *GetDataManager() const override {
111     return data_manager_.get();
112   }
113 
114  private:
115   // Initializes the object by the given data manager and predictor factory
116   // function.  Predictor factory is used to select DefaultPredictor and
117   // MobilePredictor.
118   void Init(const DataManagerInterface *data_manager,
119             PredictorInterface *(*predictor_factory)(PredictorInterface *,
120                                                      PredictorInterface *),
121             bool enable_content_word_learning);
122 
123   std::unique_ptr<const DataManagerInterface> data_manager_;
124   std::unique_ptr<const dictionary::POSMatcher> pos_matcher_;
125   std::unique_ptr<dictionary::SuppressionDictionary> suppression_dictionary_;
126   std::unique_ptr<const Connector> connector_;
127   std::unique_ptr<const Segmenter> segmenter_;
128   std::unique_ptr<dictionary::UserDictionary> user_dictionary_;
129   std::unique_ptr<dictionary::DictionaryInterface> suffix_dictionary_;
130   std::unique_ptr<dictionary::DictionaryInterface> dictionary_;
131   std::unique_ptr<const dictionary::PosGroup> pos_group_;
132   std::unique_ptr<ImmutableConverterInterface> immutable_converter_;
133   std::unique_ptr<const SuggestionFilter> suggestion_filter_;
134 
135   // TODO(noriyukit): Currently predictor and rewriter are created by this class
136   // but owned by converter_. Since this class creates these two, it'd be better
137   // if Engine class owns these two instances.
138   PredictorInterface *predictor_;
139   RewriterInterface *rewriter_;
140 
141   std::unique_ptr<ConverterInterface> converter_;
142   std::unique_ptr<UserDataManagerInterface> user_data_manager_;
143 
144   DISALLOW_COPY_AND_ASSIGN(Engine);
145 };
146 
147 }  // namespace mozc
148 
149 #endif  // MOZC_ENGINE_ENGINE_H_
150