SemanticAdaptationPythonGenerator.xtend 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * generated by Xtext 2.10.0
  3. */
  4. package be.uantwerpen.ansymo.semanticadaptation.generator
  5. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Adaptation
  6. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SemanticAdaptation
  7. import org.eclipse.emf.ecore.resource.Resource
  8. import org.eclipse.xtext.generator.AbstractGenerator
  9. import org.eclipse.xtext.generator.IFileSystemAccess2
  10. import org.eclipse.xtext.generator.IGeneratorContext
  11. /**
  12. * Generates code from your model files on save.
  13. *
  14. * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
  15. */
  16. class SemanticAdaptationPythonGenerator extends AbstractGenerator {
  17. public final static String FILENAME = 'output.test.txt'
  18. override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
  19. for (Adaptation a : resource.allContents.toIterable.filter(SemanticAdaptation).last.elements.filter(Adaptation)) {
  20. fsa.generateFile(FILENAME, a.compile)
  21. println(fsa.readTextFile(FILENAME))
  22. }
  23. }
  24. def compile(Adaptation a) '''
  25. import logging
  26. from abstract_units.AbstractSimulationUnit import AbstractSimulationUnit, \
  27. STEP_ACCEPT
  28. �/*FOR fmu:a.inner.instances.filter(AtomicFMU)�
  29. from case_study.units.ct_based.�fmu.name� import �fmu.name�
  30. �ENDFOR�
  31. �FOR i:a.inner.instances.filter(Adaptation)�
  32. NOT YET IMPLEMENTED
  33. �ENDFOR*/�
  34. l = logging.getLogger()
  35. class PowerInputAdaptation_Event(AbstractSimulationUnit):
  36. """
  37. This is the adaptation of the events coming out of the Controller Statechart.
  38. It gets as input an event, and output two signals, to be coupled to the power system.
  39. Whenever it gets an input, it stores the appropriate signals for the output until another event comes along.
  40. It's basically a ZOH.
  41. Example interaction:_______________
  42. f = PowerInputAdaptation_Event(...)
  43. f.enterInitMode()
  44. f.setValues(...,"SomeInEvent")
  45. This tells the FMU what the input is.
  46. The sFMU will record this input (if not "") to the internal state.
  47. and initial input ("SomeInEvent").
  48. (up,down) = f.getValues(...)
  49. The values that can be returned are initial_up and initial_down
  50. f.exitInitMode()
  51. f.setValues(..., "SomeInEvent")
  52. The definition of a new event gets recorded by the fmu
  53. f.doStep(..., H)
  54. The internal state gets updated according to the new input event.
  55. Or kept the same if the new event is ""
  56. (up,down) = f.getValues(...)
  57. The last event processed is in the output variable, ready to be collected.
  58. ______________________________
  59. """
  60. def __init__(self, name):
  61. self.in_event = "in_event"
  62. self.out_down = "out_down"
  63. self.out_up = "out_up"
  64. input_vars = [self.in_event]
  65. state_vars = [self.out_down, self.out_up]
  66. algebraic_functions = {}
  67. AbstractSimulationUnit.__init__(self, name, algebraic_functions, state_vars, input_vars)
  68. def _doInternalSteps(self, time, step, iteration, step_size):
  69. l.debug(">%s._doInternalSteps(%f, %d, %d, %f)", self._name, time, step, iteration, step_size)
  70. assert iteration == 0, "Fixed point iterations not supported yet."
  71. previous_output = self.getValues(step-1, iteration, self._getStateVars())
  72. current_input = self.getValues(step, iteration, self._getInputVars())
  73. l.debug("%s.previous_output=%s", self._name, previous_output)
  74. l.debug("%s.current_input=%s", self._name, current_input)
  75. next_output = previous_output
  76. in_ev = current_input[self.in_event]
  77. if in_ev != "":
  78. l.debug("Updating internal state due to input %s...", in_ev)
  79. next_out_up = 1.0 if in_ev=="up" else 0.0
  80. next_out_down = 1.0 if in_ev=="down" else 0.0
  81. next_output = {self.out_down: next_out_down, self.out_up: next_out_up}
  82. # Store the values into the state
  83. self.setValues(step, iteration, {self.out_down: next_output[self.out_down],
  84. self.out_up: next_output[self.out_up] })
  85. l.debug("<%s._doInternalSteps() = (%s, %d)", self._name, STEP_ACCEPT, step_size)
  86. return (STEP_ACCEPT, step_size)
  87. '''
  88. }