Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » QVT-OML » Blackbox library in a standalone QVTo transformation
Blackbox library in a standalone QVTo transformation [message #1220745] Fri, 13 December 2013 11:30 Go to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 105
Registered: August 2013
Senior Member
Hi

I'm trying to follow Michael Heerklotz suggestion:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=326871#c10

Here is my code
        IExtensionRegistry ecliseRegistry = RegistryFactory.createRegistry(null, null, null);
        FileInputStream is = new FileInputStream("plugin.xml");
        RegistryContributor contributor = new RegistryContributor("1", "test", null, null);
        ecliseRegistry.addContribution(is, contributor, false, null, null, null);
        
        for (IExtension ext : ecliseRegistry.getExtensions(contributor)) {
            if (ext.getExtensionPointUniqueIdentifier().equals("org.eclipse.m2m.qvt.oml.javaBlackboxUnits")) {
                for (IConfigurationElement config : ext.getConfigurationElements()) {
                    // JavaBlackboxProvider jbp = new JavaBlackboxProvider();
                    Class<?> providerClass = Class.forName("org.eclipse.m2m.internal.qvt.oml.blackbox.java.JavaBlackboxProvider"); 
                    Object jbp = providerClass.newInstance();
                    
                    // JavaBlackboxProvider.Descriptor descriptor = jbp.createDescriptor(unit);
                    Method createDescriptor = providerClass.getDeclaredMethod("createDescriptor", IConfigurationElement.class);
                    createDescriptor.setAccessible(true);
                    Object descriptor = createDescriptor.invoke(jbp, config);
                    
                    // jbp.fDescriptorMap = new HashMap<?,?>();
                    Field fDescriptorMapField = providerClass.getDeclaredField("fDescriptorMap");
                    fDescriptorMapField.setAccessible(true);
                    fDescriptorMapField.set(jbp, HashMap.class.newInstance());

                    // jbp.fDescriptorMap.put(id, descriptor);
                    Object fDescriptorMap = fDescriptorMapField.get(jbp);
                    Method putMethod = fDescriptorMapField.getType().getDeclaredMethod("put", Object.class, Object.class);
                    putMethod.invoke(fDescriptorMap, "id", descriptor);

                    // BlackboxRegistry.INSTANCE.fProviders = new LinkedList<?>()
                    Class<?> registryClass = Class.forName("org.eclipse.m2m.internal.qvt.oml.blackbox.BlackboxRegistry"); 
                    Field registryInstanceField = registryClass.getDeclaredField("INSTANCE");
                    Object registryInstance = registryInstanceField.get(null);
                    Field fProvidersField = registryClass.getDeclaredField("fProviders");
                    fProvidersField.setAccessible(true);
                    fProvidersField.set(registryInstance, LinkedList.class.newInstance());
                    
                    // BlackboxRegistry.INSTANCE.fProviders.add(jbp);
                    Object fProviders = fProvidersField.get(registryInstance);
                    Method addMethod = fProvidersField.getType().getDeclaredMethod("add", Object.class);
                    addMethod.invoke(fProviders, jbp);
                }
            }
        }

Full version is here:
https://github.com/AresEkb/OclToXPath

But blackbox library still doesn't work. Could someone help me?
Re: Blackbox library in a standalone QVTo transformation [message #1220758 is a reply to message #1220745] Fri, 13 December 2013 12:39 Go to previous messageGo to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 105
Registered: August 2013
Senior Member
I works!!

This line
                    putMethod.invoke(fDescriptorMap, "id", descriptor);

should be replaced by
                    putMethod.invoke(fDescriptorMap, "m2m.qvt.oml.ExampleJavaLib", descriptor);

Thanks a lot to Michael. I hope the code will be helpful for somebody else.
Re: Blackbox library in a standalone QVTo transformation [message #1699267 is a reply to message #1220758] Tue, 23 June 2015 07:43 Go to previous messageGo to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 105
Registered: August 2013
Senior Member
Hi

The API for blackbox libraries was added:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=326871

Thanks a lot!

But, can you provide an example? How to use it?

The following code reads blackbox unit descriptions from plugin.xml and registers them:
    private static void registerBlackboxUnits() throws FileNotFoundException, ClassNotFoundException, InvalidRegistryObjectException
    {
        IExtensionRegistry ecliseRegistry = RegistryFactory.createRegistry(null, null, null);
        FileInputStream is = new FileInputStream("plugin.xml");
        RegistryContributor contributor = new RegistryContributor("1", "test", null, null);
        ecliseRegistry.addContribution(is, contributor, false, null, null, null);

        for (IExtension ext : ecliseRegistry.getExtensions(contributor)) {
            if (ext.getExtensionPointUniqueIdentifier().equals("org.eclipse.m2m.qvt.oml.javaBlackboxUnits")) {
                for (IConfigurationElement config : ext.getConfigurationElements()) {
                    List<String> packageURIs = new ArrayList<String>();
                    for (IConfigurationElement chld : config.getChildren()) {
                        packageURIs.add(chld.getAttribute("nsURI"));
                    }
                    BlackboxRegistry.INSTANCE.addStandaloneModule(
                            Class.forName(config.getAttribute("class")),
                            "uml2xsd." + config.getAttribute("name"),
                            config.getAttribute("name"),
                            packageURIs.toArray(new String[] {}));
                }
            }
        }
    }


But I receive the warning:
Discouraged access: The method 'BlackboxRegistry.addStandaloneModule(Class<?>, String, String, String[])' is not API (restriction on required library 'C:\Work\eclipse\plugins\org.eclipse.m2m.qvt.oml_3.5.0.v20150603-1058.jar')


And also the code looks very ugly. Is there a better approach?

[Updated on: Tue, 23 June 2015 07:43]

Report message to a moderator

Re: Blackbox library in a standalone QVTo transformation [message #1699444 is a reply to message #1699267] Wed, 24 June 2015 11:21 Go to previous messageGo to next message
Sergey Boyko is currently offline Sergey BoykoFriend
Messages: 164
Registered: July 2009
Senior Member
Hi Denis ,

In order to register blackbox library for use in standalone execution mode one can use:
TransformationExecutor.BlackboxRegistry.INSTANCE.registerModules(Test.class);


So Java code might look as follows:

TransformationExecutor.BlackboxRegistry.INSTANCE.registerModules(Test.class);

TransformationExecutor executor = new TransformationExecutor(uri);
ExecutionContextImpl context = new ExecutionContextImpl();
ExecutionDiagnostic result = executor.execute(context);


where Test.class is Java blackbox library:

@Module(packageURIs={"http://www.eclipse.org/emf/2002/Ecore"})
public class Test {

	public EClass modifyClassName(EClass cls, String name) {
		cls.setName(name);
		return cls;
	}

}


Regards,
Sergey.
Re: Blackbox library in a standalone QVTo transformation [message #1699552 is a reply to message #1699444] Thu, 25 June 2015 05:29 Go to previous message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 105
Registered: August 2013
Senior Member
Hi

Thanks a lot! It works!
Previous Topic:java M2MT
Next Topic:Map one object into two models
Goto Forum:
  


Current Time: Fri Sep 04 14:39:47 GMT 2015

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

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