PCM Development/Model Builder Factories

Aus SDQ-Wiki

Instances of the PCM can be built either using editors (graphical, tree-based, textual) or programmatically (using a Java API). You can instantiate one of the model factories, and then use this factory to create model elements defined in the meta model.

Example Code

This is an example which demonstrates how to use the EMF-generated model factories:

public static void main(String[] args) {
	Resource res = new XMLResourceImpl(URI.createFileURI("file://..."));
	new Builder().doBuildBasicComponentWithProvidedInterface(res);
}

public void doBuildBasicComponentWithProvidedInterface(Resource res) {
	Repository repository = RepositoryFactory.eINSTANCE.createRepository();
	repository.setEntityName("Repository containing other elements");
	// add root (repository) to resource:
	res.getContents().add(repository);
	
	BasicComponent bc = RepositoryFactory.eINSTANCE.createBasicComponent();
	bc.setEntityName("My Basic Component");
	repository.getComponents__Repository().add(bc);  //add first class entity
	
	OperationInterface myInterface = RepositoryFactory.eINSTANCE.createOperationInterface();
	myInterface.setEntityName("My Interface");
	repository.getInterfaces__Repository().add(myInterface); //add first class entity
	
	OperationProvidedRole opProvRole = RepositoryFactory.eINSTANCE.createOperationProvidedRole();
	opProvRole.setEntityName("Provided Role of Basic Component");
	
	// set the interface for the role:
	opProvRole.setProvidedInterface__OperationProvidedRole(myInterface);
	
	// set/add the role for basic component:
	bc.getProvidedRoles_InterfaceProvidingEntity().add(opProvRole);
}	

Download Example Project

Download example Eclipse plugin project. To run the project, please install the Palladio-Bench.

Keywords: PCM, model, creation, Java, builder, EMF, factories, factory, API, programmatic