Fawkes API  Fawkes Development Version
clips_test.h
1 /***************************************************************************
2  * clips_test.h - Unit Tests with CLIPS
3  *
4  * Created: Tue 19 Sep 2017 13:40:37 CEST 13:40
5  * Copyright 2017 Till Hofmann <hofmann@kbsg.rwth-aachen.de>
6  ****************************************************************************/
7 
8 /* This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Library General Public License for more details.
17  *
18  * Read the full text in the LICENSE.GPL file in the doc directory.
19  */
20 
21 #include <gtest/gtest.h>
22 
23 #include <clipsmm.h>
24 #include <string>
25 #include <vector>
26 
27 #ifndef TESTDIR
28 # define TESTDIR "."
29 #endif
30 
31 /** Base class for unit testing with CLIPS.
32  * To define your own test setup, derive from this class.
33  * @see SimpleCLIPSTest
34  */
35 class CLIPSTest : public ::testing::Test
36 {
37 protected:
38  /** The default CLIPS environment used to run tests. */
39  CLIPS::Environment env;
40  /** Load the vector of CLIPS files into the environment.
41  * @param files A vector of paths relative to the current directory. */
42  virtual void
43  LoadCLIPSFiles(std::vector<std::string> files)
44  {
45  for (auto &file : files) {
46  std::cout << "[ ] loading file " << file << std::endl;
47  const std::string path = std::string(TESTDIR) + "/" + file;
48  env.evaluate("(load* " + path + ")");
49  }
50  }
51  /** Check if a non-ordered fact exists.
52  * This expects the same arguments as CLIPS' any-factp.
53  * @param fact_set_template The fact set template of the query,
54  * e.g., "((?a action ?p parameters))".
55  * @param query The constraints that must be satisfied by the fact set,
56  * e.g., "(eq ?a:param-values ?p:values)".
57  * @return true iff the fact exists.
58  */
59  bool
60  has_fact(const std::string &fact_set_template, const std::string &query = "TRUE")
61  {
62  const std::string &clips_query = "(any-factp " + fact_set_template + " " + query + ")";
63  return env.evaluate(clips_query)[0].as_string() == "TRUE";
64  }
65  /** Check if an ordered fact exists.
66  * @param fact_name The name of the fact, e.g., "foo".
67  * @param slot_values A vector of slot values, e.g., '{ "bar" }'.
68  * @return true iff the specified ordered fact exists.
69  */
70  bool
71  has_ordered_fact(const std::string &fact_name, const std::vector<CLIPS::Value> slot_values = {})
72  {
73  const std::string fact_set_template = "((?f " + fact_name + "))";
74  std::string query = "(eq ?f:implied (create$";
75  for (CLIPS::Value slot_val : slot_values) {
76  switch (slot_val.type()) {
77  case CLIPS::TYPE_FLOAT: query += " " + std::to_string(slot_val.as_float()); break;
78  case CLIPS::TYPE_INTEGER: query += " " + std::to_string(slot_val.as_integer()); break;
79  case CLIPS::TYPE_SYMBOL:
80  case CLIPS::TYPE_STRING:
81  default:
82  // This probably breaks for some other types.
83  query += " " + slot_val.as_string();
84  break;
85  }
86  }
87  query += "))";
88  return has_fact(fact_set_template, query);
89  }
90 };
CLIPSTest::has_fact
bool has_fact(const std::string &fact_set_template, const std::string &query="TRUE")
Check if a non-ordered fact exists.
Definition: clips_test.h:60
CLIPSTest::has_ordered_fact
bool has_ordered_fact(const std::string &fact_name, const std::vector< CLIPS::Value > slot_values={})
Check if an ordered fact exists.
Definition: clips_test.h:71
CLIPSTest
Base class for unit testing with CLIPS.
Definition: clips_test.h:35
CLIPSTest::LoadCLIPSFiles
virtual void LoadCLIPSFiles(std::vector< std::string > files)
Load the vector of CLIPS files into the environment.
Definition: clips_test.h:43
CLIPSTest::env
CLIPS::Environment env
The default CLIPS environment used to run tests.
Definition: clips_test.h:39