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

#include <InterfaceUtils.h>

Static Public Member Functions

static boost::property_tree::ptree loadSettings ()
 
static bool assertStatements (const KnowledgeBasePtr &kb, const std::vector< FormulaPtr > &args)
 
static FormulaPtr applyModality (const std::unordered_map< std::string, boost::any > &options, FormulaPtr phi)
 
static boost::property_tree::ptree loadSettings ()
 
static bool assertStatements (const KnowledgeBasePtr &kb, const std::vector< FormulaPtr > &args)
 
static FormulaPtr applyModality (const std::unordered_map< std::string, boost::any > &options, FormulaPtr phi)
 

Detailed Description

Utility function for interfaces

Definition at line 19 of file InterfaceUtils.h.

Member Function Documentation

◆ applyModality() [1/2]

FormulaPtr InterfaceUtils::applyModality ( const std::unordered_map< std::string, boost::any > &  options,
FormulaPtr  phi 
)
static

Apply a modality to a formula.

Parameters
optionsthe options.
phithe formula.
Returns
the formula with the modality applied.

Definition at line 90 of file InterfaceUtils.cpp.

91  {
92  FormulaPtr mFormula = std::move(phi);
93 
94  // Retrieve epistemicOperator and check if it is "BELIEF"
95  auto epistemicOperator = boost::any_cast<int>(options.at("epistemicOperator"));
96  if (epistemicOperator == EpistemicOperator::BELIEF) {
97  // Retrieve aboutAgentIRI and confidence
98  auto aboutAgentIRI = boost::any_cast<std::string>(options.at("aboutAgentIRI"));
99  auto confidence = boost::any_cast<double>(options.at("confidence"));
100  if (!aboutAgentIRI.empty()) {
101  if (confidence != 1.0) {
102  mFormula = std::make_shared<ModalFormula>(
103  modals::B(aboutAgentIRI, confidence), mFormula);
104  } else {
105  mFormula = std::make_shared<ModalFormula>(
106  modals::B(aboutAgentIRI), mFormula);
107  }
108  }
109  } else if (epistemicOperator == EpistemicOperator::KNOWLEDGE) {
110  // Retrieve aboutAgentIRI
111  auto aboutAgentIRI = boost::any_cast<std::string>(options.at("aboutAgentIRI"));
112  if (!aboutAgentIRI.empty()) {
113  mFormula = std::make_shared<ModalFormula>(
114  modals::K(aboutAgentIRI), mFormula);
115  }
116  }
117  // Retrieve temporalOperator
118  auto temporalOperator = boost::any_cast<int>(options.at("temporalOperator"));
119 
120  // Retrieve minPastTimestamp and maxPastTimestamp
121  auto minPastTimestamp = boost::any_cast<double>(options.at("minPastTimestamp"));
122  auto maxPastTimestamp = boost::any_cast<double>(options.at("maxPastTimestamp"));
123 
124  auto minPastTimePoint =
125  minPastTimestamp != -1 ? std::optional<TimePoint>(knowrob::time::fromSeconds(minPastTimestamp))
126  : std::nullopt;
127  auto maxPastTimePoint =
128  maxPastTimestamp != -1 ? std::optional<TimePoint>(knowrob::time::fromSeconds(maxPastTimestamp))
129  : std::nullopt;
130 
131  if (temporalOperator == TemporalOperator::SOME_PAST) {
132  if (minPastTimestamp != -1 || maxPastTimestamp != -1) {
133  if (minPastTimestamp == -1) {
134  mFormula = std::make_shared<ModalFormula>(
135  modals::P(TimeInterval(std::nullopt,
136  maxPastTimePoint)), mFormula);
137  } else if (maxPastTimestamp == -1) {
138  mFormula = std::make_shared<ModalFormula>(
139  modals::P(TimeInterval(minPastTimePoint,
140  std::nullopt)), mFormula);
141  } else {
142  mFormula = std::make_shared<ModalFormula>(
143  modals::P(TimeInterval(minPastTimePoint,
144  maxPastTimePoint)), mFormula);
145  }
146  } else {
147  mFormula = std::make_shared<ModalFormula>(
148  modals::P(), mFormula);
149  }
150  } else if (temporalOperator == TemporalOperator::ALL_PAST) {
151  if (minPastTimestamp != -1 || maxPastTimestamp != -1) {
152  if (minPastTimestamp == -1) {
153  mFormula = std::make_shared<ModalFormula>(
154  modals::H(TimeInterval(std::nullopt,
155  maxPastTimePoint)), mFormula);
156  } else if (maxPastTimestamp == -1) {
157  mFormula = std::make_shared<ModalFormula>(
158  modals::H(TimeInterval(minPastTimePoint,
159  std::nullopt)), mFormula);
160  } else {
161  mFormula = std::make_shared<ModalFormula>(
162  modals::H(TimeInterval(minPastTimePoint,
163  maxPastTimePoint)), mFormula);
164  }
165  } else {
166  mFormula = std::make_shared<ModalFormula>(
167  modals::H(), mFormula);
168  }
169  }
170  return mFormula;
171 }
@ SOME_PAST
@ ALL_PAST
@ KNOWLEDGE
@ BELIEF
std::shared_ptr< ModalFormula > K(const FormulaPtr &phi)
std::shared_ptr< ModalFormula > P(const FormulaPtr &phi)
std::shared_ptr< ModalFormula > B(const FormulaPtr &phi)
std::shared_ptr< ModalFormula > H(const FormulaPtr &phi)
TermRule & options()
Definition: terms.cpp:114
TimePoint fromSeconds(double seconds)
Definition: TimePoint.cpp:17
std::shared_ptr< Formula > FormulaPtr
Definition: Formula.h:99

◆ applyModality() [2/2]

static FormulaPtr knowrob::InterfaceUtils::applyModality ( const std::unordered_map< std::string, boost::any > &  options,
FormulaPtr  phi 
)
static

Apply a modality to a formula.

Parameters
optionsthe options.
phithe formula.
Returns
the formula with the modality applied.

◆ assertStatements() [1/2]

bool InterfaceUtils::assertStatements ( const KnowledgeBasePtr kb,
const std::vector< FormulaPtr > &  args 
)
static

Assert a list of statements.

Parameters
kbthe knowledge base.
argsthe statements.
Returns
true if all statements are true.

Definition at line 52 of file InterfaceUtils.cpp.

52  {
53  std::vector<TriplePtr> data(args.size());
54  std::vector<TriplePatternPtr> buf(args.size());
55  uint32_t dataIndex = 0;
56 
57  for (auto &phi: args) {
58  const QueryTree qt(phi);
59  if (qt.numPaths() > 1) {
60  throw QueryError("Disjunctions are not allowed in assertions. "
61  "Appears in statement {}.", *phi);
62  } else if (qt.numPaths() == 0) {
63  throw QueryError("Invalid assertion: '{}'", *phi);
64  }
65  for (auto &psi: qt.begin()->nodes()) {
66  switch (psi->type()) {
68  buf[dataIndex] = std::make_shared<TriplePattern>(
69  std::static_pointer_cast<Predicate>(psi), false);
70  data[dataIndex].ptr = new TripleCopy();
71  data[dataIndex].owned = true;
72  buf[dataIndex]->instantiateInto(*data[dataIndex].ptr);
73  dataIndex += 1;
74  break;
75  default:
76  throw QueryError("Invalid assertion: '{}'", *phi);
77  }
78  }
79  }
80  if (kb_->insertAll(data)) {
81  std::cout << "success, " << dataIndex << " statement(s) were asserted." << "\n";
82  return true;
83  } else {
84  std::cout << "assertion failed." << "\n";
85  return false;
86  }
87 }
TripleTemplate< std::string > TripleCopy
Definition: Triple.h:577

◆ assertStatements() [2/2]

static bool knowrob::InterfaceUtils::assertStatements ( const KnowledgeBasePtr kb,
const std::vector< FormulaPtr > &  args 
)
static

Assert a list of statements.

Parameters
kbthe knowledge base.
argsthe statements.
Returns
true if all statements are true.

◆ loadSettings() [1/2]

boost::property_tree::ptree InterfaceUtils::loadSettings ( )
static

Load the settings from the default file or the file specified in the environment variable KNOWROB_SETTINGS.

Returns
the settings.

Definition at line 35 of file InterfaceUtils.cpp.

35  {
36  // Check for settings file
37  std::string config_path = "default.json";
38  if (std::getenv("KNOWROB_SETTINGS")) {
39  config_path = std::getenv("KNOWROB_SETTINGS");
40  }
41 
42  // read the settings
43  boost::property_tree::ptree config;
44  boost::property_tree::read_json(
45  config_path,
46  config);
47 
48  return config;
49 
50 }
TermRule & string()
Definition: terms.cpp:63

◆ loadSettings() [2/2]

static boost::property_tree::ptree knowrob::InterfaceUtils::loadSettings ( )
static

Load the settings from the default file or the file specified in the environment variable KNOWROB_SETTINGS.

Returns
the settings.

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