task_context.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright 2016 Google Inc. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #ifndef SYNTAXNET_TASK_CONTEXT_H_
  13. #define SYNTAXNET_TASK_CONTEXT_H_
  14. #include <string>
  15. #include <vector>
  16. #include "syntaxnet/task_spec.pb.h"
  17. #include "syntaxnet/utils.h"
  18. namespace syntaxnet {
  19. // A task context holds configuration information for a task. It is basically a
  20. // wrapper around a TaskSpec protocol buffer.
  21. class TaskContext {
  22. public:
  23. // Returns the underlying task specification protocol buffer for the context.
  24. const TaskSpec &spec() const { return spec_; }
  25. TaskSpec *mutable_spec() { return &spec_; }
  26. // Returns a named input descriptor for the task. A new input is created if
  27. // the task context does not already have an input with that name.
  28. TaskInput *GetInput(const string &name);
  29. TaskInput *GetInput(const string &name, const string &file_format,
  30. const string &record_format);
  31. // Sets task parameter.
  32. void SetParameter(const string &name, const string &value);
  33. // Returns task parameter. If the parameter is not in the task configuration
  34. // the (default) value of the corresponding command line flag is returned.
  35. string GetParameter(const string &name) const;
  36. int GetIntParameter(const string &name) const;
  37. int64 GetInt64Parameter(const string &name) const;
  38. bool GetBoolParameter(const string &name) const;
  39. double GetFloatParameter(const string &name) const;
  40. // Returns task parameter. If the parameter is not in the task configuration
  41. // the default value is returned. Parameters retrieved using these methods
  42. // don't need to be defined with a DEFINE_*() macro.
  43. string Get(const string &name, const string &defval) const;
  44. string Get(const string &name, const char *defval) const;
  45. int Get(const string &name, int defval) const;
  46. int64 Get(const string &name, int64 defval) const;
  47. double Get(const string &name, double defval) const;
  48. bool Get(const string &name, bool defval) const;
  49. // Returns input file name for a single-file task input.
  50. static string InputFile(const TaskInput &input);
  51. // Returns true if task input supports the file and record format.
  52. static bool Supports(const TaskInput &input, const string &file_format,
  53. const string &record_format);
  54. private:
  55. // Underlying task specification protocol buffer.
  56. TaskSpec spec_;
  57. // Vector of parameters required by this task. These must be specified in the
  58. // task rather than relying on default values.
  59. std::vector<string> required_parameters_;
  60. };
  61. } // namespace syntaxnet
  62. #endif // SYNTAXNET_TASK_CONTEXT_H_