Rule chaining not working with Promise.all
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- javascript
- 領域
- backend
調査の方向性
まず issue の Promise.all の例を再現し、次に Engine.run と almanac.addRuntimeFact で使用されている runtime-fact の処理を調査します。samuel、joseph、anthony に対して、並列実行が逐次実行時と一致する対応する結果とイベントを生成すれば完了です。
索引モデルが issue の本文から書いたものです。
説明
If I run the engine sequentially, I am getting the desired result. If I run it in parallel, I am getting results only for the first run.
student-fav-subject.js
const { Engine } = require("json-rules-engine");
const apiClient = require("./student-api-client");
let engine = new Engine();
async function start() {
let mathematicsSubjectRule = {
conditions: {
all: [
{
fact: "student-information",
operator: "contains",
value: "Mathematics",
path: "$.subjects",
},
],
},
event: { type: "studies-mathematics" },
priority: 10,
onSuccess: async function (event, almanac) {
almanac.addRuntimeFact("studiesMathematics", true);
},
onFailure: function (event, almanac) {
almanac.addRuntimeFact("studiesMathematics", false);
},
};
engine.addRule(mathematicsSubjectRule);
let physicsFavoriteRule = {
conditions: {
all: [
{
fact: "studiesMathematics",
operator: "equal",
value: true,
},
{
fact: "student-information",
operator: "equal",
value: "Physics",
path: "$.favoriteSubject",
},
],
},
event: {
type: "physics-favorite-subject",
params: {
message: " has Physics as their favorite subject",
},
},
};
engine.addRule(physicsFavoriteRule);
engine.addFact("student-information", function (params, almanac) {
console.log("loading student information...");
return almanac.factValue("studentName").then((studentName) => {
return apiClient.getStudentInformation(studentName);
});
});
engine.on("success", (event, almanac, ruleResult) => {
almanac.factValue("studentName").then((studentName) => {
switch (event.type) {
case "physics-favorite-subject":
console.log(
studentName,
"HAS".green,
"Physics as their favorite subject"
);
}
});
});
engine.on("failure", (event, almanac, ruleResult) => {
almanac.factValue("studentName").then((studentName) => {
switch (event.type) {
case "physics-favorite-subject":
const studiesMathResult = ruleResult.conditions.all.find(
(condition) => condition.fact === "studiesMathematics"
);
if (studiesMathResult) {
if (studiesMathResult.result) {
console.log(
studentName,
"DOES NOT".red,
"have Physics as favorite subject"
);
} else {
console.log(
studentName,
"CANNOT NOT".red,
"have Physics as favorite subject as they do not study Mathematics"
);
}
}
}
});
});
Promise.all([
engine.run({ studentName: "samuel" }),
engine.run({ studentName: "joseph" }),
engine.run({ studentName: "anthony" }),
]);
// await engine.run({ studentName: "samuel" });
// await engine.run({ studentName: "joseph" });
// await engine.run({ studentName: "anthony" });
}
start();
student-api-client.js
"use strict";
require("colors");
const fs = require("fs");
let studentData = require("./student-data.json");
/**
* mock api client for retrieving student information
*/
module.exports = {
getStudentInformation: (studentName) => {
const message = 'loading student information for "' + studentName + '"';
console.log(message.dim);
return new Promise((resolve, reject) => {
setImmediate(() => {
resolve(studentData[studentName]);
});
});
},
};
student-data.json
{
"john": {
"favoriteSubject": "Mathematics",
"subjects": ["Physics", "Mathematics"]
},
"anthony": {
"favoriteSubject": "Physics",
"subjects": ["Physics", "Mathematics"]
},
"george": {
"favoriteSubject": "History",
"subjects": ["History", "Mathematics"]
},
"samuel": {
"favoriteSubject": "Physics",
"subjects": ["Physics", "History", "Geography", "Chemistry"]
},
"joseph": {
"favoriteSubject": "Economics",
"subjects": ["Economics", "Mathematics", "History"]
}
}
Output
Running sequentially
loading student information...
loading student information for "samuel"
samuel CANNOT NOT have Physics as favorite subject as they do not study Mathematics
loading student information...
loading student information for "joseph"
joseph DOES NOT have Physics as favorite subject
loading student information...
loading student information for "anthony"
anthony HAS Physics as their favorite subject
Running parallelly (Promise.all)
loading student information...
loading student information...
loading student information...
loading student information for "samuel"
loading student information for "joseph"
loading student information for "anthony"
samuel CANNOT NOT have Physics as favorite subject as they do not study Mathematics
- 主要言語
- JavaScript
- スター
- 3.1k
- フォーク
- 507
- PR マージ指標
- 30日以内にマージされた PR はありません
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
CacheControl/json-rules-engine のほかの issue
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
CacheControl/json-rules-engine#427 · リアクション 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 48/100
CacheControl/json-rules-engine#424 · コメント 1 件 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 25/100
CacheControl/json-rules-engine#421 · コメント 1 件 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 38/100
CacheControl/json-rules-engine#417 · リアクション 1 件 ·
CacheControl/json-rules-engine の issue をすべて見る
似ている issue
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
apache/cloudstack#14222 ·
-
Browser Waiting for: Product Owner
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
getsentry/sentry-javascript#24577 · コメント 1 件 ·
-
curation good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
amponce/archive-movie-browser#186 ·
-
light
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
aemdemos/patients-stryker#253 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
clerk/javascript#9852 ·