Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lang] Add @DefaultSkill annotation.
This annotation enables a SARL developer to specify a default skill implementation to a defined capacity. The dynamic loading of the specified skill is suppported by the SRE (and several SRE may not support this feature). This feature is still an hidden feature. Signed-off-by: Stéphane Galland <galland@arakhne.org>
- Loading branch information
1 parent
e9781fd
commit ca92975
Showing
7 changed files
with
256 additions
and
1 deletion.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
main/coreplugins/io.sarl.lang.core/src/io/sarl/lang/core/DefaultSkill.java
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,59 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* SARL is an general-purpose agent programming language. | ||
* More details on http://www.sarl.io | ||
* | ||
* Copyright (C) 2014-2017 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.lang.core; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** Annotation for associating a capacity and a default skill. | ||
* | ||
* <p>This annotation should be associated to a {@code Capacity}. | ||
* It takes a value that is the type of the skill that should be given to an | ||
* agent by default when it tries to use the capacity. | ||
* This annotation is similar to the {@code @ImplementedBy} annotation into | ||
* the injection library. | ||
* | ||
* <p>The SRE could use this annotation to create dynamically the skill when | ||
* the {@code Agent.getSkill()} is called. | ||
* | ||
* <p>This annotation is supported by an implementation of {@link DynamicSkillProvider}. | ||
* | ||
* @author $Author: sgalland$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
* @since 0.7 | ||
* @see DynamicSkillProvider | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface DefaultSkill { | ||
|
||
/** The type of the implementation skill. | ||
* | ||
* @return the type of the implementation skill. | ||
*/ | ||
Class<? extends Skill> value(); | ||
|
||
} |
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
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
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
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
124 changes: 124 additions & 0 deletions
124
...lang.tests/src/test/java/io/sarl/lang/tests/general/parsing/general/DefaultSkillTest.java
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,124 @@ | ||
/* | ||
* Copyright (C) 2014-2017 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.lang.tests.general.parsing.general; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
import io.sarl.lang.sarl.SarlAction; | ||
import io.sarl.lang.sarl.SarlAgent; | ||
import io.sarl.lang.sarl.SarlPackage; | ||
import io.sarl.lang.sarl.SarlScript; | ||
import io.sarl.lang.validation.IssueCodes; | ||
import io.sarl.tests.api.AbstractSarlTest; | ||
|
||
import org.eclipse.xtend.lib.annotations.Accessors; | ||
import org.eclipse.xtend.lib.annotations.EqualsHashCode; | ||
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor; | ||
import org.eclipse.xtext.diagnostics.Diagnostic; | ||
import org.eclipse.xtext.serializer.ISerializer; | ||
import org.eclipse.xtext.xbase.XbasePackage; | ||
import org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationsPackage; | ||
import org.eclipse.xtext.xtype.XtypePackage; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Suite; | ||
import org.junit.runners.Suite.SuiteClasses; | ||
import com.google.common.base.Strings; | ||
import com.google.inject.Inject; | ||
|
||
/** | ||
* @author $Author: sgalland$ | ||
* @version $Name$ $Revision$ $Date$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
*/ | ||
@SuppressWarnings("all") | ||
public class DefaultSkillTest extends AbstractSarlTest { | ||
|
||
@Test | ||
public void valid() throws Exception { | ||
SarlScript mas = file(multilineString( | ||
"import io.sarl.lang.core.DefaultSkill", | ||
"@DefaultSkill(typeof(S1))", | ||
"capacity C1 {", | ||
" def myFct", | ||
"}", | ||
"skill S1 implements C1 {", | ||
" def myFct {}", | ||
"}" | ||
)); | ||
validate(mas).assertNoErrors(); | ||
} | ||
|
||
@Test | ||
public void notASkill() throws Exception { | ||
SarlScript mas = file(multilineString( | ||
"import io.sarl.lang.core.DefaultSkill", | ||
"@DefaultSkill(typeof(S1))", | ||
"capacity C1 {", | ||
" def myFct", | ||
"}", | ||
"class S1 {", | ||
"}" | ||
)); | ||
validate(mas).assertError( | ||
XbasePackage.eINSTANCE.getXTypeLiteral(), | ||
org.eclipse.xtext.xbase.validation.IssueCodes.INCOMPATIBLE_TYPES, | ||
"Type mismatch"); | ||
} | ||
|
||
@Test | ||
public void notACapacityImplementation() throws Exception { | ||
SarlScript mas = file(multilineString( | ||
"import io.sarl.lang.core.DefaultSkill", | ||
"@DefaultSkill(typeof(S1))", | ||
"capacity C1 {", | ||
" def myFct", | ||
"}", | ||
"capacity C2 {", | ||
" def myFct2", | ||
"}", | ||
"skill S1 implements C2 {", | ||
" def myFct2 {}", | ||
"}" | ||
)); | ||
validate(mas).assertError( | ||
XbasePackage.eINSTANCE.getXTypeLiteral(), | ||
IssueCodes.INVALID_DEFAULT_SKILL_ANNOTATION, | ||
"Invalid annotation value. S1 is not an implementation of C1"); | ||
} | ||
|
||
@Test | ||
public void validSubType() throws Exception { | ||
SarlScript mas = file(multilineString( | ||
"import io.sarl.lang.core.DefaultSkill", | ||
"@DefaultSkill(typeof(S1))", | ||
"capacity C1 {", | ||
" def myFct", | ||
"}", | ||
"capacity C2 extends C1 {", | ||
" def myFct2", | ||
"}", | ||
"skill S1 implements C2 {", | ||
" def myFct {}", | ||
" def myFct2 {}", | ||
"}" | ||
)); | ||
validate(mas).assertNoErrors(); | ||
} | ||
|
||
} |
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