Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Talk:QVTOML/Examples/InvokeInJava

Alternative Script

The original script didn't work for me, so I tinked this code.

Only after adding
ProjectMap.getAdapter(resourceSet);
from
org.eclipse.ocl.examples.domain.utilities
could I specify the URI's relative to the workspace.

--Eclipse.alshu.de (talk) 12:48, 10 August 2014 (EDT)


import java.io.IOException;
import java.util.Collections;
import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.*;
import org.eclipse.emf.ecore.EPackage.Registry;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.m2m.qvt.oml.*;
import org.eclipse.ocl.examples.domain.utilities.*;

public class main {
	public static void main( String[] args ){
	
	//intput
	//create execution context
	ExecutionContextImpl context= new ExecutionContextImpl();
	//set context properties
	context.setConfigProperty("keepModeling", true);
	
	
	//create resource set
	ResourceSet resourceSet = new ResourceSetImpl();
	//create registry
	Registry reg = Registry.INSTANCE; 
	//create factory for *.ecore data
	resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new EcoreResourceFactoryImpl());
	//create factory for *.xmi data
	resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
	//get project map adapter in order to be able to specify platform URI's
	ProjectMap.getAdapter(resourceSet);
	//register input meta ecore
	EPackage testA = (EPackage)(resourceSet.getResource(URI.createPlatformResourceURI("/testthingswithjava/model/TestA1.ecore", true), true)).getContents().get(0);
	reg.put(testA.getNsURI(), testA);
	//register input model
	Resource  inResource = resourceSet.getResource(URI.createURI("platform:/resource/testthingswithjava/instance/T1in.xmi"),true);
	//register output meta ecore
	EPackage testB = (EPackage)(resourceSet.getResource(URI.createPlatformResourceURI("/testthingswithjava/model/TestB1.ecore", true), true)).getContents().get(0);
	reg.put(testB.getNsURI(), testB);
		
	EList<EObject> inObjects = inResource.getContents();
	// create the input extent with its initial contents
	ModelExtent input = new BasicModelExtent(inObjects);		
	// create an empty extent to catch the output
	ModelExtent output = new BasicModelExtent();

	//setup transformation
	URI transformationURI = URI.createPlatformResourceURI("/testthingswithjava/transforms/Test1.qvto",true);
	//executor
	TransformationExecutor executor = new TransformationExecutor(transformationURI);
		
	// setup the execution environment details -> 
	// configuration properties, logger, monitor object etc.
	

	// run the transformation assigned to the executor with the given 
	// input and output and execution context -> Test1(in, out)
	// Remark: variable arguments count is supported
	ExecutionDiagnostic transformationResult = executor.execute(context, input, output);
	//output
	List<EObject> outObjects = output.getContents();

	//some debug messages
	System.out.print(transformationResult.getMessage() + ". ");
	System.out.print(transformationURI.toString()+"\n");
	System.out.print(inObjects.toString()+"\n");
	System.out.print(outObjects.toString()+"\n");

	
	// create a new resource for saving 
	ResourceSet resourceSet2 = new ResourceSetImpl();
	//we need a new factory for *.xmi data, and the corresponding ProjectMap
	resourceSet2.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
	ProjectMap.getAdapter(resourceSet2);
		
	Resource outResource = resourceSet2.getResource(
			URI.createPlatformResourceURI("/testthingswithjava/instance/T1out.xmi",true), true);
	outResource.getContents().clear();
	outResource.getContents().addAll(outObjects);
	
	
	try {
		outResource.save(Collections.EMPTY_MAP);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
};
}

Copyright © Eclipse Foundation, Inc. All Rights Reserved.