behaviours generate just fine now, missions is next. We also need some color functions
[des2015.git] / dsl / xtend / src / robots / missions / generator / TaskDSLGenerator.xtend
1 /*
2 * generated by Xtext
3 */
4 package robots.missions.generator
5
6 import org.eclipse.emf.common.util.EList
7 import org.eclipse.emf.ecore.resource.Resource
8 import org.eclipse.xtext.generator.IFileSystemAccess
9 import org.eclipse.xtext.generator.IGenerator
10 import robots.missions.taskDSL.Behaviour
11 import robots.missions.taskDSL.OperatorE
12 import robots.missions.taskDSL.Robot
13 import robots.missions.taskDSL.StoppingExpression
14
15 /**
16 * Generates code from your model files on save.
17 *
18 * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
19 */
20 class TaskDSLGenerator implements IGenerator {
21
22 override void doGenerate(Resource resource, IFileSystemAccess fsa) {
23 var root = resource.allContents.head as Robot;
24 if(root != null){
25 fsa.generateFile("nl/ru/des/Constants.java", makeConstants(root))
26 fsa.generateFile("nl/ru/des/Behaviours.java", makeBehaviours(root.behaviour))
27 }
28 }
29
30 def makeBehaviours(EList<Behaviour> list)'''
31 package nl.ru.des;
32
33 import lejos.hardware.motor.EV3LargeRegulatedMotor;
34
35 public class Behaviours{
36 «FOR b : list»
37 public static class «b.name»Behaviour extends BasicBehaviour {
38 public «b.name»Behaviour(SensorCollector sensors, EV3LargeRegulatedMotor rightMotor,
39 EV3LargeRegulatedMotor leftMotor, ColorMemory colors){
40 super(sensors, rightMotor, leftMotor, colors);
41 }
42 «IF b.tc != null»
43 @Override public boolean takeControl(){
44 return «printExpression(b.tc)»;
45 }
46 «ENDIF»
47
48 @Override public void action(){
49 super.action();
50 «FOR a : b.actions»
51 «IF a.whichMotor != null»
52 «IF a.acc > 0»
53 «a.whichMotor.d.toString()»Motor.setAcceleration(«a.acc»);
54 «a.whichMotor.d.toString()»Motor.setSpeed(«a.spd»);
55 «ENDIF»
56 «a.whichMotor.d.toString()»Motor.«a.dir.d.toString()»();
57 «ELSE»
58 time = System.currentTimeMillis();
59 while(!suppressed«IF a.time.time > 0» && System.currentTimeMillis()-time>«a.time.time»«ENDIF»){
60 Thread.yield();
61 }
62 «ENDIF»
63 «ENDFOR»
64 reset();
65 }
66 }
67 «ENDFOR»
68 }
69 '''
70
71 def CharSequence printExpression(StoppingExpression e)'''
72 «IF e.scond != null»
73 «IF !e.scond.colors.nullOrEmpty»
74 colors.containsAll(new int[]{«FOR c : e.scond.colors SEPARATOR ","»«c.d.ordinal»«ENDFOR»})
75 «ELSEIF e.scond.touch != null»
76 sensors.«e.scond.touch.d.toString()»Touch()
77 «ELSEIF e.scond.op != null»
78 sensors.distance() «e.scond.op.d.toString()» «e.scond.dist»
79 «ELSEIF e.scond.color != null»
80 sensors.color() == «e.scond.color.d.ordinal»
81 «ENDIF»
82 «ELSE»
83 «IF e.op.d.equals(OperatorE.AND)»
84 «FOR ex : e.s BEFORE "(" SEPARATOR "&&" AFTER ")"»«printExpression(ex)»«ENDFOR»
85 «ELSE»
86 «FOR ex : e.s BEFORE "(" SEPARATOR "&&" AFTER ")"»«printExpression(ex)»«ENDFOR»
87 «ENDIF»
88 «ENDIF»
89 '''
90
91 def CharSequence makeConstants(Robot robot)'''
92 package nl.ru.des;
93
94 public class Constants{
95 public final static int speed = «robot.spd»;
96 public final static int acceleration = «robot.acc»;
97 }'''
98
99 }