go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/clustering/rules/lang/merge.go (about) 1 // Copyright 2022 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package lang 16 17 import ( 18 "bytes" 19 "sort" 20 "strings" 21 ) 22 23 // Merge merges two failure association rules with a logical "OR". 24 func Merge(rule1 string, rule2 string) (string, error) { 25 r1, err := Parse(rule1) 26 if err != nil { 27 return "", err 28 } 29 r2, err := Parse(rule2) 30 if err != nil { 31 return "", err 32 } 33 // The final "OR" conjoined terms. 34 var allTerms []*boolTerm 35 allTerms = append(allTerms, r1.expr.Terms...) 36 allTerms = append(allTerms, r2.expr.Terms...) 37 38 // Sort the top-level OR'ed terms. 39 // A common case we see is a list of tests: 40 // test = "mytest://a" OR 41 // test = "mytest://b" ... 42 // and having the terms sorted makes it easier to 43 // read the rule. 44 var stringTerms []string 45 for _, term := range allTerms { 46 var buf bytes.Buffer 47 term.format(&buf) 48 stringTerms = append(stringTerms, buf.String()) 49 } 50 sort.Strings(stringTerms) 51 52 // Note that this is only valid because OR is the operator 53 // with the lowest precedence in our language. 54 // Otherwise we would have to be concerned about inserting 55 // parentheses. 56 return strings.Join(stringTerms, " OR\n"), nil 57 }