@@ -1703,13 +1703,45 @@ Blockly.Blocks['robActions_change_bias'] = {
17031703 }
17041704} ;
17051705
1706- let inputNeuronCounter = 1 ;
1707- let outputNeuronCounter = 1 ;
1706+ var neuronNameGenerator = {
1707+ isLegalName : function ( newName , block ) {
1708+ var blocks = Blockly . mainWorkspace . getAllBlocks ( ) ;
1709+ for ( var x = 0 ; x < blocks . length ; x ++ ) {
1710+ if ( blocks [ x ] == block ) {
1711+ continue ;
1712+ }
1713+ if ( blocks [ x ] . neuron ) {
1714+ let name = blocks [ x ] . getFieldValue ( 'NAME' ) ;
1715+ if ( newName === name ) {
1716+ return false ;
1717+ }
1718+ }
1719+ }
1720+ return true ;
1721+ } ,
1722+
1723+ findLegalName : function ( name , block , prefix ) {
1724+ if ( name . match ( / ^ [ a - z A - Z ü ö ä Ü Ä Ö ß $ ] [ a - z A - Z 0 - 9 _ ü ö ä Ü Ä Ö ß $ ] * $ / ) === null ) {
1725+ name = prefix ;
1726+ }
1727+ while ( ! neuronNameGenerator . isLegalName ( name , block ) ) {
1728+ // Collision with another variable.
1729+ var r = name . match ( / ^ ( .* ?) ( \d + ) $ / ) ;
1730+ if ( ! r ) {
1731+ name += '2' ;
1732+ } else {
1733+ name = r [ 1 ] + ( parseInt ( r [ 2 ] , 10 ) + 1 ) ;
1734+ }
1735+ }
1736+ return name ;
1737+ }
1738+ }
17081739
17091740Blockly . Blocks [ 'robActions_inputneuron' ] = {
1741+ neuron : true ,
17101742 init : function ( ) {
17111743 this . setColour ( Blockly . CAT_NN ) ;
1712- var nameField = new Blockly . FieldTextInput ( '' , this . generateName ) ;
1744+ var nameField = new Blockly . FieldTextInput ( 'in ' , this . validateName ) ;
17131745 nameField . maxDisplayLength = 75 ;
17141746 this . appendDummyInput ( )
17151747 . appendField ( Blockly . Msg . NN_INPUT_NEURON )
@@ -1726,15 +1758,19 @@ Blockly.Blocks['robActions_inputneuron'] = {
17261758 bumpIfNotNeuron ( this . nextConnection . targetConnection ) ;
17271759 bumpIfNotNeuron ( this . previousConnection . targetConnection ) ;
17281760 } ,
1729- generateName : function ( oldName ) {
1730- return 'in' + inputNeuronCounter ++ ;
1761+ validate : function ( ) {
1762+ this . setFieldValue ( neuronNameGenerator . findLegalName ( this . getFieldValue ( 'NAME' ) , this , 'in' ) , 'NAME' ) ;
1763+ } ,
1764+ validateName : function ( name ) {
1765+ return neuronNameGenerator . findLegalName ( name , this . sourceBlock_ , 'in' ) ;
17311766 }
17321767} ;
17331768
17341769Blockly . Blocks [ 'robActions_outputneuron' ] = {
1770+ neuron : true ,
17351771 init : function ( ) {
17361772 this . setColour ( Blockly . CAT_NN ) ;
1737- var name = new Blockly . FieldTextInput ( '' , this . generateName ) ;
1773+ var name = new Blockly . FieldTextInput ( 'out ' , this . validateName ) ;
17381774 name . maxDisplayLength = 75 ;
17391775 this . appendDummyInput ( )
17401776 . appendField ( Blockly . Msg . NN_OUTPUT_NEURON )
@@ -1752,15 +1788,22 @@ Blockly.Blocks['robActions_outputneuron'] = {
17521788 bumpIfNotNeuron ( this . previousConnection . targetConnection ) ;
17531789 mustBeVariable ( this , [ 'VALUE' ] ) ;
17541790 } ,
1755- generateName : function ( oldName ) {
1756- return 'out' + inputNeuronCounter ++ ;
1791+ validate : function ( ) {
1792+ if ( this . isInFlyout ) {
1793+ return ;
1794+ }
1795+ this . setFieldValue ( neuronNameGenerator . findLegalName ( this . getFieldValue ( 'NAME' ) , this , "out" ) , 'NAME' ) ;
1796+ } ,
1797+ validateName : function ( name ) {
1798+ return neuronNameGenerator . findLegalName ( name , this . sourceBlock_ , 'out' ) ;
17571799 }
17581800} ;
17591801
17601802Blockly . Blocks [ 'robActions_outputneuron_wo_var' ] = {
1803+ neuron : true ,
17611804 init : function ( ) {
17621805 this . setColour ( Blockly . CAT_NN ) ;
1763- var name = new Blockly . FieldTextInput ( '' , this . generateName ) ;
1806+ var name = new Blockly . FieldTextInput ( 'out ' , this . validateName ) ;
17641807 name . maxDisplayLength = 75 ;
17651808 this . appendDummyInput ( )
17661809 . appendField ( Blockly . Msg . NN_OUTPUT_NEURON )
@@ -1773,8 +1816,14 @@ Blockly.Blocks['robActions_outputneuron_wo_var'] = {
17731816 bumpIfNotNeuron ( this . nextConnection . targetConnection ) ;
17741817 bumpIfNotNeuron ( this . previousConnection . targetConnection ) ;
17751818 } ,
1776- generateName : function ( oldName ) {
1777- return 'out' + inputNeuronCounter ++ ;
1819+ validate : function ( ) {
1820+ if ( this . isInFlyout ) {
1821+ return ;
1822+ }
1823+ this . setFieldValue ( neuronNameGenerator . findLegalName ( this . getFieldValue ( 'NAME' ) , this , "out" ) , 'NAME' ) ;
1824+ } ,
1825+ validateName : function ( name ) {
1826+ return neuronNameGenerator . findLegalName ( name , this . sourceBlock_ , 'out' ) ;
17781827 }
17791828} ;
17801829
0 commit comments