knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::OptionList Class Reference

#include <OptionList.h>

Public Member Functions

 OptionList (const TermPtr &t)
 
const std::map< std::string, TermPtr > & options () const
 
bool contains (const std::string &key) const
 
const TermPtrget (const std::string &key, const TermPtr &defaultValue) const
 
std::string_view getString (const std::string &key, const std::string &defaultValue) const
 
long getLong (const std::string &key, long defaultValue) const
 
std::optional< double > getDouble (const std::string &key) const
 
 OptionList (const TermPtr &t)
 
const std::map< std::string, TermPtr > & options () const
 
bool contains (const std::string &key) const
 
const TermPtrget (const std::string &key, const TermPtr &defaultValue) const
 
std::string_view getString (const std::string &key, const std::string &defaultValue) const
 
long getLong (const std::string &key, long defaultValue) const
 
std::optional< double > getDouble (const std::string &key) const
 

Protected Member Functions

void readOption (const TermPtr &option)
 
void readOption (const TermPtr &option)
 

Protected Attributes

std::map< std::string, TermPtroptions_
 

Detailed Description

A list of options, where each option is represented as a term.

Definition at line 18 of file OptionList.h.

Constructor & Destructor Documentation

◆ OptionList() [1/2]

OptionList::OptionList ( const TermPtr t)
explicit

Constructs an option list from a term. The term may be a list of options, or a single option value. Option terms have either the form Key = Value or Key(Value).

Parameters
ta term from which options are read.

Definition at line 13 of file OptionList.cpp.

13  {
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 }
static const AtomPtr & listFunctor()
Definition: ListTerm.cpp:17
void readOption(const TermPtr &option)
Definition: OptionList.cpp:29

◆ OptionList() [2/2]

knowrob::OptionList::OptionList ( const TermPtr t)
explicit

Constructs an option list from a term. The term may be a list of options, or a single option value. Option terms have either the form Key = Value or Key(Value).

Parameters
ta term from which options are read.

Member Function Documentation

◆ contains() [1/2]

bool OptionList::contains ( const std::string &  key) const
Parameters
keykey of option.
Returns
true is this list isMoreGeneralThan the key.

Definition at line 49 of file OptionList.cpp.

49  {
50  return options_.count(key) > 0;
51 }
std::map< std::string, TermPtr > options_
Definition: OptionList.h:70

◆ contains() [2/2]

bool knowrob::OptionList::contains ( const std::string &  key) const
Parameters
keykey of option.
Returns
true is this list isMoreGeneralThan the key.

◆ get() [1/2]

const TermPtr & OptionList::get ( const std::string &  key,
const TermPtr defaultValue 
) const
Parameters
keyan option key
defaultValuea default value
Returns
the option value, or the default value

Definition at line 53 of file OptionList.cpp.

53  {
54  auto it = options_.find(key);
55  if (it == options_.end()) {
56  return defaultValue;
57  } else {
58  return it->second;
59  }
60 }

◆ get() [2/2]

const TermPtr& knowrob::OptionList::get ( const std::string &  key,
const TermPtr defaultValue 
) const
Parameters
keyan option key
defaultValuea default value
Returns
the option value, or the default value

◆ getDouble() [1/2]

std::optional< double > OptionList::getDouble ( const std::string &  key) const

Read option value as a double.

Parameters
keyan option key
Returns
the option value, or std::nullopt

Definition at line 89 of file OptionList.cpp.

89  {
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 }

◆ getDouble() [2/2]

std::optional<double> knowrob::OptionList::getDouble ( const std::string &  key) const

Read option value as a double.

Parameters
keyan option key
Returns
the option value, or std::nullopt

◆ getLong() [1/2]

long OptionList::getLong ( const std::string &  key,
long  defaultValue 
) const

Read option value as a long.

Parameters
keyan option key
defaultValuea default value
Returns
the option value, or the default value

Definition at line 79 of file OptionList.cpp.

79  {
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 }

◆ getLong() [2/2]

long knowrob::OptionList::getLong ( const std::string &  key,
long  defaultValue 
) const

Read option value as a long.

Parameters
keyan option key
defaultValuea default value
Returns
the option value, or the default value

◆ getString() [1/2]

std::string_view OptionList::getString ( const std::string &  key,
const std::string &  defaultValue 
) const

Read option value as a string.

Parameters
keyan option key
defaultValuea default value
Returns
the option value, or the default value

Definition at line 62 of file OptionList.cpp.

62  {
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 }
auto arity() const
Definition: Function.h:52
auto & functor() const
Definition: Function.h:42

◆ getString() [2/2]

std::string_view knowrob::OptionList::getString ( const std::string &  key,
const std::string &  defaultValue 
) const

Read option value as a string.

Parameters
keyan option key
defaultValuea default value
Returns
the option value, or the default value

◆ options() [1/2]

const std::map<std::string, TermPtr>& knowrob::OptionList::options ( ) const
inline
Returns
the option map.

Definition at line 31 of file OptionList.h.

31 { return options_; }

◆ options() [2/2]

const std::map<std::string, TermPtr>& knowrob::OptionList::options ( ) const
inline
Returns
the option map.

Definition at line 31 of file OptionList.h.

31 { return options_; }

◆ readOption() [1/2]

void OptionList::readOption ( const TermPtr option)
protected

Definition at line 29 of file OptionList.cpp.

29  {
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 }
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
TermRule & option()
Definition: terms.cpp:110
TermRule & string()
Definition: terms.cpp:63

◆ readOption() [2/2]

void knowrob::OptionList::readOption ( const TermPtr option)
protected

Member Data Documentation

◆ options_

std::map< std::string, TermPtr > knowrob::OptionList::options_
protected

Definition at line 70 of file OptionList.h.


The documentation for this class was generated from the following files: