Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Introduce factory created EObjects
Introduce factory created EObjects [message #986888] Thu, 22 November 2012 09:25 Go to next message
Gaspar DinFriend
Messages: 21
Registered: September 2012
Junior Member
Hello,

in my xtext project is the need of predefining some types and function calls. There are only known at runtime.
I followed the post 295656 "Dynamically creating EObjects in the linker [message #803468]":

1. In class MyDslLinkingService some types and functions are created as described in this extracted example:
public List<EObject> getLinkedObjects(EObject context, EReference ref, INode node) throws IllegalNodeException {
    List<EObject> list = super.getLinkedObjects(context, ref, node);
    if ( list.isEmpty()) {
        URI uri = URI.createURI("dummy.projectionsatisfier");
        Resource resource = context.eResource().getResourceSet().getResource(uri, false);
        if (resource == null) {
        resource = context.eResource().getResourceSet().createResource(uri);

        MyDslFactory f = MyDslFactory.eINSTANCE;
                
        Module module = IntTermFactoryImpl.eINSTANCE.createModule();
        module.setName("DummyModule");
                
        Type type1 = f.createType();
        type1.setName("Int");
        type1.setBaseType(null);
        resource.getContents().add(type1);
        module.getStatements().add(type1);
        
        Type type2 = f.createType();
        type2.setName("String");
        type2.setBaseType(null);
        resource.getContents().add(type2);
        module.getStatements().add(type2);
        
        SerializerOptions op = new SerializerOptions(false, true);
        Map map = new HashMap();
        map.put(XtextResource.OPTION_SERIALIZATION_OPTIONS, op);
        try {
            resource.save(new FileOutputStream("c:\\temp\\resource.txt"), map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        list = Collections.singletonList((EObject)type2); // which object shall be added to this list?
        }
    }
    return list;
}


2.) In Class MyDslLazyLinker the resource will be deleted before creating a new one:
protected void beforeModelLinked(EObject model, IDiagnosticConsumer diagnosticsConsumer) {
    super.beforeModelLinked(model, diagnosticsConsumer);
    Resource res = model.eResource().getResourceSet().getResource(URI.createURI("dummy.projectionsatisfier"), false);
    if (res != null) {
        try {
            res.delete(null);
        } catch (IOException e) {
            int i=0;
        }
    }
}


But this approach produces error markers for the added Types in the xtext editor:
The feature 'types' of '...' contains a dangling reference '... TypeImpl ...'

Analog, I introduced some function definitions but there is only says "Couldn't resolve reference to Definition 'Sin'." int the xtext editor.

If I also add the types and functions to the constructor of MyDslScopeProvider (to the List<IEObjectDescription> knownObjects) and return them in the method
public IScope getScope(EObject context, EReference reference) {
    return new SimpleScope(super.getScope(context, reference), knownObjects);
}

then the function call is somehow known but there is still an error marker 'dangling reference' for the types.

So, the types are known but some other connection is still missing.

Here are my questions:
1. You can only add one object to the list (method getLinkedObjects). Does it matter, which object will be added here?
2. Am I missing something or is there any mistake in this approach?
3. What about the resource, the created file only contains an xml header - is this okay?

Greetings,
Gaspar

[Updated on: Thu, 22 November 2012 10:21]

Report message to a moderator

Re: Introduce factory created EObjects [message #987676 is a reply to message #986888] Tue, 27 November 2012 14:41 Go to previous message
Gaspar DinFriend
Messages: 21
Registered: September 2012
Junior Member
Okay, I found out. If s.o. need to know:

First, the EObjects have to be created in the MyDslLinkingService.
Then, for every EObject, an EObjectDescription has to be created like:
EObjectDescription.create(name, obj);

And last, those EObjectDescriptions have to be returned in the MyDslScopeProvider:

@Override
public IScope getScope(EObject context, EReference reference) {
    return new SimpleScope(super.getScope(context, reference), knownObjects);
}


Btw., in the MyDslLazyLinker, I have not overwritten method beforeModelLinked(..), i.e. I didn't had to delete my resource there.

Gretings,
Gaspar
Previous Topic:linking performance
Next Topic:Beginner: how to reference attribute of entity (scope, context, ??)
Goto Forum:
  


Current Time: Mon Dec 22 21:31:48 GMT 2014

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

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