NMFCode / NMF

This repository contains the entire code for the .NET Modeling Framework
BSD 3-Clause "New" or "Revised" License
36 stars 15 forks source link

Troubles loading EMF instances of an Ecore model #57

Closed wasowski closed 4 years ago

wasowski commented 4 years ago

(possibly related to issue #55)

Hi, Apologies to bother you again. Unfortunately, I have run into another problem. Issue #55 seems to be related, giving some hope. I am loading an instance of the Ecore meta-model created in EMF. This is the instance I am loading:

<?xml version="1.0" encoding="ASCII"?>
<mdsebook.fsm:Model
    xmi:version="2.0"
    xmlns:xmi="http://www.omg.org/XMI"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mdsebook.fsm="http://www.mdsebook.org/mdsebook.fsm" 
    xsi:schemaLocation="http://www.mdsebook.org/mdsebook.fsm ../model/fsm.ecore#//fsm"
    name="test-00">
  <machines
      name="SingleMachine"
      initial="a">
    <states
        name="a"/>
  </machines>
</mdsebook.fsm:Model>

(The metamodel is here: https://bitbucket.org/itu-square/mdsebook/src/master/mdsebook.fsm/model/fsm.ecore - almost the same meta-model that you are using in your examples.)

I am trying to load it with something like

var repository = new ModelRepository();
var model = repository.Resolve("test-00.xmi");
GeneratedCode.Fsm.Model fsm = model.RootElements[0] as GeneratedCode.Fsm.Model;

This compiles, but loading fails at runtime with the following message:

Unhandled exception. System.InvalidOperationException: The type Model in namespace http://www.mdsebook.org/mdsebook.fsm could not be resolved.
   at NMF.Serialization.XmlSerializer.GetTypeInfo(String ns, String localName)
   at NMF.Serialization.XmlSerializer.GetRootElementTypeInfo(XmlReader reader)
   at NMF.Serialization.XmlSerializer.CreateRoot(XmlReader reader)
   at NMF.Models.Repository.Serialization.ModelSerializer.Deserialize(Stream source, Uri modelUri, IModelRepository repository, Boolean addToRepository)
   at NMF.Models.Repository.ModelRepository.Resolve(Uri uri, String hintPath, Boolean loadOnDemand)
   at NMF.Models.Repository.ModelRepository.Resolve(Uri uri, Boolean loadOnDemand)
   at NMF.Models.Repository.ModelRepository.Resolve(String path)
   at Constraints.Program.Main(String[] args) in /home/wasowski/work/mdsebook/mdsebook.fsm.net/Constraints/Program.cs:line 30

I have been unable to understand what is wrong. My guess was that the meta-model is not loading, but I cannot load the meta-model either, and the other examples do not explicitly load meta-models.

Any ideas? I appreciate any suggestions.

Alternatively: if there is no easy way to solve it, is there a way to translate Ecore instances to NFM instances? I figured that I can translate the meta-model with ecore2code, but have not figured out how to translate the instances.

georghinkel commented 4 years ago

First of all, there is no translation between EMF and NMF on the instance level because there, the representation is the same, namely that EMF-flavored XMI representation that allows index- or name-based references inside and across files without having to refer to XMI IDs.

For your actual problem: My first guess is that you are indeed missing the metamodel Registration. In NMF, you don't do that via a method call but rather through an assembly-level annotation like here: https://github.com/NMFCode/NMF/blob/master/Models/Models.Tests/Properties/AssemblyInfo.cs#L39

For this, you will also need to include the (NMeta) metamodel as embedded resource and provide the name there. You can have Ecore2Code generate it for you by specifying the -m Option with a path to where it should be stored.

wasowski commented 4 years ago

OK. This makes sense. Obviously my .NET incompetence is shining here. I did not even know where to search. This example was really helpful. I managed to load the first instance. It should be downhill from here. Thanks!