1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const utils_1 = require("./utils");
|
4 | const fact_1 = require("./fact");
|
5 | const md5Hex = require('md5-hex');
|
6 | exports.ROOT_NAME = "bom";
|
7 | class Situation {
|
8 | constructor(parent, situation) {
|
9 | this.id = situation.id;
|
10 | this.name = situation.name;
|
11 | this.description = situation.description;
|
12 | this.version = situation.version;
|
13 | this.facts = [];
|
14 | situation.facts && situation.facts.forEach((fact) => {
|
15 | this.facts.push(new fact_1.default(fact));
|
16 | });
|
17 | }
|
18 | getFactNames() {
|
19 | return this.facts.map(fact => fact.path);
|
20 | }
|
21 | getOutputs() {
|
22 | return this.facts.filter(fact => fact.rule && fact.rule.statedAs);
|
23 | }
|
24 | getRules() {
|
25 | const result = this.getOutputs().map((fact) => {
|
26 | if (fact.rule) {
|
27 | let code = fact.rule.getRule();
|
28 | const vars = utils_1.extractUndeclaredVarsFromCode(code);
|
29 | vars.forEach((name) => {
|
30 | const factName = new RegExp(name, "gi");
|
31 | code = code.replace(factName, `${exports.ROOT_NAME}.${fact.getPath(name)}`);
|
32 | });
|
33 | return {
|
34 | name: fact.path || fact.rule.name,
|
35 | code: code
|
36 | };
|
37 | }
|
38 | else {
|
39 | return {
|
40 | name: fact.path || "",
|
41 | code: ""
|
42 | };
|
43 | }
|
44 | });
|
45 | return result;
|
46 | }
|
47 | schemaVersion() {
|
48 | const data = this.facts.map(fact => fact.path + "." + (fact.rule && fact.rule.dataType)).join("|");
|
49 | return md5Hex(data);
|
50 | }
|
51 | missingExpectedFacts() {
|
52 | let missing = [];
|
53 | this.facts.forEach((fact) => {
|
54 | const expectedFacts = fact.expectedFacts.map(name => fact.getPath(name));
|
55 | missing = [...missing, ...expectedFacts];
|
56 | });
|
57 | return missing.filter((name) => {
|
58 | const factName = new RegExp(name, "gi");
|
59 | return this.facts.find((fact) => {
|
60 | return fact.path && fact.path.match(factName);
|
61 | }) === undefined;
|
62 | });
|
63 | }
|
64 | sampleJson() {
|
65 | const sampleBom = {};
|
66 | this.missingExpectedFacts().forEach((name) => {
|
67 | utils_1.addValueToJsonPath(sampleBom, `${exports.ROOT_NAME}.${name}`, "missing");
|
68 | });
|
69 | this.facts.forEach((fact) => {
|
70 | if (fact.rule && fact.rule.statedAs === undefined) {
|
71 | const path = `${exports.ROOT_NAME}.${fact.path}`;
|
72 | let value = fact.rule.sampleValue !== undefined ? fact.rule.sampleValue : utils_1.sampleValue(fact.rule.dataType);
|
73 | utils_1.addValueToJsonPath(sampleBom, path, value);
|
74 | }
|
75 | });
|
76 | return sampleBom;
|
77 | }
|
78 | sampleJsonWithDataTypes() {
|
79 | const sampleBom = {};
|
80 | this.missingExpectedFacts().forEach((name) => {
|
81 | utils_1.addValueToJsonPath(sampleBom, `${exports.ROOT_NAME}.${name}`, "unknown:0");
|
82 | });
|
83 | this.facts.forEach((fact) => {
|
84 | const path = `${exports.ROOT_NAME}.${fact.path}`;
|
85 | let value = (fact.rule && fact.rule.dataType) || "string";
|
86 | let id = (fact.rule && fact.rule.id) || 0;
|
87 | utils_1.addValueToJsonPath(sampleBom, path, `${value}:${id}`);
|
88 | });
|
89 | return sampleBom;
|
90 | }
|
91 | }
|
92 | exports.default = Situation;
|
93 |
|
\ | No newline at end of file |