[58] | 1 | package resource.tools.xtools; |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | import java.io.File; |
---|
| 5 | import java.util.ArrayList; |
---|
| 6 | import java.util.Collection; |
---|
| 7 | import java.util.List; |
---|
| 8 | |
---|
| 9 | import org.eclipse.emf.common.util.EList; |
---|
| 10 | import org.eclipse.emf.ecore.EObject; |
---|
| 11 | import org.eclipse.emf.ecore.EStructuralFeature.Setting; |
---|
| 12 | import org.eclipse.emf.ecore.EValidator; |
---|
| 13 | import org.eclipse.emf.ecore.resource.Resource; |
---|
| 14 | import org.eclipse.emf.ecore.util.EObjectValidator; |
---|
| 15 | import org.eclipse.emf.ecore.util.EcoreUtil; |
---|
| 16 | import org.eclipse.xtext.EcoreUtil2; |
---|
| 17 | import org.eclipse.xtext.nodemodel.ICompositeNode; |
---|
| 18 | import org.eclipse.xtext.nodemodel.util.NodeModelUtils; |
---|
| 19 | import org.slf4j.LoggerFactory; |
---|
| 20 | |
---|
| 21 | import de.ugoe.cs.swe.bnftools.EbnfStandaloneSetup; |
---|
| 22 | import de.ugoe.cs.swe.bnftools.ebnf.EbnfPackage; |
---|
| 23 | import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf; |
---|
| 24 | import de.ugoe.cs.swe.bnftools.ebnf.Rule; |
---|
| 25 | import de.ugoe.cs.swe.bnftools.ebnf.RuleReference; |
---|
| 26 | import de.ugoe.cs.swe.bnftools.ebnf.impl.EbnfPackageImpl; |
---|
| 27 | import resource.tools.xtools.ResourceTool; |
---|
| 28 | |
---|
| 29 | public class EbnfResourceTool extends ResourceTool { |
---|
| 30 | |
---|
| 31 | public EbnfResourceTool(){ |
---|
| 32 | super(); |
---|
| 33 | log = LoggerFactory.getLogger(EbnfResourceTool.class); |
---|
| 34 | EbnfPackageImpl.init(); |
---|
| 35 | initializeValidator(); |
---|
| 36 | injector = new EbnfStandaloneSetup().createInjectorAndDoEMFRegistration(); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | @Override |
---|
| 40 | protected void initializeValidator(){ |
---|
| 41 | super.initializeValidator(); |
---|
| 42 | EObjectValidator validator = new EObjectValidator(); |
---|
| 43 | EValidator.Registry.INSTANCE.put(EbnfPackage.eINSTANCE, validator); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | public void processFile(File xtextFile) { |
---|
| 47 | |
---|
| 48 | //TODO: extract as configuration |
---|
| 49 | String extension = "bnf"; |
---|
| 50 | String workspace = xtextFile.getParentFile().getAbsolutePath(); |
---|
| 51 | String xTextLocation = xtextFile.getAbsolutePath(); |
---|
| 52 | String outputPath = xtextFile.getAbsolutePath()+".ebnf"; |
---|
| 53 | String outputPathValidated = xtextFile.getAbsolutePath()+".ebnf"; |
---|
| 54 | |
---|
| 55 | Resource resource = loadResourceFromXtext(workspace,xTextLocation,true); |
---|
| 56 | findReferences(resource); |
---|
| 57 | storeResourceContents(resource.getContents(), outputPath, extension); |
---|
| 58 | Resource fromXMI = loadResourceFromXMI(outputPath, extension); |
---|
| 59 | // validateResource(fromXMI); |
---|
| 60 | storeResourceContents(fromXMI.getContents(), outputPathValidated, extension); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | private void findReferences(Resource resource) { |
---|
| 64 | //custom reference finding, specific for BNF rule references |
---|
| 65 | //could be generalized for any references at the Ecore level |
---|
| 66 | //but maybe it is fully sufficient |
---|
| 67 | EtsiBnf bnf = (EtsiBnf) resource.getContents().get(0); |
---|
| 68 | |
---|
| 69 | //first approach |
---|
[59] | 70 | System.out.println("***** first approach (top down)"); |
---|
[58] | 71 | List<Rule> rules = EcoreUtil2.getAllContentsOfType(bnf, Rule.class); |
---|
| 72 | for (Rule r : rules) { |
---|
| 73 | System.out.println("Processing rule "+r.getName()); |
---|
| 74 | for (RuleReference ref : EcoreUtil2.getAllContentsOfType(r, RuleReference.class)) { |
---|
| 75 | EcoreUtil2.getContainerOfType(ref, Rule.class); |
---|
| 76 | System.out.println(" " + r.getName() + " references " + ref.getRuleref().getName()); |
---|
| 77 | System.out.println(" -> reference index shall contain \"" + ref.getRuleref().getName() + " referenced in " + r.getName() + "\""); |
---|
| 78 | |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | //second approach |
---|
[59] | 83 | System.out.println("***** second approach (bottom up)"); |
---|
[58] | 84 | for (RuleReference ref : EcoreUtil2.getAllContentsOfType(bnf, RuleReference.class)) { |
---|
| 85 | Rule r = EcoreUtil2.getContainerOfType(ref, Rule.class); |
---|
| 86 | System.out.println("" + r.getName() + " references " + ref.getRuleref().getName()); |
---|
| 87 | System.out.println(" -> reference index shall contain \"" + ref.getRuleref().getName() + " referenced in " + r.getName() + "\""); |
---|
| 88 | |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | //third approach |
---|
[59] | 92 | System.out.println("***** third approach (cross referencing)"); |
---|
[58] | 93 | for (Rule r : rules) { |
---|
| 94 | System.out.println("Processing rule "+r.getName()); |
---|
| 95 | for (Setting s : EcoreUtil.UsageCrossReferencer.find(r, bnf)) { |
---|
| 96 | RuleReference ref = (RuleReference) s.getEObject(); |
---|
| 97 | Rule referencingRule = EcoreUtil2.getContainerOfType(ref, Rule.class); |
---|
| 98 | System.out.println("" + referencingRule.getName() + " references " + r.getName()); |
---|
| 99 | System.out.println(" -> reference index shall contain \"" + r.getName() + " referenced in " + referencingRule.getName() + "\""); |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | public void process(String workspace) { |
---|
| 107 | |
---|
| 108 | //TODO: extract as configuration |
---|
| 109 | String extension = "tplan2x"; |
---|
| 110 | String xTextLocation = workspace+"/model."+extension; |
---|
| 111 | String outputPath = workspace+"/model.tplan2"; |
---|
| 112 | String outputPathValidated = workspace+"/model.tplan2"; |
---|
| 113 | |
---|
| 114 | Resource resource = loadResourceFromXtext(workspace,xTextLocation,true); |
---|
| 115 | storeResourceContents(resource.getContents(), outputPath, extension); |
---|
| 116 | Resource fromXMI = loadResourceFromXMI(outputPath, extension); |
---|
| 117 | // validateResource(fromXMI); |
---|
| 118 | storeResourceContents(fromXMI.getContents(), outputPathValidated, extension); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | } |
---|