Example project
This page shows a complete standalone project that uses the SPARTA libraries: a small Java program that:
initializes EMF,
creates a DFD model of a web shop,
links a threat type catalog to the model and saves the model,
runs the threat elicitation and risk analysis,
prints the threats and some aggregates, and
exports the threats to a CSV file.
The individual steps are explained in Development scenarios. The project was compiled and run against the SPARTA 2026.0.0 libraries.
Project layout
The project is a regular Maven project with one class, and the threat type catalog next to the pom.xml file:
example-project/
├── pom.xml
├── stride-per-interaction - Shostack.sparta
└── src/main/java/com/example/sparta/ExampleApp.java
The pom.xml file
Note
The 2026.0.0 artifacts are not available from the SPARTA Maven repository. Before you build the example, build SPARTA from source and install the artifacts into your local Maven repository (see Installing the libraries in your local Maven repository).
First create a new Maven project.
The following pom.xml file adds the Eclipse repository that provides VIATRA, and the SPARTA libraries that the program uses.
For later releases that the SPARTA Maven repository hosts, also add that repository (see Standalone Prerequisites).
<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.release>11</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sparta.version>2026.0.0</sparta.version>
</properties>
<dependencies>
<!-- meta-model: create, load and save models -->
<dependency>
<groupId>be.kuleuven.cs.distrinet.sparta</groupId>
<artifactId>sparta-model</artifactId>
<version>${sparta.version}</version>
</dependency>
<!-- threat elicitation and risk analysis -->
<dependency>
<groupId>be.kuleuven.cs.distrinet.sparta</groupId>
<artifactId>sparta-core</artifactId>
<version>${sparta.version}</version>
</dependency>
<!-- aggregate analyses -->
<dependency>
<groupId>be.kuleuven.cs.distrinet.sparta</groupId>
<artifactId>sparta-analysis</artifactId>
<version>${sparta.version}</version>
</dependency>
<!-- exporters -->
<dependency>
<groupId>be.kuleuven.cs.distrinet.sparta</groupId>
<artifactId>sparta-io</artifactId>
<version>${sparta.version}</version>
</dependency>
</dependencies>
<repositories>
<!-- VIATRA is not available from Maven Central -->
<repository>
<id>eclipse</id>
<url>https://repo.eclipse.org/content/groups/releases/</url>
</repository>
</repositories>
</project>
Some remarks:
sparta-ioalready depends on the other three libraries, so it would be enough on its own; the example lists all four because it uses classes from each of them. A program that only creates, loads and saves models only needssparta-model.The libraries are compiled for Java 11, so the project can use
maven.compiler.release11 or later.The project does not need the Tycho plug-in. Maven may print warnings that it cannot access repositories “with type p2”; you can ignore these (see Standalone Prerequisites).
To also export XLSX files, add the two extra dependencies described in Known limitation: XLSX export (known limitation in 2026.0).
The threat type catalog
Without a threat type catalog, the analysis finds no threats.
The example uses the STRIDE-per-interaction catalog from the catalogs repository (see Installing SPARTA): download STRIDE/STRIDE-per-interaction/Shostack/stride-per-interaction - Shostack.sparta and put it in the project directory.
To use another catalog, pass its path as the first program argument.
Note
Do not use the STRIDE-per-element catalog with this program: in 2026.0, the standalone analysis fails with a NullPointerException for catalog patterns that bind no data flow.
The ExampleApp class
Create the class com.example.sparta.ExampleApp in src/main/java:
1package com.example.sparta;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileWriter;
6import java.io.IOException;
7import java.util.Collections;
8import java.util.List;
9import java.util.Map;
10
11import org.eclipse.emf.common.util.URI;
12import org.eclipse.emf.ecore.resource.Resource;
13import org.eclipse.emf.ecore.resource.ResourceSet;
14import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
15import org.eclipse.emf.ecore.xmi.XMLResource;
16
17import be.kuleuven.cs.distrinet.sparta.analysis.ThreatAggregationAnalysis;
18import be.kuleuven.cs.distrinet.sparta.core.Engine;
19import be.kuleuven.cs.distrinet.sparta.core.model.Threat;
20import be.kuleuven.cs.distrinet.sparta.io.ThreatCSVWriter;
21import be.kuleuven.cs.distrinet.sparta.spartamodel.DFDModel;
22import be.kuleuven.cs.distrinet.sparta.spartamodel.DataFlow;
23import be.kuleuven.cs.distrinet.sparta.spartamodel.DataStore;
24import be.kuleuven.cs.distrinet.sparta.spartamodel.ExternalEntity;
25import be.kuleuven.cs.distrinet.sparta.spartamodel.ModelElement;
26import be.kuleuven.cs.distrinet.sparta.spartamodel.Process;
27import be.kuleuven.cs.distrinet.sparta.spartamodel.SpartaModelFactory;
28import be.kuleuven.cs.distrinet.sparta.spartamodel.SpartaModelPackage;
29import be.kuleuven.cs.distrinet.sparta.spartamodel.ThreatTypeCatalog;
30import be.kuleuven.cs.distrinet.sparta.spartamodel.TrustBoundaryContainer;
31import be.kuleuven.cs.distrinet.sparta.spartamodel.util.SpartaModelResourceFactoryImpl;
32
33public class ExampleApp {
34
35 public static void main(String[] args) throws IOException {
36 File catalogFile = new File(args.length > 0 ? args[0] : "stride-per-interaction - Shostack.sparta");
37 File modelFile = new File("webshop.sparta");
38 File csvFile = new File("threats.csv");
39
40 // 1. Initialize EMF (once per JVM)
41 SpartaModelPackage.eINSTANCE.eClass();
42 Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap()
43 .put("sparta", new SpartaModelResourceFactoryImpl());
44
45 // 2. Create the model
46 DFDModel dfd = createModel();
47
48 // 3. Load the threat type catalog, link it to the model and save the model
49 ResourceSet resourceSet = new ResourceSetImpl();
50 Resource catalogResource = resourceSet.getResource(
51 URI.createFileURI(catalogFile.getAbsolutePath()), true);
52 catalogResource.getContents().stream()
53 .filter(ThreatTypeCatalog.class::isInstance)
54 .map(ThreatTypeCatalog.class::cast)
55 .forEach(dfd.getResource()::add);
56
57 Resource modelResource = resourceSet.createResource(URI.createFileURI(modelFile.getAbsolutePath()));
58 modelResource.getContents().add(dfd);
59 modelResource.save(Collections.singletonMap(XMLResource.OPTION_ENCODING, "UTF-8"));
60
61 // 4. Run threat elicitation and risk analysis
62 Engine engine = new Engine(resourceSet);
63 List<Threat> threats = engine.runAnalysis(resourceSet);
64
65 // 5. Print the threats and some aggregates
66 for (Threat t : threats) {
67 System.out.printf("%-24s %-40s %-12s %-8s risk %.2f [%.2f, %.2f]%n",
68 t.getThreatTypeName(), t.getThreatName(),
69 t.getThreatenedElement().getName(), t.getDataFlow().getName(),
70 t.getRisk(), t.getRisk_lower(), t.getRisk_upper());
71 }
72
73 ThreatAggregationAnalysis taa = new ThreatAggregationAnalysis(threats);
74 System.out.printf("%d threats, total risk %.2f, total potential risk %.2f%n",
75 taa.getThreatCount(), taa.getTotalRisk(), taa.getTotalPotentialRisk());
76 for (Map.Entry<Class<? extends ModelElement>, Long> e : taa.getThreatCountPerModelElementType().entrySet()) {
77 System.out.printf(" %s: %d threats%n", e.getKey().getSimpleName(), e.getValue());
78 }
79
80 // 6. Export the threats to CSV
81 try (ThreatCSVWriter writer = new ThreatCSVWriter(new BufferedWriter(new FileWriter(csvFile)))) {
82 writer.writeHeader();
83 writer.write(threats.toArray(new Threat[0]));
84 }
85 System.out.println("Exported the threats to " + csvFile.getAbsolutePath());
86
87 engine.dispose();
88 }
89
90 private static DFDModel createModel() {
91 SpartaModelFactory factory = SpartaModelFactory.eINSTANCE;
92
93 DFDModel dfd = factory.createDFDModel();
94 dfd.setName("Webshop");
95 List<ModelElement> elements = dfd.getContainedElements();
96
97 TrustBoundaryContainer backend = factory.createTrustBoundaryContainer();
98 backend.setName("Backend");
99 elements.add(backend);
100
101 Process webServer = factory.createProcess();
102 webServer.setName("Web server");
103 backend.getContainedElements().add(webServer);
104
105 DataStore database = factory.createDataStore();
106 database.setName("Database");
107 backend.getContainedElements().add(database);
108
109 ExternalEntity user = factory.createExternalEntity();
110 user.setName("User");
111 elements.add(user);
112
113 DataFlow request = factory.createDataFlow();
114 request.setName("request");
115 request.setSender(user);
116 request.setRecipient(webServer);
117 elements.add(request);
118
119 DataFlow query = factory.createDataFlow();
120 query.setName("query");
121 query.setSender(webServer);
122 query.setRecipient(database);
123 elements.add(query);
124
125 return dfd;
126 }
127}
The numbered comments in main mark the steps of the program:
Initialize EMF, as explained in Setting up a standalone program.
Create the model with the method
createModel: a trust boundaryBackendwith a web server process and a database, an external entityUser, and two data flows (see Creating DFD models).Load the catalog into the resource set, link it to the model, and save the model as
webshop.sparta(see Pairing existing threat type catalogs).Run the threat elicitation and risk analysis; the engine is released with
dispose()at the end ofmain(see Performing threat elicitation and risk analysis).Print each threat and some aggregates (see Reading threat results and Analyzing threat results).
Export the threats to
threats.csv(see Exporting threat results).
Running the example
Run ExampleApp from your IDE, with the project directory as working directory.
Alternatively, compile the project and let Maven write the classpath to a file:
mvn compile dependency:build-classpath "-Dmdep.outputFile=cp.txt"
Then run the program on Linux or macOS with:
java -cp "target/classes:$(cat cp.txt)" com.example.sparta.ExampleApp
or on Windows (PowerShell) with:
java -cp "target/classes;$(Get-Content cp.txt)" com.example.sparta.ExampleApp
The output looks as follows (shortened):
WARNING:Recursive pattern call: actualRecipient -> actualRecipient (...)
WARNING:Recursive pattern call: actualSender -> actualSender (...)
Denial of Service request disrupted request request risk 0.76 [0.00, 4.89]
Information Disclosure request discloses information request request risk 0.76 [0.00, 5.01]
Elevation of Privilege Web server execution flow altered Web server request risk 0.74 [0.00, 5.62]
Spoofing User spoofed User request risk 0.78 [0.00, 5.12]
...
Spoofing Web server spoofed Web server request risk 0.69 [0.00, 4.78]
Repudiation Web server repudiates action Web server query risk 0.71 [0.00, 5.09]
15 threats, total risk 11.05, total potential risk 11.05
DataStoreImpl: 3 threats
ProcessImpl: 6 threats
DataFlowImpl: 3 threats
ExternalEntityImpl: 3 threats
Exported the threats to /path/to/example-project/threats.csv
The WARNING lines come from VIATRA, which parses the helper patterns of the catalog; they do not stop the analysis.
The risk values differ between runs, because the risk is calculated with a Monte Carlo simulation, and the order of the threats can differ as well.
Numbers are formatted in the default locale of the JVM.
The model contains no countermeasures, so the risk of each threat equals its potential risk.
To see the effect of a security solution, apply one before running the analysis, as shown in Creating and applying solutions.
The program also writes the model to webshop.sparta, which refers to the catalog file with a relative path, and the threats to threats.csv, with ; as separator.