behaviours generate just fine now, missions is next. We also need some color functions
[des2015.git] / dsl / xtend / src / robots / missions / generator / TaskDSLGenerator.xtend
index 2139ca5..ec286e9 100644 (file)
@@ -3,9 +3,14 @@
  */
 package robots.missions.generator
 
+import org.eclipse.emf.common.util.EList
 import org.eclipse.emf.ecore.resource.Resource
-import org.eclipse.xtext.generator.IGenerator
 import org.eclipse.xtext.generator.IFileSystemAccess
+import org.eclipse.xtext.generator.IGenerator
+import robots.missions.taskDSL.Behaviour
+import robots.missions.taskDSL.OperatorE
+import robots.missions.taskDSL.Robot
+import robots.missions.taskDSL.StoppingExpression
 
 /**
  * Generates code from your model files on save.
@@ -15,10 +20,80 @@ import org.eclipse.xtext.generator.IFileSystemAccess
 class TaskDSLGenerator implements IGenerator {
        
        override void doGenerate(Resource resource, IFileSystemAccess fsa) {
-//             fsa.generateFile('greetings.txt', 'People to greet: ' + 
-//                     resource.allContents
-//                             .filter(typeof(Greeting))
-//                             .map[name]
-//                             .join(', '))
+               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))
+               }
+       }
+       
+       def makeBehaviours(EList<Behaviour> list)'''
+package nl.ru.des;
+
+import lejos.hardware.motor.EV3LargeRegulatedMotor;
+
+public class Behaviours{
+       «FOR b : list»
+       public static class «b.name»Behaviour extends BasicBehaviour {
+               public «b.name»Behaviour(SensorCollector sensors, EV3LargeRegulatedMotor rightMotor,
+                               EV3LargeRegulatedMotor leftMotor, ColorMemory colors){
+                       super(sensors, rightMotor, leftMotor, colors);
+               }
+               «IF b.tc != null»
+               @Override public boolean takeControl(){
+                       return «printExpression(b.tc)»;
+               }
+               «ENDIF»
+               
+               @Override public void action(){
+                       super.action();
+                       «FOR a : b.actions»
+                               «IF a.whichMotor != null»
+                                       «IF a.acc > 0»
+                                       «a.whichMotor.d.toString()»Motor.setAcceleration(«a.acc»);
+                                       «a.whichMotor.d.toString()»Motor.setSpeed(«a.spd»);
+                                       «ENDIF»
+                                       «a.whichMotor.d.toString()»Motor.«a.dir.d.toString()»();
+                               «ELSE»
+                               time = System.currentTimeMillis();
+                               while(!suppressed«IF a.time.time > 0» && System.currentTimeMillis()-time>«a.time.time»«ENDIF»){
+                                       Thread.yield(); 
+                               }
+                               «ENDIF»
+                       «ENDFOR»
+                       reset();
+               }
        }
+       «ENDFOR»
+}
+       '''
+       
+       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»})
+               «ELSEIF e.scond.touch != null»
+               sensors.«e.scond.touch.d.toString()»Touch()
+               «ELSEIF e.scond.op != null»
+               sensors.distance() «e.scond.op.d.toString()» «e.scond.dist»
+               «ELSEIF e.scond.color != null»
+               sensors.color() == «e.scond.color.d.ordinal»
+               «ENDIF»
+       «ELSE»
+               «IF e.op.d.equals(OperatorE.AND)»
+                       «FOR ex : e.s BEFORE "(" SEPARATOR "&&" AFTER ")"»«printExpression(ex)»«ENDFOR»
+               «ELSE»
+                       «FOR ex : e.s BEFORE "(" SEPARATOR "&&" AFTER ")"»«printExpression(ex)»«ENDFOR»
+               «ENDIF»       
+       «ENDIF»
+       '''
+       
+       def CharSequence makeConstants(Robot robot)'''
+package nl.ru.des;
+
+public class Constants{
+       public final static int speed = «robot.spd»;
+       public final static int acceleration = «robot.acc»;
+}'''
+       
 }