SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
research:codecaterpillar:howto_achievements [2013/11/18 13:11]
jan.nonnen
research:codecaterpillar:howto_achievements [2018/05/09 01:59] (current)
Line 1: Line 1:
 +====== How to write the first achievement ======
 +At the end of this tutorial you have created an achievement for working on files in Eclipse on Mondays after 6 PM.
 +
 +{{:​research:​codecaterpillar:​tut_achievement_final.png|}}
 +
 +
 +Make sure that CodeCaterpillar with at least version 0.9.0 is installed. Create a Plug-in "​de.unibonn.myachievements"​ and add a dependency to "​de.unibonn.iai.se.codecaterpillar.codeachiever"​ in the Manifest. Afterwards you should be able to add an Extension Points to the extension "​de.unibonn.iai.se.codecaterpillar.codeachiever.achievements"​ (see Figure 1 below). Achievements that are registered via this extension point are only read during the start-up of Eclipse.
 +
 +
 +^{{:​research:​codecaterpillar:​tut_achievement_1_extension_point.png|}}^
 +| **Figure 1** The extension point to register achievements. |
 +
 +
 +One can add three different elements to this extension point by right-clicking on the extension (see Figure 2):
 +  * Achievement
 +  * LeveledAchievement
 +  * Category
 +
 +An //​Achievement//​ can only be gained one time, whereas on the other side the //​LeveledAchievement//​ is an achievement that increases in level based on a progress, which is tracked based on the behaviour. The //​Category//​ is used to group the achievements together in the //​Achievement Profile View// of CodeCaterpillar. In this tutorial we will use a LeveledAchievement,​ without much difficulty, the achievement could also be added as a one-time achievement.
 +
 +
 +^{{:​research:​codecaterpillar:​tut_achievement_2_extension_point.png|}}^
 +| **Figure 2** One can create an Achievement,​ LevelAchivement or Category at the extension point. |
 +
 +An achievement (one-time and leveled) has a unique id, a name that should be used to display the achievement,​ a 40x40 picture (make sure that it is 40x40) and a behaviour class. The behaviour class is the controller for the achievement. As we want to do a leveled achievement,​ we need to select a ILevelStrategy as well. Either reuse an existing one or create one on your own. This level strategy defines what amount of progress is needed to gain a specific level.
 +
 +We would like to define an achievement for those manic mondays that don't seem to end and you end up working still after 6 P.M.
 +
 +Write the following values in the LeveledAchievement element (we selected a 40x40 image an put it to "​resources/​images.jpg"​ in the plugin):
 +<​Code>​
 +id="​myfirstachievement.mondayevening"​
 +Name="​Ah those Manic Mondays"​
 +Behaviour="​achievevement.funny.MondayEveningBehaviour"​
 +LevelStrategy="​de.unibonn.iai.se.codecaterpillar.codeachiever.api.leveled.strategy.MildLevelStrategy"​
 +UIPicture="​resources/​images.jpg"​
 +</​Code>​
 +
 +After you have added all those values, your view should look like Figure 3. The name and picture are used in the extension view.
 +^{{:​research:​codecaterpillar:​tut_achievement_6.png|}}^
 +| **Figure 3** |
 +
 +/*
 +^{{:​research:​codecaterpillar:​tut_achievement_5_levelstrategy.png|}}^
 +| |
 +*/
 +
 +
 +^ <Code lang-xml>​
 +<?xml version="​1.0"​ encoding="​UTF-8"?>​
 +<?​eclipse version="​3.4"?>​
 +<​plugin>​
 +   <​extension
 +         ​point="​de.unibonn.iai.se.codecaterpillar.codeachiever.achievements">​
 +      <​LeveledAchievement
 +            Behaviour="​achievevement.funny.MondayEveningBehaviour"​
 +            LevelStrategy="​de.unibonn.iai.se.codecaterpillar.codeachiever.api.leveled.strategy.MildLevelStrategy"​
 +            Name="​Ah those Manic Mondays"​
 +            UIPicture="​resources/​images.jpg"​
 +            id="​myfirstachievement.mondayevening">​
 +      </​LeveledAchievement>​
 +   </​extension>​
 +
 +</​plugin>​
 +</​Code>​ ^
 +| Complete content of the Plugin.xml |
 +
 +
 +To have not only a name but also a description that should be used in the UI, you need to set the description of the label in the behaviour class, this is done in the //​initLabel(AchievementLabel)// ​ method (see Figure below).
 +
 +Now we want to implement that our achievement has the behaviour to make progress once we change a file on a Monday after 6 PM. CodeCaterpillar already provides an event for file changes with a timestamp, thus we register ourselves for those events by defining a method with a single parameter of type FileChangeEvent and annotate it with Subscribe (CodeCaterpillar uses the [[http://​code.google.com/​p/​guava-libraries/​wiki/​EventBusExplained| Google Guava Eventbus ]]. A FileChangeEvent has a timestamp, with the help of a CalenderUtil class in the CodeAchiever plugin, we can simply write our requirement down.
 +In the case that we have a file change event on Mondays after 6 PM we would like to increase our progress and (as we don't want to increase the progress for this session in that case) untrack the achievement for this session.
 +
 +The full code:
 +
 +^ <Code lang-java>​
 +package achievevement.funny;​
 +
 +import com.google.common.eventbus.Subscribe;​
 +
 +import de.unibonn.iai.se.codecaterpillar.codeachiever.api.leveled.LeveledAchievementBehaviour;​
 +import de.unibonn.iai.se.codecaterpillar.codeachiever.helper.CalendarUtil;​
 +import de.unibonn.iai.se.codecaterpillar.codeachiever.helper.CalendarUtil.Day;​
 +import de.unibonn.iai.se.codecaterpillar.codeachiever.label.AchievementLabel;​
 +import de.unibonn.iai.se.codecaterpillar.notification.events.FileChangeEvent;​
 +
 +public class MondayEveningBehaviour extends LeveledAchievementBehaviour {
 +
 +
 + @Override
 + public void initLabel(AchievementLabel label) {
 + label.setDescription("​Working on Mondays after 6 P.M."​);​
 + }
 +
 + @Subscribe
 + public void handleFileChangeEvent(FileChangeEvent fileChange){
 + long timestamp = fileChange.getTimestamp();​
 +
 + Day day = CalendarUtil.getWeekDay(timestamp);​
 + int hour = CalendarUtil.getLocalHour(timestamp);​
 +
 + if(day == Day.MONDAY && hour > 17){
 + progressUp();​
 + untrackForSession();​
 + }
 + }
 +
 +}
 +</​Code>​ ^
 +|  |
 +
 +
 +After you started a runtime Eclipse with your fresh implemented achievement,​ you should see your achievement being shown in the CodeCaterpillar>"​Achievement Profile"​ view as an achievement card (see Figure 4).
 +
 +^{{:​research:​codecaterpillar:​tut_achievement_final.png|}}^
 +| **Figure 4** The achievement card of the final achievement in the profile view. |
  
research/codecaterpillar/howto_achievements.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2019