Home | History | Annotate | Download | only in common
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef LIBTEXTCLASSIFIER_COMMON_TASK_CONTEXT_H_
     18 #define LIBTEXTCLASSIFIER_COMMON_TASK_CONTEXT_H_
     19 
     20 #include <string>
     21 #include <vector>
     22 
     23 #include "common/task-spec.pb.h"
     24 #include "util/base/integral_types.h"
     25 
     26 namespace libtextclassifier {
     27 namespace nlp_core {
     28 
     29 // A task context holds configuration information for a task. It is basically a
     30 // wrapper around a TaskSpec protocol buffer.
     31 class TaskContext {
     32  public:
     33   // Returns the underlying task specification protocol buffer for the context.
     34   const TaskSpec &spec() const { return spec_; }
     35   TaskSpec *mutable_spec() { return &spec_; }
     36 
     37   // Returns a named input descriptor for the task. A new input  is created if
     38   // the task context does not already have an input with that name.
     39   TaskInput *GetInput(const std::string &name);
     40   TaskInput *GetInput(const std::string &name,
     41                       const std::string &file_format,
     42                       const std::string &record_format);
     43 
     44   // Sets task parameter.
     45   void SetParameter(const std::string &name, const std::string &value);
     46 
     47   // Returns task parameter. If the parameter is not in the task configuration
     48   // the (default) value of the corresponding command line flag is returned.
     49   std::string GetParameter(const std::string &name) const;
     50   int GetIntParameter(const std::string &name) const;
     51   int64 GetInt64Parameter(const std::string &name) const;
     52   bool GetBoolParameter(const std::string &name) const;
     53   double GetFloatParameter(const std::string &name) const;
     54 
     55   // Returns task parameter. If the parameter is not in the task configuration
     56   // the default value is returned.
     57   std::string Get(const std::string &name, const std::string &defval) const;
     58   std::string Get(const std::string &name, const char *defval) const;
     59   int Get(const std::string &name, int defval) const;
     60   int64 Get(const std::string &name, int64 defval) const;
     61   double Get(const std::string &name, double defval) const;
     62   bool Get(const std::string &name, bool defval) const;
     63 
     64   // Returns input file name for a single-file task input.
     65   //
     66   // Special cases: returns the empty string if the TaskInput does not have any
     67   // input files.  Returns the first file if the TaskInput has multiple input
     68   // files.
     69   static std::string InputFile(const TaskInput &input);
     70 
     71   // Returns true if task input supports the file and record format.
     72   static bool Supports(const TaskInput &input, const std::string &file_format,
     73                        const std::string &record_format);
     74 
     75  private:
     76   // Underlying task specification protocol buffer.
     77   TaskSpec spec_;
     78 
     79   // Vector of parameters required by this task.  These must be specified in the
     80   // task rather than relying on default values.
     81   std::vector<std::string> required_parameters_;
     82 };
     83 
     84 }  // namespace nlp_core
     85 }  // namespace libtextclassifier
     86 
     87 #endif  // LIBTEXTCLASSIFIER_COMMON_TASK_CONTEXT_H_
     88