How to get value from block?

124 views
Skip to first unread message

Naresh Chaudhari

unread,
Apr 5, 2021, 6:22:45 AM4/5/21
to Blockly
Hi @all, Hope you are going great. 
I have a query and need a solution for: I have created some peace if blocks and using this block I have generated Structure based on JSON code. 
for example. 
Here is JSON code:
{
    "StatsResult": [
        {
            "GraphAttrib": [
                {
                    "Collection": "SystemStatResult", 
                    "Filter": {
                        "NodeName": "dsc_01"
                    }, 
                    "xAxis": "Timestamp", 
                    "xAxisLabel": "Timestamp", 
                    "yAxis": "CPU(%)", 
                    "yAxisLabel": "%", 
                    "yAxisPosition": "left"
                }
            ], 
            "GraphType": "Line", 
            "Name": "CPU-JVM-STATS DSC01"
        }, 
        {
            "GraphAttrib": [
                {
                    "Collection": "DIAMessageStatResult", 
                    "Filter": {
                        "NodeName": "dsc_01"
                    }, 
                    "xAxis": "Timestamp", 
                    "xAxisLabel": "Timestamp", 
                    "yAxis": "SENT_TPS", 
                    "yAxisLabel": "TPS", 
                    "yAxisPosition": "left"
                }
            ], 
            "GraphType": "Line", 
            "Name": "TPS DSC01"
        }
    ], 
    "TestRunDuration": 10
}
Here I have created an algorithm to generate blocks as per the JSON input file.
Here is the result.
Capture.PNG
Now, here this JSON block will get attached with its parent's block and there I need to do some configuration to get value from its block.

Now here I have joined JSON block with other block.
Capture.PNG
Here I have collapsed the block and attached with testcase_config block.

So here what I want convenes is.... inside init method... you can see two blocks called me_diaperf_obj.TestRunDuration and mme_diaperf_obj.StatsResult
so here if i give TestRunDuration then i should get its value as 10.
if i give StatsResult then i should get its value. how can i achieve my needs?

is it possible to create a variable for each key and store it values ? or something different... Im not able to get more ideas.... Please help me out... Any response will be highly appreciated. 

Thank you.

Beka Westberg

unread,
Apr 5, 2021, 5:58:34 PM4/5/21
to blo...@googlegroups.com
Hello,

Hmm I'm a bit confused about what exactly you're doing. Are you (1) using the JavaScript language generator with some custom blocks that support objects? Or are you (2) building some custom logic to traverse the blocks, and create your own output?

If 1, then you would need to generate code that does a member access on an object storing your testconfig information. You could store the testconfig information in a variable, or depending on how you structure your generators, you could pass the testconfig information as a parameter to a function.

If 2, I'm not sure how you should access that info, because it depends a lot on the other functionality you want your blocks-language to have. One option might be to traverse back up your blocks stack until you find one of your keys (eg "TestRunData") and then generate the json as you've shown you can already do.

I know this probably isn't super helpful :/ But I hope it gives you some place to start!

Best,
Beka

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/6c49a615-1991-4b7f-aa94-7d69e26b831cn%40googlegroups.com.

Naresh Chaudhari

unread,
Apr 6, 2021, 1:03:59 AM4/6/21
to Blockly
Thanks, Beka... Your first approach may help. 
Let me elaborate on my question. 

in first steps... I'm calling and loading JSON files to generate visual blocks similar to JSON key-pair value. (will call by dataBlock)
in the second step... that datablock I have attached with testcase_config block.
in the third step... I have some blocks like init block. where if I use TestRunDuration then it should return a value of 10. and similar to that I need the value for statsResult and it contains arrayOfObjects. then it should return arrayOfValue. 
Here my query is like how do I get these values? 

Using and storing value on variable how it will be possible for object or array? 

Beka Westberg

unread,
Apr 6, 2021, 3:52:41 PM4/6/21
to blo...@googlegroups.com
I'm sorry I'm still a bit confused :/

When you say "if I use TestRunDuration then it should return a value of 10" Do you mean that (1) your block-code generator should return the string "10"? Or (2) when the code-string returned by your block-code generator is evaluated it should evaluate to the number 10?

Would it be possible for you to share your current block-code generators? It might make it a bit easier to discuss if there's something concrete to work with. But if it's not possible that's cool too!

Best wishes,
Beka

Naresh Chaudhari

unread,
Apr 8, 2021, 10:14:23 AM4/8/21
to Blockly
Hi Beka, 

 hi, in the code you will find at the end TestCaseDuration: 10 value is given in the key-pair value and it is a number. So If we take any other key for eg: 
 "Collection": "SystemStatResult" and its storing string value.
"Filter": {
                        "NodeName": "dsc_01"
 }, 
here also "filter" is Object type. and inside object have "nodeName": as string type.

Here is JSON code
Capture.PNG

In the above image... Im using TestRunDuration  and it should return value as 10.(Number)

So here what I want convenes is.... inside init method... you can see two blocks called me_diaperf_obj.TestRunDuration and mme_diaperf_obj.StatsResult
so here if i give TestRunDuration then i should get its value as 10.
if i give StatsResult then i should get its value. how can i achieve my needs?

is it possible to create a variable for each key and store it values ? or something different... Im not able to get more ideas.... Please help me out... Any response will be highly appreciated. 

Beka Westberg

unread,
Apr 8, 2021, 4:31:00 PM4/8/21
to blo...@googlegroups.com
Hello,

Ok I think I understand that you're using a custom system to generate json, and you're not using Blockly's code generation system. Please correct me if I'm wrong.

> how can i achieve my needs?

I think you have two basic options:
1) Store the generated json from your 'testcase config' block when you first generate it, and then refer back to it from within your 'init' block (this is what I discussion below).
2) When you encounter the blocks inside your 'init' block, traverse back up your block stack until you find the definition of that key, and then generate the json for it again.


> is it possible to create a variable for each key and store it values?

In a way, yes. A map/object in javascript is kind of like "a variable for each key". If you're already generating the json for your 'testcase config' block, do you think you just use that for your lookup later in your 'init' block?

Best,
Beka

Reply all
Reply to author
Forward
0 new messages