Repository navigation
Description
Process communication though shared scripts is a generalisation of the script refinement.
One way to view this is: a called script may have multiple synchronous callers.
There are some decisions outstanding, with respect to syntax and semantics.
Below I hope to address all issues.
(work in progress)
Syntax
a, b = x // 2 partners
a, b, c = x // 3 partners
a(i:Int), b(??j: Int) = x // parameters
a(i:Int), b(??i: _) = x // parameters; type of second i not to be named
a... = x // one or more a's communicate
a..., b = x // one or more a's, and one b communicate
a..?, b = x // zero or more a's, and one b communicate
a?, b = x // zero or one a, and one b communicate
a<-(i:Int), a->(??i: _) = x // arrows are allowed as trailer of name
a<-->(??i: Int) = x // shorthand form of the previous line
a<-...->(??i: Int) = x // shorthand for a<-(i:Int), a->(??i: _)...
a<-..?->(??i: Int) = x // shorthand for a<-(i:Int), a->(??i: _)..?
For a shared parameters, only one communicating script shall define its real type;
this communication script one shall not have arity "zero or one" (a?) or "zero or more" (a..?).
In the other communication scripts the type shall be specified as _.
This way the programmer shows he knows that the parameter is shared.
A declared script that does not communicate with another one, will by default use the efficient script call mechanism, although the communication mechanism would in principle also apply. By having += rather than = in the declaration, the communication mechanism will be used instead. This way such a script may also occur in other communications, e.g.,
a,b = x
a += y
Semantics
In the execution cycle, with a just higher priority than normal code fragments execution, it is checked whether a set of communication scripts is activate that could constitute a communication.
A set of active communication scripts may do so if:
- they belong to the same owner object
- they have been activated in parallel, i.e. pairwise they pend to different child nodes of parallel operator nodes:
&,&&,|,||,(** **), root (the latter two are anchors for process launching). - if they have parameters of the same name, then those that are not actually output parameters (incl possibly adapting parameters) must all have an equal value.
Note that a given communication script may be eligible for multiple communications. E.g., for one a there may be two distinct a,b communications possible, in case there are 2 b's active. Also there may be another communication a,c available, and a c might be active.
It is not yet clear how to deal with this. Maybe it is best to find the first applicable communication, and then assume that this will be the one and only one. However, in a... we want to have a maximal set; that is: if possible the applicable communication should be extended with additional a's, as long as these belong to the same owner etc.
In case there are multiple scripts that share a parameter name, which one to take for the formal value? If none of these are not output parameters then all values should be equal; take that value.
Else take the parameter of the leftmost named script that has the real type for the parameter specified; in case that is of the form a... then any of those communication scripts will do.
Implementation
Parser
The tasks to be done in the parser seem relatively straightforward.
Special attention will be needed to check parameter names: if one communication script has a parameter with the same name as an earlier communication script, then, and only then, its type must be specified as _.
DSL
This may be the hardest part of implementing communication.
a(i:Int), b(j:Int) = println: (i+j)
a(i:Int), c = ...
would, among others, result in:
def _communication_a_b: Script[Unit] = subscript.DSL._communication_2(this) {
(partner0: CommScript) =>
(partner1: CommScript) =>
(_node: subscript.vm.ScriptTrait[Any]) =>
implicit val script = _node
subscript.DSL._tiny[Any] (_node => {
implicit val here = _node
println(subscript.DSL._maybeVarCall("i"), subscript.DSL._maybeVarCall("j"))
}, true)
}
object Communication_a_b extends Communication {
override def communication: Script[Unit] = _communication_a_b
override def nPartnerScripts: Int = 2
}
class CommScript_a(owner: Any, i:Int) extends CommScript {
override def symbol = Symbol("a")
override def communications = (Communication_a_b, 0, ARITY_ONE) ::
(Communication_a_c, 0, ARITY_ONE) :: Nil
override def parameters = Map(`i -> i)
}
def a(i:Int) = new CommScript_a(this, i)
subscript.DSL._maybeVarCall now also has to deal with formal parameters of the communicating partners.
The structure that contains the call to println should be a def; an object or class would yield a wrong this value.
The meaning of (Communication_a_b, 0, ARITY_ONE, true) is:
- refer to object
Communication_a_b - this CommScript_a is the first partner script there (index = 0)
- the arity there is 1, rather than 0 or 1, 0 or more, 1 or more.
The parameters method reveals this script's parameters. With this information it should be possible to sort out what communicating scripts have compatible parameters, and also how to transfer these parameters (note: output parameters are wrapped in ActualOutputParameter).
Also the fact that script has parameters that might be used, so the communication method will contain (partner0: CommScript) => in order to get access to this data through _maybeVarCall.
Establishing communication
The script executor or a more separate engine will establish communications.
An activated, not suspended, communication script for which no communication has yet been established, is called 'free'. As soon as it is part of an established communication, it is called a 'partner'.
When
The engine will know newly activated, deactivated, suspended and resumed communication scripts. In case there are newly activated or suspended code fragments, it should be established whether new communications need to be activated, just before a normal code fragment would be executed.
For that purpose the script executor keeps track of two sets: activatedOrResumedCommunicationScripts and deactivatedOrSuspendedCommunicationScripts (or maybe 4 sets).
Each time activatedOrResumedCommunicationScripts goes from empty to non-empty, a special message 'EstablishCommunicationsis put in the queue; this has a slightly higher priority thanCFToBeExecuted`.
Owner object
Communicating scripts must have the same owner object. This will go "automatically" since generated structures such as CommScript_a are local to the owner object.
Parallelism detection
Among others the engine should see whether a given set of communication scripts are active in parallel branches of the call graph. In Scriptic a complicated pruning algorithm was used for this. Maybe this will now become easier by collecting for each such communicating script a hash map with
- keys: the ancestor nodes that are a parallel operator
- values: the children of those parallel operators that are also an ancestor of the communicating script
Complexity and efficiency
The engine has some options for dealing with complexity.
To try all possibilities out may be order of 2^n, with n the number of free communication scripts.
In principle the application programmer should make sure that the number of possibilities will be limited, so the engine does not need to care for this aspect.
The engine should each time only try to establish communications where newly activated or resumed free communication scripts are involved. This way no unnecessary doubling of earlier attempts will happen. This is also the rationale for having the set activatedOrResumedCommunicationScripts.
The other set, deactivatedOrSuspendedCommunicationScripts, may or may not be used.
Call graph messages
The current call graph messages will become more complicated, since we will have a real call graph instead of a call tree.
Messages that went simply upward (AAStarted, AAHappened, ...) now need to multiply at Communication nodes (having upward branches); and likewise they need continuations at nodes (e.g. &) that have downward branches.
Conversely, messages that went simply downward (Exclude, Suspend, Resume) now need to do a continuation at Communication nodes.
So for each of these messages a continuation variant is needed. The simplest way would be to add a flag to the original message class. Setting this flag makes the message priority slightly lower.
Neutral Elements Issues
Neutral elements that are at the top level of a communicating script may cause seeming contradictions.
E.g.,
a,b = [] // explicit neutral element
a,c = if p then x // implicit
a,d = e // possible, depending on e
in all three cases the communication may state it behaves neutrally, i.e. as a 0 or 1. But what if a has been called from an and-like context (as in a&x), whereas the other party (b, c, or d) has been called from an or-like context? That seems a contradiction. The compiler may yield warnings in the first two cases, but the third case (a,d = e) it has not enough information. In such cases the developer should have known better. The implementation is free to choose any communication partner to determine whether context for the neutral behavior is and-like or or-like.
Misc
The currently existing methods available to code fragments that check for matching parameters and result values should do so for all communicating partners.
Activity
anatoliykmetyuk commentedon Jun 18, 2016
Communication introduces a new feature to the call graph - it becomes precisely a graph and no longer a tree, so some troubles may arise. For example:
a & b & break? & cwill not activatecuntil action is taken inb. The fact of action taking place inbis determined by the index of the AAHappened that arrives at&. Now ifacommunicates withcand[a & break? & b] & [c & break? & d], the assumptions about the indices of the children of eitheraorcmay be broken and I’m not sure how this line will behave.a & bthe Continuation for&will encounter 2 AAHappened of the same nature - how is it supposed to behave and how will it behave in reality?As far as I remember, first, each AAHappened schedules a Continuation on an n-ary node it passes and, second, AAHappened has a larger priority than Continuation. So I suspect we’ll have two parallel AAHappened passing through
&, each scheduling a Continuation, and a duplicate AAHappened will get us a duplicate Continuation. The first Continuation will consume the two AAHappened’s and make its decisions. The decisions can include sending a Success or Exclude messages, which both have a higher priority than Continuation. So it can be that when the second Continuation kicks in, the executer will die with an exception since the node it continues is already deactivated.I think on the message splitting and merging part we need a separate system. A check to be done before handling any message. It may involve a function
(Seq[Message], Node) => Booleanthat will determine whether a set of messages should be merged or splitted in a given node. Before a message is handled, we look at the queue for the messages of same type following immediately after this one (since they have the same priority, it is easy to select them; may be 0) and apply this function to see whether to merge/split them.Also, a test suite should be designed like that of the OperatorsSuite to test all the possible scenarios in combination with other operators - particularly breaks.
I also think it is about time to make some tests and specifications for the script executor itself. Hardly we can test the internal machinery by the OperatorsSuite, only the external observation.
AndreVanDelft commentedon Jun 18, 2016
aandc) and for the callee (a_c); the relevant indexes for the children of the&'s may thus well differ.anatoliykmetyuk commentedon Jun 18, 2016
AndreVanDelft commentedon Jun 18, 2016
The child indexes are unique, since a child is a caller, i.e. either
aorc, not both. This is one of the good things of having separate nodes for caller & callee. So the indexes are really different.About 25 years ago, there was only one node rather than two for a call, and that caused some problems that I don't remember well.
The logic for the AAHappened continuation messages will be fairly simple: