/** * Register all the SysML default packages * * @param packageRegistry */ private void registSysMLPackages(Registry packageRegistry) { // Register all default SYSML packages packageRegistry.put(SysmlPackage.eNS_URI, SysmlPackage.eINSTANCE); packageRegistry.put(ActivitiesPackage.eNS_URI, ActivitiesPackage.eINSTANCE); packageRegistry.put(AllocationsPackage.eNS_URI, AllocationsPackage.eINSTANCE); packageRegistry.put(BlocksPackage.eNS_URI, BlocksPackage.eINSTANCE); packageRegistry.put(ConstraintsPackage.eNS_URI, ConstraintsPackage.eINSTANCE); packageRegistry.put(InteractionsPackage.eNS_URI, InteractionsPackage.eINSTANCE); packageRegistry.put(ModelelementsPackage.eNS_URI, ModelelementsPackage.eINSTANCE); packageRegistry.put(PortandflowsPackage.eNS_URI, PortandflowsPackage.eINSTANCE); packageRegistry.put(RequirementsPackage.eNS_URI, RequirementsPackage.eINSTANCE); packageRegistry.put(StatemachinesPackage.eNS_URI, StatemachinesPackage.eINSTANCE); packageRegistry.put(UsecasesPackage.eNS_URI, UsecasesPackage.eINSTANCE); packageRegistry.put(UsecasesPackage.eNS_URI, UsecasesPackage.eINSTANCE); }
@Test public void testExternalFormOfEReference() throws Exception { Registry registry = EPackage.Registry.INSTANCE; Set<String> uris = Sets.newHashSet(registry.keySet()); for (String string : uris) { EPackage ePackage = registry.getEPackage(string); TreeIterator<Object> iterator = EcoreUtil.getAllProperContents(ePackage, true); while (iterator.hasNext()) { Object next = iterator.next(); if (next instanceof EReference) { EReference ref = (EReference) next; String externalForm = EcoreUtil2.toExternalForm(ref); assertEquals(ref.toString() + " - " + externalForm, ref, EcoreUtil2.getEReferenceFromExternalForm(registry,externalForm)); } } } }
/** * This test assumes that an EPackage with indexed references is no longer available. */ @Test public void testExternalFormOfEReferenceNoNPE() throws Exception { EReference reference = EcorePackage.Literals.EATTRIBUTE__EATTRIBUTE_TYPE; URI uri = EcoreUtil.getURI(reference); String externalForm = uri.toString(); EReference foundReference = EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, externalForm); assertSame(reference, foundReference); String brokenExternalFrom = makeInvalid(externalForm); assertNull(EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, brokenExternalFrom)); String shortExternalForm = EcoreUtil2.toExternalForm(reference); foundReference = EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, shortExternalForm); assertSame(reference, foundReference); String brokenShortExternalFrom = makeInvalid(shortExternalForm); assertNull(EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, brokenShortExternalFrom)); brokenShortExternalFrom = shortExternalForm.replace('A', 'a'); assertNull(EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, brokenShortExternalFrom)); }
private void handle(final HawkModelElementAdditionEvent ev) { final Registry registry = getResourceSet().getPackageRegistry(); final EClass eClass = HawkResourceImpl.getEClass(ev.metamodelURI, ev.typeName, registry); final EObject existing = nodeIdToEObjectMap.get(ev.id); if (existing == null) { final EObject eob = createInstance(eClass); nodeIdToEObjectMap.put(ev.id, eob); synchronized(classToEObjectsMap) { final EList<EObject> instances = classToEObjectsMap.get(eClass); if (instances != null) { instances.add(eob); } } addToResource(ev.vcsItem.repoURL, ev.vcsItem.path, eob); LOGGER.debug("Added a new {} with ID {}", eob.eClass().getName(), ev.id); instanceInserted(eob, eob.eClass()); } else { LOGGER.warn("We already have a {} with ID {}: cannot create a {} there", existing.eClass().getName(), ev.id, eClass.getName()); } }
private static EClass getEClass(final String metamodelUri, final String typeName, final Registry packageRegistry) { final EPackage pkg = packageRegistry.getEPackage(metamodelUri); if (pkg == null) { throw new NoSuchElementException(String.format( "Could not find EPackage with URI '%s' in the registry", metamodelUri)); } final EClassifier eClassifier = pkg.getEClassifier(typeName); if (!(eClassifier instanceof EClass)) { throw new NoSuchElementException(String.format( "Received an element of type '%s', which is not an EClass", eClassifier)); } final EClass eClass = (EClass) eClassifier; return eClass; }
private static EClass getEClass(final String metamodelUri, final String typeName, final Registry packageRegistry) { final EPackage pkg = packageRegistry.getEPackage(metamodelUri); if (pkg == null) { throw new NoSuchElementException(String.format( "Could not find EPackage with URI '%s' in the registry %s", metamodelUri, packageRegistry)); } final EClassifier eClassifier = pkg.getEClassifier(typeName); if (!(eClassifier instanceof EClass)) { throw new NoSuchElementException(String.format( "Received an element of type '%s', which is not an EClass", eClassifier)); } final EClass eClass = (EClass) eClassifier; return eClass; }
/** * Registers a EPackage in {@link Registry} according to its {@code prefix} and {@code uri}, from an Ecore file. * <p> * The targeted Ecore file must be present in {@code /resources/ecore}. */ protected static void registerEPackageFromEcore(String prefix, String uri) { File file = getResourceFile(ECORE_PATH.replaceAll("\\{name\\}", prefix)); EPackage ePackage = null; Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(ECORE, new EcoreResourceFactoryImpl()); ResourceSet rs = new ResourceSetImpl(); final ExtendedMetaData extendedMetaData = new BasicExtendedMetaData(rs.getPackageRegistry()); rs.getLoadOptions().put(XMLResource.OPTION_EXTENDED_META_DATA, extendedMetaData); Resource r = rs.getResource(URI.createFileURI(file.toString()), true); EObject eObject = r.getContents().get(0); if (eObject instanceof EPackage) { ePackage = (EPackage) eObject; rs.getPackageRegistry().put(ePackage.getNsURI(), ePackage); } assertThat(ePackage).isNotNull(); // "EPackage does not exist" Registry.INSTANCE.put(uri, ePackage); }
/** * Gets all available EPackages from the EPackageRegistry. * * @param filterKnown weather known packages should be filtered or not. * @return the available packages * */ public static Set<EPackage> getAvailablePackages(boolean filterKnown) { Registry registry = EPackage.Registry.INSTANCE; HashSet<Entry<String, Object>> entries = new LinkedHashSet<Entry<String, Object>>(registry.entrySet()); Set<EPackage> packages = new LinkedHashSet<EPackage>(); for (Entry<String, Object> entry : entries) { if (!filterKnown || !isKnownPackage(entry.getKey())) { try { EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(entry.getKey()); packages.add(ePackage); // BEGIN SUPRESS CATCH EXCEPTION } catch (RuntimeException e) { // END SUPRESS CATCH EXCEPTION WorkspaceUtil.logWarning("Failed to load EPackage", e); } } } return packages; }
/** * Iterates over all registered EPackages in order to retrieve all available * EClasses, excluding abstract classes and interfaces, and returns them as * a Set. * * @return a set of all EClasses that are contained in registered EPackages * @see Registry */ public List<EClass> getAllEClasses() { if (allEClasses == null) { allEClasses = new ArrayList<EClass>(); Registry registry = EPackage.Registry.INSTANCE; // for all registered EPackages for (Entry<String, Object> entry : new LinkedHashSet<Entry<String, Object>>(registry.entrySet())) { EPackage ePackage = registry.getEPackage(entry.getKey()); for (EClass eClass : getAllEClasses(ePackage)) { // no abstracts or interfaces if (canHaveInstance(eClass)) { allEClasses.add(eClass); } } } } return allEClasses; }
/** * Retrieve all EClasses from the ECore package registry. * * @return a set of EClasses */ public static Set<EClass> getAllModelElementEClasses() { if (registryEClasses != null) { return new LinkedHashSet<EClass>(registryEClasses); } final Set<EClass> result = new LinkedHashSet<EClass>(); final Registry registry = EPackage.Registry.INSTANCE; for (Entry<String, Object> entry : new LinkedHashSet<Entry<String, Object>>(registry.entrySet())) { try { final EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(entry.getKey()); result.addAll(getAllModelElementEClasses(ePackage)); } // BEGIN SUPRESS CATCH EXCEPTION catch (RuntimeException exception) { Activator.getDefault().logException("Failed to load model package " + entry.getKey(), exception); // END SUPRESS CATCH EXCEPTION } } registryEClasses = result; return result; }
/** * Returns all EPackages on the root level that are currently registered in * the registry. * * @return a Set of all root EPackages * @see Registry */ public Set<EPackage> getAllRootEPackages() { // were the root packages computed before? if (rootModelPackages != null) { return rootModelPackages; } rootModelPackages = new LinkedHashSet<EPackage>(); Registry registry = EPackage.Registry.INSTANCE; for (Entry<String, Object> entry : registry.entrySet()) { EPackage ePackage = registry.getEPackage(entry.getKey()); // is the current EPackage a root package? if (ePackage.getESuperPackage() == null) { rootModelPackages.add(ePackage); } } return rootModelPackages; }
/** * Iterates over all registered EPackages in order to retrieve all available * EClasses, excluding abstract classes and interfaces, and returns them as * a Set. * * @return a set of all EClasses that are contained in registered EPackages * @see Registry */ public List<EClass> getAllEClasses() { // were all EClasses computed before? if (allEClasses != null) { return allEClasses; } allEClasses = new LinkedList<EClass>(); Registry registry = EPackage.Registry.INSTANCE; // for all registered EPackages for (Entry<String, Object> entry : registry.entrySet()) { EPackage ePackage = registry.getEPackage(entry.getKey()); for (EClass eClass : getAllEClasses(ePackage)) { // no abstracts or interfaces if (canHaveInstance(eClass)) { allEClasses.add(eClass); } } } return allEClasses; }
/** * Initialize the UML resourceSet and register all the packages by default * * @return resourceSet * @throws IOException */ public ResourceSet initResourceSet() throws IOException { String method = "InitResourceSet_initResourceSet(): "; long startTime = System.currentTimeMillis(); MyLog.info(method + "start"); ResourceSet resourceSet = new ResourceSetImpl(); Registry packageRegistry = resourceSet.getPackageRegistry(); // Read the namespace configuration file String namespaceConfigPath = FileLocator .toFileURL(Activator.getDefault().getBundle().getEntry(NAMESPACE_CONFIG_FILE_PAHT)).getPath(); File namespaceConfigFile = new File(namespaceConfigPath); Properties properties = new Properties(); properties.load(new FileInputStream(namespaceConfigFile)); // Load and register the self-definition namespaces for (String key : properties.stringPropertyNames()) { String value = properties.getProperty(key); packageRegistry.put(value, UMLPackage.eINSTANCE); } registSysMLPackages(packageRegistry); // Add the load option resourceSet.getLoadOptions().put(XMIResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.TRUE); // Define the extension to factory map Map<String, Object> extensionToFactoryMap = resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap(); UMLResourceFactoryImpl umlResourceFactory = new UMLResourceFactoryImpl(); extensionToFactoryMap.put(XMI2UMLResource.FILE_EXTENSION, umlResourceFactory); MyLog.info(method + "end with " + (System.currentTimeMillis() - startTime) + " millisecond"); MyLog.info(); return resourceSet; }
@Test public void testClone() throws Exception { Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()); EPackage.Registry.INSTANCE.put(EcorePackage.eINSTANCE.getNsURI(), EcorePackage.eINSTANCE); ResourceSetImpl rs = new ResourceSetImpl(); Resource r1 = rs.createResource(URI.createURI("foo.xmi"), ContentHandler.UNSPECIFIED_CONTENT_TYPE); Resource r2 = rs.createResource(URI.createURI("bar.xmi"), ContentHandler.UNSPECIFIED_CONTENT_TYPE); EClass a = EcoreFactory.eINSTANCE.createEClass(); a.setName("a"); EClass b = EcoreFactory.eINSTANCE.createEClass(); r1.getContents().add(a); b.setName("b"); b.getESuperTypes().add(a); r2.getContents().add(b); ResourceSetImpl clone = EcoreUtil2.clone(new ResourceSetImpl(), rs); EList<Resource> list = clone.getResources(); Resource resA = list.get(0); assertEquals(URI.createURI("foo.xmi"),resA.getURI()); assertNotSame(resA, r1); Resource resB = list.get(1); assertEquals(URI.createURI("bar.xmi"),resB.getURI()); assertNotSame(resB, r2); EClass a1 = (EClass)resA.getContents().get(0); EClass b1 = (EClass)resB.getContents().get(0); assertEquals("a", a1.getName()); assertNotSame(a, a1); assertEquals("b", b1.getName()); assertNotSame(b, b1); assertSame(b1.getESuperTypes().get(0),a1); assertSame(b.getESuperTypes().get(0),a); }
private EObject createEObject(final ModelElement me) throws IOException { final Registry registry = getResourceSet().getPackageRegistry(); final EClass eClass = getEClass(me.metamodelUri, me.typeName, registry); final EObject obj = createInstance(eClass); if (me.isSetId()) { nodeIdToEObjectMap.put(me.id, obj); synchronized(classToEObjectsMap) { final EList<EObject> instances = classToEObjectsMap.get(eClass); if (instances != null) { instances.add(obj); } } } if (me.isSetAttributes()) { final EFactory factory = registry.getEFactory(eClass.getEPackage().getNsURI()); for (final AttributeSlot s : me.attributes) { SlotDecodingUtils.setFromSlot(factory, eClass, obj, s); } } else if (!descriptor.getLoadingMode().isGreedyAttributes()) { getLazyResolver().putLazyAttributes(me.id, obj); } return obj; }
private void fillInReferences(final List<ModelElement> elems, final TreeLoadingState state) throws IOException { final Registry packageRegistry = getResourceSet().getPackageRegistry(); for (final ModelElement me : elems) { final EObject sourceObj = state.meToEObject.remove(me); fillInReferences(packageRegistry, me, sourceObj, state); } }
@Override public void modelElementAddition(VcsCommitItem s, IHawkObject element, IGraphNode elementNode, boolean isTransient) { // new model elements are assumed to be roots unless proven otherwise if (isTransient) return; final IHawkClassifier type = element.getType(); final Registry packageRegistry = resource.getResourceSet().getPackageRegistry(); final EPackage ePackage = packageRegistry.getEPackage(type.getPackageNSURI()); final EClass eClass = (EClass)ePackage.getEClassifier(type.getName()); addedRootNodes.put(elementNode, eClass); }
public BPMNMetaModelResourceFactory() { if (EPackage.Registry.INSTANCE.getEPackage(EcorePackage.eNS_URI) == null) { EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE); } resourceSet = new ResourceSetImpl(); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() .put("ecore", new EcoreResourceFactoryImpl()); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() .put("*", new XMIResourceFactoryImpl()); }
private LinkedList<String> checkRegistry() { Registry globalRegistry = EPackage.Registry.INSTANCE; LinkedList<String> missingPackages = new LinkedList<>(); if (!globalRegistry .containsKey("http://www.eclipse.org/emf/2003/XMLType")) missingPackages.add("http://www.eclipse.org/emf/2003/XMLType"); if (!globalRegistry .containsKey("http://www.omg.org/spec/BPMN/20100524/MODEL-XMI")) missingPackages .add("http://www.omg.org/spec/BPMN/20100524/MODEL-XMI"); if (!globalRegistry .containsKey("http://www.omg.org/spec/DD/20100524/DC-XMI")) missingPackages.add("http://www.omg.org/spec/DD/20100524/DC-XMI"); if (!globalRegistry .containsKey("http://www.eclipse.org/emf/2002/Ecore")) missingPackages.add("http://www.eclipse.org/emf/2002/Ecore"); if (!globalRegistry .containsKey("http://www.omg.org/spec/BPMN/20100524/DI-XMI")) missingPackages.add("http://www.omg.org/spec/BPMN/20100524/DI-XMI"); if (!globalRegistry .containsKey("http://www.omg.org/spec/DD/20100524/DI-XMI")) missingPackages.add("http://www.omg.org/spec/DD/20100524/DI-XMI"); return missingPackages; }
/** * Compute the {@link EClass} associated to the model element with the provided {@link Id}. * * @param id the {@link Id} of the model element to compute the {@link EClass} from * * @return an {@link EClass} representing the metaclass of the element */ private EClass resolveInstanceOf(Id id) { try { Result result = table.get(new Get(Bytes.toBytes(id.toString()))); String nsURI = Bytes.toString(result.getValue(TYPE_FAMILY, METAMODEL_QUALIFIER)); String className = Bytes.toString(result.getValue(TYPE_FAMILY, ECLASS_QUALIFIER)); if (nonNull(nsURI) && nonNull(className)) { return (EClass) Registry.INSTANCE.getEPackage(nsURI).getEClassifier(className); } } catch (IOException e) { NeoLogger.error("Unable to get instance of information for {0}", id); } return null; }
public static void register(EPackage... pkgs) { for(int i = 0; i < pkgs.length; i++) { if (!EPackage.Registry.INSTANCE.containsKey( pkgs[i].getNsURI() )) { EPackage.Registry.INSTANCE.put(pkgs[i].getNsURI(), pkgs[i]); } } }
public static void register(Registry registry, EPackage... pkgs) { for(int i = 0; i < pkgs.length; i++) { if (! registry.containsKey( pkgs[i].getNsURI() )) { registry.put(pkgs[i].getNsURI(), pkgs[i]); } } }
@Override protected boolean processResource(Resource resource) { ResourceSet resourceSet = domain.getResourceSet(); if (!resourceSet.getResources().contains(resource)) { Registry packageRegistry = resourceSet.getPackageRegistry(); for (EPackage ePackage : getAllPackages(resource)) { packageRegistry.put(ePackage.getNsURI(), ePackage); registeredPackages.add(ePackage); } } return true; }
public Registry getRegistry() { return registry; }
public void setRegistry(Registry registry) { this.registry = registry; }
public LazyEObjectFactory(Registry registry, MethodInterceptor interceptor) { this.packageRegistry = registry; this.methodInterceptor = interceptor; }
@Override public HashSet<IHawkMetaModelResource> getStaticMetamodels() { HashSet<IHawkMetaModelResource> set = new HashSet<>(); LinkedList<String> missingPackages = checkRegistry(); Registry globalRegistry = EPackage.Registry.INSTANCE; // if not running in eclipse if (missingPackages.size() > 0) { // System.err.println("missing packages detected, adding them now..."); RegisterMeta.registerPackages(EcorePackageImpl.eINSTANCE); RegisterMeta.registerPackages(XMLTypePackageImpl.eINSTANCE); RegisterMeta.registerPackages(Bpmn2Package.eINSTANCE); RegisterMeta.registerPackages(BpmnDiPackage.eINSTANCE); RegisterMeta.registerPackages(DiPackage.eINSTANCE); RegisterMeta.registerPackages(DcPackage.eINSTANCE); missingPackages = checkRegistry(); } if (missingPackages.size() == 0) { set.add(new BPMNMetaModelResource(globalRegistry.getEPackage( "http://www.eclipse.org/emf/2003/XMLType").eResource(), this)); set.add(new BPMNMetaModelResource(globalRegistry.getEPackage( "http://www.omg.org/spec/BPMN/20100524/MODEL-XMI") .eResource(), this)); set.add(new BPMNMetaModelResource(globalRegistry.getEPackage( "http://www.omg.org/spec/DD/20100524/DC-XMI").eResource(), this)); set.add(new BPMNMetaModelResource(globalRegistry.getEPackage( "http://www.eclipse.org/emf/2002/Ecore").eResource(), this)); set.add(new BPMNMetaModelResource( globalRegistry.getEPackage( "http://www.omg.org/spec/BPMN/20100524/DI-XMI") .eResource(), this)); set.add(new BPMNMetaModelResource(globalRegistry.getEPackage( "http://www.omg.org/spec/DD/20100524/DI-XMI").eResource(), this)); } else { System.err .println("WARNING: one or more of the static metamodels of BPMN were not found, no static metamodels inserted for this plugin, please insert the relevant metamodels manually:\n" + missingPackages.toString()); } // System.err.println(set); return set; }
static public void registerFactories(/*MDLog log*/){ Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xsd", xsdResourceFactory); Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("ecore", ecoreResourceFactory); // Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("sch", schematronResourceFactory); }
/** * The constructor. */ public PackageRegistryView() { registry = EPackage.Registry.INSTANCE; }
/** * Registers a EPackage in the EPackageRegistry. * * @param pkg the EPackage to be registered */ public static void registerEPackage(EPackage pkg) { EPackage.Registry.INSTANCE.put(pkg.getNsURI(), pkg); }
/** * Returns the EPackage to the specified <code>nsURI</code>. * * @param nsURI * the NsUri of the EPackage to get * @return the EPackage belonging to <code>nsURI</code> * @see Registry#getEPackage(String) */ public static EPackage getEPackage(String nsURI) { return EPackage.Registry.INSTANCE.getEPackage(nsURI); }
/** * Returns the EPackage to the specified <code>nsURI</code>. * * @param nsURI * the NsUri of the EPackage to get * @return the EPackage belonging to <code>nsURI</code> * @see Registry#getEPackage(String) */ public EPackage getEPackage(String nsURI) { return EPackage.Registry.INSTANCE.getEPackage(nsURI); }