Skip to content

Instantly share code, notes, and snippets.

@Kairides
Last active April 14, 2020 13:01
Show Gist options
  • Save Kairides/3d3f94928ee99a5ac42107fa88c5956f to your computer and use it in GitHub Desktop.
Save Kairides/3d3f94928ee99a5ac42107fa88c5956f to your computer and use it in GitHub Desktop.
Example of an AleRuleProvider and AleRule
package rules;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecoretools.ale.Operation;
import org.eclipse.emf.ecoretools.ale.Tag;
import org.eclipse.emf.ecoretools.ale.Unit;
import org.eclipse.gemoc.dsl.Dsl;
import org.eclipse.gemoc.dsl.Entry;
import org.eclipse.gemoc.xdsmlframework.api.extensions.metaprog.IRule;
import org.eclipse.gemoc.xdsmlframework.api.extensions.metaprog.Message;
import org.eclipse.gemoc.xdsmlframework.api.extensions.metaprog.Severity;
/**
* Validation rule used by the Ale meta-programming approach
*
* @author GUEGUEN Ronan
*
*/
public class AleRule implements IRule{
@Override
public Message execute(Dsl dsl) {
ArrayList<String> entriesNames = new ArrayList<String>();
for (Entry e : dsl.getEntries()) {
entriesNames.add(e.getKey());
}
if(!entriesNames.contains("ale")) {
return (new Message("Missing entry \"ale\"", Severity.ERROR));
}
return (new Message("",Severity.DEFAULT));
}
@Override
public Message execute(Entry entry) {
if("ale".matches(entry.getKey())) {
URI uri = URI.createURI(entry.getValue());
if(!uri.isPlatformResource()) {
return (new Message("File for \"ale\" entry not in the workspace", Severity.ERROR));
}
ResourceSet rs = new ResourceSetImpl();
Resource res;
try {
res = rs.getResource(uri, true);
List<EObject> contents = res.getContents().get(0).eContents();
if(contents.isEmpty()) {
return (new Message("No classes in ale file", Severity.ERROR));
}
TreeIterator<EObject> tree = res.getAllContents();
List<Tag> tags = new ArrayList<Tag>();
while(tree.hasNext()) {
EObject node = tree.next();
if(node instanceof Unit) {
// TODO: If I have the time, i should perform a check on the behaviour name
// Unit nodeUnit = (Unit) node;
}
if(node instanceof Operation) {
Operation nodeOperation = (Operation) node;
if(!nodeOperation.getTag().isEmpty()) {
tags.addAll(nodeOperation.getTag());
}
}
}
ArrayList<String> tagNames = new ArrayList<>();
for(Tag tag : tags) {
tagNames.add(tag.getName());
}
if(!tagNames.contains("init")) {
return (new Message("The Ale file does not contain an \"@init\" operation", Severity.ERROR));
}
if(!tagNames.contains("main")) {
return (new Message("The Ale file does not contain an \"@main\" operation", Severity.ERROR));
}
}catch (RuntimeException e) {
return (new Message("The file for the \"ale\" entry does not exist", Severity.ERROR));
}
}
return (new Message("", Severity.DEFAULT));
}
}
package metaprogramming.aleapproach;
import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.gemoc.xdsmlframework.api.extensions.metaprog.IRule;
import org.eclipse.gemoc.xdsmlframework.api.extensions.metaprog.IRuleProvider;
import org.eclipse.gemoc.xdsmlframework.api.extensions.metaprog.EcoreRule;
import rules.AleRule;
/**
* RuleProvider used for the Ale meta-programming approach.
* Uses the Ecore RuleProvider
*
* @author GUEGUEN Ronan
*
*/
public class AleRuleProvider implements IRuleProvider {
private ArrayList<IRule> ruleSet = new ArrayList<>();
/**
* Creates a RuleProvider for the Ale meta-programming approach, contains rules from the Ecore RuleProvider
*/
public AleRuleProvider() {
ruleSet.add(new EcoreRule());
ruleSet.add(new AleRule());
}
@Override
public Collection<IRule> getValidationRules() {
return ruleSet;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment