Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » A Parser For a Specific Model
A Parser For a Specific Model [message #722166] Sun, 04 September 2011 17:41 Go to next message
John Guerson is currently offline John Guerson
Messages: 51
Registered: August 2011
Member
Hi guys,
First, here is my code:

public class Parser {
   public static void main(String[] args) {
      try{		
      // Create a resource set.
      ResourceSet resourceSet = new ResourceSetImpl();	

      // Register the default resource factory -- only needed for stand-alone.				
      resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ontouml",  new XMIResourceFactoryImpl());

      // Register the package -- only needed for stand-alone.
      resourceSet.getPackageRegistry().put(OntoUMLPackage.eNS_URI, OntoUMLPackage.eINSTANCE);		
		
      // Get the URI of the model file.
      URI fileURI = URI.createFileURI(new File("src/pkg/Enrollment.ontouml").getAbsolutePath());
		
      // Demand load the resource for this file, here the actual loading is done.
      Resource ontoumlResource = resourceSet.getResource(fileURI, true);						
      ontoumlResource.load(null);		
		
      // Get model elements from the resource
      EObject container = ontoumlResource.getContents().get(0);		
					
      InputStream in = new FileInputStream("src/pkg/Enrollment.ocl");		
      OCLInput document = new OCLInput(in);

      EcoreEnvironmentFactory envFactory = new EcoreEnvironmentFactory(resourceSet.getPackageRegistry());
      System.out.println(envFactory.getEPackageRegistry());
		
      Environment rootEnv = envFactory.createEnvironment();
      System.out.println(rootEnv.isEmpty());
							
      OCL ocl = OCL.newInstance(envFactory);
      ocl.setParseTracingEnabled(true);		
      List <Constraint> constraints = ocl.parse(document);
		
      System.out.println("ok!");

      }catch (Exception e) {		
	  e.printStackTrace();
      }		
   }		
}	


I need to write my constraints according to the elements of my specific model.
The elements of my model are not in the OntoUMLPackage, they are in my XMI model (Enrollment.ontouml)
More specifically, in the container.

<?xml version="1.0" encoding="UTF-8"?>
<OntoUML:Container xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:OntoUML="OntoUML" xmi:id="_2dpYcZ9qEeC3S9OXekj7uw">
  <InstanciarElement xsi:type="OntoUML:Kind" xmi:id="_4A4FwJ9qEeC3S9OXekj7uw" name="Person"/>
  <InstanciarElement xsi:type="OntoUML:Kind" xmi:id="_6YvVAJ9qEeC3S9OXekj7uw" name="Organization"/>
  <InstanciarElement xsi:type="OntoUML:Role" xmi:id="__QRrsJ9qEeC3S9OXekj7uw" name="School"/>
  ...
  ...
</OntoUML:Container>


So, my constraints are related to elements in this container,like School, Person and Organization:
My OCL document (Enrollment.ocl)

context School
inv one_enrollment_for_student :
    self.schoolOf->forAll( x: Student | x.enrollments->intersection(self.enrollments)->size() = 1 )


The parser should look at the context School and realize that School is in my container. The same with the schoolfOf and etc..

Well, said that, here we go:

In this topic: http://www.eclipse.org/forums/index.php/m/21235/?srch=OCLInput#msg_21235
i found the code below:

EPackage pPackage = (EPackage) ecoreResource.getContents().get(0);
org.eclipse.emf.ecore.EPackage.Registry.INSTANCE.put(pPackage.getNsURI(),pPackage);


The first element in ecoreResource is an EPackage. He make a package registry with pPackage.
If i'm not wrong, when he call ocl.parse(input) , the parser will look at this package registry for type resolver.
Am i correct?

In my case, the parser is looking in the OntoUMLPackage registry. That is wrong. The parser should look for my container.
The container is not an EPackage. My container is an EObject :

//Extracted of OntoUML.Container
public interface Container extends EObject { ... }


i can't do the casting, so i got an error:

EPackage pPackage = (EPackage) container;
EPackage.Registry.INSTANCE.put(pPackage.getNsURI(),pPackage);

java.lang.ClassCastException: OntoUML.impl.ContainerImpl cannot be cast to org.eclipse.emf.ecore.EPackage
Container: OntoUML.impl.ContainerImpl@54acb158
	at pkg.Parser.main(Parser.java:71)


So, how can the parser look at my model(container) for type resolver?

I appreciate any help. I realize that maybe this is more a problem with my specific package than a problem
with OCL and the API. Any ideia?

If not, how can i get the elements of this OCL document (including the contexts), without any relation with my specific model?
For doing some transformation with the constraints and the contexts.

Thanks

Regards,
John
Re: A Parser For a Specific Model [message #722262 is a reply to message #722166] Mon, 05 September 2011 03:37 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4027
Registered: July 2009
Senior Member
Hi

As I wrote before:

"I'm sorry. I have only a limited amount of time that I can devote to
answering queries. If you want help, you must provide something that
helps me to help.

I'm afraid that I cannot help further unless you provide a complete
executable demonstration of your query, and the instructions on how to
reproduce your problem. (ZIP your project tree and optionally delete the
bin tree to save space). "

Regards

Ed Willink


On 04/09/2011 22:42, John wrote:
> Hi guys,
> First, here is my code:
>
>
> public class Parser {
> public static void main(String[] args) {
> try{
> // Create a resource set.
> ResourceSet resourceSet = new ResourceSetImpl();
>
> // Register the default resource factory -- only needed for
> stand-alone.
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ontouml",
> new XMIResourceFactoryImpl());
>
> // Register the package -- only needed for stand-alone.
> resourceSet.getPackageRegistry().put(OntoUMLPackage.eNS_URI,
> OntoUMLPackage.eINSTANCE);
>
> // Get the URI of the model file.
> URI fileURI = URI.createFileURI(new
> File("src/pkg/Enrollment.ontouml").getAbsolutePath());
>
> // Demand load the resource for this file, here the actual
> loading is done.
> Resource ontoumlResource = resourceSet.getResource(fileURI, true);
> ontoumlResource.load(null);
>
> // Get model elements from the resource
> EObject container = ontoumlResource.getContents().get(0);
>
> InputStream in = new FileInputStream("src/pkg/Enrollment.ocl");
> OCLInput document = new OCLInput(in);
>
> EcoreEnvironmentFactory envFactory = new
> EcoreEnvironmentFactory(resourceSet.getPackageRegistry());
> System.out.println(envFactory.getEPackageRegistry());
>
> Environment rootEnv = envFactory.createEnvironment();
> System.out.println(rootEnv.isEmpty());
>
> OCL ocl = OCL.newInstance(envFactory);
> ocl.setParseTracingEnabled(true);
> List <Constraint> constraints = ocl.parse(document);
>
> System.out.println("ok!");
>
> }catch (Exception e) {
> e.printStackTrace();
> }
> }
> }
>
>
> I need to write my constraints according to the elements of my
> specific model.
> The elements of my model are not in the OntoUMLPackage, they are in my
> XMI model (Enrollment.ontouml)
> More specifically, in the container.
>
> <?xml version="1.0" encoding="UTF-8"?>
> <OntoUML:Container xmi:version="2.0"
> xmlns:xmi="http://www.omg.org/XMI"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:OntoUML="OntoUML" xmi:id="_2dpYcZ9qEeC3S9OXekj7uw">
> <InstanciarElement xsi:type="OntoUML:Kind"
> xmi:id="_4A4FwJ9qEeC3S9OXekj7uw" name="Person"/>
> <InstanciarElement xsi:type="OntoUML:Kind"
> xmi:id="_6YvVAJ9qEeC3S9OXekj7uw" name="Organization"/>
> <InstanciarElement xsi:type="OntoUML:Role"
> xmi:id="__QRrsJ9qEeC3S9OXekj7uw" name="School"/>
> ...
> ...
> </OntoUML:Container>
>
>
> So, my constraints are related to elements in this container,like
> School, Person and Organization:
> My OCL document (Enrollment.ocl)
>
>
> context School
> inv one_enrollment_for_student :
> self.schoolOf->forAll( x: Student |
> x.enrollments->intersection(self.enrollments)->size() = 1 )
>
>
> The parser should look at the context School and realize that School
> is in my container. The same with the schoolfOf and etc..
>
> Well, said that, here we go:
>
> In this topic:
> http://www.eclipse.org/forums/index.php/m/21235/?srch=OCLInput#msg_21235
> i found the code below:
>
>
> EPackage pPackage = (EPackage) ecoreResource.getContents().get(0);
> org.eclipse.emf.ecore.EPackage.Registry.INSTANCE.put(pPackage.getNsURI(),pPackage);
>
>
>
> The first element in ecoreResource is an EPackage. He make a package
> registry with pPackage.
> If i'm not wrong, when he call ocl.parse(input) , the parser will look
> at this package registry for type resolver.
> Am i correct?
>
> In my case, the parser is looking in the OntoUMLPackage registry. That
> is wrong. The parser should look for my container.
> The container is not an EPackage. My container is an EObject :
>
>
> //Extracted of OntoUML.Container
> public interface Container extends EObject { ... }
>
>
> i can't do the casting, so i got an error:
>
>
> EPackage pPackage = (EPackage) container;
> EPackage.Registry.INSTANCE.put(pPackage.getNsURI(),pPackage);
>
> java.lang.ClassCastException: OntoUML.impl.ContainerImpl cannot be
> cast to org.eclipse.emf.ecore.EPackage
> Container: OntoUML.impl.ContainerImpl@54acb158
> at pkg.Parser.main(Parser.java:71)
>
>
> So, how can the parser look at my model(container) for type resolver?
>
> I appreciate any help. I realize that maybe this is more a problem
> with my specific package than a problem
> with OCL and the API. Any ideia?
>
> If not, how can i get the elements of this OCL document (including the
> contexts), without any relation with my specific model?
> For doing some transformation with the constraints and the contexts.
>
> Thanks
> Regards,
> John
>
Re: A Parser For a Specific Model [message #722366 is a reply to message #722262] Mon, 05 September 2011 10:08 Go to previous message
John Guerson is currently offline John Guerson
Messages: 51
Registered: August 2011
Member
Edward,

I sent to you on that topic before, but I think that you have not seen the package. So here it is:


thank's
John.

[Updated on: Tue, 20 September 2011 13:47]

Report message to a moderator

Previous Topic:OCL and strings
Next Topic:Writing Inter Class constraints
Goto Forum:
  


Current Time: Sat Aug 23 03:31:18 EDT 2014

Powered by FUDForum. Page generated in 0.01913 seconds