Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [Xcore] Data type create and convert access in code?
[Xcore] Data type create and convert access in code? [message #1220800] Fri, 13 December 2013 16:42 Go to next message
Marcel V is currently offline Marcel VFriend
Messages: 2
Registered: December 2013
Junior Member
Hi,

I am new to Xcore(and have 0.0 Ecore experience though I know what it is) and I have not yet really understood what the create and convert parts of a type are there for or rather I know that they are for creating an object by giving a String and for converting from an object to a String.
But where and how are they supposed to be used?
Classes written in a Xcode model file can be created by the YourPackageNameFactory but the createXYZ methods for data types are not being written in the factory interface, they are put directly implemented in the YourPackageNameFactoryImpl.

So is one supposed to use the YourPackageNameFactory implementation returned by YourPackageNameFactoryImpl.init and for types the YourPackageNameFactoryImpl directly even if it serves a more complete purpose compared to YourPackageNameFactory?
Or is no one supposed to use these two methods in the actual program?

I give you some code so you may understand better what I mean.

Thanks for your help.

The model:
package de.mv.test

import java.util.Date
import java.text.SimpleDateFormat
import java.text.ParseException

class Test
{
	String name
	Date date
}

// From the xcore tutorial
type Date wraps Date
create
{
	try
	{
		if( it != null ) new SimpleDateFormat("yyyy-MM-dd").parse(it);
	}
	catch( ParseException exception )
	{
		throw new RuntimeException( exception );
	}
}
convert
{
	if( it != null )
	{
		new SimpleDateFormat( "yyyy-MM-dd" ).format( it );
	}
}


And here the usage in a program:
package de.mv.test;

import de.mv.test.impl.TestFactoryImpl;

public class Main
{
	public static void main( String[] args )
	{
                // Which one is the 'correct' one to use?
		TestFactory factory = TestFactoryImpl.init();
		TestFactoryImpl factoryImpl = new TestFactoryImpl( );
	
                // Works with factory too as the TestFactory interface knows createTest
		Test test = factoryImpl.createTest();
		test.setName( "Test" );
		
                // Now here I need the TestFactoryImpl, why is createDate only to be found in the implementation?
                test.setDate( factoryImpl.createDate( "2013-12-13" ) );
		
		System.out.println( test.getName( ) );

                // Same here, convertDate only to be found in TestFactoryImpl
		System.out.println( factoryImpl.convertDate( test.getDate( ) ) );
		System.out.println( test );
	}
}
Re: [Xcore] Data type create and convert access in code? [message #1220850 is a reply to message #1220800] Sat, 14 December 2013 09:37 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Marcel,

Comments below.

On 13/12/2013 6:16 PM, Marcel V wrote:
> Hi,
>
> I am new to Xcore(and have 0.0 Ecore experience though I know what it
> is) and I have not yet really understood what the create and convert
> parts of a type are there for or rather I know that they are for
> creating an object by giving a String and for converting from an
> object to a String.
> But where and how are they supposed to be used?
Keep in mind that data types wrap existing classes and existing class
have constructors and/or static utility methods for creating instances,
so EMF is not generally attempting to augment that with additional API.
> Classes written in a Xcode model file can be created by the
> YourPackageNameFactory but the createXYZ methods for data types are
> not being written in the factory interface, they are put directly
> implemented in the YourPackageNameFactoryImpl.
You can set the Data Type Converters property on the GenPackage to true
if you want API in the factory interface; as I suggested that's
typically of little or no value, but is available if it's valuable in
your case, i.e., @GenModel(dataTypeConverters="true") on the package
declaration.
>
> So is one supposed to use the YourPackageNameFactory implementation
> returned by YourPackageNameFactoryImpl.init and for types the
> YourPackageNameFactoryImpl directly
No.
> even if it serves a more complete purpose compared to
> YourPackageNameFactory?
No, as I suggested it's generally expected that you already have a way
of creating your data type instances from a string.
> Or is no one supposed to use these two methods in the actual program?
Not by using the implementation class. If you need such things, you can
use the option I mentioned, or introduce API in your factory, or
introduce API in some static utility class (as EcoreUtil does for Ecore).
>
> I give you some code so you may understand better what I mean.
>
> Thanks for your help.
>
> The model:
> package de.mv.test
>
> import java.util.Date
> import java.text.SimpleDateFormat
> import java.text.ParseException
>
> class Test
> {
> String name
> Date date
> }
>
> // From the xcore tutorial
> type Date wraps Date
> create
> {
> try
> {
> if( it != null ) new SimpleDateFormat("yyyy-MM-dd").parse(it);
> }
> catch( ParseException exception )
> {
> throw new RuntimeException( exception );
> }
> }
> convert
> {
> if( it != null )
> {
> new SimpleDateFormat( "yyyy-MM-dd" ).format( it );
> }
> }
>
> And here the usage in a program:
> package de.mv.test;
>
> import de.mv.test.impl.TestFactoryImpl;
>
> public class Main
> {
> public static void main( String[] args )
> {
> // Which one is the 'correct' one to use?
> TestFactory factory = TestFactoryImpl.init();
> TestFactoryImpl factoryImpl = new TestFactoryImpl( );
>
> // Works with factory too as the TestFactory interface
> knows createTest
> Test test = factoryImpl.createTest();
> test.setName( "Test" );
>
> // Now here I need the TestFactoryImpl, why is
> createDate only to be found in the implementation?
> test.setDate( factoryImpl.createDate( "2013-12-13" ) );
The next guy might want to use "13/12/2013" instead with their own
format (and not care how you format a Date when you serialize it), or
just "new Date
>
> System.out.println( test.getName( ) );
>
> // Same here, convertDate only to be found in
> TestFactoryImpl
> System.out.println( factoryImpl.convertDate( test.getDate( ) ) );
> System.out.println( test );
> }
> }


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: [Xcore] Data type create and convert access in code? [message #1220854 is a reply to message #1220850] Sat, 14 December 2013 10:16 Go to previous message
Marcel V is currently offline Marcel VFriend
Messages: 2
Registered: December 2013
Junior Member
Thank you, that clarifies much.

Though one more question: If not thought to be explicitly called in the actual Java program, where are these create and convert methods called and used?
For example if I define, in the model, an operation "init" in which I want the members to take default values, I can't write "date = '2013-12-14'" although I would have thought that at this point the convert methods would be called.
So where are they actually used? Is it for XMI purposes?

[Updated on: Sat, 14 December 2013 10:25]

Report message to a moderator

Previous Topic:OSGI Configuration (Newbie)
Next Topic:[CDO] How to import XMI resource in an empty repository
Goto Forum:
  


Current Time: Sat Apr 27 00:03:33 GMT 2024

Powered by FUDForum. Page generated in 0.03783 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top