knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
ListTerm.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 <gtest/gtest.h>
7 #include "knowrob/terms/ListTerm.h"
8 #include "knowrob/terms/String.h"
9 #include "knowrob/integration/python/utils.h"
10 
11 using namespace knowrob;
12 
13 ListTerm::ListTerm(const std::vector<TermPtr> &elements)
14  : Function(listFunctor(), elements) {
15 }
16 
18  static const AtomPtr fun = Atom::Tabled("[]");
19  return fun;
20 }
21 
22 std::shared_ptr<ListTerm> ListTerm::nil() {
23  static std::shared_ptr<ListTerm> x(new ListTerm(
24  std::vector<std::shared_ptr<Term>>(0)));
25  return x;
26 }
27 
28 bool ListTerm::isNIL() const {
29  return arguments_.empty();
30 }
31 
32 void ListTerm::write(std::ostream &os) const {
33  os << '[';
34  for (uint32_t i = 0; i < arguments_.size(); i++) {
35  os << *arguments_[i];
36  if (i + 1 < arguments_.size()) {
37  os << ',' << ' ';
38  }
39  }
40  os << ']';
41 }
42 
43 namespace knowrob::py {
44  template<>
46  using namespace boost::python;
47  class_<ListTerm, std::shared_ptr<ListTerm>, bases<Function>>
48  ("ListTerm", init<const std::vector<TermPtr> &>())
49  .def("__iter__", range(&ListTerm::begin, &ListTerm::end))
50  .def("isNIL", &ListTerm::isNIL)
51  .def("elements", &ListTerm::elements, return_value_policy<copy_const_reference>());
52  }
53 }
54 
55 /******************************************/
56 /************** Unit Tests ****************/
57 /******************************************/
58 
59 TEST(list_term, NIL) {
60  EXPECT_TRUE(ListTerm::nil()->elements().empty());
61  EXPECT_TRUE(ListTerm({}).elements().empty());
62  EXPECT_TRUE(ListTerm({}).isNIL());
63  EXPECT_FALSE(ListTerm::nil()->isAtomic());
64  EXPECT_TRUE(ListTerm::nil()->isGround());
65 }
66 
67 TEST(list_term, lists) {
68  auto x = std::make_shared<String>("x");
69  auto y = std::make_shared<String>("y");
70  EXPECT_TRUE(ListTerm({x}).isGround());
71  EXPECT_FALSE(ListTerm({x}).isAtomic());
72  EXPECT_FALSE(ListTerm({x}).elements().empty());
73  EXPECT_EQ(ListTerm({x, y}).elements()[0], x);
74  EXPECT_EQ(ListTerm({x, y}).elements()[1], y);
75 }
TEST(list_term, NIL)
Definition: ListTerm.cpp:59
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
const std::vector< TermPtr > arguments_
Definition: Function.h:67
auto & elements() const
Definition: ListTerm.h:39
bool isNIL() const
Definition: ListTerm.cpp:28
ListTerm(const std::vector< TermPtr > &elements)
Definition: ListTerm.cpp:13
void write(std::ostream &os) const override
Definition: ListTerm.cpp:32
static std::shared_ptr< ListTerm > nil()
Definition: ListTerm.cpp:22
static const AtomPtr & listFunctor()
Definition: ListTerm.cpp:17
void createType< ListTerm >()
Definition: ListTerm.cpp:45
const IRIAtomPtr range
Definition: rdfs.h:20
std::shared_ptr< Atom > AtomPtr
Definition: Atom.h:69