Minsky
port.cc
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2015
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 #include "cairoItems.h"
21 #include "port.h"
22 #include "item.h"
23 #include "wire.h"
24 #include "group.h"
25 #include "selection.h"
26 #include "lasso.h"
27 #include "operation.h"
28 #include "plotWidget.h"
29 #include "SVGItem.h"
30 #include "switchIcon.h"
31 #include "port.rcd"
32 #include "port.xcd"
33 #include "minsky_epilogue.h"
34 using namespace std;
35 
36 namespace minsky
37 {
38 
39  float Port::x() const
40  {
41  return m_x+item().x();
42  }
43 
44  float Port::y() const
45  {
46  return m_y+item().y();
47  }
48 
49  void Port::moveTo(float x, float y)
50  {
51  m_x=x-item().x();
52  m_y=y-item().y();
53  }
54 
56  {
57  return m_item.group.lock();
58  }
59 
60  void Port::eraseWire(Wire* w)
61  {
62  for (auto i=m_wires.begin(); i!=m_wires.end(); ++i)
63  if (*i==w)
64  {
65  m_wires.erase(i);
66  break;
67  }
68  }
69 
70  void Port::deleteWires()
71  {
73  if (auto g=item().group.lock())
74  {
75  auto& gg=g->globalGroup();
76  auto wires=m_wires; // save copy, as Group::removeWire mutates it
77  vector<WirePtr> wireHold; // postpone actual wire destruction until after loop
78  wireHold.reserve(wires.size());
79  for (auto& w: wires)
80  wireHold.push_back(gg.removeWire(*w));
81  }
82  m_wires.clear();
83  }
84 
86  void PortExclude::setVariableValue(const std::shared_ptr<VariableValue>& v) {
87  if (!input())
88  variableValue=v;
89  }
90 
92  double Port::value() const {
93  if (input())
94  {
95  double r=identity();
96  for (auto* w: m_wires)
97  combineInput(r, w->from()->value());
98  return r;
99  }
100  auto vv=getVariableValue();
101  if (vv && vv->type()!=VariableType::undefined)
102  return vv->value();
103  return item().value();
104  }
105 
106 
107  Units Port::units(bool check) const
108  {
109  if (!wires().empty())
110  return wires()[0]->units(check);
111  return {};
112  }
113 
114  shared_ptr<VariableValue> PortExclude::getVariableValue() const {
115  if (input() && !m_wires.empty())
116  return m_wires[0]->from()->getVariableValue();
117  return variableValue.lock();
118  }
119 
120 }
121 
Definition: input.py:1
STL namespace.
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
represents the units (in sense of dimensional analysis) of a variable.
Definition: units.h:34
std::shared_ptr< Group > GroupPtr
Definition: port.h:32
CLASSDESC_ACCESS_EXPLICIT_INSTANTIATION(minsky::Port)
Definition: group.tcl:84