|Re: [Xcore] Data type create and convert access in code? [message #1220850 is a reply to message #1220800]
||Sat, 14 December 2013 09:37
| Ed Merks
Registered: July 2009
On 13/12/2013 6:16 PM, Marcel V wrote:
> 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
> 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
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
> if( it != null ) new SimpleDateFormat("yyyy-MM-dd").parse(it);
> catch( ParseException exception )
> throw new RuntimeException( exception );
> 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
> System.out.println( factoryImpl.convertDate( test.getDate( ) ) );
> System.out.println( test );
|Re: [Xcore] Data type create and convert access in code? [message #1220854 is a reply to message #1220850]
||Sat, 14 December 2013 10:16
| Marcel V
Registered: December 2013
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
Powered by FUDForum
. Page generated in 0.01982 seconds