Skip to content

Commit

Permalink
[all] Move the guard evaluator detector from Janus to io.sarl.util.
Browse files Browse the repository at this point in the history
The new location of these tools will enable any SRE implementation to
use the same algorithm for extracting the guard evaluator from the
generated SARL classes.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Nov 15, 2016
1 parent 25d801c commit 7ed4b96
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 22 deletions.
1 change: 1 addition & 0 deletions eclipse-sarl/features/io.sarl.lib/feature.xml
Expand Up @@ -226,6 +226,7 @@
<import plugin="org.eclipse.xtext.xbase.lib" version="2.10.0" match="greaterOrEqual"/>
<import plugin="javax.inject" version="1.0.0" match="greaterOrEqual"/>
<import feature="org.eclipse.xtext.xbase.lib" version="2.10.0" match="greaterOrEqual"/>
<import plugin="org.eclipse.osgi" version="3.11.1" match="greaterOrEqual"/>
</requires>

<plugin
Expand Down
6 changes: 4 additions & 2 deletions eclipse-sarl/plugins/io.sarl.util/META-INF/MANIFEST.MF
Expand Up @@ -5,6 +5,8 @@ Bundle-Version: 0.5.0.qualifier
Bundle-Vendor: %Bundle-Vendor
Bundle-Name: %Bundle-Name
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: io.sarl.util
Require-Bundle: io.sarl.lang.core;bundle-version="0.5.0";visibility:=reexport
Export-Package: io.sarl.eventdispatching,
io.sarl.util
Require-Bundle: io.sarl.lang.core;bundle-version="0.5.0";visibility:=reexport,
org.eclipse.osgi;bundle-version="3.11.1"

Expand Up @@ -19,7 +19,7 @@
* limitations under the License.
*/

package io.janusproject.kernel.bic.internaleventdispatching;
package io.sarl.eventdispatching;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -69,7 +69,7 @@ static BehaviorGuardEvaluator create(Object listener, Method method) {
* {@code PerceptGuardEvaluator} method is declared
* @throws InvocationTargetException - exception during evaluation, can find the method to invoke
*/
void evaluateGuard(final Object event, Collection<Runnable> behaviorsMethodsToExecute)
public void evaluateGuard(final Object event, Collection<Runnable> behaviorsMethodsToExecute)
throws InvocationTargetException {
invokeBehaviorGuardEvaluatorMethod(event, behaviorsMethodsToExecute);
}
Expand Down
Expand Up @@ -19,7 +19,7 @@
* limitations under the License.
*/

package io.janusproject.kernel.bic.internaleventdispatching;
package io.sarl.eventdispatching;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -47,6 +47,7 @@
import com.google.common.reflect.TypeToken;
import com.google.common.util.concurrent.UncheckedExecutionException;

import io.sarl.lang.annotation.PerceptGuardEvaluator;
import io.sarl.lang.core.Event;

/**
Expand Down Expand Up @@ -110,18 +111,24 @@ public ImmutableList<Method> load(Class<?> concreteClass) throws Exception {
* - The annotation used to identify methods considered as the evaluator of the guard of a given behavior (on clause in SARL behavior)
* If class has a such method, it is considered as a {@code BehaviorGuardEvaluator}.
*/
BehaviorGuardEvaluatorRegistry(Class<? extends Annotation> annotation) {

public BehaviorGuardEvaluatorRegistry(Class<? extends Annotation> annotation) {
this.perceptGuardEvaluatorAnnotation = annotation;
}

/**
* Instanciates a new registry linked with the {@link PerceptGuardEvaluator} annotation.
*/
public BehaviorGuardEvaluatorRegistry() {
this(PerceptGuardEvaluator.class);
}

/**
* Registers all {@code PerceptGuardEvaluator} methods on the given listener object.
*
* @param listener
* - the new {@code BehaviorGuardEvaluator} to add
*/
void register(Object listener) {
public void register(Object listener) {
final Multimap<Class<? extends Event>, BehaviorGuardEvaluator> listenerMethods = findAllBehaviorGuardEvaluators(listener);

for (final Map.Entry<Class<? extends Event>, Collection<BehaviorGuardEvaluator>> entry : listenerMethods.asMap().entrySet()) {
Expand All @@ -144,7 +151,7 @@ void register(Object listener) {
*
* @param listener the new {@code BehaviorGuardEvaluator} to remove
*/
void unregister(Object listener) {
public void unregister(Object listener) {
final Multimap<Class<? extends Event>, BehaviorGuardEvaluator> listenerMethods = findAllBehaviorGuardEvaluators(listener);

for (final Map.Entry<Class<? extends Event>, Collection<BehaviorGuardEvaluator>> entry : listenerMethods.asMap().entrySet()) {
Expand Down Expand Up @@ -182,7 +189,7 @@ void unregister(Object listener) {
* -the event to process
* @return the set of guard evaluators associated to the specified event
*/
Collection<BehaviorGuardEvaluator> getBehaviorGuardEvaluators(Event event) {
public Collection<BehaviorGuardEvaluator> getBehaviorGuardEvaluators(Event event) {
final ImmutableSet<Class<?>> eventTypes = flattenHierarchy(event.getClass());

final List<BehaviorGuardEvaluator> iBehaviorGuardEvaluators = Lists.newArrayListWithCapacity(eventTypes.size());
Expand Down Expand Up @@ -239,7 +246,7 @@ private ImmutableList<Method> getAnnotatedMethodsNotCached(Class<?> clazz) {
|| parameterTypes[1] == null) {
//|| parameterTypes[1].getClassLoader().loadClass(Collection.class.getName()).isAssignableFrom(parameterTypes[0])) {
throw new IllegalArgumentException(
MessageFormat.format(Messages.BehaviorGuardEvaluatorRegistry_1,
MessageFormat.format(Messages.BehaviorGuardEvaluatorRegistry_0,
method, this.perceptGuardEvaluatorAnnotation.toString(),
Integer.valueOf(parameterTypes.length),
Arrays.toString(parameterTypes)));
Expand Down Expand Up @@ -307,17 +314,11 @@ private static final class MethodIdentifier {
this.parameterTypes = Arrays.asList(parameterTypes);
}

/**
* {@inheritDoc}.
*/
@Override
public int hashCode() {
return Objects.hashCode(this.name, this.parameterTypes);
}

/**
* {@inheritDoc}.
*/
@Override
public boolean equals(Object object) {
if (object instanceof MethodIdentifier) {
Expand Down
Expand Up @@ -19,7 +19,7 @@
* limitations under the License.
*/

package io.janusproject.kernel.bic.internaleventdispatching;
package io.sarl.eventdispatching;

import org.eclipse.osgi.util.NLS;

Expand All @@ -31,11 +31,10 @@
*/
@SuppressWarnings("all")
public class Messages extends NLS {
private static final String BUNDLE_NAME = "io.janusproject.kernel.bic.internaleventdispatching.messages"; //$NON-NLS-1$
private static final String BUNDLE_NAME = "io.sarl.lang.eventdispatching.messages"; //$NON-NLS-1$
public static String BehaviorGuardEvaluator_0;
public static String BehaviorGuardEvaluator_1;
public static String BehaviorGuardEvaluatorRegistry_0;
public static String BehaviorGuardEvaluatorRegistry_1;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
Expand Down
@@ -1,4 +1,3 @@
BehaviorGuardEvaluator_0=Argument rejected by the perception guard evaluator method: {0}
BehaviorGuardEvaluator_1=Perception guard evaluator method becames inaccessible: {0}
BehaviorGuardEvaluatorRegistry_0=Missing for a method annotated with @PerceptGuardEvaluator. Is {0} registered?
BehaviorGuardEvaluatorRegistry_1=Method {0} has @{1} annotation but with invalid parameters. Perception guard evaluator methods must have exactly 2 parameters: the event occurence to dispatch, a subtype of Event; and the collection of BehaviorUnit methods to execute, Collection<Runnable>. The declared parameters are: {3}.
BehaviorGuardEvaluatorRegistry_0=Method {0} has @{1} annotation but with invalid parameters. Perception guard evaluator methods must have exactly 2 parameters: the event occurence to dispatch, a subtype of Event; and the collection of BehaviorUnit methods to execute, Collection<Runnable>. The declared parameters are: {2}.
Expand Up @@ -37,6 +37,8 @@
import org.arakhne.afc.util.OutputParameter;
import org.eclipse.xtext.xbase.lib.Pair;

import io.sarl.eventdispatching.BehaviorGuardEvaluator;
import io.sarl.eventdispatching.BehaviorGuardEvaluatorRegistry;
import io.sarl.lang.core.DeadEvent;
import io.sarl.lang.core.Event;

Expand Down

0 comments on commit 7ed4b96

Please sign in to comment.