SemanticAdaptationGenerator.xtend 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * generated by Xtext 2.10.0
  3. */
  4. package be.uantwerpen.ansymo.semanticadaptation.generator
  5. import be.uantwerpen.ansymo.semanticadaptation.log.Log
  6. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Adaptation
  7. import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SemanticAdaptation
  8. import org.eclipse.core.runtime.IExtensionPoint
  9. import org.eclipse.core.runtime.IExtensionRegistry
  10. import org.eclipse.core.runtime.Platform
  11. import org.eclipse.emf.ecore.resource.Resource
  12. import org.eclipse.xtext.generator.AbstractGenerator
  13. import org.eclipse.xtext.generator.IFileSystemAccess2
  14. import org.eclipse.xtext.generator.IGeneratorContext
  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 SemanticAdaptationGenerator extends AbstractGenerator {
  21. override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
  22. Log.push("Generating semantic adaptation for file " + resource.URI + "...")
  23. Log.println("Resource URI information:")
  24. Log.println("\t resource.URI.lastSegment = " + resource.URI.lastSegment())
  25. Log.println("\t resource.URI.trimFileExtension = " + resource.URI.trimFileExtension())
  26. val chainGenerator = loadSemanticAdaptationCustomGenerator()
  27. // Create in memory representation of canonical SA file
  28. var adaptations = resource.allContents.toIterable.filter(SemanticAdaptation).last.elements.filter(Adaptation);
  29. check(adaptations.size == 1, "Only one semantic adaptation is supported per .sa file")
  30. var adaptation = adaptations.head
  31. chainGenerator.generate(adaptation, fsa, resource.URI)
  32. Log.pop("Generating semantic adaptation for file " + resource.URI + "...")
  33. }
  34. def SemanticAdaptationCustomGenerator loadSemanticAdaptationCustomGenerator() {
  35. Log.push("loadSemanticAdaptationCustomGenerator")
  36. val IExtensionRegistry registry = Platform.getExtensionRegistry();
  37. val IExtensionPoint point = registry.getExtensionPoint("be.uantwerpen.ansymo.semanticadaptation.generator_extension");
  38. if (point === null) {
  39. Log.println("Extension point be_uantwerpen_ansymo_semanticadaptation_generator must be provided.")
  40. throw new Exception("Extension point be_uantwerpen_ansymo_semanticadaptation_generator must be provided.");
  41. }
  42. Log.println("Extension point found.")
  43. var chainExtension = point.getExtension("be.uantwerpen.ansymo.semanticadaptation.cg.chain.chain_generator_extension")
  44. if (chainExtension === null) {
  45. throw new Exception("An extension called chain_generator_extension must be provided to Extension Point be.uantwerpen.ansymo.semanticadaptation.generator_extension.");
  46. }
  47. Log.println("Extension to extension point found.")
  48. val configElement = chainExtension.configurationElements.head
  49. if (configElement === null) {
  50. throw new Exception("Configuration element not found.");
  51. }
  52. val obj = configElement.createExecutableExtension("class")
  53. if (obj === null) {
  54. throw new Exception("Class attribute of extension not proper java class.");
  55. }
  56. if (!(obj instanceof SemanticAdaptationCustomGenerator)) {
  57. throw new Exception("Instance of SemanticAdaptationCustomGenerator expected. Found " + obj.class);
  58. }
  59. Log.println("Instance of SemanticAdaptationCustomGenerator found.")
  60. val chainGenerator = obj as SemanticAdaptationCustomGenerator
  61. Log.pop("loadSemanticAdaptationCustomGenerator")
  62. return chainGenerator
  63. }
  64. def check(Boolean condition, String msg){
  65. if (! condition){
  66. throw new Exception("Assertion error: " + msg)
  67. }
  68. }
  69. }