Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » emf json problem
icon4.gif  emf json problem [message #1701693] Wed, 15 July 2015 05:20 Go to next message
Alexander Emelianov is currently offline Alexander EmelianovFriend
Messages: 10
Registered: July 2015
Junior Member
Hi,

I have a problen with json serialization of ecore model. I'm trying to create .uml and .json (with emfjson) files like this:

ResourceSet umlResourceSet;

    @Before
    public void prepare(){
        umlResourceSet = new ResourceSetImpl();
        umlResourceSet.setURIConverter(URIConverter.INSTANCE);
        umlResourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("json", new JsonResourceFactoryCit());
        umlResourceSet.getPackageRegistry().put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
        UMLResourcesUtil.init(umlResourceSet);
    }

    @Test
    public void test(){
        List<EObject> eObjects = new ArrayList<>();
        List<String> useCaseNames = Arrays.asList("Кредитовый перевод",
                "Инициирование кредитового перевода", "Коммерческий платеж",
                "Налоговый платеж", "Валютный платеж", "Межбанковский перевод", "Уведомление кредитора");
        List<String> actorNames = Arrays.asList("Дебитор", "Банк дебитора", "Банк-посредник",
                "Кредитор", "Банк кредитора");
        Map<String, UseCase> useCaseList = new HashMap<>();
        useCaseNames.stream().forEach(n -> {
            UseCase uc = UMLFactory.eINSTANCE.createUseCase();
            uc.setName(n);
            useCaseList.put(n, uc);
        });
        org.eclipse.uml2.uml.Package p = UMLFactoryImpl.init().createPackage();
        eObjects.add(p);
        Resource primitiveLib = umlResourceSet.getResource(URI.createURI(UMLResource.UML_PRIMITIVE_TYPES_LIBRARY_URI), true);
        Package primitiveLibPackage = (Package) EcoreUtil.getObjectByType(primitiveLib.getContents(), UMLPackage.Literals.PACKAGE);
        p.createPackageImport(primitiveLibPackage);
        p.setName("NPModel");
        Resource profileLib = umlResourceSet.getResource(URI.createURI("model/ISO20022Profile.profile.uml"), true);
        Package isoP = (Package) EcoreUtil.getObjectByType(profileLib.getContents(), UMLPackage.Literals.PACKAGE);
        p.applyProfile(isoP.containingProfile());
        actorNames.stream().forEach(n -> p.createPackagedElement(n, UMLFactoryImpl.getPackage().getActor()));
        isoP.containingProfile().getOwnedStereotypes().stream()
                .filter(s -> s.getName().contains("BusinessRole")).findFirst().ifPresent(st -> {
            NamedElement el = p.getPackagedElement("Банк дебитора");
            eObjects.add(el.applyStereotype(st));
        });
        useCaseList.forEach((n, uc) -> p.getPackagedElements().add(uc));
        Resource resource = umlResourceSet.createResource(URI.createURI("test.uml"));
        resource.getContents().addAll(eObjects);
        try {
            resource.save(null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Resource resourceJSON = umlResourceSet.createResource(URI.createURI("test.json"));
        resourceJSON.getContents().addAll(eObjects);
        try {
            resourceJSON.save(null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


Created .uml is right:
<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="20131001" xmlns:xmi="http://www.omg.org/spec/XMI/20131001" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ISO20022Profile="http:///schemas/ISO20022Profile/_Z1baoCR_EeW-eZ1ZLTAX7g/50" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:uml="http://www.eclipse.org/uml2/5.0.0/UML" xsi:schemaLocation="http:///schemas/ISO20022Profile/_Z1baoCR_EeW-eZ1ZLTAX7g/50 model/ISO20022Profile.profile.uml#_Z1fsECR_EeW-eZ1ZLTAX7g">
  <uml:Package xmi:id="_eJYk0CquEeWRmPUlcrfDwQ" name="NPModel">
    <packageImport xmi:id="_eJYk0SquEeWRmPUlcrfDwQ">
      <importedPackage xmi:type="uml:Model" href="pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#_0"/>
    </packageImport>
    <packagedElement xmi:type="uml:Actor" xmi:id="_eJYk0iquEeWRmPUlcrfDwQ" name="Дебитор"/>
    <packagedElement xmi:type="uml:Actor" xmi:id="_eJYk0yquEeWRmPUlcrfDwQ" name="Банк дебитора"/>
    <packagedElement xmi:type="uml:Actor" xmi:id="_eJYk1CquEeWRmPUlcrfDwQ" name="Банк-посредник"/>
    <packagedElement xmi:type="uml:Actor" xmi:id="_eJYk1SquEeWRmPUlcrfDwQ" name="Кредитор"/>
    <packagedElement xmi:type="uml:Actor" xmi:id="_eJYk1iquEeWRmPUlcrfDwQ" name="Банк кредитора"/>
    <packagedElement xmi:type="uml:UseCase" xmi:id="_eJYk1yquEeWRmPUlcrfDwQ" name="Инициирование кредитового перевода"/>
    <packagedElement xmi:type="uml:UseCase" xmi:id="_eJYk2CquEeWRmPUlcrfDwQ" name="Межбанковский перевод"/>
    <packagedElement xmi:type="uml:UseCase" xmi:id="_eJYk2SquEeWRmPUlcrfDwQ" name="Коммерческий платеж"/>
    <packagedElement xmi:type="uml:UseCase" xmi:id="_eJZL4CquEeWRmPUlcrfDwQ" name="Уведомление кредитора"/>
    <packagedElement xmi:type="uml:UseCase" xmi:id="_eJZL4SquEeWRmPUlcrfDwQ" name="Кредитовый перевод"/>
    <packagedElement xmi:type="uml:UseCase" xmi:id="_eJZL4iquEeWRmPUlcrfDwQ" name="Налоговый платеж"/>
    <packagedElement xmi:type="uml:UseCase" xmi:id="_eJZL4yquEeWRmPUlcrfDwQ" name="Валютный платеж"/>
    <profileApplication xmi:id="_eJZL5CquEeWRmPUlcrfDwQ">
      <eAnnotations xmi:id="_eJZL5SquEeWRmPUlcrfDwQ" source="http://www.eclipse.org/uml2/2.0.0/UML">
        <references xmi:type="ecore:EPackage" href="model/ISO20022Profile.profile.uml#_Z1fsECR_EeW-eZ1ZLTAX7g"/>
      </eAnnotations>
      <appliedProfile href="model/ISO20022Profile.profile.uml#_rspygA6UEeWHY4KRRC3kUQ"/>
    </profileApplication>
  </uml:Package>
  <ISO20022Profile:BusinessRole xmi:id="_eJZL5iquEeWRmPUlcrfDwQ" base_NamedElement="_eJYk0yquEeWRmPUlcrfDwQ" base_Actor="_eJYk0yquEeWRmPUlcrfDwQ"/>
</xmi:XMI>


But .json is not, there no attributes (in this case "name" attribute):
[
  {
    "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//Package",
    "_id": "_ezM94CquEeWRmPUlcrfDwQ",
    "packageImport": [
      {
        "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//PackageImport",
        "_id": "_ezM94SquEeWRmPUlcrfDwQ",
        "visibility": "public",
        "importedPackage": {
          "$ref": "pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#_0"
        },
        "importingNamespace": {
          "$ref": "_ezM94CquEeWRmPUlcrfDwQ"
        }
      }
    ],
    "packagedElement": [
      {
        "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//Actor",
        "_id": "_ezM94iquEeWRmPUlcrfDwQ"
      },
      {
        "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//Actor",
        "_id": "_ezM94yquEeWRmPUlcrfDwQ"
      },
      {
        "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//Actor",
        "_id": "_ezM95CquEeWRmPUlcrfDwQ"
      },
      {
        "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//Actor",
        "_id": "_ezM95SquEeWRmPUlcrfDwQ"
      },
      {
        "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//Actor",
        "_id": "_ezM95iquEeWRmPUlcrfDwQ"
      },
      {
        "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//UseCase",
        "_id": "_ezM95yquEeWRmPUlcrfDwQ"
      },
      {
        "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//UseCase",
        "_id": "_ezM96CquEeWRmPUlcrfDwQ"
      },
      {
        "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//UseCase",
        "_id": "_ezM96SquEeWRmPUlcrfDwQ"
      },
      {
        "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//UseCase",
        "_id": "_ezM96iquEeWRmPUlcrfDwQ"
      },
      {
        "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//UseCase",
        "_id": "_ezM96yquEeWRmPUlcrfDwQ"
      },
      {
        "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//UseCase",
        "_id": "_ezM97CquEeWRmPUlcrfDwQ"
      },
      {
        "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//UseCase",
        "_id": "_ezM97SquEeWRmPUlcrfDwQ"
      }
    ],
    "profileApplication": [
      {
        "eClass": "http://www.eclipse.org/uml2/5.0.0/UML#//ProfileApplication",
        "_id": "_ezM97iquEeWRmPUlcrfDwQ",
        "eAnnotations": [
          {
            "eClass": "http://www.eclipse.org/emf/2002/Ecore#//EAnnotation",
            "_id": "_ezM97yquEeWRmPUlcrfDwQ",
            "source": "http://www.eclipse.org/uml2/2.0.0/UML",
            "references": [
              {
                "$ref": "model/ISO20022Profile.profile.uml#_Z1fsECR_EeW-eZ1ZLTAX7g"
              }
            ]
          }
        ],
        "appliedProfile": {
          "$ref": "model/ISO20022Profile.profile.uml#_rspygA6UEeWHY4KRRC3kUQ"
        },
        "applyingPackage": {
          "$ref": "_ezM94CquEeWRmPUlcrfDwQ"
        }
      }
    ]
  },
  {
    "eClass": "model/ISO20022Profile.profile.uml#_Z1hhpyR_EeW-eZ1ZLTAX7g",
    "_id": "_ezNk8CquEeWRmPUlcrfDwQ",
    "registrationStatus": "PROVISIONALLY_REGISTERED ",
    "base_NamedElement": {
      "$ref": "_ezM94yquEeWRmPUlcrfDwQ"
    },
    "base_Actor": {
      "$ref": "_ezM94yquEeWRmPUlcrfDwQ"
    }
  }
]


What happen? What I should do?
Re: emf json problem [message #1701698 is a reply to message #1701693] Wed, 15 July 2015 06:26 Go to previous messageGo to next message
Alexander Emelianov is currently offline Alexander EmelianovFriend
Messages: 10
Registered: July 2015
Junior Member
Attributes filtered in org.emfjson.jackson.streaming.StreamWriter.generate() on line 151, here calls org.emfjson.common.EObjects.isCandidate():
    public static boolean isCandidate(EObject object, EAttribute attribute) {
        return (object.eIsSet(attribute) || attribute.getEType() instanceof EEnum) && 
                !attribute.isDerived() && 
                !attribute.isTransient() && 
                !attribute.isUnsettable();
    }

For some needed attributes object.eIsSet(attribute) is false or !attribute.isUnsettable() is false.

Who know this filter purpose?
Re: emf json problem [message #1701708 is a reply to message #1701698] Wed, 15 July 2015 07:49 Go to previous messageGo to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 343
Registered: August 2013
Senior Member
It seems that name attribute of the uml::NamedElement is unsetable but we need to serialize it. Is there a workaround?
Re: emf json problem [message #1701764 is a reply to message #1701698] Wed, 15 July 2015 13:13 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

That looks like a bug, that somebody misinterpreted "unsettable" as
meaning "cannot be set" instead of its true meaning of "can be
explicitly reverted to the unset state".

Cheers,

Christian


On 2015-07-15 06:26:47 +0000, Alexander Emelianov said:

> Attributes filtered in
> org.emfjson.jackson.streaming.StreamWriter.generate() on line 151, here
> calls org.emfjson.common.EObjects.isCandidate():
>
> public static boolean isCandidate(EObject object, EAttribute attribute) {
> return (object.eIsSet(attribute) || attribute.getEType()
> instanceof EEnum) && !attribute.isDerived() &&
> !attribute.isTransient() &&
> !attribute.isUnsettable();
> }
>
> For some needed attributes object.eIsSet(attribute) is false or
> !attribute.isUnsettable() is false.
>
> Who know this filter purpose?
Re: emf json problem [message #1701768 is a reply to message #1701764] Wed, 15 July 2015 13:32 Go to previous message
Alexander Emelianov is currently offline Alexander EmelianovFriend
Messages: 10
Registered: July 2015
Junior Member
Tnx, Christian!

I hope it will be fixed by author. (I can do a pull request - works fine without !attribute.isUnsettable())
Previous Topic:[CDO] Concurrent accesses and ObjectNotFoundException
Next Topic:[Ecore] xsd2ecore files not readable any longer after migration to Mars
Goto Forum:
  


Current Time: Thu Apr 18 07:45:51 GMT 2024

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

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

Back to the top