Implementing colors collection and mission exit
[des2015.git] / dsl / xtend / src / robots / missions / generator / TaskDSLGenerator.xtend
index ec286e9..58e115f 100644 (file)
@@ -8,6 +8,7 @@ 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
@@ -22,12 +23,42 @@ class TaskDSLGenerator implements IGenerator {
        override void doGenerate(Resource resource, IFileSystemAccess fsa) {
                var root = resource.allContents.head as Robot;
                if(root != null){
-                       fsa.generateFile("nl/ru/des/Constants.java", makeConstants(root))
-                       fsa.generateFile("nl/ru/des/Behaviours.java", makeBehaviours(root.behaviour))
+                       fsa.generateFile("nl/ru/des/Constants.java", makeConstants(root));
+                       fsa.generateFile("nl/ru/des/Behaviours.java", makeBehaviours(root.behaviour));
+                       fsa.generateFile("nl/ru/des/Missions.java", makeMissions(root.mission));
                }
        }
        
-       def makeBehaviours(EList<Behaviour> list)'''
+       def CharSequence makeMissions(EList<Mission> list)'''
+package nl.ru.des;
+
+import java.util.LinkedList;
+
+import lejos.hardware.motor.EV3LargeRegulatedMotor;
+import lejos.robotics.subsumption.Behavior;
+import nl.ru.des.Behaviours;
+
+public class Missions{
+       public static LinkedList<Mission> getMissions(SensorCollector sensors, EV3LargeRegulatedMotor rightMotor,
+                       EV3LargeRegulatedMotor leftMotor, ColorMemory colors){
+               LinkedList<Mission> missions = new LinkedList<Mission>();
+               «FOR m : list»
+               missions.add(new Mission("«m.name»", new Behavior[]{
+                       «FOR b : m.behaviours SEPARATOR ","»
+                       new Behaviours.«b.name»Behaviour(sensors, rightMotor, leftMotor, colors)
+                       «ENDFOR»,
+                       new ShutdownBehaviour(sensors, rightMotor, leftMotor, colors, "«m.name»"){
+                               @Override public boolean takeControl(){
+                                       return «printExpression(m.se)»;
+                               }
+                       }}
+               «ENDFOR»));
+               return missions;
+       }
+}      
+'''
+       
+       def CharSequence makeBehaviours(EList<Behaviour> list)'''
 package nl.ru.des;
 
 import lejos.hardware.motor.EV3LargeRegulatedMotor;
@@ -57,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»
@@ -71,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»
@@ -91,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<Integer> colorsToFind = Arrays.asList(new Integer[] {«FOR c : m.se.scond.colors SEPARATOR ","»«c.d.ordinal»«ENDFOR»});
+               «ENDIF»
+       «ENDFOR»
 }'''
        
-}
+}
\ No newline at end of file