4 package robots.missions.generator
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
16 * Generates code from your model files on save.
18 * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
20 class TaskDSLGenerator implements IGenerator {
22 override void doGenerate(Resource resource, IFileSystemAccess fsa) {
23 var root = resource.allContents.head as Robot;
25 fsa.generateFile("nl/ru/des/Constants.java", makeConstants(root))
26 fsa.generateFile("nl/ru/des/Behaviours.java", makeBehaviours(root.behaviour))
30 def makeBehaviours(EList<Behaviour> list)'''
33 import lejos.hardware.motor.EV3LargeRegulatedMotor;
35 public class Behaviours{
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);
43 @Override public boolean takeControl(){
44 return «printExpression(b.tc)»;
48 @Override public void action(){
51 «IF a.whichMotor != null»
53 «a.whichMotor.d.toString()»Motor.setAcceleration(«a.acc»);
54 «a.whichMotor.d.toString()»Motor.setSpeed(«a.spd»);
56 «a.whichMotor.d.toString()»Motor.«a.dir.d.toString()»();
58 time = System.currentTimeMillis();
59 while(!suppressed«IF a.time.time > 0» && System.currentTimeMillis()-time>«a.time.time»«ENDIF»){
71 def CharSequence printExpression(StoppingExpression e)'''
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»
83 «IF e.op.d.equals(OperatorE.AND)»
84 «FOR ex : e.s BEFORE "(" SEPARATOR "&&" AFTER ")"»«printExpression(ex)»«ENDFOR»
86 «FOR ex : e.s BEFORE "(" SEPARATOR "&&" AFTER ")"»«printExpression(ex)»«ENDFOR»
91 def CharSequence makeConstants(Robot robot)'''
94 public class Constants{
95 public final static int speed = «robot.spd»;
96 public final static int acceleration = «robot.acc»;