From ccd97d242b2d643ecc257786bdb4254b6ed59de3 Mon Sep 17 00:00:00 2001 From: Nicolas Gaud Date: Tue, 25 Feb 2020 18:31:47 +0100 Subject: [PATCH] [SRE] Tuning BehaviorGuardEvaluator compareTo and the managmeent of the registry concerning inherited ancestors methods Closes #977 --- .../sarl/sre/internal/BehaviorGuardEvaluator.sarl | 5 ++++- .../internal/BehaviorGuardEvaluatorRegistry.sarl | 13 ++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/sre/io.janusproject/io.janusproject.plugin/src/io/sarl/sre/internal/BehaviorGuardEvaluator.sarl b/sre/io.janusproject/io.janusproject.plugin/src/io/sarl/sre/internal/BehaviorGuardEvaluator.sarl index 4c580cd69c..bfa60ac9b2 100644 --- a/sre/io.janusproject/io.janusproject.plugin/src/io/sarl/sre/internal/BehaviorGuardEvaluator.sarl +++ b/sre/io.janusproject/io.janusproject.plugin/src/io/sarl/sre/internal/BehaviorGuardEvaluator.sarl @@ -125,11 +125,14 @@ class BehaviorGuardEvaluator implements Comparable { if (other.method === null) { return 1 } + var cmpName = this.method.name <=> other.method.name if (cmpName !== 0) { return cmpName } - if (this.target === other.target) { + + //Warning the targets could be equals but method declaring classes could be different, especially in the case of inherited methods + if ((this.method.declaringClass === other.method.declaringClass) && (this.target === other.target)) { return 0 } val id0 = System::identityHashCode(this.target) diff --git a/sre/io.janusproject/io.janusproject.plugin/src/io/sarl/sre/internal/BehaviorGuardEvaluatorRegistry.sarl b/sre/io.janusproject/io.janusproject.plugin/src/io/sarl/sre/internal/BehaviorGuardEvaluatorRegistry.sarl index f6215b1ba7..be7417964e 100644 --- a/sre/io.janusproject/io.janusproject.plugin/src/io/sarl/sre/internal/BehaviorGuardEvaluatorRegistry.sarl +++ b/sre/io.janusproject/io.janusproject.plugin/src/io/sarl/sre/internal/BehaviorGuardEvaluatorRegistry.sarl @@ -34,6 +34,7 @@ import java.lang.reflect.Method import java.text.MessageFormat import java.util.Arrays import java.util.Collection +import java.util.Comparator import java.util.Iterator import java.util.List import java.util.Map @@ -44,10 +45,11 @@ import java.util.TreeSet import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentLinkedDeque import java.util.concurrent.ConcurrentSkipListSet +import java.util.concurrent.CopyOnWriteArraySet /** * Registry of all {@code BehaviorGuardEvaluator} classes containing a method to evaluate the guard of a given behavior (on clause in SARL behavior). - * This class has been inspired by the {@code com.google.common.eventbus.SuscriberRegistry} class of Google Guava library. + * This class has been inspired by the {@code com.google.common.eventbus.SubscriberRegistry} class of Google Guava library. * *

This class is not thread-safe. * @@ -391,7 +393,8 @@ class BehaviorGuardEvaluatorRegistry { *

The {@link CopyOnWriteArraySet} values make it easy and relatively lightweight to get an immutable snapshot of all current * {@code BehaviorGuardEvaluator}s to an event without any locking. */ - val behaviorGuardEvaluators : ConcurrentHashMap, Pair<(Event) => Boolean, ConcurrentSkipListSet>> + // FIXME replace the CopyOnWriteArraySet by an adapted concurrent implementations of set but not ConcurrentSkipList that also implement list and thus use the compareTo that is not working well on BehaviorGuardEvaluator + val behaviorGuardEvaluators : ConcurrentHashMap, Pair<(Event)=>Boolean, CopyOnWriteArraySet>> var active = true @@ -401,7 +404,7 @@ class BehaviorGuardEvaluatorRegistry { *

The registry will use concurrent data structures. */ new { - this.behaviorGuardEvaluators = new ConcurrentHashMap, Pair<(Event)=>Boolean, ConcurrentSkipListSet>>() + this.behaviorGuardEvaluators = new ConcurrentHashMap, Pair<(Event)=>Boolean, CopyOnWriteArraySet>>() } /** @@ -426,10 +429,10 @@ class BehaviorGuardEvaluatorRegistry { val eventMethodsInListener = entry.value val pair = this.behaviorGuardEvaluators.get(eventType) - var eventSubscribers : ConcurrentSkipListSet + var eventSubscribers : CopyOnWriteArraySet if (pair === null) { - eventSubscribers = new ConcurrentSkipListSet + eventSubscribers = new CopyOnWriteArraySet() var p = new Pair(filter, eventSubscribers) this.behaviorGuardEvaluators.put(eventType, p) firstInit = true