Skip to content

Commit

Permalink
Compute confidence for composite correlators
Browse files Browse the repository at this point in the history
Also, the concept of correlator authority was replaced by correlator
tiers. The dependencies between correlators are now checked thoroughly.

Work in progress.
  • Loading branch information
mederly committed Aug 9, 2022
1 parent ab56844 commit 98b9c30
Show file tree
Hide file tree
Showing 21 changed files with 589 additions and 325 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ private static void addCorrelationDefinitionFromAttribute(
}
correlators.getItems().add(
new ItemsCorrelatorType()
.authority(attributeCorrelationDefBean.getAuthority())
.confidence(CloneUtil.clone(attributeCorrelationDefBean.getConfidence()))
.item(new ItemCorrelationType()
.path(itemPathBean.clone())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public static String identify(@Nullable AbstractCorrelatorType configBean) {
.append(configBean.getExtending())
.append("', ");
}
if (configBean.getTier() != null) {
sb.append("tier ")
.append(configBean.getTier())
.append(", ");
}
if (configBean.getOrder() != null) {
sb.append("order ")
.append(configBean.getOrder())
Expand All @@ -90,11 +95,6 @@ public static String identify(@Nullable AbstractCorrelatorType configBean) {
if (Boolean.FALSE.equals(configBean.isEnabled())) {
sb.append("disabled, ");
}
if (configBean.getAuthority() != null) {
sb.append("authority: ")
.append(configBean.getAuthority())
.append(", ");
}
sb.append("having ")
.append(configBean.asPrismContainerValue().size())
.append(" item(s)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,27 +200,69 @@
</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="order" type="xsd:int" minOccurs="0">
<xsd:element name="name" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Order in which this correlator is to be evaluated. (Related to other correlators.)
Smaller numbers go first. Entries with no order go last.
Name of the correlator. Should be unique in the given context (object type definition,
resource, or system-wide).
</xsd:documentation>
<xsd:appinfo>
<a:displayName>AbstractCorrelatorType.order</a:displayName>
<a:displayName>AbstractCorrelatorType.name</a:displayName>
<a:displayOrder>10</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>

<xsd:element name="authority" type="tns:CorrelatorAuthorityLevelType" minOccurs="0">
<xsd:element ref="tns:displayName" minOccurs="0">
<xsd:annotation>
<xsd:appinfo>
<a:displayName>AbstractCorrelatorType.displayName</a:displayName>
<a:displayOrder>20</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>

<xsd:element ref="tns:description" minOccurs="0">
<xsd:annotation>
<xsd:appinfo>
<a:displayName>AbstractCorrelatorType.description</a:displayName>
<a:displayOrder>30</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>

<xsd:element ref="tns:documentation" minOccurs="0">
<xsd:annotation>
<xsd:appinfo>
<a:displayName>AbstractCorrelatorType.documentation</a:displayName>
<a:displayOrder>40</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>

<xsd:element name="tier" type="xsd:int" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
TODO
Tier this correlator belongs to. All correlators in a given tier are evaluated together.
Only if they provide no decisive result (i.e. one above "owner" threshold), the next tier is evaluated.
Smaller numbers go first. Entries with no tier number present the last tier.
</xsd:documentation>
<xsd:appinfo>
<a:displayName>AbstractCorrelatorType.order</a:displayName>
<a:displayOrder>50</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>

<xsd:element name="order" type="xsd:int" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Order in which this correlator is to be evaluated. (Related to other correlators in the given tier.)
Smaller numbers go first. Entries with no order go last.
</xsd:documentation>
<xsd:appinfo>
<a:displayName>AbstractCorrelatorType.authority</a:displayName>
<a:displayOrder>15</a:displayOrder>
<a:displayName>AbstractCorrelatorType.order</a:displayName>
<a:displayOrder>60</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
Expand All @@ -234,7 +276,7 @@
<xsd:appinfo>
<a:since>4.6</a:since>
<a:displayName>AbstractCorrelatorType.confidence</a:displayName>
<a:displayOrder>20</a:displayOrder>
<a:displayOrder>70</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
Expand All @@ -246,7 +288,7 @@
<xsd:appinfo>
<a:since>4.6</a:since>
<a:displayName>AbstractCorrelatorType.thresholds</a:displayName>
<a:displayOrder>20</a:displayOrder>
<a:displayOrder>70</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
Expand All @@ -270,7 +312,7 @@
<xsd:appinfo>
<a:since>4.6</a:since>
<a:displayName>AbstractCorrelatorType.ignoreIfMatchedBy</a:displayName>
<a:displayOrder>20</a:displayOrder>
<a:displayOrder>80</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
Expand All @@ -282,7 +324,7 @@
</xsd:documentation>
<xsd:appinfo>
<a:displayName>AbstractCorrelatorType.enabled</a:displayName>
<a:displayOrder>30</a:displayOrder>
<a:displayOrder>90</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
Expand All @@ -296,7 +338,7 @@
</xsd:documentation>
<xsd:appinfo>
<a:displayName>AbstractCorrelatorType.ref</a:displayName>
<a:displayOrder>50</a:displayOrder>
<a:displayOrder>100</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
Expand All @@ -309,50 +351,10 @@
</xsd:documentation>
<xsd:appinfo>
<a:displayName>AbstractCorrelatorType.ref</a:displayName>
<a:displayOrder>50</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>

<xsd:element name="name" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Name of the correlator. Should be unique in the given context (object type definition,
resource, or system-wide).
</xsd:documentation>
<xsd:appinfo>
<a:displayName>AbstractCorrelatorType.name</a:displayName>
<a:displayOrder>100</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>

<xsd:element ref="tns:displayName" minOccurs="0">
<xsd:annotation>
<xsd:appinfo>
<a:displayName>AbstractCorrelatorType.displayName</a:displayName>
<a:displayOrder>110</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>

<xsd:element ref="tns:description" minOccurs="0">
<xsd:annotation>
<xsd:appinfo>
<a:displayName>AbstractCorrelatorType.description</a:displayName>
<a:displayOrder>120</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>

<xsd:element ref="tns:documentation" minOccurs="0">
<xsd:annotation>
<xsd:appinfo>
<a:displayName>AbstractCorrelatorType.documentation</a:displayName>
<a:displayOrder>130</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:long"/>
</xsd:complexType>
Expand Down Expand Up @@ -1023,17 +1025,6 @@
</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="authority" type="tns:CorrelatorAuthorityLevelType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
An authority - if this attribute creates its own correlation rule.
</xsd:documentation>
<xsd:appinfo>
<a:displayName>AbstractCorrelatorType.authority</a:displayName>
<a:displayOrder>15</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="confidence" type="tns:CorrelationConfidenceDefinitionType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (C) 2010-2022 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.model.api.correlator;

import java.io.Serializable;
import java.util.Objects;

import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

/**
* Equals/hashCode contract: looks after {@link #oid} and {@link #confidence} only.
*/
public class CandidateOwner<O extends ObjectType> implements Serializable {

@NotNull private final String oid;
@NotNull private final O object;
private Double confidence;

CandidateOwner(@NotNull O object, Double confidence) {
this.oid = MiscUtil.requireNonNull(
object.getOid(),
() -> new IllegalArgumentException("No oid of " + object));
this.object = object;
this.confidence = confidence;
}

public @NotNull String getOid() {
return oid;
}

public @NotNull O getObject() {
return object;
}

public Double getConfidence() {
return confidence;
}

@Override
public String toString() {
return "CandidateOwner{" +
"object=" + object +
", confidence=" + confidence +
'}';
}

Double addMatching(CandidateOwner<O> another) {
assert oid.equals(another.getOid());
Double anotherConfidence = another.getConfidence();
if (anotherConfidence != null) {
if (confidence == null) {
confidence = anotherConfidence;
} else {
confidence += anotherConfidence;
}
}
return confidence;
}

public boolean isAtLeast(double threshold) {
return threshold <= 0
|| confidence != null && confidence >= threshold;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CandidateOwner<?> that = (CandidateOwner<?>) o;
return oid.equals(that.oid) && Objects.equals(confidence, that.confidence);
}

@Override
public int hashCode() {
return Objects.hash(oid, confidence);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2010-2022 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.model.api.correlator;

import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class CandidateOwnerMap<O extends ObjectType> implements Serializable {

@NotNull private final Map<String, CandidateOwner<O>> map = new HashMap<>();

public static <O extends ObjectType> CandidateOwnerMap<O> from(Collection<CandidateOwner<O>> source) {
CandidateOwnerMap<O> map = new CandidateOwnerMap<>();
source.forEach(map::put);
return map;
}

public void put(@NotNull O candidate, @Nullable Double confidence) {
put(new CandidateOwner<>(candidate, confidence));
}

public void put(@NotNull CandidateOwner<O> candidateOwner) {
map.put(candidateOwner.getOid(), candidateOwner);
}

public @NotNull Collection<CandidateOwner<O>> values() {
return map.values();
}

public Double add(CandidateOwner<O> candidateOwner) {
CandidateOwner<O> existing = map.get(candidateOwner.getOid());
if (existing != null) {
return existing.addMatching(candidateOwner);
} else {
put(candidateOwner);
return candidateOwner.getConfidence();
}
}

public @NotNull CandidateOwnerMap<O> copyAtLeast(double threshold) {
CandidateOwnerMap<O> targetMap = new CandidateOwnerMap<>();
for (CandidateOwner<O> value : values()) {
if (value.isAtLeast(threshold)) {
targetMap.put(value);
}
}
return targetMap;
}

public Collection<CandidateOwner<O>> selectWithConfidenceAtLeast(double threshold) {
return map.values().stream()
.filter(candidateOwner -> candidateOwner.isAtLeast(threshold))
.collect(Collectors.toSet());
}
}

0 comments on commit 98b9c30

Please sign in to comment.