source: default/trunk/de.ugoe.cs.swe.bnftools.xtools/src/resource/tools/xtools/EbnfResourceTool.java @ 92

Last change on this file since 92 was 59, checked in by phdmakk, 11 years ago

+ fixed minor typo

  • Property svn:mime-type set to text/plain
File size: 4.6 KB
RevLine 
[58]1package resource.tools.xtools;
2
3
4import java.io.File;
5import java.util.ArrayList;
6import java.util.Collection;
7import java.util.List;
8
9import org.eclipse.emf.common.util.EList;
10import org.eclipse.emf.ecore.EObject;
11import org.eclipse.emf.ecore.EStructuralFeature.Setting;
12import org.eclipse.emf.ecore.EValidator;
13import org.eclipse.emf.ecore.resource.Resource;
14import org.eclipse.emf.ecore.util.EObjectValidator;
15import org.eclipse.emf.ecore.util.EcoreUtil;
16import org.eclipse.xtext.EcoreUtil2;
17import org.eclipse.xtext.nodemodel.ICompositeNode;
18import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
19import org.slf4j.LoggerFactory;
20
21import de.ugoe.cs.swe.bnftools.EbnfStandaloneSetup;
22import de.ugoe.cs.swe.bnftools.ebnf.EbnfPackage;
23import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf;
24import de.ugoe.cs.swe.bnftools.ebnf.Rule;
25import de.ugoe.cs.swe.bnftools.ebnf.RuleReference;
26import de.ugoe.cs.swe.bnftools.ebnf.impl.EbnfPackageImpl;
27import resource.tools.xtools.ResourceTool;
28
29public 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}
Note: See TracBrowser for help on using the repository browser.