Skip to content

Commit 931ffc0

Browse files
committed
docs: add model-client v2 documentation
1 parent 8f3b41f commit 931ffc0

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
= How-To use the `model-client` v2 (Kotlin)
2+
:navtitle: Use the `model-client` v2 (Kotlin)
3+
4+
NOTE: In order to use the client, you will need a xref:core:howto/usage-model-server.adoc[running model-server] to connect to.
5+
6+
== Gradle Dependency
7+
8+
To use the `model-client` v2 you have to add the `model-client` library to your dependencies.
9+
Add the following to your `build.gradle.kts`:
10+
11+
[source,kotlin]
12+
--
13+
repositories {
14+
mavenLocal {}
15+
maven { url = uri("https://artifacts.itemis.cloud/repository/maven-mps/") }
16+
// ...
17+
}
18+
// ...
19+
dependencies {
20+
implementation("org.modelix:model-client:4.6.0")
21+
// ...
22+
}
23+
--
24+
25+
26+
== Usage
27+
28+
Once set up, creating an instance that loads the entire model from the server can be done like this:
29+
30+
31+
[source, kotlin]
32+
--
33+
34+
val client = ModelClientV2.builder()
35+
.url("http://localhost:28101/v2")
36+
.build()
37+
--
38+
39+
Nearly all operations are interfacing with a remote repository, consequently most operations in the client are https://kotlinlang.org/docs/composing-suspending-functions.html[suspending functions^].
40+
As a result, we need to use https://kotlinlang.org/docs/coroutines-basics.html[Kotlin Coroutines^] to execute these functions.
41+
To initialize the client, we can call:
42+
43+
[source, kotlin]
44+
--
45+
// init is a suspend fun, so we use a coroutine
46+
runBlocking(CoroutineScope(Dispatchers.Default).coroutineContext){
47+
client.init()
48+
}
49+
50+
--
51+
52+
Afterward, we create a repository and replicate it locally (we assume coroutine execution):
53+
54+
[source, kotlin]
55+
--
56+
57+
var rootNode: INode? = null
58+
// we need to start a read transaction to obtain model content
59+
replicatedModel.getBranch().getArea().executeRead {
60+
// initial root node after repository initialization has the id 1
61+
rootNode = replicatedModel.getBranch().getRootNode()
62+
}
63+
64+
// we need to exit the read transaction before we start a write transaction
65+
replicatedModel.getBranch().getArea().executeWrite {
66+
val addNewChild: INode? = rootNode?.addNewChild("myRole")
67+
println("Added node $addNewChild under parent ${addNewChild?.parent}")
68+
}
69+
70+
// to get data from the replicated model, we need to start a read transaction
71+
replicatedModel.getBranch().getArea().executeRead {
72+
println("All nodes: ${rootNode?.getDescendants(true)?.map { (it as PNodeAdapter).nodeId }}")
73+
}
74+
75+
--
76+
77+
CAUTION: If you try to access a node that does not exist, an exception is thrown.
78+
79+
For more information how to deal with `INodes` and the `client`, check the https://api.modelix.org/latest/model-api/index.html[model API] and the https://api.modelix.org/latest/model-client/org.modelix.model.client2/-model-client-v2/index.html[ModelClientV2 API] respectively.

0 commit comments

Comments
 (0)