Minsky
pyminsky.cc
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2023
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 // TODO figure out how to switch buffer types... For now, use JSON
21 //#define REST_PROCESS_BUFFER classdesc::PythonBuffer
22 #include "minsky.h"
23 #include "minsky_epilogue.h"
24 #include "pythonBuffer.h"
25 
26 namespace pyminsky
27 {
30 
31  minsky::Item& findObject(const std::string& typeName)
32  {
33  using namespace minsky;
34  auto& canvas = minsky.canvas;
35 
36  canvas.item.reset(); // Reset the item to ensure no leftover references
37 
38  bool found = false;
39 
40  if (typeName == "Group" && !canvas.model->groups.empty())
41  {
42  canvas.item = canvas.model->groups.front();
43  found = true;
44  }
45  else
46  {
47  found = minsky.model->recursiveDo(&GroupItems::items, [&](const Items&, Items::const_iterator i)
48  {
49  if ((*i)->classType() == typeName)
50  {
51  canvas.item = *i;
52  return true; // Stop recursion
53  }
54  return false;
55  });
56  }
57 
58  // Check if an object was found
59  if (!found || !canvas.item)
60  {
61  // std::cerr << "findObject: Object of type '" << typeName << "' not found or invalid!" << std::endl;
62  throw std::runtime_error("Object not found");
63  }
64 
65  return *canvas.item; // Return the dereferenced item
66  }
67 
68  // Find a variable by its name
69  minsky::Item& findVariable(const std::string& name)
70  {
71  using namespace minsky;
72  auto& canvas = minsky.canvas;
73 
74  canvas.item.reset(); // Reset item to ensure clean start
75 
76  bool found = minsky.model->recursiveDo(
77  &GroupItems::items,
78  [&](const Items&, Items::const_iterator i) {
79  if (auto v = dynamic_cast<VariableBase*>(i->get()))
80  {
81  if (v->name() == name)
82  {
83  canvas.item = *i;
84  return true; // Stop recursion
85  }
86  }
87  return false;
88  });
89 
90  if (!found || !canvas.item)
91  {
92  // std::cerr << "findVariable: Variable '" << name << "' not found or invalid!" << std::endl;
93  throw std::runtime_error("Variable not found");
94  }
95 
96  return *canvas.item; // Return the dereferenced item
97  }
100 }
101 
103 
104 namespace minsky
105 {
106  LocalMinsky::LocalMinsky(Minsky&) {}
107  LocalMinsky::~LocalMinsky() {}
108  // GUI callback needed only to solve linkage problems
109  void doOneEvent(bool) {}
110  Minsky& minsky() {return pyminsky::minsky;}
111 }
void doOneEvent(bool idletasksOnly)
checks if any GUI events are waiting, and proces an event if so
Definition: tclmain.cc:161
minsky::Item & findVariable(const std::string &name)
Definition: pyminsky.cc:69
minsky::Minsky minsky
Definition: pyminsky.cc:28
CLASSDESC_PYTHON_MODULE(pyminsky)
minsky::Item & findObject(const std::string &typeName)
Definition: pyminsky.cc:31
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
std::vector< ItemPtr > Items
Definition: item.h:366
CLASSDESC_ADD_GLOBAL(minsky)
CLASSDESC_ADD_FUNCTION(findObject)