0

我基本上有两种类型的块:规则块和事实块(就像在 Prolog 中一样)。它们都可以相互连接。规则块需要两个“事实”类型的输入。但是,不可能将多个附加的事实块作为单个输入。因此,每当附加事实块作为规则块的输入时,我必须将“setNextStatement”设置为 false。

这就是我在事实块中尝试做的事情:

this.setOnChange(function(changeEvent) {
    if(changeEvent.type == Blockly.Events.MOVE) {
        let prevBlock = this.getPreviousBlock();
        if(prevBlock != null && prevBlock.type == "rule") {
            let nextBlock = prevBlock.getNextBlock();
            if((nextBlock != null && nextBlock != this) || (nextBlock == null)) {

                this.setNextStatement(false);
            }
        } else {
            this.setNextStatement(true);
        }
    }
});

当然还有规则块:

Blockly.Blocks['rule'] = {
    init: function() {
        this.appendDummyInput("RULE_DATA")
            .appendField('Rule: ');

        this.appendStatementInput('INPUT_HEAD')
            .setCheck("fact")
            .appendField("Head");

        this.appendStatementInput('INPUT_BODY')
            .setCheck("fact")
            .appendField("Body");
...

这实际上有效,但是当我将事实与规则的输入部分分开时,我总是会收到以下错误:

Uncaught Error: Connection lists did not match in length.
    at Blockly.BlockSvg.Blockly.Block.getMatchingConnection (blockly_compressed.js:1447)
    at Blockly.InsertionMarkerManager.connectMarker_ (blockly_compressed.js:1125)
    at Blockly.InsertionMarkerManager.showPreview_ (blockly_compressed.js:1118)
    at Blockly.InsertionMarkerManager.maybeShowPreview_ (blockly_compressed.js:1117)
    at Blockly.InsertionMarkerManager.update (blockly_compressed.js:1110)
    at Blockly.BlockDragger.dragBlock (blockly_compressed.js:1130)
    at Blockly.TouchGesture.Blockly.Gesture.startDraggingBlock_ (blockly_compressed.js:1177)
    at Blockly.TouchGesture.Blockly.Gesture.updateIsDraggingBlock_ (blockly_compressed.js:1174)
    at Blockly.TouchGesture.Blockly.Gesture.updateIsDragging_ (blockly_compressed.js:1176)
    at Blockly.TouchGesture.Blockly.Gesture.updateFromEvent_ (blockly_compressed.js:1171)

有人有什么想法吗?

4

1 回答 1

1

嗨,您需要的只是从 blockly 覆盖 getMatchingConnection 函数并评论函数中的检查

window.Blockly.Block.prototype.getMatchingConnection = function(otherBlock, conn) {
  var connections = this.getConnections_(true);
  var otherConnections = otherBlock.getConnections_(true);
  // if (connections.length !== otherConnections.length) {
  //   throw Error("Connection lists did not match in length.");
  // }
  for (var i = 0; i < otherConnections.length; i++) {
    if (otherConnections[i] === conn) {
      return connections[i];
    }
  }
  return null;
};
于 2019-09-11T03:57:51.393 回答