var model = {
playerName: "",
name: "",
race: {
selected: "human",
options: ["human", "elf", "dwarf", "halfling", "gnome"],
valid: true,
validate: validateSelection
},
class: {
selected: "wizard",
options: ["fighter", "wizard", "cleric", "rogue"],
valid: true,
validate: validateSelection,
updateOptions: function(next) {
// update available classes based on AD&D 1st and 2nd edition
var race = this.get("../race/selected");
var options = [];
if (race === "dwarf" || race === "halfling") {
options.push("fighter");
options.push("rogue");
} else if (race === "elf" || race === "gnome") {
options.push("wizard");
} else if (race === "human") {
options.push("cleric");
}
this.set("options", options);
return next();
}
}
};
function validateSelection(next) {
var selected = this.get("selected");
var options = this.get("options");
var index = options.indexOf(selected);
var valid = (index > -1);
this.set("valid", valid);
return next();
}
var InfernalEngine = require("infernal-engine");
var engine = new InfernalEngine();
engine.load(model);
engine.set("/race/selected", "halfling");
engine.infer(function() {
// Displays the full model state
console.log("Final State:");
console.log(JSON.stringify(engine.getFacts(), null, " "));
// Displays only what changed in the model during inference execution
console.log("Updated facts: ");
console.log(JSON.stringify(engine.getDiff(), null, " "));
});
console.log("started!");
// Press Shift+Enter to execute...