-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced_matching.go
894 lines (756 loc) · 22.9 KB
/
advanced_matching.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
package cpe
import (
"fmt"
"regexp"
"strings"
)
// AdvancedMatchOptions 定义了高级匹配选项
type AdvancedMatchOptions struct {
// 是否使用正则表达式匹配
UseRegex bool
// 是否忽略大小写
IgnoreCase bool
// 是否使用模糊匹配
UseFuzzyMatch bool
// 是否只匹配常见字段 (part, vendor, product, version)
MatchCommonOnly bool
// 部分匹配
PartialMatch bool
// 匹配模式 (exact, subset, superset, distance)
MatchMode string
// 版本比较模式 (exact, greater, less, range)
VersionCompareMode string
// 版本低界限
VersionLower string
// 版本高界限
VersionUpper string
// 字段特定选项
FieldOptions map[string]FieldMatchOption
// 匹配得分阈值 (0.0-1.0)
ScoreThreshold float64
}
// FieldMatchOption 定义字段特定的匹配选项
type FieldMatchOption struct {
// 匹配权重 (0.0-1.0)
Weight float64
// 是否必须匹配此字段
Required bool
// 匹配方法
MatchMethod string
}
// NewAdvancedMatchOptions 创建默认的高级匹配选项
func NewAdvancedMatchOptions() *AdvancedMatchOptions {
return &AdvancedMatchOptions{
UseRegex: false,
IgnoreCase: false,
UseFuzzyMatch: false,
MatchCommonOnly: false,
PartialMatch: false,
MatchMode: "exact",
VersionCompareMode: "exact",
VersionLower: "",
VersionUpper: "",
FieldOptions: make(map[string]FieldMatchOption),
ScoreThreshold: 0.7, // 默认要求70%的匹配度
}
}
// AdvancedMatchCPE 执行高级CPE匹配
func AdvancedMatchCPE(criteria *CPE, target *CPE, options *AdvancedMatchOptions) bool {
if criteria == nil || target == nil {
return false
}
// 使用默认选项如果没有提供
if options == nil {
options = NewAdvancedMatchOptions()
}
// 根据匹配模式选择不同的匹配策略
switch options.MatchMode {
case "exact":
// 基本字段匹配
return matchCommonFields(criteria, target, options)
case "subset":
// 子集匹配
return matchSubset(criteria, target, options)
case "superset":
// 超集匹配
return matchSuperset(criteria, target, options)
case "distance":
// 距离匹配
return matchDistance(criteria, target, options)
default:
// 如果未指定匹配模式或使用未知模式,继续尝试不同方式
}
// 如果启用了正则表达式匹配
if options.UseRegex {
return matchWithRegex(criteria, target, options)
}
// 如果启用了部分匹配
if options.PartialMatch {
return matchPartial(criteria, target, options)
}
// 默认使用常规字段匹配
return matchCommonFields(criteria, target, options)
}
// matchCommonFields 仅匹配常见字段
func matchCommonFields(criteria *CPE, target *CPE, options *AdvancedMatchOptions) bool {
// 匹配Part
if !matchField(string(criteria.Part.ShortName), string(target.Part.ShortName), options) {
return false
}
// 匹配Vendor
if !matchField(string(criteria.Vendor), string(target.Vendor), options) {
return false
}
// 匹配ProductName
if !matchField(string(criteria.ProductName), string(target.ProductName), options) {
return false
}
// 匹配Version - 根据选项决定使用哪种版本比较方式
if options.VersionCompareMode != "exact" {
// 使用版本比较逻辑
if !compareVersions(criteria, target, options) {
return false
}
} else {
// 使用普通字段匹配
if !matchField(string(criteria.Version), string(target.Version), options) {
return false
}
}
return true
}
// matchWithRegex 使用正则表达式匹配
func matchWithRegex(criteria *CPE, target *CPE, options *AdvancedMatchOptions) bool {
// 匹配Part
if !matchFieldWithRegex(string(criteria.Part.ShortName), string(target.Part.ShortName), options) {
return false
}
// 匹配Vendor
if !matchFieldWithRegex(string(criteria.Vendor), string(target.Vendor), options) {
return false
}
// 匹配ProductName
if !matchFieldWithRegex(string(criteria.ProductName), string(target.ProductName), options) {
return false
}
// 匹配Version
if !matchFieldWithRegex(string(criteria.Version), string(target.Version), options) {
return false
}
// 如果不仅匹配常见字段,还需匹配其它字段
if !options.MatchCommonOnly {
// 匹配Update
if !matchFieldWithRegex(string(criteria.Update), string(target.Update), options) {
return false
}
// 匹配Edition
if !matchFieldWithRegex(string(criteria.Edition), string(target.Edition), options) {
return false
}
// 匹配Language
if !matchFieldWithRegex(string(criteria.Language), string(target.Language), options) {
return false
}
// 匹配SoftwareEdition
if !matchFieldWithRegex(criteria.SoftwareEdition, target.SoftwareEdition, options) {
return false
}
// 匹配TargetSoftware
if !matchFieldWithRegex(criteria.TargetSoftware, target.TargetSoftware, options) {
return false
}
// 匹配TargetHardware
if !matchFieldWithRegex(criteria.TargetHardware, target.TargetHardware, options) {
return false
}
// 匹配Other
if !matchFieldWithRegex(criteria.Other, target.Other, options) {
return false
}
}
return true
}
// matchPartial 部分匹配 - 只匹配非空字段
func matchPartial(criteria *CPE, target *CPE, options *AdvancedMatchOptions) bool {
// 匹配Part (如果有值)
if string(criteria.Part.ShortName) != "" && string(criteria.Part.ShortName) != "*" {
if !matchField(string(criteria.Part.ShortName), string(target.Part.ShortName), options) {
return false
}
}
// 匹配Vendor (如果有值)
if string(criteria.Vendor) != "" && string(criteria.Vendor) != "*" {
if !matchField(string(criteria.Vendor), string(target.Vendor), options) {
return false
}
}
// 匹配ProductName (如果有值)
if string(criteria.ProductName) != "" && string(criteria.ProductName) != "*" {
if !matchField(string(criteria.ProductName), string(target.ProductName), options) {
return false
}
}
// 匹配Version (如果有值)
if string(criteria.Version) != "" && string(criteria.Version) != "*" {
// 如果使用版本比较
if options.VersionCompareMode != "exact" {
if !compareVersions(criteria, target, options) {
return false
}
} else {
if !matchField(string(criteria.Version), string(target.Version), options) {
return false
}
}
}
// 如果不仅匹配常见字段,还需匹配其它非空字段
if !options.MatchCommonOnly {
// 匹配Update (如果有值)
if string(criteria.Update) != "" && string(criteria.Update) != "*" {
if !matchField(string(criteria.Update), string(target.Update), options) {
return false
}
}
// 匹配Edition (如果有值)
if string(criteria.Edition) != "" && string(criteria.Edition) != "*" {
if !matchField(string(criteria.Edition), string(target.Edition), options) {
return false
}
}
// 匹配Language (如果有值)
if string(criteria.Language) != "" && string(criteria.Language) != "*" {
if !matchField(string(criteria.Language), string(target.Language), options) {
return false
}
}
// 匹配SoftwareEdition (如果有值)
if criteria.SoftwareEdition != "" && criteria.SoftwareEdition != "*" {
if !matchField(criteria.SoftwareEdition, target.SoftwareEdition, options) {
return false
}
}
// 匹配TargetSoftware (如果有值)
if criteria.TargetSoftware != "" && criteria.TargetSoftware != "*" {
if !matchField(criteria.TargetSoftware, target.TargetSoftware, options) {
return false
}
}
// 匹配TargetHardware (如果有值)
if criteria.TargetHardware != "" && criteria.TargetHardware != "*" {
if !matchField(criteria.TargetHardware, target.TargetHardware, options) {
return false
}
}
// 匹配Other (如果有值)
if criteria.Other != "" && criteria.Other != "*" {
if !matchField(criteria.Other, target.Other, options) {
return false
}
}
}
return true
}
// matchField 匹配单个字段值,考虑匹配选项
func matchField(a, b string, options *AdvancedMatchOptions) bool {
if a == "*" || b == "*" {
return true
}
if a == "-" && b == "-" {
return true
}
if options.IgnoreCase {
a = strings.ToLower(a)
b = strings.ToLower(b)
}
if options.UseFuzzyMatch {
// 简单的模糊匹配实现:检查一个是否包含另一个
return strings.Contains(a, b) || strings.Contains(b, a)
}
return a == b
}
// matchFieldWithRegex 使用正则表达式匹配字段
func matchFieldWithRegex(a, b string, options *AdvancedMatchOptions) bool {
// 如果a为空,表示没有约束条件,匹配成功
if a == "" || a == "*" {
return true
}
// 如果b为空或是NA值,不匹配
if b == "" || b == "-" {
return false
}
// 如果b是通配符,匹配成功
if b == "*" {
return true
}
// 创建正则表达式
var re *regexp.Regexp
var err error
// 处理不同的选项
if options != nil && options.IgnoreCase {
// 忽略大小写,添加(?i)前缀
re, err = regexp.Compile("(?i)" + a)
} else {
re, err = regexp.Compile(a)
}
// 如果正则表达式有问题,则执行精确匹配
if err != nil {
if options != nil && options.IgnoreCase {
return strings.EqualFold(a, b)
}
return a == b
}
// 执行正则表达式匹配
return re.MatchString(b)
}
// matchNonVersionFields 匹配除版本外的所有字段
func matchNonVersionFields(criteria *CPE, target *CPE, options *AdvancedMatchOptions) bool {
// 匹配Part
if !matchField(string(criteria.Part.ShortName), string(target.Part.ShortName), options) {
return false
}
// 匹配Vendor
if !matchField(string(criteria.Vendor), string(target.Vendor), options) {
return false
}
// 匹配ProductName
if !matchField(string(criteria.ProductName), string(target.ProductName), options) {
return false
}
// 如果不仅匹配常见字段,还需匹配其它字段
if !options.MatchCommonOnly {
// 非必须字段,可以返回true即使这些字段不匹配
// 计算匹配得分,如果得分超过阈值,返回true
totalWeight := 3.0 // Part, Vendor, Product已经匹配,权重各1.0
matchedWeight := 3.0
// 使用默认字段权重
fieldWeights := map[string]float64{
"update": 0.6,
"edition": 0.6,
"language": 0.4,
"softwareEdition": 0.4,
"targetSoftware": 0.4,
"targetHardware": 0.4,
"other": 0.2,
}
// Update
w := fieldWeights["update"]
totalWeight += w
if matchField(string(criteria.Update), string(target.Update), options) {
matchedWeight += w
}
// Edition
w = fieldWeights["edition"]
totalWeight += w
if matchField(string(criteria.Edition), string(target.Edition), options) {
matchedWeight += w
}
// Language
w = fieldWeights["language"]
totalWeight += w
if matchField(string(criteria.Language), string(target.Language), options) {
matchedWeight += w
}
// SoftwareEdition
w = fieldWeights["softwareEdition"]
totalWeight += w
if matchField(criteria.SoftwareEdition, target.SoftwareEdition, options) {
matchedWeight += w
}
// TargetSoftware
w = fieldWeights["targetSoftware"]
totalWeight += w
if matchField(criteria.TargetSoftware, target.TargetSoftware, options) {
matchedWeight += w
}
// TargetHardware
w = fieldWeights["targetHardware"]
totalWeight += w
if matchField(criteria.TargetHardware, target.TargetHardware, options) {
matchedWeight += w
}
// Other
w = fieldWeights["other"]
totalWeight += w
if matchField(criteria.Other, target.Other, options) {
matchedWeight += w
}
// 计算匹配得分,如果得分超过0.7,则匹配成功
score := matchedWeight / totalWeight
return score >= 0.7
}
return true
}
// compareVersions 比较CPE版本
func compareVersions(criteria *CPE, target *CPE, options *AdvancedMatchOptions) bool {
criteriaVersion := string(criteria.Version)
targetVersion := string(target.Version)
// 如果任一版本为通配符或NA,使用标准匹配规则
if criteriaVersion == "*" || targetVersion == "*" || criteriaVersion == "-" || targetVersion == "-" {
return matchField(criteriaVersion, targetVersion, options)
}
// 根据版本比较模式执行比较
switch options.VersionCompareMode {
case "greater":
// target版本必须大于criteria版本
return compareVersionStrings(targetVersion, criteriaVersion) > 0
case "greaterOrEqual":
// target版本必须大于等于criteria版本
return compareVersionStrings(targetVersion, criteriaVersion) >= 0
case "less":
// target版本必须小于criteria版本
return compareVersionStrings(targetVersion, criteriaVersion) < 0
case "lessOrEqual":
// target版本必须小于等于criteria版本
return compareVersionStrings(targetVersion, criteriaVersion) <= 0
case "range":
// 版本必须在指定范围内
if options.VersionLower != "" && compareVersionStrings(targetVersion, options.VersionLower) < 0 {
return false
}
if options.VersionUpper != "" && compareVersionStrings(targetVersion, options.VersionUpper) > 0 {
return false
}
return true
default:
// 默认使用精确匹配
return criteriaVersion == targetVersion
}
}
// compareVersionStrings 比较两个版本字符串
// 返回值: -1 表示 v1 < v2, 0 表示 v1 == v2, 1 表示 v1 > v2
func compareVersionStrings(v1, v2 string) int {
// 分割版本号
parts1 := splitVersion(v1)
parts2 := splitVersion(v2)
// 逐部分比较
for i := 0; i < len(parts1) && i < len(parts2); i++ {
p1 := parts1[i]
p2 := parts2[i]
// 比较数字部分
if isNumeric(p1) && isNumeric(p2) {
n1, _ := parseInt(p1)
n2, _ := parseInt(p2)
if n1 < n2 {
return -1
}
if n1 > n2 {
return 1
}
continue
}
// 比较字符串部分
if p1 < p2 {
return -1
}
if p1 > p2 {
return 1
}
}
// 处理不同长度的版本号
if len(parts1) < len(parts2) {
return -1
}
if len(parts1) > len(parts2) {
return 1
}
// 版本号相等
return 0
}
// splitVersion 将版本字符串分割为组件
func splitVersion(version string) []string {
// 通过常见分隔符分割
re := regexp.MustCompile(`[\.\-_]`)
parts := re.Split(version, -1)
var result []string
// 将字母数字混合的部分进一步分割
for _, part := range parts {
if part == "" {
continue
}
// 分离字母和数字
re := regexp.MustCompile(`(\d+)|([a-zA-Z]+)`)
matches := re.FindAllString(part, -1)
result = append(result, matches...)
}
return result
}
// isNumeric 检查字符串是否为数字
func isNumeric(s string) bool {
_, err := parseInt(s)
return err == nil
}
// parseInt 将字符串解析为整数
func parseInt(s string) (int, error) {
var result int
_, err := fmt.Sscanf(s, "%d", &result)
return result, err
}
// matchSubset 检查target是否是criteria的子集
func matchSubset(criteria *CPE, target *CPE, options *AdvancedMatchOptions) bool {
if criteria == nil || target == nil {
return false
}
// 检查Part
if criteria.Part.ShortName != "" && criteria.Part.ShortName != "*" &&
criteria.Part.ShortName != target.Part.ShortName {
return false
}
// 检查Vendor
if string(criteria.Vendor) != "" && string(criteria.Vendor) != "*" &&
!matchField(string(criteria.Vendor), string(target.Vendor), options) {
return false
}
// 检查Product
if string(criteria.ProductName) != "" && string(criteria.ProductName) != "*" &&
!matchField(string(criteria.ProductName), string(target.ProductName), options) {
return false
}
// 如果需要匹配版本
if !options.MatchCommonOnly {
// 检查Version
if string(criteria.Version) != "" && string(criteria.Version) != "*" &&
!compareVersions(criteria, target, options) {
return false
}
// 检查Update
if string(criteria.Update) != "" && string(criteria.Update) != "*" &&
!matchField(string(criteria.Update), string(target.Update), options) {
return false
}
// 检查Edition
if string(criteria.Edition) != "" && string(criteria.Edition) != "*" &&
!matchField(string(criteria.Edition), string(target.Edition), options) {
return false
}
// 检查Language
if string(criteria.Language) != "" && string(criteria.Language) != "*" &&
!matchField(string(criteria.Language), string(target.Language), options) {
return false
}
// 检查SoftwareEdition
if criteria.SoftwareEdition != "" && criteria.SoftwareEdition != "*" &&
!matchField(criteria.SoftwareEdition, target.SoftwareEdition, options) {
return false
}
// 检查TargetSoftware
if criteria.TargetSoftware != "" && criteria.TargetSoftware != "*" &&
!matchField(criteria.TargetSoftware, target.TargetSoftware, options) {
return false
}
// 检查TargetHardware
if criteria.TargetHardware != "" && criteria.TargetHardware != "*" &&
!matchField(criteria.TargetHardware, target.TargetHardware, options) {
return false
}
// 检查Other
if criteria.Other != "" && criteria.Other != "*" &&
!matchField(criteria.Other, target.Other, options) {
return false
}
}
return true
}
// matchSuperset 检查target是否是criteria的超集
func matchSuperset(criteria *CPE, target *CPE, options *AdvancedMatchOptions) bool {
if criteria == nil || target == nil {
return false
}
// 检查Part
if target.Part.ShortName != "" && target.Part.ShortName != "*" &&
criteria.Part.ShortName != target.Part.ShortName {
return false
}
// 检查Vendor
if string(target.Vendor) != "" && string(target.Vendor) != "*" &&
!matchField(string(criteria.Vendor), string(target.Vendor), options) {
return false
}
// 检查Product
if string(target.ProductName) != "" && string(target.ProductName) != "*" &&
!matchField(string(criteria.ProductName), string(target.ProductName), options) {
return false
}
// 如果需要匹配版本
if !options.MatchCommonOnly {
// 检查Version - 超集匹配中,版本比较是特殊的
if string(target.Version) != "" && string(target.Version) != "*" {
// 在超集匹配中,如果目标有指定版本,标准也必须有对应版本
if string(criteria.Version) == "" || string(criteria.Version) == "*" {
return false
}
// 使用版本比较
if !compareVersions(criteria, target, options) {
return false
}
}
// 检查Update
if string(target.Update) != "" && string(target.Update) != "*" &&
!matchField(string(criteria.Update), string(target.Update), options) {
return false
}
// 检查Edition
if string(target.Edition) != "" && string(target.Edition) != "*" &&
!matchField(string(criteria.Edition), string(target.Edition), options) {
return false
}
// 检查Language
if string(target.Language) != "" && string(target.Language) != "*" &&
!matchField(string(criteria.Language), string(target.Language), options) {
return false
}
// 检查SoftwareEdition
if target.SoftwareEdition != "" && target.SoftwareEdition != "*" &&
!matchField(criteria.SoftwareEdition, target.SoftwareEdition, options) {
return false
}
// 检查TargetSoftware
if target.TargetSoftware != "" && target.TargetSoftware != "*" &&
!matchField(criteria.TargetSoftware, target.TargetSoftware, options) {
return false
}
// 检查TargetHardware
if target.TargetHardware != "" && target.TargetHardware != "*" &&
!matchField(criteria.TargetHardware, target.TargetHardware, options) {
return false
}
// 检查Other
if target.Other != "" && target.Other != "*" &&
!matchField(criteria.Other, target.Other, options) {
return false
}
}
return true
}
// matchDistance 基于字段相似度的匹配
func matchDistance(criteria *CPE, target *CPE, options *AdvancedMatchOptions) bool {
// 计算总权重和匹配权重
totalWeight := 0.0
matchedWeight := 0.0
// 使用默认字段权重
fieldWeights := map[string]float64{
"part": 1.0,
"vendor": 1.0,
"product": 1.0,
"version": 0.8,
"update": 0.6,
"edition": 0.6,
"language": 0.4,
"softwareEdition": 0.4,
"targetSoftware": 0.4,
"targetHardware": 0.4,
"other": 0.2,
}
// 覆盖默认权重为用户自定义权重
for field, option := range options.FieldOptions {
if _, ok := fieldWeights[field]; ok {
fieldWeights[field] = option.Weight
}
}
// Part
w := fieldWeights["part"]
totalWeight += w
if matchField(string(criteria.Part.ShortName), string(target.Part.ShortName), options) {
matchedWeight += w
} else if isRequiredField(options, "part") {
return false
}
// Vendor
w = fieldWeights["vendor"]
totalWeight += w
if matchField(string(criteria.Vendor), string(target.Vendor), options) {
matchedWeight += w
} else if isRequiredField(options, "vendor") {
return false
}
// Product
w = fieldWeights["product"]
totalWeight += w
if matchField(string(criteria.ProductName), string(target.ProductName), options) {
matchedWeight += w
} else if isRequiredField(options, "product") {
return false
}
// Version
w = fieldWeights["version"]
totalWeight += w
if options.VersionCompareMode != "exact" {
if compareVersions(criteria, target, options) {
matchedWeight += w
} else if isRequiredField(options, "version") {
return false
}
} else {
if matchField(string(criteria.Version), string(target.Version), options) {
matchedWeight += w
} else if isRequiredField(options, "version") {
return false
}
}
// 如果需要匹配所有字段
if !options.MatchCommonOnly {
// Update
w = fieldWeights["update"]
totalWeight += w
if matchField(string(criteria.Update), string(target.Update), options) {
matchedWeight += w
} else if isRequiredField(options, "update") {
return false
}
// Edition
w = fieldWeights["edition"]
totalWeight += w
if matchField(string(criteria.Edition), string(target.Edition), options) {
matchedWeight += w
} else if isRequiredField(options, "edition") {
return false
}
// Language
w = fieldWeights["language"]
totalWeight += w
if matchField(string(criteria.Language), string(target.Language), options) {
matchedWeight += w
} else if isRequiredField(options, "language") {
return false
}
// SoftwareEdition
w = fieldWeights["softwareEdition"]
totalWeight += w
if matchField(criteria.SoftwareEdition, target.SoftwareEdition, options) {
matchedWeight += w
} else if isRequiredField(options, "softwareEdition") {
return false
}
// TargetSoftware
w = fieldWeights["targetSoftware"]
totalWeight += w
if matchField(criteria.TargetSoftware, target.TargetSoftware, options) {
matchedWeight += w
} else if isRequiredField(options, "targetSoftware") {
return false
}
// TargetHardware
w = fieldWeights["targetHardware"]
totalWeight += w
if matchField(criteria.TargetHardware, target.TargetHardware, options) {
matchedWeight += w
} else if isRequiredField(options, "targetHardware") {
return false
}
// Other
w = fieldWeights["other"]
totalWeight += w
if matchField(criteria.Other, target.Other, options) {
matchedWeight += w
} else if isRequiredField(options, "other") {
return false
}
}
// 计算匹配得分
score := matchedWeight / totalWeight
return score >= options.ScoreThreshold
}
// isRequiredField 检查字段是否是必需的
func isRequiredField(options *AdvancedMatchOptions, field string) bool {
option, ok := options.FieldOptions[field]
return ok && option.Required
}