knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
OptionList.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #include "knowrob/terms/OptionList.h"
7 #include "knowrob/terms/ListTerm.h"
8 #include "knowrob/formulas/Predicate.h"
9 #include "knowrob/terms/Numeric.h"
10 
11 using namespace knowrob;
12 
14  bool optionsRead = false;
15  if (t->isFunction()) {
16  auto *fn = (Function *) t.get();
17  if (*fn->functor() == *ListTerm::listFunctor()) {
18  for (auto &arg: fn->arguments()) {
19  readOption(arg);
20  }
21  optionsRead = true;
22  }
23  }
24  if (!optionsRead) {
25  readOption(t);
26  }
27 }
28 
30  static const auto a_eq = Atom::Tabled("=");
31 
32  if (option->termType() != TermType::FUNCTION) return;
33  auto *fn = (Function *) option.get();
34 
35  if (fn->arity() == 2 && *fn->functor() == *a_eq) {
36  // an option of the form `Key = Value`
37  auto keyTerm = fn->arguments()[0];
38  auto valTerm = fn->arguments()[1];
39  if (keyTerm->isAtom()) {
40  auto keyAtom = std::static_pointer_cast<Atom>(keyTerm);
41  options_[std::string(keyAtom->stringForm())] = valTerm;
42  }
43  } else if (fn->arity() == 1) {
44  // an option of the form `Key(Value)`
45  options_[std::string(fn->functor()->stringForm())] = fn->arguments()[0];
46  }
47 }
48 
49 bool OptionList::contains(const std::string &key) const {
50  return options_.count(key) > 0;
51 }
52 
53 const TermPtr &OptionList::get(const std::string &key, const TermPtr &defaultValue) const {
54  auto it = options_.find(key);
55  if (it == options_.end()) {
56  return defaultValue;
57  } else {
58  return it->second;
59  }
60 }
61 
62 std::string_view OptionList::getString(const std::string &key, const std::string &defaultValue) const {
63  auto it = options_.find(key);
64  if (it == options_.end()) {
65  return defaultValue;
66  } else if (it->second->termType() == TermType::FUNCTION) {
67  Function *fn = ((Function *) it->second.get());
68  if (fn->arity() == 0) {
69  return fn->functor()->stringForm();
70  } else {
71  return defaultValue;
72  }
73  } else if (it->second->isAtomic()) {
74  return std::static_pointer_cast<Atomic>(it->second)->stringForm();
75  }
76  return defaultValue;
77 }
78 
79 long OptionList::getLong(const std::string &key, long defaultValue) const {
80  auto it = options_.find(key);
81  if (it == options_.end()) {
82  return defaultValue;
83  } else if (it->second->isNumeric()) {
84  return std::static_pointer_cast<Numeric>(it->second)->asLong();
85  }
86  return defaultValue;
87 }
88 
89 std::optional<double> OptionList::getDouble(const std::string &key) const {
90  auto it = options_.find(key);
91  if (it == options_.end()) {
92  return std::nullopt;
93  } else if (it->second->isNumeric()) {
94  return std::static_pointer_cast<Numeric>(it->second)->asDouble();
95  }
96  return std::nullopt;
97 }
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
auto arity() const
Definition: Function.h:52
auto & functor() const
Definition: Function.h:42
static const AtomPtr & listFunctor()
Definition: ListTerm.cpp:17
void readOption(const TermPtr &option)
Definition: OptionList.cpp:29
const TermPtr & get(const std::string &key, const TermPtr &defaultValue) const
Definition: OptionList.cpp:53
std::string_view getString(const std::string &key, const std::string &defaultValue) const
Definition: OptionList.cpp:62
bool contains(const std::string &key) const
Definition: OptionList.cpp:49
OptionList(const TermPtr &t)
Definition: OptionList.cpp:13
std::optional< double > getDouble(const std::string &key) const
Definition: OptionList.cpp:89
long getLong(const std::string &key, long defaultValue) const
Definition: OptionList.cpp:79
std::map< std::string, TermPtr > options_
Definition: OptionList.h:70
TermRule & option()
Definition: terms.cpp:110
TermRule & string()
Definition: terms.cpp:63
std::shared_ptr< Term > TermPtr
Definition: Term.h:117