use Ecore/XMI editor for instance creation from Ecore metamodel - code-generation

I have a metamodel in the form of an Ecore file.
I saw in some previous projects that is possible to generate an xmi file from which it is possible to edit an instance of the Ecore metamodel.
I have generated the XMI file by clicking a class in the metamodel then "Create Dynamic Instance ..." but then when I click nodes in xmi file, I miss the *new Child* command that let me to create the instance fields as in the folowing picture :
Any idea how to solve this

Well, without your metamodel and more information about exactly from which root object you created your dynamic instance, it's hard to answer properly.
Usually, if the New Child menu doesn't pop up, it's because your metaclass misses containment EReferences. The generic EMF tree editor relies on the EStructuralFeatures of each metaclass to display the various panels/menu to set information of your instances. If there is no containment references, then, no child can be created.

Related

Setting up RDF class instances in KOMMA

Looking at the documentation for KOMMA plug-in for Eclipse IDE, there doesn't seem to be any way of setting up class instances in its OWL editor, only through Java code:
http://komma.enilink.net/docs/editors/owl_editor/index.html.
Is there something I'm missing here?
You can use the views called "Instances" or "Instance Tree" to create instances of RDFS/OWL classes. First select a class, for example in the classes view, and then use one of the plus buttons to create a corresponding instance.

EMF NotSerializableException

I build application in Java using EMF.
I use Java8 JDK.
All I need is persist instance of my model's class.
I run the code in debug mode on WildFly 10 server, remotely from Eclipse.
private String getXml(Audit audit) throws NeoflexException {
XMLResource res = new XMLResourceImpl();
res.getContents().add(audit);
StringWriter sw = new StringWriter();
try {
res.save(sw, null);
} catch (IOException e) {
throw new NeoflexException(e);
}
return sw.toString();
}
On serialization I get a NotSerializableException.
I see that generated Audit class isn't marked as Serializable.
I can not edit generated code, how ever, I also don't have option to mark this class in diagram as serializable.
As described here: https://www.eclipse.org/forums/index.php/t/261475/
I need to create an interface and derive it from serializable, but I don't have such option. See screenshot attached.
In general, EMF serializability is not bound to the ISerializable interface, but a containment hierarchy formed between the model elements. Furthermore, you should not make EMF interfaces extend the ISerializable inferface at all, as it is misleading (EMF model objects are not supposed to be serializable using the basic Java serialization).
Basically, the instances of your classes should for a containment tree: one object should be the root of the tree, and all other instances should be contained in it. Then you could save this entire containment hierarchy into a file by adding the model root into the resource.
Without knowing the concrete error message in the exception, my first tip would be to check whether the Audit element references some other model element, because if that element is referenced in a non-containment relation, than the serialization will fail.
To set a relation containment, edit your metamodel (Audit.ecore file) and set the property called containment true for the reference. However, you have to make sure that the containment subtree can be set up correctly: there is only a single model root element; all other elements can be reached by exactly one path of containment references from the model root. If an element is referenced by a cross-reference, it must also be included in the containment hierarchy in order for the EMF serialization (Resource.save) to work.
A further issue I see that you create the EMF Resource file manually, without any URI (that determines where you want to save your model) and without any resourceset (that is used to split your models into multiple files/resources with separate containment hierarchies).
In other words, you should create a ResourceSet instance, and use that to create your model Resource instances.
For more details, I suggest to check the basic and serialization specific tutorial at vogella.com.

Entity Framework generated classes are not in the namespace I require, is there a way of changing the namespace it uses without regenerating?

If I need to regenerate where should the namespace be specified. I am trying to use partial classes from within the Models namespace however they don't match.
The simplified code fragment below is where the entity framework classes have been generated
namespace projectname
{
#region Contexts
/// <summary>
/// No Metadata Documentation available.
/// </summary>
public partial class MyClass
{
}
}
When I add a class to my models folder,
namespace projectname.Models
{
public partial class MyClass
{
//etc, etc
}
}
As you can see the namespaces don't match causing issues when I try and use them as the compiler is seeing both projectname.Models.MyClass and projectname.MyClass.
I would like some advice on the correct way to fix this, preferably to update the E.F. classes so they exist in the projectname.Models namespace, but I am not sure how to go about it.
In line with Roman O's comment, namespace can be changed by updating "Custom Tool Namespace" property of text transform (.tt) file, which ties generated entity classes (in Database First approach) to entity model (.edmx) file. This works in VS 2012 Express with EF 5.0.
I would post a screenshot but for lack of reputation.
IIRC, if you are using an Entity data model, you should be able to change the namespace directly through the xml file or files (you may have to change it for all the various storage and and conceptual models). There might even be some way to change it in the designer.. seems simple enough.
Actually, the solution might be here:
Generated Code Overview (Entity Data Model Designer) (see Custom Tool Namespace)
Right click the .edmx file in the project in Solution Explorer and select Properties
In the VS properties pane, fine Custom Tool Namespace
Enter the full CLR namespace you'd like
Rebuild the project
Namespace of auto-generated classes will be inherited from VS project settings (and even change if you change it there). Of course you should have "Custom Tool Namespace" property empty. (just tested it with VS2013 and EF6.1)
Unlike settings files which had always been pita :)
For older version there is a good video on manual changing :
https://youtu.be/B7Cqc9F0Ih8

how to create new class files based on existing ones - in Xcode

Is there a way to create a new class based on another which already exists in the project?
Ideally one could just make a copy of a group (which may inlude .h, .m -xib) and change whatever code on this copy to create a new class.
Currently I create a new group, create the new class with it's new name and then copy the code for these files - immediately renaming the old class name into the new class name
The alternative would be to do "Show in Finder" and create duplicates for the files, drag them back into xCode, create a new group and drag them there...
Is there some better way to do this?
ps in Eclipse there is even an explicit option in the menu for this purpose
Many thanks
I think you should use a subclass for that. Create a new Objective-C class, and choose your old class as the parent class.
Have a look a this, it may help you understand this principle if you're not familiar with it : http://www.techotopia.com/index.php/Objective-C_Inheritance
The concept of inheritance brings something of a real-world view to programming. It allows a class to be defined that has a certain set of characteristics (such as methods and instance variables) and then other classes to be created which are derived from that class. The derived class inherits all of the features of the parent class and typically then adds some features of its own.
I don't know what will happen to the xib file, but at least you can re-use your classes as much as you want !

How to step inside NSManagedObject; access individual attributes

NSManagedObject *entryObj = [self.fetchedResultsController
objectAtIndexPath:indexPath];
entryObj consists of four String attributes.
If I NSLog entryObj, I get the information I want. I cannot figure out how to access each of these properties individually. I read a similar post where the solution was to call "entity." I cannot figure out how to use "entity" to access a specific attribute.
Any ideas? References? Tutorials?
Thanks in advance.
Properties on managed objects are KVC/KVO compliant so you can access them via:
[entryObj valueForKey:#"name"]
Alternatively you can generate a custom Core Data class with real properties to access these values. See this documentation for more information. The Xcode core data modelling tool can generate these classes for you. While you have the model open, choose "File->New File" and you should see a "Managed Object Class" item. Choose this and select the entities you wish to generate classes for.
Once you have done this and the core data entities have their class name set appropriately, you just cast the NSManagedObject to an instance of your new class and access its properties, i.e.
MyObject *entryObj = (MyObject *) [self.fetchedResultsController
objectAtIndexPath:indexPath];
NSLog(#"Property is %#", entryObj.whatever);
If you build your NSManaged objects with the designer then you can export model classes. From the xcdatamodel do File/New File then pick CocoaTouch Class/Managed Object Class. Next then Next then tick each of your classes. Leave generate accessors and generate obj-c 2.0 properties ticked and click finished.
Now you can include the generated files in your projects and use dot accessor syntax.
Alternatively use [entryObject valueForKey:#"keyname"]; but I prefer to stick to the dot accessor syntax where possible.
For generating real classes with properties to call from your object model, I highly recommend using mogenerator:
http://github.com/rentzsch/mogenerator
That's the main project, but the easy to download installer is here:
http://rentzsch.github.com/mogenerator/
You also get primitive value accessors for numeric types, for free.