Rule chaining not working with Promise.all
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 35/100
- Type d'issue
- Bug
- Clarté
- Plutôt claire
- Activité
- À l'abandon
- Stack technique
- javascript
- Domaine
- backend
Piste de recherche
Commencez par reproduire l’exemple de Promise.all dans l’issue, puis examinez Engine.run et la gestion de runtime-fact utilisée par almanac.addRuntimeFact. Le travail est terminé lorsque les exécutions parallèles produisent les résultats et les événements correspondants pour samuel, joseph et anthony, conformément au comportement séquentiel.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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
- Langage dominant
- JavaScript
- Étoiles
- 3.1k
- Forks
- 507
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de CacheControl/json-rules-engine
-
Difficulté 4/5 3-5 jours Accessibilité débutants 35/100
CacheControl/json-rules-engine#427 · 1 réaction ·
-
Difficulté 4/5 3-5 jours Accessibilité débutants 35/100
-
Difficulté 3/5 1-2 jours Accessibilité débutants 48/100
CacheControl/json-rules-engine#424 · 1 commentaire ·
-
How to handle array of objects?Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 25/100
CacheControl/json-rules-engine#421 · 1 commentaire ·
-
Difficulté 3/5 1-2 jours Accessibilité débutants 38/100
CacheControl/json-rules-engine#417 · 1 réaction ·
Toutes les issues de CacheControl/json-rules-engine
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 1 jour
-
[quality] useFocusTrap's Shift+Tab wrap and non-Tab/non-Escape key arms are never driven end to endPeut-être pris @hivecommons-hive l’a pris aujourd’hui. Ouverteagent/quality hive/covered-by-pr hive/hosted-available-lke648397-260827-5n31 quality testing
Difficulté 2/5 1-3 heures Accessibilité débutants 85/100
Les mainteneurs répondent en général sous 1 jour
-
[aw] Upgrade availableOuverteagentic-workflows
Difficulté 1/5 Moins d'une heure Accessibilité débutants 85/100
githubnext/gh-aw-workshop#4220 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 92/100
JuliusBrussee/caveman#1189 ·
Les mainteneurs répondent en général sous 1 jour
-
priority:low ready-for-dev
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
OpenHands/extensions#738 ·
Les mainteneurs répondent en général sous 1 jour