跳至內容
Astera Technology

找出並行計劃引用尚未產生的結果

同批呼叫結果尚未可引用,逐批驗參數依賴而不執行工具。

下載完整個案資料及參考答案 ↓

訂明本例驗收任務

假設工具計劃由依次執行的並行批次組成。每步只可引用較早已完成批次的結果;同批即使列在前面也未可引用。呼叫編號不可重複,前批沒出現的引用都未可用。只驗計劃,不執行。

  1. 只記錄較早完成批次
  2. 核對各步結果依賴
  3. 查完整批才加入本批編號

引用未可用或編號重複須修計劃。

使用已填個案

原創假設輸入及確切答案鍵
個案已提供輸入預期結構化結果判斷及原因
依賴同批結果{"batches":[[{"id":"S1","needs":[]},{"id":"S2","needs":["S1"]}]]}{"errors":[{"call":"S2","error":"unavailable","dependency":"S1"}],"valid":false}同批列 S1 在前,也不代表已完成。
引用前批結果{"batches":[[{"id":"S1","needs":[]}],[{"id":"S2","needs":["S1"]}]]}{"errors":[],"valid":true}第二批開始前 S1 結果已可用。
依賴結果從未產生{"batches":[[{"id":"S2","needs":["S9"]}]]}{"errors":[{"call":"S2","error":"unavailable","dependency":"S9"}],"valid":false}前批沒有 S9,不虛構結果。
下載可編輯 CSV ↓

逐步處理 unknown-dependency 個案,須得到甚麼結果?原因是甚麼?

查看參考答案

{"errors":[{"call":"S2","error":"unavailable","dependency":"S9"}],"valid":false}。前批沒有 S9,不虛構結果。

執行完整本機驗算

另存為 .mjs,以 Node.js 執行;只驗提供的個案
import assert from 'node:assert/strict';

function evaluate(x){
 const done=new Set(),all=new Set(),errors=[];
 for(const batch of x.batches){for(const c of batch){if(all.has(c.id))errors.push({call:c.id,error:'duplicate'});all.add(c.id);for(const d of c.needs)if(!done.has(d))errors.push({call:c.id,error:'unavailable',dependency:d});}for(const c of batch)done.add(c.id);}
 return {errors,valid:errors.length===0};
}

const cases = [
  {
    "id": "same-batch-dependency",
    "input": {
      "batches": [
        [
          {
            "id": "S1",
            "needs": []
          },
          {
            "id": "S2",
            "needs": [
              "S1"
            ]
          }
        ]
      ]
    },
    "expected": {
      "errors": [
        {
          "call": "S2",
          "error": "unavailable",
          "dependency": "S1"
        }
      ],
      "valid": false
    }
  },
  {
    "id": "earlier-batch",
    "input": {
      "batches": [
        [
          {
            "id": "S1",
            "needs": []
          }
        ],
        [
          {
            "id": "S2",
            "needs": [
              "S1"
            ]
          }
        ]
      ]
    },
    "expected": {
      "errors": [],
      "valid": true
    }
  },
  {
    "id": "unknown-dependency",
    "input": {
      "batches": [
        [
          {
            "id": "S2",
            "needs": [
              "S9"
            ]
          }
        ]
      ]
    },
    "expected": {
      "errors": [
        {
          "call": "S2",
          "error": "unavailable",
          "dependency": "S9"
        }
      ],
      "valid": false
    }
  }
];
for (const c of cases) assert.deepEqual(evaluate(c.input), c.expected, c.id);
console.log(JSON.stringify({checked:cases.length,caseIds:cases.map(c=>c.id)}));
下載可執行例子 ↓

列被拒呼叫及依賴配對供覆核。批次次序有效不代表操作成功、所需時間或獲准使用工具。

相關資源