UNPKG

@gmetrixr/rjson

Version:
310 lines (309 loc) 15.7 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.rulePrintUtils = void 0; const chalk_1 = __importDefault(require("chalk")); const _1 = require("."); const __1 = require(".."); const R_1 = require("../../R"); const recordFactories_1 = require("../../recordFactories"); const ElementFactory_1 = require("../../recordFactories/ElementFactory"); const elements_1 = require("../elements"); const special_1 = require("../special"); const variables_1 = require("../variables"); /** * Separating out this class as this doesn't need to be included in production and also, * because this imports potentially heavy "chalk" dependency */ class rulePrintUtils { } exports.rulePrintUtils = rulePrintUtils; /** Get an instance of Console Rule Printer */ rulePrintUtils.crp = () => ConsoleRulePrinter.getInstance(); /** Get an instance of Friendly Rule Printer */ rulePrintUtils.frp = () => FriendlyRulePrinter.getInstance(); rulePrintUtils.generateRuleTextsAndPrint = (scene, varDefMap = (0, R_1.emptyROM)()) => { let ruleText; const sceneF = new recordFactories_1.SceneFactory(scene); for (const rule of sceneF.getRecords(R_1.RT.rule)) { ruleText = rulePrintUtils.crp().generateRuleText(rule, scene, varDefMap); rulePrintUtils.crp().consoleRuleTextPrinter(ruleText); } }; rulePrintUtils.generateFriendlyRuleTextsAndPrint = (project, sceneIds) => { const projectF = new recordFactories_1.ProjectFactory(project); const varDefROM = projectF.getROM(R_1.RT.variable); for (const sceneId of sceneIds) { const scene = projectF.getRecord(R_1.RT.scene, sceneId); if (scene !== undefined) { rulePrintUtils.frp().generateRuleTextsAndPrint(project, scene, varDefROM); } } }; rulePrintUtils.generateFriendlyRuleTexts = (project, sceneIds) => { const projectF = new recordFactories_1.ProjectFactory(project); const varDefROM = projectF.getROM(R_1.RT.variable); let rulesText = ""; const frp = rulePrintUtils.frp(); for (const sceneId of sceneIds) { const scene = projectF.getRecord(R_1.RT.scene, sceneId); if (scene !== undefined) { const sceneF = new recordFactories_1.SceneFactory(scene); for (const rule of sceneF.getRecords(R_1.RT.rule)) { const ruleText = frp.generateRuleText(rule, project, scene, varDefROM); rulesText += frp.friendlyRuleLine(ruleText); rulesText += "\n"; // new line } } } return rulesText; }; /** * Use this class via the singleton getter * Eg: ConsoleRulePrinter.getInstance().doSomething.... */ class ConsoleRulePrinter { constructor() { this.generateRuleTextsAndPrint = (scene, varDefMap = (0, R_1.emptyROM)()) => { let ruleText; const sceneF = new recordFactories_1.SceneFactory(scene); for (const rule of sceneF.getRecords(R_1.RT.rule)) { ruleText = this.generateRuleText(rule, scene, varDefMap); this.consoleRuleTextPrinter(ruleText); } }; this.generateRuleText = (rule, scene, varDefMap = (0, R_1.emptyROM)()) => { const rf = new R_1.RecordFactory(rule); const whenEventsArray = []; const thenActionsArray = []; rf.getRecords(R_1.RT.when_event).forEach(whenEvent => { //whenEventsArray.push(`${whenEvent.ce_type}(${whenEvent.ce_id}) ${cEventsDisplayNames[<ConnectionEvent>whenEvent.event]} ${whenEvent.properties}`.trim()); whenEventsArray.push(this.weText(whenEvent, scene, varDefMap)); }); rf.getRecords(R_1.RT.then_action).forEach(thenAction => { //thenActionsArray.push(`${thenAction.ce_type}(${thenAction.ce_id}) should ${cActionsDisplayNames[<ConnectionAction>thenAction.action]} ${thenAction.properties}`.trim()); thenActionsArray.push(this.taText(thenAction, scene, varDefMap)); }); return { ruleIdText: this.ruleIdText(rule), weTexts: whenEventsArray, weAndOr: rf.get(R_1.rtp.rule.events_and) ? "AND" : "OR", taTexts: thenActionsArray, }; }; this.consoleRuleTextPrinter = (ruleText) => { console.log(`${chalk_1.default.yellow.bold("RULE".padStart(5).padEnd(6))}${chalk_1.default.yellow(ruleText.ruleIdText)}`); let startText; for (let i = 0; i < ruleText.weTexts.length; i++) { startText = i === 0 ? "WHEN" : ruleText.weAndOr; console.log(`${chalk_1.default.green.bold(startText.padStart(5).padEnd(6))}${chalk_1.default.green(ruleText.weTexts[i])}`); } for (let i = 0; i < ruleText.taTexts.length; i++) { startText = i === 0 ? "THEN" : "AND"; console.log(`${chalk_1.default.blue.bold(startText.padStart(5).padEnd(6))}${chalk_1.default.blue(ruleText.taTexts[i])}`); } }; this.ruleIdText = (rule) => { var _a; return `${rule.name}(${rule.id}) ${(_a = rule.props.comment) !== null && _a !== void 0 ? _a : ""}`; }; this.weText = (we, scene, varDefMap = (0, R_1.emptyROM)(), values) => { var _a; const displayName = this.coIdToName(we.props.co_id, we.props.co_type, scene, varDefMap); const event = _1.rEventDisplayName[we.props.event]; let propertiesText = ""; if (we.props.properties !== undefined && we.props.properties.length !== 0) { propertiesText = `[${(_a = we.props.properties) === null || _a === void 0 ? void 0 : _a.join(",")}]`; } let valuesText = ""; if (values !== undefined && values.length !== 0) { valuesText = ` values[${values.join(",")}]`; } return `${displayName} ${event}${propertiesText}${valuesText}`; }; this.taText = (ta, scene, varDefMap = (0, R_1.emptyROM)(), values) => { var _a; const displayName = this.coIdToName(ta.props.co_id, ta.props.co_type, scene, varDefMap); const action = _1.rActionDisplayName[ta.props.action]; let propertiesText = ""; if (ta.props.properties !== undefined && ta.props.properties.length !== 0) { propertiesText = `[${(_a = ta.props.properties) === null || _a === void 0 ? void 0 : _a.join(",")}]`; } let valuesText = ""; if (values !== undefined && values.length !== 0) { valuesText = ` values[${values.join(",")}]`; } let delayText = ""; if (ta.props.delay !== undefined && ta.props.delay !== 0) { delayText = ` after ${ta.props.delay}s`; } return `${displayName} should ${action}${propertiesText}${valuesText}${delayText}`; }; this.coIdToName = (coId, coType, scene, varDefMap = (0, R_1.emptyROM)()) => { var _a, _b, _c, _d, _e, _f, _g; let name = ""; let type = "UnknownType"; if ((0, special_1.isSpecialType)(coType)) { type = special_1.specialElementDisplayNames[coType]; if (coType === special_1.SpecialType.scene) { name = (_a = scene.name) !== null && _a !== void 0 ? _a : ""; } } else if ((0, variables_1.isVariableType)(coType)) { type = coType; name = (_c = (_b = varDefMap.map[coId]) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : ""; } else if ((0, elements_1.isElementType)(coType)) { type = (_e = (_d = ElementFactory_1.ElementUtils.getElementDefinition(coType)) === null || _d === void 0 ? void 0 : _d.elementDefaultName) !== null && _e !== void 0 ? _e : ""; const e = new R_1.RecordFactory(scene).getAllDeepChildrenWithFilter(R_1.RT.element, e => e.id === coId); name = (_g = (_f = e[0]) === null || _f === void 0 ? void 0 : _f.name) !== null && _g !== void 0 ? _g : ""; } if (name === "") { return `${type}(${coId})`; } return `${type} ${name}(${coId})`; }; } } ConsoleRulePrinter.getInstance = () => { if (ConsoleRulePrinter.instance === undefined) { ConsoleRulePrinter.instance = new ConsoleRulePrinter(); } return ConsoleRulePrinter.instance; }; /** * Use this class via the singleton getter * Eg: FriendlyRulePrinter.getInstance().doSomething.... */ class FriendlyRulePrinter { constructor() { this.generateRuleTextsAndPrint = (project, scene, varDefMap = (0, R_1.emptyROM)()) => { let ruleText; const sceneF = new recordFactories_1.SceneFactory(scene); for (const rule of sceneF.getRecords(R_1.RT.rule)) { ruleText = this.generateRuleText(rule, project, scene, varDefMap); console.log(this.friendlyRuleLine(ruleText)); } }; this.generateRuleText = (rule, project, scene, varDefMap = (0, R_1.emptyROM)()) => { const rf = new R_1.RecordFactory(rule); const whenEventsArray = []; const thenActionsArray = []; rf.getRecords(R_1.RT.when_event).forEach(whenEvent => { whenEventsArray.push(this.weText(whenEvent, scene, varDefMap)); }); rf.getRecords(R_1.RT.then_action).forEach(thenAction => { thenActionsArray.push(this.taText(thenAction, project, scene, varDefMap)); }); return { ruleIdText: `${rule.name}${rule.props.comment ? " " + rule.props.comment : ""}`, weTexts: whenEventsArray, weAndOr: rf.get(R_1.rtp.rule.events_and) ? "and" : "or", taTexts: thenActionsArray, }; }; this.friendlyRuleLine = (ruleText) => { const ruleStart = `RULE ${ruleText.ruleIdText}`; let ruleMid = "When "; for (let i = 0; i < ruleText.weTexts.length; i++) { ruleMid += i === 0 ? "" : ` ${ruleText.weAndOr} `; ruleMid += ruleText.weTexts[i]; } let ruleEnd = "Then "; for (let i = 0; i < ruleText.taTexts.length; i++) { ruleEnd += i === 0 ? "" : " and "; ruleEnd += ruleText.taTexts[i]; } const ruleS = `${ruleStart}: ${ruleMid} ${ruleEnd}`; return ruleS; }; // * Used in cog to print matched triggerred events this.weText = (we, scene, varDefMap = (0, R_1.emptyROM)(), values) => { var _a; const displayName = this.coIdToName(we.props.co_id, we.props.co_type, scene, varDefMap); const event = _1.rEventDisplayName[we.props.event]; let propertiesText = ""; if (we.props.properties !== undefined && we.props.properties.length !== 0) { propertiesText = `[${(_a = we.props.properties) === null || _a === void 0 ? void 0 : _a.join(",")}]`; } let valuesText = ""; if (values !== undefined && values.length !== 0) { valuesText = ` values[${values.join(",")}]`; } return `${displayName} ${event}${propertiesText}${valuesText}`; }; // * Used in cog to print matched triggerred actions this.taText = (ta, project, scene, varDefMap = (0, R_1.emptyROM)(), values) => { var _a; const displayName = this.coIdToName(ta.props.co_id, ta.props.co_type, scene, varDefMap); const action = _1.rActionDisplayName[ta.props.action]; let propertiesText = ""; //Calculate propertiesText based on the action type if (ta.props.properties !== undefined && ta.props.properties.length !== 0) { const properties = ta.props.properties; let coIdElement, e, coIdScene, propsScene; switch (ta.props.action) { case __1.rn.RuleAction.point_to: //properties[0] is the element id coIdElement = properties[0]; e = new R_1.RecordFactory(scene).getAllDeepChildrenWithFilter(R_1.RT.element, e => e.id === coIdElement); propertiesText = this.coIdToName(properties[0], (_a = e[0]) === null || _a === void 0 ? void 0 : _a.props.element_type, scene, varDefMap); break; case __1.rn.RuleAction.change_scene: //properties[0] is the scene id coIdScene = properties[0]; propsScene = new recordFactories_1.ProjectFactory(project).getRecord(R_1.RT.scene, coIdScene); if ((propsScene === null || propsScene === void 0 ? void 0 : propsScene.name) !== undefined) { propertiesText = ` ${propsScene.name}`; //need a space before the scene name } break; default: propertiesText = `[${properties.join(",")}]`; break; } } //Values are needed only at runtime. Not while printing pre-existing rules let valuesText = ""; if (values !== undefined && values.length !== 0) { valuesText = ` values[${values.join(",")}]`; } let delayText = ""; if (ta.props.delay !== undefined && ta.props.delay !== 0) { delayText = ` after ${ta.props.delay}s`; } return `${displayName} should ${action}${propertiesText}${valuesText}${delayText}`; }; this.coIdToName = (coId, coType, scene, varDefMap = (0, R_1.emptyROM)()) => { var _a, _b, _c, _d, _e, _f, _g; let name = ""; let type = "UnknownType"; if ((0, special_1.isSpecialType)(coType)) { type = special_1.specialElementDisplayNames[coType]; if (coType === special_1.SpecialType.scene) { name = (_a = scene.name) !== null && _a !== void 0 ? _a : ""; } } else if ((0, variables_1.isVariableType)(coType)) { type = coType; name = (_c = (_b = varDefMap.map[coId]) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : ""; } else if ((0, elements_1.isElementType)(coType)) { type = (_e = (_d = ElementFactory_1.ElementUtils.getElementDefinition(coType)) === null || _d === void 0 ? void 0 : _d.elementDefaultName) !== null && _e !== void 0 ? _e : ""; const e = new R_1.RecordFactory(scene).getAllDeepChildrenWithFilter(R_1.RT.element, e => e.id === coId); name = (_g = (_f = e[0]) === null || _f === void 0 ? void 0 : _f.name) !== null && _g !== void 0 ? _g : ""; } if (name === "") { return `${type}`; } return `${type} ${name}`; }; } } FriendlyRulePrinter.getInstance = () => { if (FriendlyRulePrinter.instance === undefined) { FriendlyRulePrinter.instance = new FriendlyRulePrinter(); } return FriendlyRulePrinter.instance; };