From d9e24efc7491c75496d20fbfd1e48954605ea301 Mon Sep 17 00:00:00 2001 From: Natanael Adityasatria Date: Fri, 20 Nov 2015 22:55:27 +0100 Subject: [PATCH] Implementing colors collection and mission exit -Update the order of the Color Enum, so the value will be the same as the integer value as stated in Lejos's Color documentation --- dsl/runtime/specs/spec1.tdsl | 3 ++- dsl/runtime/src/nl/ru/des/ColorMemory.java | 16 ++++++++++-- dsl/runtime/src/nl/ru/des/MarsRover.java | 14 ++++++++++- dsl/runtime/src/nl/ru/des/Mission.java | 6 +++++ .../src/nl/ru/des/ShutdownBehaviour.java | 6 ++++- dsl/xtend/src/robots/missions/TaskDSL.xtext | 13 +++++----- .../missions/generator/TaskDSLGenerator.xtend | 25 ++++++++++++++----- .../validation/TaskDSLValidator.xtend | 21 ++++++++++++++-- 8 files changed, 84 insertions(+), 20 deletions(-) diff --git a/dsl/runtime/specs/spec1.tdsl b/dsl/runtime/specs/spec1.tdsl index 45e74a3..ccd0607 100644 --- a/dsl/runtime/specs/spec1.tdsl +++ b/dsl/runtime/specs/spec1.tdsl @@ -7,6 +7,7 @@ Behaviour Wander left motor forward right motor forward wait forever + collect color Behaviour AvoidLowLeftObjects take control: Touched on left action: @@ -44,4 +45,4 @@ using AvoidHighObjects StayInLine and stops when - Collected at least Blue Green Yellow + Collected at least Blue Green Yellow \ No newline at end of file diff --git a/dsl/runtime/src/nl/ru/des/ColorMemory.java b/dsl/runtime/src/nl/ru/des/ColorMemory.java index cec193e..61b0f2b 100644 --- a/dsl/runtime/src/nl/ru/des/ColorMemory.java +++ b/dsl/runtime/src/nl/ru/des/ColorMemory.java @@ -1,14 +1,26 @@ package nl.ru.des; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + import lejos.robotics.SampleProvider; public class ColorMemory { + private Set colors; + public ColorMemory(SampleProvider color) { + colors = new HashSet(); } - public boolean containsAll(int[] is) { - return false; + public void addColor(int current) { + if(!colors.contains(current)){ + colors.add(current); + } + } + public boolean containsAll(List is) { + return colors.containsAll(is); } } diff --git a/dsl/runtime/src/nl/ru/des/MarsRover.java b/dsl/runtime/src/nl/ru/des/MarsRover.java index c4b3ddf..41a5f90 100644 --- a/dsl/runtime/src/nl/ru/des/MarsRover.java +++ b/dsl/runtime/src/nl/ru/des/MarsRover.java @@ -1,5 +1,7 @@ package nl.ru.des; +import java.util.LinkedList; + import lejos.hardware.ev3.EV3; import lejos.hardware.ev3.LocalEV3; import lejos.hardware.lcd.Font; @@ -14,6 +16,7 @@ import lejos.robotics.subsumption.Arbitrator; public class MarsRover { public static final float SAMPLERATE = 100; + public static LinkedList missions = new LinkedList(); @SuppressWarnings("resource") public static void main(String[] args) { @@ -51,10 +54,19 @@ public class MarsRover { ColorMemory colorMemory = new ColorMemory(color); Arbitrator a; - for(Mission m : Missions.getMissions(sensors, rightMotor, leftMotor, colorMemory)){ + missions = Missions.getMissions(sensors, rightMotor, leftMotor, colorMemory); + for(Mission m : missions){ LCDPrinter.print("Start " + m.name + " mission..."); a = new Arbitrator(m.behaviours); + m.SetArbitrator(a); a.start(); } } + + public static void FinishMission(String missionName){ + Mission m = missions.stream().filter(o -> o.name.equalsIgnoreCase(missionName)).findFirst().get(); + if(m != null){ + m.arbitrator.stop(); + } + } } \ No newline at end of file diff --git a/dsl/runtime/src/nl/ru/des/Mission.java b/dsl/runtime/src/nl/ru/des/Mission.java index c80678e..41b7c03 100644 --- a/dsl/runtime/src/nl/ru/des/Mission.java +++ b/dsl/runtime/src/nl/ru/des/Mission.java @@ -1,13 +1,19 @@ package nl.ru.des; +import lejos.robotics.subsumption.Arbitrator; import lejos.robotics.subsumption.Behavior; public class Mission { public String name; public Behavior[] behaviours; + public Arbitrator arbitrator; public Mission(String name, Behavior[] behaviours){ this.name = name; this.behaviours = behaviours; } + + public void SetArbitrator(Arbitrator arbitrator){ + this.arbitrator = arbitrator; + } } diff --git a/dsl/runtime/src/nl/ru/des/ShutdownBehaviour.java b/dsl/runtime/src/nl/ru/des/ShutdownBehaviour.java index 85aa07b..9b34b95 100644 --- a/dsl/runtime/src/nl/ru/des/ShutdownBehaviour.java +++ b/dsl/runtime/src/nl/ru/des/ShutdownBehaviour.java @@ -4,12 +4,16 @@ import lejos.hardware.motor.EV3LargeRegulatedMotor; public class ShutdownBehaviour extends BasicBehaviour{ + private String missionName; + public ShutdownBehaviour(SensorCollector sensors, EV3LargeRegulatedMotor leftMotor, - EV3LargeRegulatedMotor rightMotor, ColorMemory colors) { + EV3LargeRegulatedMotor rightMotor, ColorMemory colors, String missionName) { super(sensors, leftMotor, rightMotor, colors); + this.missionName = missionName; } @Override public void action(){ //Here it should stop the current arbitrator, not sure how yet... + MarsRover.FinishMission(missionName); } } diff --git a/dsl/xtend/src/robots/missions/TaskDSL.xtext b/dsl/xtend/src/robots/missions/TaskDSL.xtext index 9c14fa3..456ac4c 100644 --- a/dsl/xtend/src/robots/missions/TaskDSL.xtext +++ b/dsl/xtend/src/robots/missions/TaskDSL.xtext @@ -13,8 +13,7 @@ Mission: 'Mission' name=ID 'using' behaviours+=[Behaviour]+ 'and stops when' se= StoppingExpression: '(' op=Operator s+=StoppingExpression s+=StoppingExpression+ ')' | - scond=StoppingCondition -; + scond=StoppingCondition; StoppingCondition: 'Collected at least' colors+=Color+ | @@ -24,7 +23,7 @@ StoppingCondition: Behaviour: 'Behaviour' name=ID 'take control:' tc=StoppingExpression? - 'action:' actions+=Action+; + 'action:' actions+=Action+ (clcColor='collect color')?; Action: whichMotor=LeftRight 'motor' dir=Direction ('with speed' spd=INT 'acceleration' acc=INT)? | @@ -42,7 +41,7 @@ LeftRight: d=LeftRightE; enum LeftRightE: LEFT='left' | RIGHT='right'; Color: d=ColorE; enum ColorE: - BLACK='Black' | BLUE='Blue' | BROWN='Brown' | CYAN='Cyan' | - DARKGRAY='DarkGray' | GRAY='Gray' | GREEN='Green' | - LIGHTGRAY='LightGray' | MAGENTA='Magenta' | ORANGE='Orange' | PINK='Pink' | - RED='Red' | WHITE='White' | YELLOW='Yellow'; \ No newline at end of file + RED='Red' | GREEN='Green' | BLUE='Blue' | YELLOW='Yellow' | + MAGENTA='Magenta'| ORANGE='Orange'| WHITE='White' | BLACK='Black' | + PINK='Pink' | GRAY='Gray' | LIGHTGRAY='LightGray' | + DARKGRAY='DarkGray' | CYAN='Cyan' | BROWN='Brown'; \ No newline at end of file diff --git a/dsl/xtend/src/robots/missions/generator/TaskDSLGenerator.xtend b/dsl/xtend/src/robots/missions/generator/TaskDSLGenerator.xtend index 9aecf7e..58e115f 100644 --- a/dsl/xtend/src/robots/missions/generator/TaskDSLGenerator.xtend +++ b/dsl/xtend/src/robots/missions/generator/TaskDSLGenerator.xtend @@ -8,10 +8,10 @@ import org.eclipse.emf.ecore.resource.Resource import org.eclipse.xtext.generator.IFileSystemAccess import org.eclipse.xtext.generator.IGenerator import robots.missions.taskDSL.Behaviour +import robots.missions.taskDSL.Mission import robots.missions.taskDSL.OperatorE import robots.missions.taskDSL.Robot import robots.missions.taskDSL.StoppingExpression -import robots.missions.taskDSL.Mission /** * Generates code from your model files on save. @@ -33,14 +33,13 @@ class TaskDSLGenerator implements IGenerator { package nl.ru.des; import java.util.LinkedList; -import java.util.List; import lejos.hardware.motor.EV3LargeRegulatedMotor; import lejos.robotics.subsumption.Behavior; import nl.ru.des.Behaviours; public class Missions{ - public static List getMissions(SensorCollector sensors, EV3LargeRegulatedMotor rightMotor, + public static LinkedList getMissions(SensorCollector sensors, EV3LargeRegulatedMotor rightMotor, EV3LargeRegulatedMotor leftMotor, ColorMemory colors){ LinkedList missions = new LinkedList(); «FOR m : list» @@ -48,7 +47,7 @@ public class Missions{ «FOR b : m.behaviours SEPARATOR ","» new Behaviours.«b.name»Behaviour(sensors, rightMotor, leftMotor, colors) «ENDFOR», - new ShutdownBehaviour(sensors, rightMotor, leftMotor, colors){ + new ShutdownBehaviour(sensors, rightMotor, leftMotor, colors, "«m.name»"){ @Override public boolean takeControl(){ return «printExpression(m.se)»; } @@ -89,6 +88,12 @@ public class Behaviours{ «ELSE» time = System.currentTimeMillis(); while(!suppressed«IF a.time.time > 0» && System.currentTimeMillis()-time>«a.time.time»«ENDIF»){ + «IF !b.clcColor.nullOrEmpty» + int currentColor = sensors.color(); + if (Constants.colorsToFind.contains(currentColor)) { + colors.addColor(currentColor); + } + «ENDIF» Thread.yield(); } «ENDIF» @@ -103,7 +108,7 @@ public class Behaviours{ def CharSequence printExpression(StoppingExpression e)''' «IF e.scond != null» «IF !e.scond.colors.nullOrEmpty» - colors.containsAll(new int[]{«FOR c : e.scond.colors SEPARATOR ","»«c.d.ordinal»«ENDFOR»}) + colors.containsAll(Constants.colorsToFind) «ELSEIF e.scond.touch != null» sensors.«e.scond.touch.d.toString()»Touch() «ELSEIF e.scond.op != null» @@ -123,9 +128,17 @@ public class Behaviours{ def CharSequence makeConstants(Robot robot)''' package nl.ru.des; +import java.util.Arrays; +import java.util.List; + public class Constants{ public final static int speed = «robot.spd»; public final static int acceleration = «robot.acc»; + «FOR m : robot.mission» + «IF !m.se.scond.colors.nullOrEmpty» + public final static List colorsToFind = Arrays.asList(new Integer[] {«FOR c : m.se.scond.colors SEPARATOR ","»«c.d.ordinal»«ENDFOR»}); + «ENDIF» + «ENDFOR» }''' -} +} \ No newline at end of file diff --git a/dsl/xtend/src/robots/missions/validation/TaskDSLValidator.xtend b/dsl/xtend/src/robots/missions/validation/TaskDSLValidator.xtend index 67fdc6e..ae4ba7a 100644 --- a/dsl/xtend/src/robots/missions/validation/TaskDSLValidator.xtend +++ b/dsl/xtend/src/robots/missions/validation/TaskDSLValidator.xtend @@ -3,11 +3,13 @@ */ package robots.missions.validation -//import org.eclipse.xtext.validation.Check +import org.eclipse.xtext.validation.Check +import robots.missions.taskDSL.Mission +//import org.eclipse.xtext.validation.Check /** * This class contains custom validation rules. - * + * * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#validation */ class TaskDSLValidator extends AbstractTaskDSLValidator { @@ -22,4 +24,19 @@ class TaskDSLValidator extends AbstractTaskDSLValidator { // INVALID_NAME) // } // } + @Check + def checkStoppingExpressionShouldHaveOneBehaviourToCollectColor(Mission mission) { + var isCollectColorExist = false; + if (!mission.se.scond.colors.nullOrEmpty) { + var blist = mission.behaviours; + for (var j = 0; j < blist.size; j++) { + if (!blist.get(j).clcColor.nullOrEmpty) { + isCollectColorExist = true; + } + } + } + if (!isCollectColorExist) { + error("Must specify one behaviour to collect color", null) + } + } } -- 2.20.1