UNPKG

4.18 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright © 2019 Atomist, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.any = exports.all = exports.whenNot = void 0;
19const logger_1 = require("@atomist/automation-client/lib/util/logger");
20const PredicateMapping_1 = require("../PredicateMapping");
21const defaultPredicateMappingCostAnalyzer_1 = require("./defaultPredicateMappingCostAnalyzer");
22const PredicateMappingCostAnalyzer_1 = require("./PredicateMappingCostAnalyzer");
23/**
24 * Return the opposite of this predicate mapping
25 */
26function whenNot(t) {
27 return {
28 name: `not (${t.name})`,
29 mapping: async (pi) => !(await t.mapping(pi)),
30 structure: {
31 components: [t],
32 compositionStyle: PredicateMapping_1.PredicateMappingCompositionStyle.Not,
33 },
34 };
35}
36exports.whenNot = whenNot;
37/**
38 * Wrap all these predicates in a single predicate
39 * AND: Return true if all are satisfied
40 * @param {PredicateMapping} predicates
41 * @param analyzer analyzer to use for performance optimization
42 * @return {PredicateMapping}
43 */
44function all(predicates, analyzer = defaultPredicateMappingCostAnalyzer_1.DefaultPredicateMappingCostAnalyzer) {
45 return {
46 name: predicates.map(g => g.name).join(" && "),
47 mapping: async (pci) => optimizedAndEvaluation(predicates, analyzer)(pci),
48 structure: {
49 components: predicates,
50 compositionStyle: PredicateMapping_1.PredicateMappingCompositionStyle.And,
51 },
52 };
53}
54exports.all = all;
55/**
56 * Wrap all these predicates in a single predicate
57 * OR: Return true if any is satisfied
58 * @param {PredicateMapping} predicates
59 * @param analyzer analyzer to use for performance optimization
60 * @return {PredicateMapping}
61 */
62function any(predicates, analyzer = defaultPredicateMappingCostAnalyzer_1.DefaultPredicateMappingCostAnalyzer) {
63 return {
64 name: predicates.map(g => g.name).join(" || "),
65 mapping: async (pci) => {
66 // Cannot short-circuit this
67 const allResults = await gatherResults(predicates)(pci);
68 return allResults.includes(true);
69 },
70 structure: {
71 components: predicates,
72 compositionStyle: PredicateMapping_1.PredicateMappingCompositionStyle.Or,
73 },
74 };
75}
76exports.any = any;
77/**
78 * Evaluate predicates for an AND, running non-expensive ones first
79 * @param {Array<PredicateMapping<F>>} predicates
80 * @param {PredicateMappingCostAnalyzer<F>} analyzer
81 * @return {(f: F) => Promise<boolean>}
82 */
83function optimizedAndEvaluation(predicates, analyzer) {
84 const cheap = [];
85 const remaining = [];
86 for (const p of predicates) {
87 const cost = analyzer(p);
88 if (cost !== PredicateMappingCostAnalyzer_1.ExpectedPredicateMappingCost.expensive) {
89 cheap.push(p);
90 }
91 else {
92 remaining.push(p);
93 }
94 }
95 logger_1.logger.debug("Cheap: [%j], remaining: [%j]", cheap, remaining);
96 return async (pci) => {
97 const cheapResults = await gatherResults(cheap)(pci);
98 if (cheapResults.includes(false)) {
99 return false;
100 }
101 const remainingResults = await gatherResults(remaining)(pci);
102 return !remainingResults.includes(false);
103 };
104}
105function gatherResults(predicates) {
106 return pci => {
107 return Promise.all(predicates.map(async (pt) => {
108 const result = await pt.mapping(pci);
109 logger_1.logger.debug(`Result of PushTest '${pt.name}' was ${result}`);
110 return result;
111 }));
112 };
113}
114//# sourceMappingURL=predicateUtils.js.map
\No newline at end of file