how to identify a callback or link a callback to a previous function? - callback

just started learning and bumped into this callback thing, not sure I fully follows this example below, callback is an argument for function inputName, but how is it being linked to the previous function sayHello? I think I missed the logic flow here>, is it possible there are two callbacks? how to decide which one to refer to then, many thanks.
enter image description here

Related

Anylogic Custom Flow Block how to call a function

I am wondering that how could we call function in custom flow block in anylogic. Should we need to use some parameters? I used MYAGVWorkstation as Custom flow block as https://anylogic.help/library-reference-guides/process-modeling-library/custom-block.html. Now I want to call function at seizeAGV-SizeTransport. I tried but its giving error.
Looking forward to some help and thanks in advance.
Reference

What are callbacks in Julia and how do I use them?

I have seen a few different documentations refer to the term "callbacks" (Flux.jl and some SciML packages) but it is not clear what that means in the context of Julia.
I know that a callback is a function passed as an argument to another function but is there more to it than that? What would the use case for such a paradigm be?
A classic example of use of a callback is the progress bar. A callback is supplied to the function that is doing work within some kind of sequence. At regular intervals during the sequence the callback is called with some kind of information about the work being done (the percent completed in the case of the progress bar, which updates a progress display).
Flux can call a callback each time it completes a segment of training, or in the example in the source code, every 10 seconds:
https://github.com/FluxML/Flux.jl/blob/b78a27b01c9629099adb059a98657b995760b617/src/optimise/train.jl#L80-L95

How is Callback handling implemented?

In the past, I have used libraries that would allow me to register a callback so that the library can call my method when some event happens (e.g. it is common to see in code that use GUI libraries to look like button.onClick(clickHandler)).
Naively, I suppose the library's handling mechanism could be implemented like:
while(1){
if (event1) { event1Handler(); }
if (event2) { event2Handler(); }
...
}
but that would be really wasteful right? Or is that really how it is done (for instance do well known GUI libraries like java swing, or GTK+ do it this way)?
background:
This question hadn't really occured to me until I encountered curses. I thought about implementing my own callback system, until I realized I didn't know how.
The while loop will typically wait for an interrupt from the user (GetMessage in Windows). When an interrupt arrives GetMessage returns and then it ends up in the callback function. The if statements are typically implemented as a switch-case. See Windows Message Loop on Wikipedia.
In more detail, what happens is the following:
The user application calls GetMessage, which forces the process to sleep until an input message for that application arrives from the systems queue. When a message arrives, the user app calls DispatchMessage, which calls the callback function associated with the window that the message was aimed at.
Windows API uses one callback which handles all events in a switch case. Other libraries use one callback per event class instead.
The function pointers themselves are stored together with other window data in a struct.
Callback system implementation probably has different implementation in different technologies, however, I suppose they should be working this way:
A data structure stores the callback IDs and pointers to the handlers.
A callback handler has a validator
Event handlers have callback callers, which know what are the possible callbacks and check their validity this way:
for each callback in event.callbacks
if (callback.isValid())
call callback()
end if
end for
When you add a handler to a function the system will automatically know where the callback is valid and will add the callback to the datastructure described in 1.
Correct me if I'm wrong, this description is just a guess.

Is it possible to use multiple callbacks in the WindowButtonMotionFcn?

I created a class which adds functionality to a figure on construction. This class creates a listener for the WindowMouseMotion event; however, to get this event to fire I had to add a dummy callback function for the figure's WindowButtonMotionFcn property. I first check if this property is already populated. If it isn't then I set it to a dummy callback function that does nothing.
Instead of checking if the property is already set or not, can I simply add this dummy callback to any existing callbacks? Is it possible for a callback property to call multiple functions?
EDIT
When using the handle.listener approach to handle the WindowButtonMotionEvent event given below, be sure to use eventdata.CurrentPoint to access the current mouse position. The CurrentPoint property of the figure does not get updated before handling the WindowButtonMotionEvent event in this manner.
A related article can be found on Yair Altman's Undocumented MATLAB blog, from guest blogger Matt Whitaker. What you are alluding to is callback chaining, and quoting from the blog:
Frankly, having written code
previously that handles callback
chaining, I would rather poke myself
in the eye with a fork!
Luckily, there appears to be an alternative solution in that article. Using a snippet from the code posted there, I was able to get a function to execute on mouse movement without having to set the 'WindowButtonMotionFcn'. I added a listener to the current figure like so:
myListener = handle.listener(gcf,'WindowButtonMotionEvent',...
#(hSource,eventData) disp('hello'));
And the message hello was displayed when I moved the mouse in the window.
You can do this via cellfun and feval, as answered on Mathworks site: http://www.mathworks.com/matlabcentral/answers/10664-multiple-callback-functions
obj = uicontrol(...,'style','popupmenu',...
'Callback', #(h,e)(cellfun(#(x)feval(x,h,e), ...
{#(h,e)this.myfunc(h), ...
#(h,e)this.myfunc2(h), ...
#(h,e)this.myfunc2(h)}))
Note that the callback is set to an anonymous function using cellfun to evaluate each handler.

What is a callback?

Is it a function?
Is it a function being called from the source?
Or, is it a function being returned from the destination?
Or, is it just executing a function at the destination?
Or, is it a value returned from a function passed to the destination?
A callback is the building block of asynchronous processing.
Think of it this way: when you call someone and they don't answer, you leave a message and your phone number. Later on, the person calls you back based on the phone number you left.
A callback works in a similar manner.
You ask an API for a long running operation and you provide a method from within your code to be called with the result of the operation. The API does its work and when the result is ready, it calls your callback method.
From the great Wikipedia:
In computer programming, a callback is
executable code that is passed as an
argument to other code. It allows a
lower-level software layer to call a
subroutine (or function) defined in a
higher-level layer.
Said another way, when you pass a callback to your method, it's as if you are providing additional instructions (e.g., what you should do next). An attempt at making a simple human example follows:
Paint this wall this shade of green (where "paint" is analagous to the method called, while "wall" and "green" are similar to arguments).
When you have finished painting, call me at this number to let me know that you're done and I'll tell you what to do next.
In terms of practical applications, one place where you will sometimes see callbacks is in situations with asynchronous message passing. You might want to register a particular message as an item of interest for class B.
However, without something like a callback, there's no obvious way for class A to know that class B has received the message. With a callback, you can tell class B, here's the message that I want you to listen for and this is the method in class A that I want you to call when you receive it.
Here is a Java example of a callback from a related question.