一个尝试生成笛卡尔积的块
This commit is contained in:
@@ -537,4 +537,141 @@ export const turn_to_int = {
|
||||
this.setOutput(true, Number);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_TOOLTIP_TOHEX)
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const generate_cartesian_product = {
|
||||
/**
|
||||
* Block for creating a list with any number of elements of any type.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(MATH_HUE);
|
||||
this.itemCount_ = 1;
|
||||
this.updateShape_();
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['lists_create_with_item'], this));
|
||||
this.appendValueInput('repeat')
|
||||
.appendField(Blockly.Msg.MIXLY_EVERY_PER_ELEPER_ELEMENT);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.CONTROLS_REPEAT_TITLE_TIMES);
|
||||
this.setPreviousStatement(false)
|
||||
this.setNextStatement(false)
|
||||
this.setOutput(true)
|
||||
this.setTooltip(Blockly.Msg.LISTS_CREATE_WITH_PYTHON_TOOLTIP);
|
||||
},
|
||||
/**
|
||||
* Create XML to represent list inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
container.setAttribute('items', this.itemCount_);
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the list inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
||||
this.updateShape_();
|
||||
},
|
||||
/**
|
||||
* Populate the mutator's dialog with this block's components.
|
||||
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||
* @return {!Blockly.Block} Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
decompose: function (workspace) {
|
||||
var containerBlock =
|
||||
workspace.newBlock('lists_create_with_container');
|
||||
containerBlock.initSvg();
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var itemBlock = workspace.newBlock('lists_create_with_item');
|
||||
itemBlock.initSvg();
|
||||
connection.connect(itemBlock.previousConnection);
|
||||
connection = itemBlock.nextConnection;
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
/**
|
||||
* Reconfigure this block based on the mutator dialog's components.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
compose: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
// Count number of inputs.
|
||||
var connections = [];
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
connections[i] = itemBlock.valueConnection_;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
i++;
|
||||
}
|
||||
this.itemCount_ = i;
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
if (connections[i]) {
|
||||
this.getInput('ADD' + i).connection.connect(connections[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Store pointers to any connected child blocks.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
saveConnections: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
var input = this.getInput('ADD' + i);
|
||||
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
||||
i++;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Modify this block to have the correct number of inputs.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('EMPTY')) {
|
||||
this.removeInput('EMPTY');
|
||||
} else {
|
||||
var i = 0;
|
||||
while (this.getInput('ADD' + i)) {
|
||||
this.removeInput('ADD' + i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
// Rebuild block.
|
||||
if (this.itemCount_ == 0) {
|
||||
this.appendDummyInput('EMPTY')
|
||||
.appendField(Blockly.Msg.MIXLY_EMPTY_REMINDER);
|
||||
} else {
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var input = this.appendValueInput('ADD' + i);
|
||||
if (i == 0) {
|
||||
input.appendField(Blockly.Msg.MIXLY_PRODUCT+Blockly.Msg.MIXLY_GENERATE_CARTESIAN_PRODUCT);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -307,4 +307,18 @@ export const turn_to_int = function (_, generator) {
|
||||
generator.definitions_.import_hexlify = "from ubinascii import hexlify";
|
||||
var str = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
|
||||
return ["hexlify(" + str + ').decode()', generator.ORDER_ATOMIC];
|
||||
}
|
||||
|
||||
export const generate_cartesian_product = function (_, generator) {
|
||||
generator.definitions_.import_itertools = "import itertools";
|
||||
var re = generator.valueToCode(this, 'repeat', generator.ORDER_ATOMIC);
|
||||
var code = new Array(this.itemCount_);
|
||||
var default_value = '0';
|
||||
for (var n = 0; n < this.itemCount_; n++) {
|
||||
code[n] = generator.valueToCode(this, 'ADD' + n,
|
||||
generator.ORDER_NONE) || default_value;
|
||||
}
|
||||
// var code = '[' + code.join(', ') + ']';
|
||||
// var code = 'itertools.product('+'repeat='+re+')';
|
||||
return [code, generator.ORDER_ATOMIC];
|
||||
}
|
||||
Reference in New Issue
Block a user