package test : test = 'http://test/ecore'
{
class Model
{
property classa : Class[*|1] { ordered composes };
property imports : Model[*|1] { ordered };
attribute name : String[?];
invariant ClassDependsLocalOrImported: classa.depends->forAll(let model = oclContainer()
in model = self or imports->includes(model));
}
class Class
{
property depends : Class[*|1] { ordered };
attribute name : String[?];
}
}
public class ValidatorAction implements IObjectActionDelegate {
private Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();
private ISelection selection;
private static final String ECORE_PLATFORM_URI = "platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore";
private ResourceSet resourceSet = new ResourceSetImpl();
public ValidatorAction() {
super();
}
@Override
public void run(IAction action) {
// TODO Auto-generated method stub
IFile file = (IFile) ((IStructuredSelection) selection).getFirstElement();
System.out.println("file " + file);
EPackage.Registry.INSTANCE.put(ECORE_PLATFORM_URI, EcorePackage.eINSTANCE);
resourceSet = new ResourceSetImpl();
Model model = (Model) getRootElement(resourceSet, "file:" + file.getLocation().toString());
validate(model);
}
private void validate(Model model) {
Map<Object, Object> validationContext = LabelUtil.createDefaultContext(Diagnostician.INSTANCE);
BasicDiagnostic diagnostics = Diagnostician.INSTANCE.createDefaultDiagnostic(model);
if (!Diagnostician.INSTANCE.validate(model, diagnostics, validationContext)) {
StringBuilder message = null;
for (Diagnostic diagnostic : diagnostics.getChildren()) {
if (diagnostic.getData().size() > 0) {
if (message == null) {
message = new StringBuilder();
} else {
message.append("\n");
}
message.append(diagnostic.getMessage());
}
}
if (message != null) {
MessageDialog.openError(shell, "Invalid Model", message.toString());
return;
}
}
else {
MessageDialog.openInformation(shell, "Valid Model", "The requested model is valid");
}
}
public void selectionChanged(IAction action, ISelection selection) {
this.selection = selection;
}
@Override
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
shell = targetPart.getSite().getShell();
}
public static EObject getRootElement(ResourceSet resourceSet, String path) {
org.eclipse.emf.ecore.resource.Resource resource = resourceSet.getResource(URI.createURI(path), true);
EcoreUtil.resolveAll(resource);
return resource.getContents().get(0);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<test:Model xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:test="http://test/ecore" name="model2">
<classa name="B"/>
<classa name="C">
<depends href="My.test#//@classa.0"/>
<depends href="#//@classa.0"/>
</classa>
<imports href="My.test#/"/>
</test:Model>