Я написал пример приложения ниже, чтобы создать EJB
стереотип и применить его к классу TimeEntry
:
import java.io.File;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.Resource.Factory;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.uml2.uml.*;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.UMLPackage.Literals;
import org.eclipse.uml2.uml.resource.UMLResource;
public class SampleProfile
{
private static final ResourceSet RESOURCE_SET = new ResourceSetImpl();
public static void main( String[] args ) throws Exception
{
Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
UMLResource.FILE_EXTENSION,
UMLResource.Factory.INSTANCE
);
final Model umlMetamodel = (Model) loadPackage( UMLResource.UML_METAMODEL_URI );
final Model sampleModel = UMLFactory.eINSTANCE.createModel();
sampleModel.setName( "Sample Model" );
final Profile sampleProfile = UMLFactory.eINSTANCE.createProfile();
sampleProfile.setName( "Sample Profile" );
final Stereotype ejbStereo = sampleProfile.createOwnedStereotype( "EJB" );
extendMetaclass( umlMetamodel, sampleProfile, "Class", ejbStereo );
sampleProfile.define();
final Package samplePackage = sampleModel.createNestedPackage( "sample" );
samplePackage.applyProfile( sampleProfile );
final Class sampleClass = samplePackage.createOwnedClass( "TimeEntry", false );
sampleClass.applyStereotype( ejbStereo );
final File outputFile = new File( "sample_model.uml" );
final URI outputUri = URI.createFileURI( outputFile.getAbsolutePath() );
final Resource resource = RESOURCE_SET.createResource( outputUri );
resource.getContents().add( sampleModel );
resource.getContents().add( sampleProfile );
resource.save( null );
}
private static Package loadPackage( final String uri )
{
System.out.println( "uri = " + uri );
final Resource resource = RESOURCE_SET.getResource( URI.createURI( uri ), true );
EcoreUtil.resolveAll( resource );
return (org.eclipse.uml2.uml.Package) EcoreUtil.getObjectByType( resource.getContents(), Literals.PACKAGE );
}
private static void extendMetaclass( final Model umlMetamodel,
final Profile profile,
final String name,
final Stereotype stereotype )
{
stereotype.createExtension( referenceMetaclass( umlMetamodel, profile, name ), true );
}
private static org.eclipse.uml2.uml.Class referenceMetaclass( final Model umlMetamodel,
final Profile profile,
final String name )
{
final Class metaclass = (Class) umlMetamodel.getOwnedType( name );
profile.createMetaclassReference( metaclass );
return metaclass;
}
}
Однако, запустив приложение, я получаю сообщение об ошибке в консоли:
uri = pathmap://UML_METAMODELS/UML.metamodel.uml
Exception in thread "main" org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: java.net.MalformedURLException: unknown protocol: pathmap
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:315)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:397)
Есть идеи, что, вероятно, не так с моим кодом и как эта проблема может быть решена?Приложение представляет собой приложение командной строки, которое запускается вне Eclipse.