How to Connect Blocks Parallelly

336 views
Skip to first unread message

shinu sam

unread,
May 23, 2018, 6:36:48 AM5/23/18
to Blockly
Hai sir,
    
       When i tried to connect blocks  serially i used this code,
                 [  var code = Blockly.JavaScript.workspaceToCode(workspace);   ]

and i tried to connect blocks paralelly using the below code
         

         var xml = Blockly.Xml.workspaceToDom(workspace);
// Find and remove all top blocks.
var topBlocks = [];
for (var i = xml.childNodes.length - 1, node; block = xml.childNodes[i]; i--) {
 
if (block.tagName == 'BLOCK') {
    xml
.removeChild(block);
    topBlocks
.unshift(block);
 
}
}
// Add each top block one by one and generate code.
var allCode = [];
for (var i = 0, block; block = topBlocks[i]; i++) {
 
var headless = new Blockly.Workspace();
  xml
.appendChild(block);
 
Blockly.Xml.domToWorkspace(xml, headless);
  allCode
.push(Blockly.JavaScript.workspaceToCode(headless));
  headless
.dispose();
  xml
.removeChild(block);
}
But this not working for my blocks. Please give other opinion to connect blocks parallelly.
Thanku

Andrew n marshall

unread,
May 23, 2018, 12:48:36 PM5/23/18
to blo...@googlegroups.com
Hi Sam,

I'm not sure I'm understanding what you mean by "connect blocks parallelly". If you mean how blocks connection on screen, maybe you can demonstrate in a screenshot (though I understand you're trying to work headlessly).


But looking at your code, maybe "connected" is misleading.

If you are aiming for parallel execution of multiple stacks (similar to scratch) a standard JavaScript VM will not work (nor will any of the other languages we support). JavaScript is defined to be a single threaded environment. However, the JS Interpreter (docs) does support this sort of execution. See this threading demo. For scratch-style execution, you would have one code file (one pass of workspaceToCode(..)) loaded into the interpreter, and then multiple threads started, usually by event headers (like the green flag in scratch).

We have a section in the Blockly documentation on how to integrate the JS Interpreter. Also check out how I implemented a wait block for the interpreter (live demo). This is a good example for to start with for any block that takes time to process.

Be aware, the JS Interpreter solution has race conditions for share global data that the JavaScript language was not designed to handle.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

shinu sam

unread,
May 24, 2018, 1:38:44 AM5/24/18
to Blockly

Sir,

      Thank you for your reply



               I connect  blocks one after other (serailly), output shows in right side. But if i connect  blocks parallelly it will not work shown in below screenshot


                        


Andrew n marshall

unread,
May 24, 2018, 3:51:09 PM5/24/18
to blo...@googlegroups.com
Thanks.  I understand now. You need the code for the attach child blocks.

If you have a input_value that looks like this:  
The BlockFactory will give you a generator template that looks like this:
Blockly.JavaScript['example_input_value'] = function(block) {
  var value_code = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = '...;';
  return code;
};

value_code is the code for the children attached to VALUE, if any.  You'll need to compose that into your code variable.  For example:
  var code = 'block_type(' + value_code + ');';
Make the necessary changes to your import block's generator function.




If, instead, you have an input_statement like this
you will use statementToCode(...) instead of valueToCode(..)​:
Blockly.JavaScript['example_input_statement'] = function(block) {
  var value_code = Blockly.JavaScript.statementToCode(block, 'VALUE');
  var code = 'VALUE {\n  ' + value_code + '\n}\n';
  return code;
};
 

shinu sam

unread,
May 29, 2018, 6:20:04 AM5/29/18
to Blockly
Thank You sir
    
     Its working properly. I need an another help sir, you give the javascript code for custom blocks.
Can i make the same with python?


The code is
    From (gpiozero) import (led);
corresponding python code is
     From gpiozero import led; 
I need this python code.                      
      Kindly waiting for your reply.



Andrew n marshall

unread,
May 29, 2018, 3:03:07 PM5/29/18
to blo...@googlegroups.com
The parenthesis may be a side effect of order of operations precedence, the second member of the returned array:

Blockly.Python['my_block_type'] = function(block) {
  // TODO: Assemble Python into code variable.
  var code = '...';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Python.ORDER_NONE];
};
You'll notice the block factory uses ORDER_NONE by default, and includes a TODO directing you to change it. The possible values are found here. One of those should work.

Alternatively, you can just detect them and remove the at the level of the code string.

Message has been deleted

shinu sam

unread,
May 30, 2018, 2:49:37 AM5/30/18
to Blockly

Thank You Sir,

 Its working properly.  Your guideness is very helpful for my  project, I kindly expecting more helps from you in future.
Thank you so much sir for your kind replys.

Andrew n marshall

unread,
May 30, 2018, 1:09:41 PM5/30/18
to blo...@googlegroups.com
I would guess you're returning a pair with Blockly.Python.ORDER_FUNCTION_CALL as the second parameter.  Statement blocks (with top a top connector) only need to return the raw code string, unlike a value block (with a left connector).

However, I can't say with any certainty unless you share your generator code.

shinu sam

unread,
May 31, 2018, 12:22:41 AM5/31/18
to Blockly

Sir, 
     
       Is it possible to add background image for workspace? 

picklesrus

unread,
May 31, 2018, 4:21:33 PM5/31/18
to Blockly
Hi,

You should be able to do this with css.

I added a background-image:url(<my image>) to the element with the blocklySvg class and that worked okay.  You might want yours on a different element, but it should work.

-picklesrus

shinu sam

unread,
Jun 6, 2018, 2:45:46 AM6/6/18
to Blockly

Thank You Sir,
  
   
    You reply is very helpful to me.  I also added a background-image:url() to the element with the blocklySvg class and that worked okay. Thank You for your kind reply Sir.

Reply all
Reply to author
Forward
0 new messages