[5] | 1 | package de.ugoe.cs.swe.bnftools.ui.actions;
|
---|
| 2 |
|
---|
| 3 | import java.io.ByteArrayInputStream;
|
---|
| 4 | import java.io.InputStream;
|
---|
| 5 | import org.eclipse.core.resources.IFile;
|
---|
| 6 | import org.eclipse.core.resources.IResource;
|
---|
| 7 | import org.eclipse.core.resources.ResourcesPlugin;
|
---|
| 8 | import org.eclipse.core.runtime.CoreException;
|
---|
| 9 | import org.eclipse.core.runtime.IPath;
|
---|
| 10 | import org.eclipse.core.runtime.Path;
|
---|
| 11 | import org.eclipse.emf.common.util.EList;
|
---|
| 12 | import org.eclipse.emf.common.util.URI;
|
---|
| 13 | import org.eclipse.emf.ecore.EObject;
|
---|
| 14 | import org.eclipse.emf.ecore.resource.Resource;
|
---|
| 15 | import org.eclipse.jface.action.IAction;
|
---|
| 16 | import org.eclipse.jface.dialogs.InputDialog;
|
---|
| 17 | import org.eclipse.jface.viewers.ISelection;
|
---|
| 18 | import org.eclipse.jface.viewers.IStructuredSelection;
|
---|
| 19 | import org.eclipse.jface.viewers.StructuredSelection;
|
---|
| 20 | import org.eclipse.jface.viewers.TreePath;
|
---|
| 21 | import org.eclipse.jface.viewers.TreeSelection;
|
---|
| 22 | import org.eclipse.ui.PlatformUI;
|
---|
| 23 | import org.eclipse.ui.actions.ActionDelegate;
|
---|
| 24 | import org.eclipse.xtext.parsetree.AbstractNode;
|
---|
| 25 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
| 26 | import org.eclipse.xtext.parsetree.LeafNode;
|
---|
| 27 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
| 28 | import org.eclipse.xtext.resource.XtextResource;
|
---|
| 29 | import org.eclipse.xtext.resource.XtextResourceSet;
|
---|
| 30 |
|
---|
| 31 | import de.ugoe.cs.swe.bnftools.ebnf.ExtRule;
|
---|
| 32 | import de.ugoe.cs.swe.bnftools.ebnf.Import;
|
---|
| 33 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
| 34 | import de.ugoe.cs.swe.bnftools.ebnf.RuleReference;
|
---|
| 35 | import de.ugoe.cs.swe.bnftools.ebnf.impl.EtsiBnfImpl;
|
---|
| 36 |
|
---|
| 37 | public class PackageConsistencyCheckerAction extends ActionDelegate {
|
---|
| 38 | private IStructuredSelection selection = StructuredSelection.EMPTY;
|
---|
| 39 | private IFile file=null;
|
---|
| 40 | private XtextResource coreResource = null;
|
---|
| 41 | private XtextResource updatedResource = null;
|
---|
| 42 |
|
---|
| 43 | @Override
|
---|
| 44 | public void run(IAction action) {
|
---|
| 45 | TreeSelection treeSelection = (TreeSelection) selection;
|
---|
| 46 | TreePath[] paths = treeSelection.getPaths();
|
---|
| 47 |
|
---|
| 48 | for (int i = 0; i < paths.length; i++) {
|
---|
| 49 | TreePath path = paths[i];
|
---|
| 50 | IFile f = (IFile) path.getLastSegment();
|
---|
| 51 | XtextResourceSet set = new XtextResourceSet();
|
---|
| 52 | String fp = f.getFullPath().toString();
|
---|
| 53 | URI uri = URI.createPlatformResourceURI(fp,true);
|
---|
| 54 |
|
---|
| 55 | Resource resource = set.getResource(uri, true);
|
---|
| 56 | if (resource instanceof XtextResource) {
|
---|
| 57 |
|
---|
| 58 | XtextResource xtextResource = (XtextResource) resource;
|
---|
| 59 | CompositeNode compNode = xtextResource.getParseResult().getRootNode();
|
---|
| 60 | Iterable<AbstractNode> allNodes = NodeUtil.getAllContents(compNode);
|
---|
| 61 |
|
---|
| 62 | if(!((EtsiBnfImpl) xtextResource.getParseResult().getRootNode().getElement()).getType().equals("/delta"))
|
---|
| 63 | continue;
|
---|
| 64 |
|
---|
| 65 | Resource coreRes = null;
|
---|
| 66 | Resource updatedRes = null;
|
---|
[17] | 67 | EList<Import> imports = ((EtsiBnfImpl) xtextResource.getParseResult().getRootNode().getElement()).getImportSection().getImports();
|
---|
[5] | 68 |
|
---|
| 69 | for(int j=0; j<imports.size(); j++) {
|
---|
| 70 | if(imports.get(j).getGrammarType().equals("core")) {
|
---|
| 71 | String packageUri = uri.trimSegments(1).toPlatformString(true);
|
---|
| 72 | String fullUri = packageUri + "/" +imports.get(j).getImportURI();
|
---|
| 73 | coreRes= set.getResource( URI.createPlatformResourceURI(fullUri, true), true);
|
---|
| 74 | }
|
---|
| 75 | if(imports.get(j).getGrammarType().equals("update")) {
|
---|
| 76 | String packageUri = uri.trimSegments(1).toPlatformString(true);
|
---|
| 77 | String fullUri = packageUri + "/" +imports.get(j).getImportURI();
|
---|
| 78 | updatedRes= set.getResource( URI.createPlatformResourceURI(fullUri, true), true);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 |
|
---|
| 82 | }
|
---|
| 83 | if( (coreRes==null) || (updatedRes==null))
|
---|
| 84 | return;
|
---|
| 85 |
|
---|
| 86 | if(coreRes instanceof XtextResource)
|
---|
| 87 | coreResource = (XtextResource) coreRes;
|
---|
| 88 | if(updatedRes instanceof XtextResource)
|
---|
| 89 | updatedResource = (XtextResource) updatedRes;
|
---|
| 90 |
|
---|
| 91 | InputDialog di = new InputDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Name Selection",
|
---|
| 92 | "Please type a name for the consistency check file.", uri.trimFileExtension().segment(uri.segmentCount()-1) + "_cons.txt", null);
|
---|
| 93 | di.open();
|
---|
| 94 | String fname = di.getValue();
|
---|
| 95 |
|
---|
| 96 | String loc = uri.trimSegments(1).toPlatformString(true); //uri.trimFileExtension().trimSegments(1).toPlatformString(true);
|
---|
| 97 | loc = loc.concat("/" + fname);
|
---|
| 98 | IPath filePath = new Path(loc);
|
---|
| 99 | file = ResourcesPlugin.getWorkspace().getRoot().getFile(filePath);
|
---|
| 100 |
|
---|
| 101 | try {
|
---|
| 102 | if (!file.exists()) {
|
---|
| 103 | byte[] bytes = ("Consistency check for: " + uri.trimFileExtension().segment(uri.segmentCount()-1) + '\n').getBytes();
|
---|
| 104 | InputStream source = new ByteArrayInputStream(bytes);
|
---|
| 105 | file.create(source, IResource.NONE, null);
|
---|
| 106 | }
|
---|
| 107 | else return;
|
---|
| 108 | } catch (CoreException e) {
|
---|
| 109 | e.printStackTrace();
|
---|
| 110 | }
|
---|
| 111 | createNewLine("The only rules shown are the ones that were found inconsistent between the two versions." + '\n');
|
---|
| 112 |
|
---|
| 113 | /*
|
---|
| 114 | * the idea: get the core grammar and the updated grammar
|
---|
| 115 | * for each extension rule in the delta grammar,
|
---|
| 116 | * find the corresponding rule in the updated grammar and the core grammar
|
---|
| 117 | * if it doesn't exist in the updated grammar write the inconsistency
|
---|
| 118 | * if it exists, compare to the rule in the core grammar
|
---|
| 119 | */
|
---|
| 120 | for(AbstractNode node: allNodes) {
|
---|
| 121 | if(node.getElement() instanceof ExtRule) {
|
---|
| 122 | if(!checkForConsistency(node))
|
---|
| 123 | createNewLine("Extension Rule: " + node.serialize().trim()+'\n' + '\n');
|
---|
| 124 |
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | private boolean checkForConsistency(AbstractNode node) {
|
---|
| 137 | //updated grammar:
|
---|
| 138 | AbstractNode updatedRule = null;
|
---|
| 139 | AbstractNode coreRule = null;
|
---|
| 140 | for(AbstractNode uNode : NodeUtil.getAllContents(updatedResource.getParseResult().getRootNode())) {
|
---|
| 141 | if(uNode.getElement() instanceof Rule)
|
---|
| 142 | if(((Rule) uNode.getElement()).getName().equals(((ExtRule) node.getElement()).getName())) {
|
---|
| 143 | updatedRule = uNode;
|
---|
| 144 | break;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | if(updatedRule == null){
|
---|
| 149 | createNewLine("The following core grammar rule used in the package does not exist in the updated grammar: \n");
|
---|
| 150 | return false;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | //core grammar:
|
---|
| 154 | else {
|
---|
| 155 | for(AbstractNode cNode : NodeUtil.getAllContents(coreResource.getParseResult().getRootNode())) {
|
---|
| 156 | if(cNode.getElement() instanceof Rule)
|
---|
| 157 | if(((Rule) cNode.getElement()).getName().equals(((ExtRule) node.getElement()).getName())) {
|
---|
| 158 | coreRule = cNode;
|
---|
| 159 | break;
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | if(coreRule == null){
|
---|
| 164 | createNewLine("Error at the following rule: (no corresponding rule found in the core grammar!):" + '\n');
|
---|
| 165 | return false;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | if(compareRules(coreRule, updatedRule))
|
---|
| 169 | return true;
|
---|
| 170 | else {
|
---|
| 171 | createNewLine("Inconsistency found at the following rule:" + '\n');
|
---|
| 172 | createNewLine("Core Rule: ");
|
---|
| 173 | createNewLine(coreRule.serialize().trim()+ '\n');
|
---|
| 174 | createNewLine("Updated Rule: ");
|
---|
| 175 | createNewLine(updatedRule.serialize().trim() + '\n');
|
---|
| 176 |
|
---|
| 177 | return false;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 |
|
---|
| 184 | //returns true, if the rules are equal
|
---|
| 185 | private boolean compareRules(AbstractNode cNode, AbstractNode uNode) {
|
---|
| 186 | EList<LeafNode> cLeaves = removeWS(cNode.getLeafNodes());
|
---|
| 187 | EList<LeafNode> uLeaves = removeWS(uNode.getLeafNodes());
|
---|
| 188 | if(cLeaves.size()!=uLeaves.size())
|
---|
| 189 | return false;
|
---|
| 190 |
|
---|
| 191 | for(int i=0; i< cLeaves.size(); i++) {
|
---|
| 192 | if(!(cLeaves.get(i).serialize().equals(uLeaves.get(i).serialize())))
|
---|
| 193 | return false;
|
---|
| 194 | }
|
---|
| 195 | return true;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 | private EList<LeafNode> removeWS(EList<LeafNode> leaves) {
|
---|
| 200 | for(int i=0; i<leaves.size(); i++) {
|
---|
| 201 | if(!(leaves.get(i).getGrammarElement() instanceof org.eclipse.xtext.impl.TerminalRuleImpl))
|
---|
| 202 | leaves.remove(i);
|
---|
| 203 | }
|
---|
| 204 | return leaves;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | private boolean checkReferences(AbstractNode rule){
|
---|
| 208 | EList<EObject> refs = rule.getElement().eContents();
|
---|
| 209 | for(int i=0; i<refs.size(); i++){
|
---|
| 210 | if(refs.get(i) instanceof RuleReference)
|
---|
| 211 | System.out.println(refs.get(i));
|
---|
| 212 | }
|
---|
| 213 | // EList<LeafNode> references = ruleref(removeWS(rule.getLeafNodes()));
|
---|
| 214 | // for(int i=0; i<references.size(); i++)
|
---|
| 215 | // if(references.get(i).getElement() instanceof RuleReference)
|
---|
| 216 | // System.out.println(references.get(i));
|
---|
| 217 |
|
---|
| 218 | return false;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 |
|
---|
| 222 |
|
---|
| 223 | private void createNewLine(String rule) {
|
---|
| 224 | try {
|
---|
| 225 | if (file.exists()) {
|
---|
| 226 | byte[] bytes = (rule).getBytes();
|
---|
| 227 | InputStream source = new ByteArrayInputStream(bytes);
|
---|
| 228 | file.appendContents(source, IResource.NONE, null);
|
---|
| 229 | }
|
---|
| 230 | } catch (CoreException e) {
|
---|
| 231 | e.printStackTrace();
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 | @Override
|
---|
| 235 | public void selectionChanged(IAction action, ISelection selection) {
|
---|
| 236 | if (selection instanceof IStructuredSelection) {
|
---|
| 237 | this.selection = (IStructuredSelection) selection;
|
---|
| 238 | } else {
|
---|
| 239 | this.selection = StructuredSelection.EMPTY;
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | }
|
---|