Skip to content

Commit 89367db

Browse files
committed
better generator fpr default names
1 parent 296866e commit 89367db

3 files changed

Lines changed: 70 additions & 19 deletions

File tree

blockly_compressed.js

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blocks/robActions.js

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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-zA-ZüöäÜÄÖß$][a-zA-Z0-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

17091740
Blockly.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

17341769
Blockly.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

17601802
Blockly.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

msg/json/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"@metadata": {
33
"author": "Ellen Spertus <ellen.spertus@gmail.com>",
4-
"lastupdated": "2022-06-28 09:19:07.457620",
4+
"lastupdated": "2022-06-28 14:51:33.349889",
55
"locale": "en",
66
"messagedocumentation" : "qqq"
77
},

0 commit comments

Comments
 (0)