Minsky: 3.17.0
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 minsky
27 {
28  class Group;
29  typedef std::shared_ptr<Group> GroupPtr;
31  struct VariableValuePtr: public std::shared_ptr<VariableValue>
32  {
34  std::shared_ptr<VariableValue>(std::make_shared<VariableValue>(type,name))
35  {get()->m_scope=scope(group,name);}
36  VariableValuePtr(VariableType::Type type, const VariableValue& vv): std::shared_ptr<VariableValue>(std::make_shared<VariableValue>(type,vv)) {}
37  VariableValuePtr(const std::shared_ptr<VariableValue>& x): std::shared_ptr<VariableValue>(x) {assert(x);}
38  };
39 
40  class VariableValues: public ConstMap<std::string, VariableValuePtr>
41  {
42  static VariableValuePtr& zero();
43  static VariableValuePtr& one();
45  public:
47  void clear() {
49  // add special values for zero and one, used for the derivative
50  // operator in SystemOfEquations
51  emplace("constant:zero", zero());
52  emplace("constant:one", one());
53  }
55  std::string newName(const std::string& name) const;
57  void reset();
59  void resetValue(VariableValue&) const;
63  TensorVal initValue
64  (const VariableValue&, std::set<std::string>& visited) const;
65  TensorVal initValue(const VariableValue& v) const {
66  std::set<std::string> visited;
67  return initValue(v, visited);
68  }
69 
70  std::vector<Summary> summarise() const {
71  std::vector<Summary> summary;
72  for (auto& v: *this)
73  summary.emplace_back(v.second->summary());
74  return summary;
75  }
76 
78  bool validEntries() const;
79  void resetUnitsCache() {
80  for (auto& i: *this)
81  i.second->unitsCached=false;
82  }
83  };
84 }
85 
86 namespace classdesc
87 {
88  template <>
89  struct is_associative_container<minsky::ConstMap<std::string, minsky::VariableValue> >: public classdesc::true_type {};
90  // allows for RESTProcess to recognise this as a smart pointer, and to process the target
91  template <> struct is_smart_ptr<minsky::VariableValuePtr>: public true_type {};
92 }
93 
94 #include "variableValues.cd"
95 #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
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())
VariableValuePtr(const std::shared_ptr< VariableValue > &x)
static VariableValuePtr & zero()
CLASSDESC_ACCESS(VariableValues)