Minsky
variableValues.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2021
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef VARIABLEVALUES_H
21 #define VARIABLEVALUES_H
22 
23 #include "variableValue.h"
24 #include "constMap.h"
25 
26 namespace ecolab
27 {
28  // for TCL_obj processing
29  template <> struct is_map<minsky::ConstMap<std::string, minsky::VariableValue> >: public is_map_map {};
30 }
31 
32 namespace minsky
33 {
34  class Group;
35  typedef std::shared_ptr<Group> GroupPtr;
37  struct VariableValuePtr: public std::shared_ptr<VariableValue>
38  {
40  std::shared_ptr<VariableValue>(std::make_shared<VariableValue>(type,name))
41  {get()->m_scope=scope(group,name);}
42  VariableValuePtr(VariableType::Type type, const VariableValue& vv): std::shared_ptr<VariableValue>(std::make_shared<VariableValue>(type,vv)) {}
43  VariableValuePtr(const std::shared_ptr<VariableValue>& x): std::shared_ptr<VariableValue>(x) {assert(x);}
44  };
45 
46  class VariableValues: public ConstMap<std::string, VariableValuePtr>
47  {
48  static VariableValuePtr& zero();
49  static VariableValuePtr& one();
51  public:
53  void clear() {
55  // add special values for zero and one, used for the derivative
56  // operator in SystemOfEquations
57  emplace("constant:zero", zero());
58  emplace("constant:one", one());
59  }
61  std::string newName(const std::string& name) const;
63  void reset();
65  void resetValue(VariableValue&) const;
69  TensorVal initValue
70  (const VariableValue&, std::set<std::string>& visited) const;
71  TensorVal initValue(const VariableValue& v) const {
72  std::set<std::string> visited;
73  return initValue(v, visited);
74  }
75 
76  std::vector<Summary> summarise() const {
77  std::vector<Summary> summary;
78  for (auto& v: *this)
79  summary.emplace_back(v.second->summary());
80  return summary;
81  }
82 
84  bool validEntries() const;
85  void resetUnitsCache() {
86  for (auto& i: *this)
87  i.second->unitsCached=false;
88  }
89  };
90 }
91 
92 namespace classdesc
93 {
94  template <>
95  struct is_associative_container<minsky::ConstMap<std::string, minsky::VariableValue> >: public classdesc::true_type {};
96  // allows for RESTProcess to recognise this as a smart pointer, and to process the target
97  template <> struct is_smart_ptr<minsky::VariableValuePtr>: public true_type {};
98 }
99 
100 #include "variableValues.cd"
101 #endif
std::string newName(const std::string &name) const
generate a new valueId not otherwise in the system
TensorVal initValue(const VariableValue &, std::set< std::string > &visited) const
evaluates the initial value of a given variableValue in the context given by this. visited is used to check for circular definitions
std::vector< Summary > summarise() const
bool validEntries() const
checks that all entry names are valid
void resetValue(VariableValue &) const
reset a give variable value to it&#39;s initial condition, in this context
void reset()
reset all variableValues to their initial conditions
STL namespace.
size_t scope(const string &name)
extract scope from a qualified variable name
Definition: valueId.cc:83
a shared_ptr that default constructs a default target, and is always valid
VariableValuePtr(VariableType::Type type, const VariableValue &vv)
TensorVal initValue(const VariableValue &v) const
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
static VariableValuePtr & one()
std::shared_ptr< Group > GroupPtr
Definition: port.h:32
VariableValuePtr(VariableType::Type type=VariableType::undefined, const std::string &name="", const GroupPtr &group=GroupPtr())
Definition: group.tcl:84
VariableValuePtr(const std::shared_ptr< VariableValue > &x)
static VariableValuePtr & zero()
CLASSDESC_ACCESS(VariableValues)