Minsky
minsky::UserFunction Class Reference

#include <userFunction.h>

Inheritance diagram for minsky::UserFunction:
Inheritance graph
Collaboration diagram for minsky::UserFunction:
Collaboration graph

Classes

struct  Impl
 

Public Member Functions

 UserFunction ()
 
 UserFunction (const std::string &name, const std::string &expression="")
 
std::vector< std::string > symbolNames () const
 
void compile ()
 
double evaluate (double x, double y)
 
double operator() (const std::vector< double > &p) override
 evaluate function on arbitrary number of arguments (exprtk support) More...
 
Units units (bool check=false) const override
 compute the dimensional units More...
 
void displayTooltip (cairo_t *cr, const std::string &tt) const override
 display tooltip text, eg on mouseover More...
 
std::string description (const std::string &) override
 name of the associated data operation More...
 
std::string name () const override
 function name, shorn of argument decorators More...
 
void draw (cairo_t *cairo) const override
 draw this item into a cairo context More...
 
virtual std::string description () const
 name of the associated data operation More...
 
virtual std::string description (const std::string &)
 
- Public Member Functions inherited from minsky::ItemT< UserFunction, Operation< OperationType::userFunction > >
std::string classType () const override
 
ItemTclone () const override
 
void TCL_obj (classdesc::TCL_obj_t &t, const std::string &d) override
 
void RESTProcess (classdesc::RESTProcess_t &rp, const std::string &d) override
 dummy template definition for use with gui executable More...
 
void RESTProcess (classdesc::RESTProcess_t &rp, const std::string &d) const override
 
void json_pack (classdesc::json_pack_t &j) const override
 
 ItemT ()=default
 
 ItemT (const ItemT &)=default
 
 ItemT (ItemT &&)=delete
 
ItemToperator= (const ItemT &)=default
 
ItemToperator= (ItemT &&)=delete
 
- Public Member Functions inherited from minsky::Operation< OperationType::userFunction >
Type type () const override
 
void iconDraw (cairo_t *) const override
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *cairo) const
 visual representation of operation on the canvas More...
 
void iconDraw (cairo_t *) const
 visual representation of operation on the canvas More...
 
std::size_t numPorts () const override
 
 Operation ()
 
 Operation (const Operation &x)
 
 Operation (Operation &&x)
 
Operationoperator= (const Operation &x)
 
Operationoperator= (Operation &&x)
 
std::string classType () const override
 
- Public Member Functions inherited from minsky::ItemT< Operation< T >, OperationBase >
void RESTProcess (classdesc::RESTProcess_t &rp, const std::string &d) override
 dummy template definition for use with gui executable More...
 
void RESTProcess (classdesc::RESTProcess_t &rp, const std::string &d) const override
 
 ItemT ()=default
 
 ItemT (const ItemT &)=default
 
 ItemT (ItemT &&)=delete
 
ItemToperator= (const ItemT &)=default
 
ItemToperator= (ItemT &&)=delete
 
- Public Member Functions inherited from minsky::OperationBase
const OperationBaseoperationCast () const override
 
OperationBaseoperationCast () override
 
std::string portValues () const
 returns a list of values the ports currently have More...
 
bool multiWire () const
 
virtual void addPorts ()
 
void drawUserFunction (cairo_t *cairo) const
 
void resize (const LassoBox &b) override
 resize this item on the canvas More...
 
float scaleFactor () const override
 factor by which item has been resized More...
 
double value () const override
 current value of output port More...
 
std::vector< std::string > dimensions () const
 return dimension names of tensor object attached to input if binary op, then the union of dimension names is returned More...
 
- Public Member Functions inherited from minsky::BottomRightResizerItem
bool onResizeHandle (float x, float y) const override
 
void drawResizeHandles (cairo_t *cairo) const override
 
virtual Point resizeHandleCoords () const
 returns coordinates of the resizer handle More...
 
- Public Member Functions inherited from minsky::Item
 Item ()
 
virtual std::weak_ptr< Portports (std::size_t i) const
 callback to be run when item deleted from group More...
 
std::size_t portsSize () const
 number of ports More...
 
float portX (std::size_t i)
 
float portY (std::size_t i)
 
virtual bool contains (float xx, float yy) const
 
bool contains (const Point &p) const
 
void updateBoundingBox () override
 
void throw_error (const std::string &) const
 mark item on canvas, then throw More...
 
virtual bool ioVar () const
 indicates this is a group I/O variable More...
 
double rotation () const
 
double rotation (const double &r)
 
std::pair< double, bool > rotationAsRadians () const
 return the rotation as radians, and whether rotation should have additional straight angle added for text that stays upright. More...
 
float iWidth () const
 
float iWidth (const float &w)
 
float iHeight () const
 
float iHeight (const float &h)
 
virtual void flip ()
 rotate icon though 180∘ More...
 
std::string id () const
 return an id uniquely identifying this item More...
 
virtual float x () const
 
virtual float y () const
 
virtual float zoomFactor () const
 
void ensureBBValid () const
 
float width () const
 
float height () const
 
virtual std::vector< Pointcorners () const
 
float left () const
 
float right () const
 
float top () const
 
float bottom () const
 
std::string bookmarkId () const
 Id of bookmark associated with this. More...
 
void adjustBookmark () const override
 adjust bookmark list to reflect current configuration More...
 
float resizeHandleSize () const
 resize handles should be at least a percentage if the icon size (#1025) More...
 
virtual bool inItem (float x, float y) const
 
virtual bool onItem (float x, float y) const
 returns true if (x,y) is on the icon More...
 
virtual void onMouseDown (float x, float y)
 respond to mouse down events More...
 
virtual void onMouseUp (float x, float y)
 respond to mouse up events More...
 
virtual bool onMouseMotion (float x, float y)
 respond to mouse motion events with button pressed More...
 
virtual bool onMouseOver (float x, float y)
 respond to mouse motion events (hover) without button pressed More...
 
virtual void onMouseLeave ()
 respond to mouse leave events (when mouse leaves item) More...
 
virtual bool onKeyPress (int keySym, const std::string &utf8, int state)
 respond to key press events More...
 
virtual void deleteAttachedWires ()
 delete all attached wires More...
 
virtual bool visible () const
 whether this item is visible on the canvas. More...
 
void moveTo (float x, float y)
 
virtual float scaleFactor (const float &sf)
 
void dummyDraw () const
 draw into a dummy cairo context, for purposes of calculating port positions More...
 
virtual void updateIcon (double t)
 update display after a step() More...
 
 Item (const Item &)=default
 
Itemoperator= (const Item &)=default
 
virtual ~Item ()
 
void drawPorts (cairo_t *cairo) const
 
virtual ClickType::Type clickType (float x, float y) const
 returns the clicktype given a mouse click at x, y. More...
 
virtual std::shared_ptr< PortclosestOutPort (float x, float y) const
 returns closest output port to x,y More...
 
virtual std::shared_ptr< PortclosestInPort (float x, float y) const
 
virtual std::shared_ptr< Itemselect (float x, float y) const
 returns the variable if point (x,y) is within a visible variable icon, null otherwise. More...
 
void RESTProcess (classdesc::RESTProcess_t &rp, const std::string &d) override
 runs the RESTProcess descriptor suitable for this type More...
 
void RESTProcess (classdesc::RESTProcess_t &rp, const std::string &d) const override
 
virtual void displayDelayedTooltip (float x, float y)
 enable extended tooltip help message appropriate for mouse at (x,y) More...
 
virtual void disableDelayedTooltip ()
 
virtual bool editorMode () const
 some items have an editor mode attribute More...
 
virtual void toggleEditorMode ()
 
Units checkUnits () const
 perform units consistency checks More...
 
ItemPtr itemPtrFromThis () const
 return a shared_ptr to this More...
 
virtual void destroyFrame ()
 destroy any popup windows associated with this More...
 
- Public Member Functions inherited from minsky::NoteBase
virtual std::string const & detailedText () const
 
virtual std::string const & detailedText (const std::string &x)
 
virtual std::string const & tooltip () const
 
virtual std::string const & tooltip (const std::string &x)
 
virtual ~NoteBase ()=default
 
- Public Member Functions inherited from minsky::NamedOp
 NamedOp ()
 
virtual std::string description () const
 name of the associated data operation More...
 

Static Public Member Functions

static UserFunctioncreate (OperationType::Type t)
 
- Static Public Member Functions inherited from minsky::OperationBase
static OperationBasecreate (Type type)
 factory method. More...
 
- Static Public Member Functions inherited from minsky::Item
static void drawSelected (cairo_t *cairo)
 
- Static Public Member Functions inherited from minsky::OperationType
static std::string typeName (int type)
 return the symbolic name of type More...
 
static Group classify (Type t)
 

Public Attributes

std::vector< std::string > argNames
 
std::vector< double > argVals
 
std::string expression
 
- Public Attributes inherited from minsky::OperationBase
double arg =1
 operation argument. For example, the offset used in a difference operator, or binsize in a binning op More...
 
std::string axis
 axis selector in tensor operations More...
 
- Public Attributes inherited from minsky::Item
float m_x =0
 
float m_y =0
 position in canvas, or within group More...
 
float m_sf =1
 scale factor of item on canvas, or within group More...
 
bool onResizeHandles =false
 set to true to indicate mouse is ovcaler resize handles More...
 
bool onBorder =false
 true to indicate mouse hovering over border More...
 
std::string deleteCallback
 
BoundingBox bb
 canvas bounding box. More...
 
- Public Attributes inherited from minsky::NoteBase
bool mouseFocus =false
 true if target of a mouseover More...
 
bool selected =false
 true if selected for cut, copy or group operation More...
 
bool bookmark =false
 Is this item also a bookmark? More...
 

Static Public Attributes

static int nextId =0
 
- Static Public Attributes inherited from minsky::OperationBase
static constexpr float l =-8
 
static constexpr float h =12
 
static constexpr float r =12
 

Private Member Functions

void updateBB () override
 
 CLASSDESC_ACCESS (UserFunction)
 

Private Attributes

std::shared_ptr< Implimpl
 

Additional Inherited Members

- Public Types inherited from minsky::Operation< OperationType::userFunction >
typedef OperationType::Type Type
 
- Public Types inherited from minsky::OperationBase
typedef OperationType::Type Type
 
- Public Types inherited from minsky::OperationType
enum  Type {
  constant, time, integrate, differentiate,
  data, ravel, euler, pi,
  zero, one, inf, percent,
  add, subtract, multiply, divide,
  min, max, and_, or_,
  log, pow, polygamma, lt,
  le, eq, userFunction, copy,
  sqrt, exp, ln, sin,
  cos, tan, asin, acos,
  atan, sinh, cosh, tanh,
  abs, floor, frac, not_,
  Gamma, fact, sum, product,
  infimum, supremum, any, all,
  infIndex, supIndex, runningSum, runningProduct,
  difference, differencePlus, innerProduct, outerProduct,
  index, gather, meld, merge,
  slice, size, shape, mean,
  median, stdDev, moment, histogram,
  covariance, correlation, linearRegression, numOps
}
 
enum  Group {
  general, constop, binop, function,
  reduction, scan, tensor, statistics
}
 
- Protected Member Functions inherited from minsky::OperationBase
void setCachedText (cairo_t *, const std::string &, double) const
 check if cachedPango is up to date, and if not recreate More...
 
- Protected Member Functions inherited from minsky::NamedOp
 CLASSDESC_ACCESS (NamedOp)
 
- Static Protected Member Functions inherited from minsky::Item
static void drawResizeHandle (cairo_t *cairo, double x, double y, double sf, double angle)
 
- Protected Attributes inherited from minsky::OperationBase
classdesc::Exclude< std::shared_ptr< ecolab::Pango > > cachedPango
 
- Protected Attributes inherited from minsky::Item
double m_width =10
 
double m_height =10
 
ItemPortVector m_ports
 
minsky::Item::MemoisedRotator memoisedRotator
 
- Protected Attributes inherited from minsky::NamedOp
std::string m_description
 

Detailed Description

Definition at line 27 of file userFunction.h.

Constructor & Destructor Documentation

◆ UserFunction() [1/2]

minsky::UserFunction::UserFunction ( )
inline

Definition at line 38 of file userFunction.h.

Referenced by create().

38 : UserFunction("uf"+std::to_string(nextId++)+"(x,y)") {}
string to_string(CONST84 char *x)
Definition: minskyTCLObj.h:33
Here is the caller graph for this function:

◆ UserFunction() [2/2]

minsky::UserFunction::UserFunction ( const std::string &  name,
const std::string &  expression = "" 
)

Definition at line 134 of file userFunction.cc.

134  : impl(make_shared<Impl>()), argNames{"x","y"}, expression(expression) {
136  }
std::vector< std::string > argNames
Definition: userFunction.h:35
virtual std::string description() const
name of the associated data operation
Definition: operation.cc:573
std::shared_ptr< Impl > impl
Definition: userFunction.h:29
std::string name() const override
function name, shorn of argument decorators
std::string expression
Definition: userFunction.h:37

Member Function Documentation

◆ CLASSDESC_ACCESS()

minsky::UserFunction::CLASSDESC_ACCESS ( UserFunction  )
private

◆ compile()

void minsky::UserFunction::compile ( )

Definition at line 175 of file userFunction.cc.

References minsky::anonymous_namespace{userFunction.cc}::addTimeVariables(), argNames, argVals, minsky::canonicalName(), expression, f, impl, minsky::minsky(), minsky::anonymous_namespace{userFunction.cc}::parser, symbolNames(), minsky::Item::throw_error(), minsky::Minsky::userFunctions, minsky::valueIdFromScope(), and minsky::Minsky::variableValues.

176  {
177  impl->compiledExpression=exprtk::expression<double>();
178 
179  // build symbol table
180  impl->symbols.clear();
181  impl->functions.clear();
182  addTimeVariables(impl->symbols);
183  for (auto& i: symbolNames())
184  {
185  auto scopedName=valueIdFromScope(group.lock(),canonicalName(i));
186  auto v=minsky().variableValues.find(scopedName);
187  if (v!=minsky().variableValues.end())
188  {
189  impl->symbols.add_variable(i, (*v->second)[0]);
190  continue;
191  }
192  auto f=minsky().userFunctions.find(scopedName);
193  if (f!=minsky().userFunctions.end())
194  {
195  impl->functions.emplace_back(f->second);
196  impl->symbols.add_function(i,impl->functions.back());
197  }
198  }
199 
200  // add arguments
201  argVals.resize(argNames.size());
202  for (size_t i=0; i<argNames.size(); ++i)
203  impl->symbols.add_variable(argNames[i], argVals[i]);
204  impl->compiledExpression.register_symbol_table(impl->symbols);
205 
206  if (!parser.compile(expression, impl->compiledExpression))
207  {
208  string errorInfo;
209  for (size_t i=0; i<parser.error_count(); ++i)
210  errorInfo+=parser.get_error(i).diagnostic+'\n';
211  throw_error("Invalid function expression:\n"+errorInfo);
212  }
213  }
function f
Definition: canvas.m:1
std::vector< double > argVals
Definition: userFunction.h:36
std::vector< std::string > argNames
Definition: userFunction.h:35
VariableValues variableValues
Definition: minsky.h:200
std::vector< std::string > symbolNames() const
void addTimeVariables(exprtk::symbol_table< double > &table)
Definition: userFunction.cc:98
void throw_error(const std::string &) const
mark item on canvas, then throw
Definition: item.cc:86
string canonicalName(const string &name)
convert a raw name into a canonical name - this is not idempotent.
Definition: valueId.cc:63
std::shared_ptr< Impl > impl
Definition: userFunction.h:29
Exclude< std::map< std::string, std::shared_ptr< CallableFunction > > > userFunctions
Definition: minsky.h:253
string valueIdFromScope(const GroupPtr &scope, const std::string &name)
value Id from scope and canonical name name
Definition: valueId.cc:128
std::string expression
Definition: userFunction.h:37
Definition: group.tcl:84
Minsky & minsky()
global minsky object
Definition: minskyTCL.cc:51
Here is the call graph for this function:

◆ create()

static UserFunction* minsky::UserFunction::create ( OperationType::Type  t)
inlinestatic

Definition at line 57 of file userFunction.h.

References UserFunction(), and minsky::OperationType::userFunction.

Here is the call graph for this function:

◆ description() [1/3]

string minsky::NamedOp::description

name of the associated data operation

Definition at line 573 of file operation.cc.

574  {
575  return m_description;
576  }
std::string m_description
Definition: operation.h:104

◆ description() [2/3]

string minsky::NamedOp::description

Definition at line 578 of file operation.cc.

579  {
581  updateBB(); // adjust icon bounding box - see ticket #1121
582  return m_description;
583  }
virtual float x() const
Definition: item.cc:107
void updateBB() override
Definition: userFunction.h:31
std::string m_description
Definition: operation.h:104

◆ description() [3/3]

string minsky::UserFunction::description ( const std::string &  x)
overridevirtual

name of the associated data operation

Reimplemented from minsky::NamedOp.

Definition at line 231 of file userFunction.cc.

References argNames, and minsky::NamedOp::description().

Referenced by MathDAG::OperationDAG< OperationType::subtract >::render().

232  {
234  static const regex extractArgList(R"([^(]*\(([^)]*)\))");
235  smatch match;
236  string argList;
237  if (regex_match(nm,match,extractArgList))
238  argList=match[1];
239 
240  argNames.clear();
241  auto end=argList.find(',');
242  decltype(end) begin=0;
243  for (; end!=string::npos; begin=end+1, end=argList.find(',',begin))
244  argNames.push_back(argList.substr(begin,end-begin));
245  argNames.push_back(argList.substr(begin));
246  return nm;
247  }
std::vector< std::string > argNames
Definition: userFunction.h:35
virtual std::string description() const
name of the associated data operation
Definition: operation.cc:573
Here is the call graph for this function:
Here is the caller graph for this function:

◆ displayTooltip()

void minsky::UserFunction::displayTooltip ( cairo_t *  cairo,
const std::string &  tooltip 
) const
inlineoverridevirtual

display tooltip text, eg on mouseover

Reimplemented from minsky::Item.

Definition at line 48 of file userFunction.h.

References minsky::Item::displayTooltip(), and expression.

49  {Item::displayTooltip(cr,tt.empty()? expression: tt+" "+expression);}
virtual void displayTooltip(cairo_t *, const std::string &) const
display tooltip text, eg on mouseover
Definition: item.cc:398
std::string expression
Definition: userFunction.h:37
Here is the call graph for this function:

◆ draw()

void minsky::UserFunction::draw ( cairo_t *  cairo) const
inlineoverridevirtual

draw this item into a cairo context

Reimplemented from minsky::OperationBase.

Definition at line 60 of file userFunction.h.

References minsky::OperationBase::drawUserFunction().

60 {drawUserFunction(cairo);}
void drawUserFunction(cairo_t *cairo) const
Definition: operation.cc:143
Here is the call graph for this function:

◆ evaluate()

double minsky::UserFunction::evaluate ( double  x,
double  y 
)

Definition at line 215 of file userFunction.cc.

References argVals, and impl.

Referenced by minsky::EvalOp< minsky::OperationType::constant >::evaluate().

216  {
217  if (!argVals.empty()) argVals[0]=in1;
218  if (argVals.size()>1) argVals[1]=in2;
219  for (size_t i=2; i<argVals.size(); ++i) argVals[i]=0;
220  return impl->compiledExpression.value();
221  }
std::vector< double > argVals
Definition: userFunction.h:36
std::shared_ptr< Impl > impl
Definition: userFunction.h:29
Here is the caller graph for this function:

◆ name()

string minsky::UserFunction::name ( ) const
overridevirtual

function name, shorn of argument decorators

Implements minsky::CallableFunction.

Definition at line 249 of file userFunction.cc.

References minsky::NamedOp::description().

250  {
251  static const regex extractName(R"(([^(]*).*)");
252  smatch match;
253  auto d=description();
254  regex_match(d, match, extractName);
255  assert (match.size()>1);
256  return match[1];
257  }
virtual std::string description() const
name of the associated data operation
Definition: operation.cc:573
Here is the call graph for this function:

◆ operator()()

double minsky::UserFunction::operator() ( const std::vector< double > &  p)
overridevirtual

evaluate function on arbitrary number of arguments (exprtk support)

Implements minsky::CallableFunction.

Definition at line 223 of file userFunction.cc.

References argVals, and impl.

224  {
225  size_t i=0;
226  for (; i<p.size() && i<argVals.size(); ++i) argVals[i]=p[i];
227  for (; i<argVals.size(); ++i) argVals[i]=0;
228  return impl->compiledExpression.value();
229  }
std::vector< double > argVals
Definition: userFunction.h:36
std::shared_ptr< Impl > impl
Definition: userFunction.h:29

◆ symbolNames()

vector< string > minsky::UserFunction::symbolNames ( ) const

Definition at line 138 of file userFunction.cc.

References expression.

Referenced by compile().

139  {
140  std::set<std::string> symbolNames;
141 
142  string word;
143  bool inWord=false, inString=false, quoted=false;
144  for (auto c: expression)
145  {
146  switch (c)
147  {
148  case '\'': if (!quoted) inString=!inString; break;
149  case '\\': quoted=true; break;
150  default: quoted=false; break; // I'm assuming that \" embeds a quote, but may not be true
151  }
152 
153  if (!inWord && !inString)
154  inWord=isalpha(c);
155 
156  if (inWord)
157  {
158  if (isalnum(c) || c=='_' || c=='.')
159  word+=c;
160  else
161  {
162  // trailing '.' not allowed
163  if (word.back()=='.') word.erase(word.end()-1);
164  symbolNames.insert(word);
165  word.clear();
166  inWord=false;
167  }
168  }
169  }
170  if (!word.empty()) // we ended on an identifier
171  symbolNames.insert(word);
172  return {symbolNames.begin(), symbolNames.end()};
173  }
std::vector< std::string > symbolNames() const
std::string expression
Definition: userFunction.h:37
Here is the caller graph for this function:

◆ units()

Units minsky::UserFunction::units ( bool  check = false) const
overridevirtual

compute the dimensional units

Parameters
check- if true, then perform consistency checks
Exceptions
ifcheck=true and dimensions inconsistent

Reimplemented from minsky::OperationBase.

Definition at line 44 of file userFunction_units.cc.

References minsky::canonicalName(), minsky::cminsky(), f, pyminsky::minsky, minsky::timeUnit, minsky::UnitsExpressionWalker::units, minsky::anonymous_namespace{userFunction_units.cc}::unitsParser, minsky::Minsky::userFunctions, minsky::valueIdFromScope(), and minsky::Minsky::variableValues.

45  {
47  vector<UnitsExpressionWalker> args(argNames.size());
48  if (!args.empty()) args[0].units=m_ports[1]->units(check);
49  if (args.size()>1) args[1].units=m_ports[2]->units(check);
50 
51  timeUnit.units=Units(cminsky().timeUnit);
52 
53  vector<UnitsExpressionWalker> externalUnits;
54  exprtk::symbol_table<UnitsExpressionWalker> symbolTable, unknownVariables;
55  exprtk::expression<UnitsExpressionWalker> compiled;
56  compiled.register_symbol_table(unknownVariables);
57  compiled.register_symbol_table(symbolTable);
58  for (size_t i=0; i<args.size(); ++i)
59  {
60  args[i].check=check;
61  symbolTable.add_variable(argNames[i],args[i]);
62  }
63 
64  const std::vector<std::string> externalIds=symbolNames();
65  vector<ExprTkCallableFunction> externalFunctions;
66 
67  externalUnits.reserve(externalIds.size());
68  for (auto& i: externalIds)
69  {
70  if (find(argNames.begin(), argNames.end(), i)!=argNames.end()) continue; // skip arguments
71  auto id=valueIdFromScope(group.lock(),canonicalName(i));
72  auto v=minsky().variableValues.find(id);
73  if (v!=minsky().variableValues.end())
74  {
75  externalUnits.emplace_back();
76  externalUnits.back().units=v->second->units;
77  externalUnits.back().check=check;
78  unknownVariables.add_variable(i, externalUnits.back());
79  continue;
80  }
81  auto f=minsky().userFunctions.find(id);
82  if (f!=minsky().userFunctions.end())
83  {
84  externalFunctions.emplace_back(f->second);
85  unknownVariables.add_function(i,externalFunctions.back());
86  }
87  }
88  unknownVariables.add_variable("time",minsky::timeUnit);
89 
90  try
91  {
92  unitsParser.compile(expression, compiled);
93  return compiled.value().units;
94  }
95  catch (const std::exception& ex)
96  {
97  if (check)
98  throw_error(ex.what());
99  else
100  return {};
101  }
102  }
function f
Definition: canvas.m:1
std::vector< std::string > argNames
Definition: userFunction.h:35
VariableValues variableValues
Definition: minsky.h:200
Units units(bool check=false) const override
compute the dimensional units
std::vector< std::string > symbolNames() const
const Minsky & cminsky()
const version to help in const correctness
Definition: minsky.h:549
void throw_error(const std::string &) const
mark item on canvas, then throw
Definition: item.cc:86
ItemPortVector m_ports
Definition: item.h:156
string canonicalName(const string &name)
convert a raw name into a canonical name - this is not idempotent.
Definition: valueId.cc:63
Exclude< std::map< std::string, std::shared_ptr< CallableFunction > > > userFunctions
Definition: minsky.h:253
string valueIdFromScope(const GroupPtr &scope, const std::string &name)
value Id from scope and canonical name name
Definition: valueId.cc:128
exprtk::parser< UnitsExpressionWalker > unitsParser
std::string expression
Definition: userFunction.h:37
UnitsExpressionWalker timeUnit
Definition: group.tcl:84
Minsky & minsky()
global minsky object
Definition: minskyTCL.cc:51
Here is the call graph for this function:

◆ updateBB()

void minsky::UserFunction::updateBB ( )
inlineoverrideprivatevirtual

Implements minsky::NamedOp.

Definition at line 31 of file userFunction.h.

References minsky::Item::bb, and minsky::BoundingBox::update().

31 {bb.update(*this);}
BoundingBox bb
canvas bounding box.
Definition: item.h:195
void update(const Item &x)
Definition: item.cc:46
Here is the call graph for this function:

Member Data Documentation

◆ argNames

std::vector<std::string> minsky::UserFunction::argNames

Definition at line 35 of file userFunction.h.

Referenced by compile(), and description().

◆ argVals

std::vector<double> minsky::UserFunction::argVals

Definition at line 36 of file userFunction.h.

Referenced by compile(), evaluate(), and operator()().

◆ expression

std::string minsky::UserFunction::expression

Definition at line 37 of file userFunction.h.

Referenced by compile(), displayTooltip(), minsky::Minsky::setDefinition(), and symbolNames().

◆ impl

std::shared_ptr<Impl> minsky::UserFunction::impl
private

Definition at line 29 of file userFunction.h.

Referenced by compile(), evaluate(), and operator()().

◆ nextId

int minsky::UserFunction::nextId =0
static

Definition at line 34 of file userFunction.h.


The documentation for this class was generated from the following files: