【问题标题】:How to prevent duplicate action execution in Bixby?如何防止 Bixby 中的重复操作执行?
【发布时间】:2019-05-22 03:29:36
【问题描述】:

我想实现一个胶囊,如果用户提供了计算所需的完整输入,或者如果用户没有在第一个请求中提供完整输入,则要求用户提供必要的输入。如果用户提供完整的请求,一切正常。如果用户没有提供完整的请求,但 Bixby 需要更多信息,我会遇到一些奇怪的行为,其中多次调用计算并且 Bixby 从另一个计算的结果中获取计算的必要信息,看起来像在调试图中。

为了更容易演示我的问题,我扩展了骰子样本胶囊 capsule-sample-dice 并将 numSidesnumDice 添加到 RollResultConcept,以便我可以访问结果中的骰子数和边数。 RollResult.model.bxb 现在看起来像这样:

structure (RollResultConcept) {
  description (The result object produced by the RollDice action.)
  property (sum) {
    type (SumConcept)
    min (Required)
    max (One)
  }
  property (roll) {
    description (The list of results for each dice roll.)
    type (RollConcept)
    min (Required)
    max (Many)
  }
  // The two properties below have been added
  property (numSides) {
    description (The number of sides that the dice of this roll have.)
    type (NumSidesConcept)
    min (Required)
    max (One)
  }
  property (numDice) {
    description (The number of dice in this roll.)
    type (NumDiceConcept)
    min (Required)
    max (One)
  }
}

我还在RollResult.view.bxb 中添加了single-lines,以便在掷骰后向用户显示边数和骰子数。 RollResult.view.bxb:

 result-view {
   match {
     RollResultConcept (rollResult)
   }

   render {
     layout {
       section {
         content {
           single-line {
             text {
               style (Detail_M)
               value ("Sum: #{value(rollResult.sum)}")
             }
           }
           single-line {
             text {
               style (Detail_M)
               value ("Rolls: #{value(rollResult.roll)}")
             }
           }
           // The two single-line below have been added
           single-line {
             text {
               style (Detail_M)
               value ("Dice: #{value(rollResult.numDice)}")
             }
           }
           single-line {
             text {
               style (Detail_M)
               value ("Sides: #{value(rollResult.numSides)}")
             }
           }
         }
       }
     }
   }
 }

编辑:我忘记添加我在RollDice.js 中更改的代码,见下文: RollDice.js

// RollDice
// Rolls a dice given a number of sides and a number of dice

// Main entry point
module.exports.function = function rollDice(numDice, numSides) {

  var sum = 0;
  var result = [];

  for (var i = 0; i < numDice; i++) {
    var roll = Math.ceil(Math.random() * numSides);
    result.push(roll);
    sum += roll;
  }

  // RollResult
  return {
    sum: sum,           // required Sum
    roll: result,       // required list Roll
    numSides: numSides, // required for numSides
    numDice: numDice    // required for numDice
  }
}

结束编辑


我现在在模拟器中运行以下查询

intent {
  goal: RollDice
  value: NumDiceConcept(2)
}

缺少所需的NumSidesConcept

调试视图显示下图,缺少NumSidesConcept(如预期的那样)。

我现在在模拟器中运行以下查询

intent {
  goal: RollDice
  value: NumDiceConcept(2)
  value: NumSidesConcept(6)
}

这会在调试视图中生成以下图表:

在我看来,为了得到结果,计算被执行了两次。我已经尝试将feature { transient } 分配给模型,但这并没有改变任何东西。谁能告诉我这里发生了什么?我是否不允许在输出中使用相同的原始模型,因为 Bixby 在尝试执行操作时会使用它们?

【问题讨论】:

    标签: bixby bixbystudio


    【解决方案1】:

    我尝试按照您的方式修改代码,但无法运行意图(成功)。

    开始编辑

    我在RollDice.js 中添加了额外的行,并且能够看到您所看到的计划。

    双重执行的原因是您连续运行了意图,Bixby 从第二个意图派生了您未在第一个意图中指定的 NumSidesConcept 的值,并执行了第一个意图。

    您可以通过在每个意图中为 NumSidesConcept 和 NumDiceConcept 提供一组不同的值来验证上述内容。

    如果您在这两个意图之间留出了足够的时间,那么结果会有所不同。在您的场景中,第一个意图正在等待 NumSidesConcept 可用,并且一旦 Planner 找到它(从第二个意图的结果),执行就完成了。

    如何避免这种情况?确保每个输入都有一个输入视图,以便 Bixby 可以提示用户输入未通过 NL(或 Aligned NL)的任何值。

    结束编辑

    这是另一种不需要更改 RollResultConcept 并且将根据您的期望工作的方法(访问result-view 中的骰子数和边数)

     result-view {
      match: RollResultConcept (rollResult) {
        from-output: RollDice(action)
      }
    
    
       render {
         layout {
           section {
             content {
               single-line {
                 text {
                   style (Detail_M)
                   value ("Sum: #{value(rollResult.sum)}")
                 }
               }
               single-line {
                 text {
                   style (Detail_M)
                   value ("Rolls: #{value(rollResult.roll)}")
                 }
               }
               // The two single-line below have been added
               single-line {
                 text {
                   style (Detail_M)
                   value ("Dice: #{value(action.numDice)}")
                 }
               }
               single-line {
                 text {
                   style (Detail_M)
                   value ("Sides: #{value(action.numSides)}")
                 }
               }
             }
           }
         }
       }
     }
    

    试一试,让我们知道它是否有效!

    【讨论】:

    • 感谢您的回答!我忘了在我的回答中包括我还编辑了 RollDice.js 以返回 numSides 和 numDice,这就是你无法运行意图的原因。我用代码编辑了我最初的问题。您的回答很有帮助,因为它告诉我我可以从以前的操作中访问值,但是我的问题仍然是为什么我的代码会执行两次计算?
    • 我修改了原始答案,说明了双重执行的原因以及如何避免它。感谢您提出这个问题!
    • 感谢您更新答案。我有一个后续问题。鉴于我在第一个意图要求更多输入后为用户提供了input-view,但用户决定无论如何使用 NL 输入再次询问相同的意图,那么我仍然会以相同的循环结束,其中多次计算正在做。我怎样才能防止这种情况发生?
    • 我不相信有办法阻止它。如果您真的想涵盖这种情况,您始终可以访问先前操作的值并相应地对您的概念进行建模。即在 RollResultConcept 中不包括 NumSidesConcept 和 NumDiceConcept。希望这会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 2013-12-31
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多