Examples
This section discusses a concrete example of a standalone project using the SPARTA libraries.
Standalone example project
First create a new maven project.
The following code snippet shows an example pom.xml file.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.sparta</groupId>
<artifactId>example-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SPARTA Example project</name>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
Next, in the SPARTA maven repository, the tycho plugin, and the sparta-model dependency.
<project>
...
<dependencies>
<dependency>
<groupId>be.kuleuven.cs.distrinet.sparta</groupId>
<artifactId>sparta-model</artifactId>
<version>2022.0.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>sparta-repository</id>
<url>https://maven.sparta.distrinet-research.be</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>2.7.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Next Create an ExampleApp class and add the initialization code.
package com.example.sparta;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import be.kuleuven.cs.distrinet.sparta.spartamodel.SpartaModelPackage;
public class ExampleApp {
public static void main(String[] args) {
// Initialization
SpartaModelPackage.eINSTANCE.eClass();
// Register resource factory
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("sparta", new XMIResourceFactoryImpl());
}
}
With the initialization done, we can now create and modify sparta models, and can process .sparta files correctly.
Next, we can add the actual code to create a simple DFD Model and save it.
SpartaModelFactory fact = new SpartaModelFactoryImpl();
DFDModel dfdmodel = fact.createDFDModel();
dfdmodel.setName("DFD Model Name");
List<ModelElement> dfdElements = dfdmodel.getContainedElements();
Process proc = fact.createProcess();
proc.setName("Process");
dfdElements.add(proc);
ExternalEntity ee = fact.createExternalEntity();
ee.setName("user" );
dfdElements.add(ee);
DataFlow df = fact.createDataFlow();
df.setSender(ee);
df.setRecipient(proc);
dfdElements.add(df);
// Obtain a new resource set
ResourceSet resSet = new ResourceSetImpl();
// create a resource
URI fileURI = URI.createFileURI(System.getProperty("user.dir") + "/" + "filename.sparta");
Resource resource = resSet.createResource(fileURI);
// add the model
resource.getContents().add(dfdmodel);
// save the resource to disk (map with save options or null)
try {
resource.save(null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}