Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
181 changed files
with
22,164 additions
and
0 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
sre/io.janusproject/io.janusproject.plugin/src/io/sarl/revision/BehaviorGuardEvaluator.sarl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* SARL is an general-purpose agent programming language. | ||
* More details on http://www.sarl.io | ||
* | ||
* Copyright (C) 2014-2019 the original authors or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.sarl.revision | ||
|
||
import com.google.common.base.Strings | ||
import java.lang.reflect.Method | ||
import java.text.MessageFormat | ||
import java.util.Collection | ||
|
||
/** | ||
* Describes each class having one of its methods annotated with {@code PerceptGuardEvaluator} annotation corresponding to the | ||
* method in charge of evaluating the guard associated to a given event and returns the list of behaviors runnable that must be | ||
* executed according to the result of the guard evaluation. | ||
* @author $Author: ngaud$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
* | ||
*/ | ||
class BehaviorGuardEvaluator { | ||
|
||
/** The object with the {@code PerceptGuardEvaluator} method. */ | ||
val target : Object | ||
|
||
/** {@code PerceptGuardEvaluator} method. */ | ||
val method : Method | ||
|
||
/** Creates a {@code Subscriber} for {@code method} on {@code listener}. | ||
* | ||
* @param target the listener | ||
* @param method the method to call to evaluate a guard | ||
*/ | ||
new (target : Object, method : Method) { | ||
this.target = target | ||
this.method = method | ||
} | ||
|
||
/** | ||
* Evaluates the guard associated to the specified {@code event} and returns the list of behaviors methods that must be | ||
* executed. | ||
* @param event the event triggering behaviors | ||
* @param behaviorsMethodsToExecute the list of behavior methods that will be completed according to the result of the guard | ||
* evaluation, BE CARFEUL: we suppose that these behavior methods are parts of the SAME object where the | ||
* {@code PerceptGuardEvaluator} method is declared | ||
* @throws InvocationTargetException - exception during evaluation, can find the method to invoke | ||
*/ | ||
def evaluateGuard(^event : Object, behaviorsMethodsToExecute : Collection<Runnable>) : void { | ||
try { | ||
this.method.accessible = true | ||
this.method.invoke(this.target, ^event, behaviorsMethodsToExecute) | ||
} catch (e : IllegalArgumentException) { | ||
throw new Error(MessageFormat::format(Messages::BehaviorGuardEvaluator_0, ^event), e) | ||
} catch (e : IllegalAccessException) { | ||
throw new Error(MessageFormat::format(Messages::BehaviorGuardEvaluator_1, ^event), e) | ||
} | ||
} | ||
|
||
/** | ||
* Returns he object instance containing the {@code PerceptGuardEvaluator}. | ||
* @return the object instance containing the {@code PerceptGuardEvaluator} | ||
*/ | ||
def getTarget : Object { | ||
this.target | ||
} | ||
|
||
override hashCode : int { | ||
(31 + this.method.hashCode) * 31 + System::identityHashCode(this.target) | ||
} | ||
|
||
override equals(obj : Object) : boolean { | ||
if (obj instanceof BehaviorGuardEvaluator) { | ||
// Use == so that different equal instances will still receive events. | ||
// We only guard against the case that the same object is registered | ||
// multiple times | ||
return this.target === obj.target && this.method == obj.method | ||
} | ||
return false | ||
} | ||
|
||
override toString : String { | ||
if (this.method === null) Strings::emptyToNull(null) else this.method.name | ||
} | ||
|
||
} |
Oops, something went wrong.