Home » Modeling » EMF » [EMF-JAVA] Loading models and metamodels
[EMF-JAVA] Loading models and metamodels [message #633108] |
Fri, 15 October 2010 11:56 |
Dave Messages: 37 Registered: September 2010 |
Member |
|
|
Hi all,
I've got a model conform to a certain metamodel. Both the model and the matemodel are defined by means of .ecore files.
I must create a Java program that executes an OCL query on the model.
I've written the following Java class:
package failuresdetector;
import java.io.IOException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.impl.EPackageRegistryImpl;
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.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.emf.query.conditions.Condition;
import org.eclipse.emf.query.conditions.eobjects.EObjectCondition;
import org.eclipse.emf.query.ocl.conditions.BooleanOCLCondition;
import org.eclipse.emf.query.statements.*;
import org.eclipse.ocl.ParserException;
import org.eclipse.ocl.ecore.EcoreEnvironmentFactory;
import org.eclipse.ocl.ecore.OCL;
import org.eclipse.ocl.ecore.OCLExpression;
import org.eclipse.ocl.helper.OCLHelper;
public class QueryExecutor {
/**
* The method thats execute ocl queries
* @param query the ocl query that must be executed
*/
public static IQueryResult executeQuery(Resource ecoreResource, String query) {
OCL myOcl = OCL.newInstance();
myOcl.setEvaluationTracingEnabled(true);
myOcl.setParseTracingEnabled(true);
OCLHelper oclHelper = myOcl.createOCLHelper();
oclHelper.setInstanceContext(ecoreResource);
OCLExpression oclInv = null;
//String myQuery = "self.required->size()=1";
try {
oclInv = (OCLExpression) oclHelper.createQuery(query);
System.out.println("still in try");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("in catch now");
e.printStackTrace();
}
myOcl.check(ecoreResource, oclInv);
return null;
}
public static void main(String[] args) throws IOException {
// Create a resource set to hold the resources.
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new EcoreResourceFactoryImpl());
URI modelUri = URI.createFileURI("model/systemModel.ecore");
resourceSet.getPackageRegistry().put("Configuration", new EPackageRegistryImpl());
Resource resource = resourceSet.getResource(modelUri, true);
System.out.println("Loaded " + resource);
IQueryResult res = QueryExecutor.execute(resource, "mancoosi::installedPackages.allInstances()");
}
}
but it throws the following exception:
Exception in thread "main" org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Package with uri 'mancoosi' not found. (file:///C:/Documents%20and%20Settings/Dave/Documenti/mySoftwares/workspace/FailuresDetector/model/systemModel.ecore, 2, 222)
at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.handleErrors(XMLLoadImpl.java:83)
at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:191)
at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:180)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1494)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1282)
at failuresdetector.QueryExecutor.main(QueryExecutor.java:109)
Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'mancoosi' not found. (file:///C:/Documents%20and%20Settings/Dave/Documenti/mySoftwares/workspace/FailuresDetector/model/systemModel.ecore, 2, 222)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(XMLHandler.java:2591)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefix(XMLHandler.java:2422)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType(XMLHandler.java:1299)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XMLHandler.java:1468)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XMLHandler.java:1019)
at org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMIHandler.java:83)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:1001)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:712)
at org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHandler.java:169)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:181)
... 4 more
I think it could be generated because the metamodel is not registered. In fact, just yesterday I've solved a similar issue: Eclipse wasn't able to open the model in the Ecore Model Editor. The thrown Exception was the same. I've registered the metamodel (in ATL perspective) and the problem was solved.
So, I'm lookin' for the metamodel registration from Java code or something else useful to solve my problem.
It could be useful to know the the following line of the ecore model XMI file:
<mancoosi:Configuration xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mancoosi="mancoosi" creationTime="Thu Jan 28 09:23:09 CET 2010" systemType="Ubuntu">
When I was having the problem with the Ecore Model Editor, Eclipse was not able to find the uri "mancoosi" until the metamodel had been registered. In the Java code, the matamodel is not explicitly registered so, when the parser encounters "mancoosi", it cannot resolve the resource.
Anyone could help me, please?
Many thanks in advance,
Dave.
|
|
|
Re: [EMF-JAVA] Loading models and metamodels [message #633115 is a reply to message #633108] |
Fri, 15 October 2010 12:12 |
Ed Merks Messages: 33137 Registered: July 2009 |
Senior Member |
|
|
Dave,
Comments below.
Dave wrote:
> Hi all,
>
> I've got a model conform to a certain metamodel. Both the model and
> the matemodel are defined by means of .ecore files.
> I must create a Java program that executes an OCL query on the model.
>
> I've written the following Java class:
>
> package failuresdetector;
>
> import java.io.IOException;
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.EClass;
> import org.eclipse.emf.ecore.EClassifier;
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.emf.ecore.impl.EPackageRegistryImpl;
> 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.ecore.xmi.impl.EcoreResourceFactoryImpl;
> import org.eclipse.emf.query.conditions.Condition;
> import org.eclipse.emf.query.conditions.eobjects.EObjectCondition;
> import org.eclipse.emf.query.ocl.conditions.BooleanOCLCondition;
> import org.eclipse.emf.query.statements.*;
> import org.eclipse.ocl.ParserException;
> import org.eclipse.ocl.ecore.EcoreEnvironmentFactory;
> import org.eclipse.ocl.ecore.OCL;
> import org.eclipse.ocl.ecore.OCLExpression;
> import org.eclipse.ocl.helper.OCLHelper;
>
> public class QueryExecutor {
>
> /**
> * The method thats execute ocl queries
> * @param query the ocl query that must be executed
> */
> public static IQueryResult executeQuery(Resource ecoreResource,
> String query) {
> OCL myOcl = OCL.newInstance();
>
> myOcl.setEvaluationTracingEnabled(true);
> myOcl.setParseTracingEnabled(true);
>
> OCLHelper oclHelper = myOcl.createOCLHelper();
>
> oclHelper.setInstanceContext(ecoreResource);
>
> OCLExpression oclInv = null;
> //String myQuery = "self.required->size()=1";
> try {
> oclInv = (OCLExpression) oclHelper.createQuery(query);
> System.out.println("still in try");
> } catch (Exception e) {
> // TODO Auto-generated catch block
> System.out.println("in catch now");
> e.printStackTrace();
> }
> myOcl.check(ecoreResource, oclInv);
>
> return null;
> }
>
>
> public static void main(String[] args) throws IOException {
> // Create a resource set to hold the resources.
> ResourceSet resourceSet = new ResourceSetImpl();
>
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "ecore",
> new EcoreResourceFactoryImpl());
>
> URI modelUri = URI.createFileURI("model/systemModel.ecore");
A relative path is typically a terrible idea.
>
> resourceSet.getPackageRegistry().put("Configuration", new
> EPackageRegistryImpl());
This can't be sensible. You should be putting EPackage instances in
there (or EPackage.Descriptor).
>
> Resource resource = resourceSet.getResource(modelUri, true);
> System.out.println("Loaded " + resource);
>
> IQueryResult res = QueryExecutor.execute(resource,
> "mancoosi::installedPackages.allInstances()");
>
> }
>
> }
>
> but it throws the following exception:
>
> Exception in thread "main"
> org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Package
> with uri 'mancoosi' not found.
> ( file:///C:/Documents%20and%20Settings/Dave/Documenti/mySoftw ares/workspace/FailuresDetector/model/systemModel.ecore,
> 2, 222)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.handleErrors(XMLL oadImpl.java:83)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:191)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1494)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1282)
>
> at failuresdetector.QueryExecutor.main(QueryExecutor.java:109)
> Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package
> with uri 'mancoosi' not found.
> ( file:///C:/Documents%20and%20Settings/Dave/Documenti/mySoftw ares/workspace/FailuresDetector/model/systemModel.ecore,
> 2, 222)
You should ensure that your generated package is registered. A good way
to do that is to use an idiom like XyzPackage.eINSTANCE.eClass();
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(X MLHandler.java:2591)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefi x(XMLHandler.java:2422)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1299)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XM LHandler.java:1468)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XML Handler.java:1019)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMI Handler.java:83)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:1001)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:712)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHa ndler.java:169)
>
> at
> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .startElement(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator. startElement(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanStartElement(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$ContentDriver.scanRootElementHook(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl$FragmentContentDriver.next(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$PrologDriver.next(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl.next(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanDocument(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(U nknown
> Source)
> at
> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .parse(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSA XParser.parse(Unknown
> Source)
> at javax.xml.parsers.SAXParser.parse(Unknown Source)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:181)
> ... 4 more
>
>
> I think it could be generated because the metamodel is not registered.
For sure.
> In fact, just yesterday I've solved a similar issue: Eclipse wasn't
> able to open the model in the Ecore Model Editor. The thrown Exception
> was the same. I've registered the metamodel (in ATL perspective) and
> the problem was solved.
> So, I'm lookin' for the metamodel registration from Java code or
> something else useful to solve my problem.
>
> It could be useful to know the the following line of the ecore model
> XMI file:
> <mancoosi:Configuration xmi:version="2.0"
> xmlns:xmi="http://www.omg.org/XMI"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:mancoosi="mancoosi" creationTime="Thu Jan 28 09:23:09 CET 2010"
> systemType="Ubuntu">
>
> When I was having the problem with the Ecore Model Editor, Eclipse was
> not able to find the uri "mancoosi" until the metamodel had been
> registered. In the Java code, the matamodel is not explicitly
> registered so, when the parser encounters "mancoosi", it cannot
> resolve the resource.
>
> Anyone could help me, please?
If you invoke Generate Test Code and look at the generated
XyzExample.java you'll see the necessary things for registering your
package, your resource factory, and for accessing files in the file
system with absolute URIs.
>
> Many thanks in advance,
> Dave.
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Goto Forum:
Current Time: Fri Apr 19 17:12:21 GMT 2024
Powered by FUDForum. Page generated in 0.02724 seconds
|