-
Notifications
You must be signed in to change notification settings - Fork 17
Code Examples
In order to use NeoEMF, you first need to register a persistence backend factory (concrete subclass of [PersistenceBackendFactory][persistenceBackendFactory-link]). This factory is responsible of the creation of the persistence backend (concrete subclass of [PersistenceBackend][persistenceBackend-link]) that is in charge of the model storage.
Persistence backend factories bundled with NeoEMF are:
- Neo4j under the Blueprints API (dedicated page [here][neo4j-link])
- MapDB (dedicated page [here][mapdb-link])
- HBase (dedicated page [here][hbase-link])
PersistenceBackendFactoryRegistry.register(BlueprintsURI.SCHEME, BlueprintsPersistenceBackendFactory.getInstance());
PersistenceBackendFactoryRegistry.register(MapURI.SCHEME, MapPersistenceBackendFactory.getInstance());
PersistenceBackendFactoryRegistry.register(HBaseURI.SCHEME, HBasePersistenceBackendFactory.getInstance());
As regular EMF initialization, you need to register the [PersistentResourceFactory][persistentResourceFactory-link] implementation in the resource set specifying the URI protocol. Each backend implementation provides a subclass of [PersistenceURI][neoUri-link] to ease protocol definition. Note that the associated [PersistentResourceFactory][persistentResourceFactory-link] and the created [PersistentResource][persistentResource-link] do not depend on the selected backend.
ResourceSet resSet = new ResourceSetImpl();
resSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put(BlueprintsURI.SCHEME, PersistentResourceFactory.getInstance());
Resource resource = resSet.createResource(BlueprintsURI.createFileURI(new File("path_to_neodb")));
ResourceSet resSet = new ResourceSetImpl();
resSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put(MapURI.SCHEME, PersistentResourceFactory.getInstance());
Resource resource = resSet.createResource(MapURI.createFileURI(new File("path_to_mapdb")));
ResourceSet resSet = new ResourceSetImpl();
resSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put(HBaseURI.SCHEME, PersistentResourceFactory.getInstance());
Resource resource = resSet.createResource(HBaseURI.createURI("hbaseHost", "hbasePort", "modelURI"));
Once this two initialization steps has been performed, the resulting resource can be used as a regular EMF [Resource][resource-link].
/* Saving depending on the backend */
resource.save(AbstractPersistenceOptionsBuilder.noOption());
/* Loading depending on the backend */
resource.load(AbstractPersistenceOptionsBuilder.noOption());
PersistenceBackendFactoryRegistry.register(BlueprintsURI.SCHEME, BlueprintsPersistenceBackendFactory.getInstance());
ResourceSet resSet = new ResourceSetImpl();
resSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put(BlueprintsURI.SCHEME, PersistentResourceFactory.getInstance());
Resource resource = resSet.createResource(BlueprintsURI.createFileURI(new File("path_to_neodb")));
// Load the resource
resource.load(BlueprintsOptionsBuilder.noOption());
// Perform modifications
MyClass myClass = (MyClass) resource.getContents().get(0);
myClass.setName("NewName");
// Save the modifications
resource.save(BlueprintsOptionsBuilder.noOption());
// Unload the resource and shutdown the database engine
if (resource instanceof PersistentResource) ((PersistentResource) resource).close();
else resource.unload();
PersistenceBackendFactoryRegistry.register(MapURI.SCHEME, MapPersistenceBackendFactory.getInstance());
ResourceSet resSet = new ResourceSetImpl();
resSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put(MapURI.SCHEME, PersistentResourceFactory.getInstance());
Resource resource = resSet.createResource(MapURI.createFileURI(new File("path_to_neodb")));
// Load the resource
resource.load(MapOptionsBuilder.noOption());
// Perform modifications
MyClass myClass = (MyClass) resource.getContents().get(0);
myClass.setName("NewName");
// Save the modifications
resource.save(MapOptionsBuilder.noOption());
// Unload the resource and shutdown the database engine
if (resource instanceof PersistentResource) ((PersistentResource) resource).close();
else resource.unload();