Fawkes API  Fawkes Development Version
clips-executive-rest-api.h
1 
2 /***************************************************************************
3  * clips-executive-rest-api.h - CLIPS Executive REST API
4  *
5  * Created: Fri Mar 16 17:15:34 2018
6  * Copyright 2006-2018 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #pragma once
23 
24 #include "model/DomainFact.h"
25 #include "model/DomainObject.h"
26 #include "model/DomainOperator.h"
27 #include "model/DomainPredicate.h"
28 #include "model/Goal.h"
29 #include "model/GroundedFormula.h"
30 #include "model/GroundedPDDLFormula.h"
31 #include "model/GroundedPDDLPredicate.h"
32 #include "model/PDDLFormula.h"
33 #include "model/PDDLGrounding.h"
34 #include "model/PDDLPredicate.h"
35 #include "model/Plan.h"
36 
37 #include <aspect/logging.h>
38 #include <aspect/webview.h>
39 #include <clipsmm/fact.h>
40 #include <core/threading/thread.h>
41 #include <core/utils/lockptr.h>
42 #include <plugins/clips/aspect/clips_manager.h>
43 #include <webview/rest_api.h>
44 #include <webview/rest_array.h>
45 
46 namespace CLIPS {
47 class Environment;
48 }
49 
51  public fawkes::LoggingAspect,
52  public fawkes::WebviewAspect,
54 {
55 public:
58 
59  virtual void init();
60  virtual void loop();
61  virtual void finalize();
62 
63 private:
64  typedef std::pair<std::string, std::string> PlanKey;
65  typedef std::tuple<std::string, std::string, int64_t> PlanActionKey;
66  typedef std::tuple<std::string, std::string> GroundedPDDLKey;
67  typedef std::list<CLIPS::Fact::pointer> ClipsFactList;
68  typedef std::map<PlanActionKey, ClipsFactList> PreCompoundMap;
69  typedef std::map<PlanActionKey, ClipsFactList> PreAtomMap;
70  typedef std::map<PlanKey, CLIPS::Fact::pointer> PlanMap;
71  typedef std::map<PlanKey, ClipsFactList> PlanActionMap;
72  typedef std::map<std::string, CLIPS::Fact::pointer> PDDLGroundingMap;
73  typedef std::map<std::string, CLIPS::Fact::pointer> PDDLFormulaMap;
74  typedef std::map<std::string, CLIPS::Fact::pointer> PDDLPredicateMap;
75  typedef std::map<std::string, ClipsFactList> GroundedPDDLFormulaMap;
76  typedef std::map<std::string, ClipsFactList> GroundedPDDLPredicateMap;
77  typedef std::tuple<CLIPS::Fact::pointer, CLIPS::Fact::pointer> PDDLFormulaTreeNode;
78  typedef std::list<PDDLFormulaTreeNode> PDDLFormulaTreeLevel;
79  typedef std::map<std::string, PDDLFormulaTreeLevel> PDDLFormulaTreeMap;
80  typedef std::map<std::string, std::shared_ptr<GroundedFormula>> GroundedFormulaMap;
81 
82 private:
83  WebviewRestArray<Goal> cb_list_goals();
84  WebviewRestArray<DomainOperator> cb_list_domain_operators();
85  WebviewRestArray<DomainObject> cb_list_domain_objects();
86  WebviewRestArray<DomainPredicate> cb_list_domain_predicates();
87  WebviewRestArray<DomainFact> cb_list_domain_facts();
88  WebviewRestArray<Plan> cb_list_plans();
89  WebviewRestArray<PDDLGrounding> cb_list_pddl_groundings();
90  WebviewRestArray<PDDLFormula> cb_list_pddl_formulas();
91  WebviewRestArray<PDDLPredicate> cb_list_pddl_predicates();
92  WebviewRestArray<GroundedPDDLFormula> cb_list_grounded_pddl_formulas();
93  WebviewRestArray<GroundedPDDLPredicate> cb_list_grounded_pddl_predicates();
94 
95  Goal cb_get_goal(fawkes::WebviewRestParams &params);
96  Plan cb_get_plan(fawkes::WebviewRestParams &params);
97  PDDLGrounding cb_get_pddl_groundings(fawkes::WebviewRestParams &params);
98  PDDLFormula cb_get_pddl_formulas(fawkes::WebviewRestParams &params);
99  PDDLPredicate cb_get_pddl_predicates(fawkes::WebviewRestParams &params);
100  GroundedPDDLFormula cb_get_grounded_pddl_formulas(fawkes::WebviewRestParams &params);
101  GroundedPDDLPredicate cb_get_grounded_pddl_predicates(fawkes::WebviewRestParams &params);
102 
103  Goal generate_goal(CLIPS::Fact::pointer fact);
104  void gen_plan_precompute(std::map<PlanKey, CLIPS::Fact::pointer> &plans,
105  std::map<PlanKey, ClipsFactList> & plan_actions,
106  PreCompoundMap & prec,
107  PreAtomMap & prea,
108  PDDLGroundingMap & pgm,
109  PDDLFormulaMap & pfm,
110  PDDLPredicateMap & ppm,
111  GroundedPDDLFormulaMap & gpfm,
112  GroundedPDDLPredicateMap & gppm);
113  GroundedFormula gen_plan_compute_precons(PDDLFormulaTreeNode node,
114  PDDLFormulaTreeMap tree,
115  PDDLGroundingMap groundings);
116 
117  Plan gen_plan(const PlanKey & plan_key,
118  const CLIPS::Fact::pointer fact,
119  PlanActionMap & plan_actions,
120  PreCompoundMap & prec,
121  PreAtomMap & prea,
122  PDDLGroundingMap & pgm,
123  PDDLFormulaMap & pfm,
124  PDDLPredicateMap & ppm,
125  GroundedPDDLFormulaMap & gpfm,
126  GroundedPDDLPredicateMap & gppm);
127 
128  std::shared_ptr<PDDLGrounding> gen_pddl_grounding(const CLIPS::Fact::pointer fact);
129 
130  std::shared_ptr<PDDLFormula> gen_pddl_formula(const CLIPS::Fact::pointer fact);
131 
132  std::shared_ptr<PDDLPredicate> gen_pddl_predicate(const CLIPS::Fact::pointer fact);
133 
134  std::shared_ptr<GroundedPDDLFormula> gen_grounded_pddl_formula(const CLIPS::Fact::pointer fact);
135 
136  std::shared_ptr<GroundedPDDLPredicate>
137  gen_grounded_pddl_predicate(const CLIPS::Fact::pointer fact);
138 
139 private:
140  fawkes::WebviewRestApi * rest_api_;
142 };
REST API backend for the CLIPS executive.
virtual void finalize()
Finalize the thread.
virtual void init()
Initialize the thread.
virtual void loop()
Code to execute in the thread.
Environment representation for JSON transfer.
Definition: Environment.h:28
Goal representation for JSON transfer.
Definition: Goal.h:28
GroundedFormula representation for JSON transfer.
GroundedPDDLFormula representation for JSON transfer.
GroundedPDDLPredicate representation for JSON transfer.
PDDLFormula representation for JSON transfer.
Definition: PDDLFormula.h:28
PDDLGrounding representation for JSON transfer.
Definition: PDDLGrounding.h:28
PDDLPredicate representation for JSON transfer.
Definition: PDDLPredicate.h:28
Plan representation for JSON transfer.
Definition: Plan.h:29
Container to return array via REST.
Definition: rest_array.h:36
Thread aspect access the CLIPS environment manager.
Definition: clips_manager.h:36
Thread aspect to log output.
Definition: logging.h:33
Thread class encapsulation of pthreads.
Definition: thread.h:46
Thread aspect to provide web pages via Webview.
Definition: webview.h:37
Webview REST API component.
Definition: rest_api.h:221
REST parameters to pass to handlers.
Definition: rest_api.h:125