Skip to main content

Introductory Example

The framework itself provides simple means for storing and loading bigraphical metamodels and instance models using the capabilities of Eclipse EMF.

Therefore, the utility class org.bigraphs.framework.core.BigraphFileModelManagement within the bigraph-core module comprises methods for persisting bigraphical models to the filesystem. Basically, BigraphFileModelManagement is a simple file-based model management utility class that serializes (deserializes) to (from) XMI and Ecore:

  • Serializes/Exports Ecore-based bigraph and signature model objects (EObject and EPackage) to *.xmi and *.ecore, respectively.
  • Deserializes/Imports Ecore files (*.xmi and *.ecore) to Ecore-based bigraph and signature model objects (EObject and EPackage)

Both operations are provided by the inner classes Load and Store of org.bigraphs.framework.core.BigraphFileModelManagement to easily distinguish and use them.

XML Metadata Interchange

XMI provides an XML representation for Ecore constructs.

Distributed Bigraph Database

For a more sophisticated persistence solution, we refer the reader to Eclipse Connected Data Objects (CDO) Model Repository and the corresponding implementation Spring Data CDO when working with Spring framework in Java.

Complete Example​

This example shows how to store a bigraph and its signature to the filesystem, and how to load them again.

First, we create a signature and a bigraph for testing:

import org.bigraphs.framework.core.impl.signature.DefaultDynamicSignature;

public DefaultDynamicSignature sig() {
// (1) Create Sample Signature
return pureSignatureBuilder()
.addControl("A", 0)
.addControl("B", 1)
.addControl("C", 2)
.create();
}
import org.bigraphs.framework.core.impl.pure.PureBigraph;

// (2) Create Sample Bigraph
public PureBigraph big() {
return pureBuilder(sig()).createRoot()
.addChild("A")
.addChild("B")
.addChild("C")
.createBigraph();
}

Storing and Loading a Signature​

public void save_and_load_signature_model() throws IOException {
// (0) Create Sample Signature
DefaultDynamicSignature sigOriginal = sig();

// (1) Store Signature on Filesystem
// Console output
BigraphFileModelManagement.Store.exportAsInstanceModel(sigOriginal, System.out);
// File output
BigraphFileModelManagement.Store.exportAsInstanceModel(sigOriginal, new FileOutputStream("sigModel.xmi"));
BigraphFileModelManagement.Store.exportAsMetaModel(sigOriginal, new FileOutputStream("sigMetaModel.ecore"));

// (2) Load Signature From Filesystem
List<EObject> eObjectsSig = BigraphFileModelManagement.Load.signatureInstanceModel(
"sigMetaModel.ecore",
"sigModel.xmi"
);
DefaultDynamicSignature sigLoaded = createOrGetSignature(eObjectsSig.get(0));

// (3) Compare
System.out.println(sigOriginal);
System.out.println(sigLoaded);
}

Storing and Loading a Bigraph​

public void save_and_load_bigraph_model() throws IOException {
// (0) Create Sample Bigraph
PureBigraph bigOriginal = big();

// (1) Store Bigraph MetaModel on Filesystem
BigraphFileModelManagement.Store.exportAsMetaModel(bigOriginal, new FileOutputStream("bigraphMetaModel.ecore"));

// (2) Store the concrete Bigraph Instance Model on Filesystem
BigraphFileModelManagement.Store.exportAsInstanceModel(bigOriginal, new FileOutputStream("model.xmi"));

// (3) Load the Bigraph MetaModel from the file system
EPackage ePackage = BigraphFileModelManagement.Load.bigraphMetaModel("bigraphMetaModel.ecore", false);

// (3) Or, re-create it like this, since the signature is loaded:
// EPackage ePackage = createOrGetBigraphMetaModel(sig());

// (4) Initialize the EMF EPackage registry with the bigraphMetaModel
EPackage.Registry.INSTANCE.put(ePackage.getNsURI(), ePackage);

// (5) Load the instance model (do not provide the bigraphMetaModel)
List<EObject> eObjects = BigraphFileModelManagement.Load.bigraphInstanceModel(
"model.xmi"
);

// (6) Bigraph instance model loaded
PureBigraph bigraphLoaded = BigraphUtil.toBigraph(ePackage, eObjects.get(0), sig());

// (7) Compare the original and the loaded bigraphLoaded
bigOriginal.getNodes().forEach(System.out::println);
bigraphLoaded.getNodes().forEach(System.out::println);
}