Using "query" function for SQL-like access to the assets - hyperledger

I've seen this function in the "Animal Network" example of the "Composer UI Playground". It seems that the 'findAnimalsByOwnerIdWithDetails' function is never called and thus the 'query' function is never executed in that example.
function findAnimalsByOwnerIdWithDetails(farmerId)
{
return query('select resolve(a, a.location, a.owner) from Animal a where a.owner == :farmerId');
}
I've tried to incorporate the 'query' function into my own code but the run-time execution engine complains about an "unknown function". The API description also doesn't tell anything about it.
Can it be used in some way to query/manipulate the assets (and thus the ledger) or if not, then do such plans exist? -- Thx.

That functionality is not yet implemented in the runtime. The user story for that is here:
https://github.com/fabric-composer/fabric-composer/issues/67
We are holding off implementing this story until we finish the port to HL v1, which includes CouchDB; supporting much more powerful queries over world state.

Related

What is the difference between new ActiveXObject() and WScript.CreateObject()?

According to the Microsoft documentation, one can create instances of the COM objects using both the ActiveXObject() and the WScript.CreateObject() functions. It seems like the lines
var objXL = new ActiveXObject("Excel.Application");
and
var objXL = WScript.CreateObject("Excel.Application");
are identical. Is this a true assumption? and if not what is the difference? Examples to show the difference would be highly appreciated.
P.S. The post this has been flagged as a duplicate to is about the difference between VBScript's CreateObject() method and JScript's WScript.CreateObject(). It answers mention the JScript's ActiveXObject() constructor with no further elaborations.
Are they the same?
The short the answer is Yes they are the same (in the sense they perform the same job of instantiating an automation object).
Basically unlike VBScript which has the global function CreateObject() there is no such equivalent in JScript which was based on ECMAScript 3rd Edition. So, Microsoft added its own extension ActiveXObject which does the same job as CreateObject.
Both languages can be hosted in the Windows Scripting Host which gives them access to WScript.CreateObject() which is another method that does exactly the same function but only in the context of the WScript object that is only available through the Windows Scripting Host.
Following up
There has been some debate about whether they are the same, I still stand by my original answer they are the same. However, I will concede that I was comparing VBScript CreateObject() and JScript new ActiveXObject() not Wscript.CreateObject() (which is slightly different).
Let's be clear though, all these functions and objects serve the same purpose which is to instantiate an automation object (COM). To back this up here is the official description of each;
WScript - CreateObject() Method
Creates a COM object
JScript - ActiveXObject Method
Enables and returns a reference to an Automation object
VBScript - CreateObject() Function
Creates and returns a reference to an Automation object
If they were completely the same what would the point of them be? We already have language-specific automation instantiation methods, so what would the point of Wscript.CreateObject() be?
The difference is when called with a second parameter it allows you to specify a prefix that will use to distinguish event handlers for that COM object.
Here is an example taken from this answer that shows how the second argument is used to set a prefix of objIE_ that will then be used to prefix any event handlers associated with that COM object, in this case, the InternetExplorer.Application object.
// JScript
var objIE = WScript.CreateObject("InternetExplorer.Application","objIE_")
objIE.Visible = true
while (objIE.Visible){
WScript.Sleep(500);
}
function objIE_NavigateComplete2(pDisp, URL){
WScript.Echo("You just navigated to", URL)
}
function objIE_OnQuit(){
boolBrowserRunning = false ;
}
It allows an Internet Explorer instance to be opened and the URL navigated to captured through the bound event, once the Internet Explorer Window is closed the script will end.
So while not identical they do perform the same function of instantiating an Automation (COM) object.
Useful Links
Answer to What is the difference between CreateObject and Wscript.CreateObject?

cannot traverse the nodes of an AST, while assigning each node an ID

This is more a simple personal attempt to understand what goes on inside Rascal. There must be better (if not already supported) solution.
Here's the code:
fileLoad = |home:///PHPAnalysis/systems/ApilTestScripts/simple1.php|;
fileAST=loadPHPFile(fileLoad,true,false);
//assign a simple id to each node
public map[value,int] assignID12(node N)
{
myID=();
visit(N)
{
case node M:
{
name=getName(M);
myID[name] =999;
}
}
return myID;
}
ids=assignID12(fileAST);
gives me
|stdin:///|(92,4,<1,92>,<1,96>): Expected str, but got value
loadPHPFile returns a node of type: list[Stmt], where each Stmt is one of the many types of statements that could occur in a program (PHP, in my case). Without going into why I'd do this, why doesn't the above code work? Especially frustrating because a very simple example is worked out in the online documentation. See: http://tutor.rascal-mpl.org/Recipes/Basic/Basic.html#/Recipes/Common/CountConstructors/CountConstructors.html
I started a new console, and it seems to work. Of course, I changed the return type from map[value,int] to map[str,int] as it was originally in the example.
The problem I was having was that I may have erroneously defined the function previously. While I quickly fixed an apparent problem, it kept giving me errors. I realized that in Rascal, when you've started a console and imported certain definitions, it (seems)is impossible to overwrite those definitions. The interpreter keeps making reference to the very first definition that you provided. This could just be the interpreter performing a type-check, and preventing unintentional and/or incompatible assignments further down the road. That makes sense for variables (in the typical program sense), but it doesn't seem like the best idea to enforce that on functions (or methods). I feel it becomes cumbersome, because a user typically has to undergo some iterations before he/she is satisfied with a function definition. Just my opinion though...
Most likely you already had the name ids in scope as having type map[str,int], which would be the direct source of the error. You can look in script https://github.com/cwi-swat/php-analysis/blob/master/src/lang/php/analysis/cfg/LabelState.rsc at the function labelScript to see how this is done in PHP AiR (so you don't need to write this code yourself). What this will give you is a script where all the expressions and statements have an assigned ID, as well as the label state, which just keeps track of some info used in this labeling operation (mainly the counter to generate a unique ID).
As for the earlier response, the best thing to do is to give your definitions in modules which you can import. If you do that, any changes to types, etc will be picked up (automatically if the module is already imported, since Rascal will reimport the module for you if it has changed, or when you next import the module). However, if you define something directly in the console, this won't happen. Think of the console as one large module that you keep adding to. Since we can have overloads of functions, if you define the function again you are really defining a new alternative to the function, but this may not work like you expect.

How to name a function that produces a closure

I understand closures, even though I scarcely use them, but whenever I can squeeze one I have no idea of how to name it.
The best I can think of is sticking a "make" before what would be the name of the function:
function makeSortSelection(settings1, settings2) {
return function() {
/* sort stuff attending to settings1 and settings2 */
};
}
$("#sort-button").click(makeSortSelection('ascending',foo));
(I almost always use them in Javascript, but I guess this is a very language-agnostic question)
Sadly, most examples I found of closures just use "foo" or "sayHello". I like to give all my functions a verb as name: functions "do stuff", and their name reflects it ("sortSelection", "populateForm"). In the same spirit, how should I name closures, that "do things that do stuff"? What conventions do you use, and what are the most common?
PD: I tend to use Google's style guide when in doubt, but it says nothing about this.
Closures aren't nameable entities. Functions are nameable but a closure isn't a function.
Rather than define "a closure" it is easier to define the circumstances under which closure arises.
A (lexical) closure (in javascript) occurs and continues to exist as long a persistent external reference to an inner function exists, but neither the outer function nor the inner function nor the reference to the inner function is, in itself, a closure. Pragmatically speaking, a closure is a construct comprising all these elements plus a feature of the language by which garbage collection is suppressed when they exist.
In my opinion it is wrong, as some claim, that "all functions are closures". Yes, all functions have a scope chain, but a closure should only be regarded as existing once an outer function has completed and returned, and a persistent reference to an inner function exists.
By all means give the returned function a verb-based name, just like any other named function - there's no need to regard it differently just because it is was returned by another function. In every respect the returned function is just a function - it just happens to be a function with access to the scope chain of the (execution context of the) outer function that brought it into being - nothing more than that.
EDIT:
I just realised I didn't address the critical point of the question - rephrased "how to name a function that exists for the express purpose of forming a closure by returning a function".
Like you, I have a problem here.
Most often I use the "make" prefix, as in your example. This seems to be best most of the time.
I have also used "_closure" as a suffix, This doesn't obey the "verb rule" but has the advantage of being independent of natural language. "Make" is strictly an English word and speakers of other languages will probably choose to use their own "faire", "machen" etc. On the other hand, "closure" is universal - it remains, as far as I'm aware, untranslated in other languages. Therefore, "closure" as a suffix (or prefix) could be better in scripts that are likely to be used/modded on a world-wide basis.

How to get concrete object of a static method via mirror API?

I have something like this:
class MyClass
{
static void DoSomething(arg1, arg2){...}
}
Via reflection, I am able to get the ClassMirror of this class. From this point, how would I get to the concrete static function so I can call it.
Note that I tried to use:
ObjectMirror.invoke('DoSomething', [arg1, arg2]);
which would initially appear to work, but it doesn't support passing of complex types as arguments, This static function requires a complex type as one of it's arguments.
Ideally, I'd like to get the 'Function' object that represents the static method so I can invoke it directly.
a. The current state of affairs is temporary. The plan is that the mirror API will wrap the arguments with mirrors for you.
b. The API may eventually support a getProperty method that will give you a Future on the function object. However, you will not get a Function object directly, so this won't really make any difference in this case.
c. The core idea is that the API fundamentally works on mirrors. To make it more usable, it should accept non-mirrors as input and wrap them in mirrors for you. It will always return mirrors, and in some cases return futures on these. This is so the API works the same for remote and local cases.
d. Resources for understanding mirrors:
http://www.bracha.org/mirrors.pdf (academic paper, tough going)
http://www.hpi.uni-potsdam.de/hirschfeld/events/past/media/100105_Bracha_2010_LinguisticReflectionViaMirrors_HPI.mp4 (a video, pre-Dart, discusses earlier Mirror systems)
http://gbracha.blogspot.com/2010/03/through-looking-glass-darkly.html (an old, pre-dart, blog post of mine on mirrors)
http://www.wirfs-brock.com/allen/posts/228 (Allen Wirfs-Brock's blog. Allen was a mirror pioneer back in Smalltalk in the 90s)
http://www.wirfs-brock.com/allen/posts/245
You can also search my blog, or Allen Wirf-Brock's for posts on the topic.

Usage of inline closures / function delegates in Actionscript

Why are inline closures so rarely used in Actionscript? They are very powerful and I think quite readable. I hardly ever see anyone using them so maybe I'm just looking at the wrong code. Google uses them in their Google Maps API for Flash samples, but I think thats the only place I've seen them.
I favor them because you have access to local variables in the scope that defines them and you keep the logic in one method and dont end up with lots of functions for which you have to come up with a name.
Are there any catches of using them? Do they work pretty much the same way as in C#.
I actually only just discovered that AS3 supports them, and I'm quite annoyed becasue I had thought I read that they were deprecated in AS#. So I'm back to using them!
private function showPanel(index:int):void {
_timer = new Timer(1000, 1);
_timer.addEventListener(TimerEvent.TIMER, function(event:Event):void
{
// show the next panel
showPanel(index++);
});
The biggest gotcha to watch out for is that often 'this' is not defined in the inline closure. Sometimes you can set a 'this', but it's not always the right 'this' that you would have available to set, depending on how you're using them.
But I'd say most of the Flex code I've worked on has had inline closures rampantly throughout the code--since callbacks are the only way to get work done, and often you don't need the bring out a whole separate function.
Sometimes when the function nested is getting to be too much, I'll break it out slightly with Function variables in the function; this helps me organize a bit by giving labels to the functions but keeping some of the characteristics of inline closures (access to the local variables, for example).
Hope this helps.
One additional problem is that garbage collection is broken when it comes to closures (at least in Flash 9). The first instance of a given closure (from a lexical standpoint) will never be garbage collected - along with anything else referenced by the closure in the scope chain.
I found what originally made me NOT want to do this, but I had forgotten the details:
http://livedocs.adobe.com/flex/3/html/16_Event_handling_6.html#119539
(This is what Mitch mentioned - as far as the 'this' keyword being out of scope)
So thats Adobe's answer, however I am much more likely to need to refer to local variables than 'this'.
How do others interpret Adobe's recommendation ?

Resources