How-To use the light-model-client
If you are interested in a more practical usage of what is presented here, check out the samples project |
Creating an instance that loads the entire model from the server can be done like this:
val client = LightModelClient.builder()
.url("ws://localhost/json/v2/test-repo/ws") // optional, by default it connects to the MPS plugin
.build()
You have to set a model query using changeQuery()
to tell the server in what data you are interested in.
Without a query the client will not receive any data. |
client.changeQuery(buildModelQuery {
root {
descendants { }
}
})
To read or write any nodes you have to start a read/write transaction by using runRead {}
/runWrite {}
.
An exception is thrown when you try to access a node outside a transaction.
val rootNode = client.waitForRootNode()!!
client.runRead {
val modules = rootNode.getChildren("modules")
// ...
}
If you try to access a node that is not included in your model query an exception is thrown. |
You can use INode.isLoaded()
to check if a node was loaded on the client to prevent this exception.
You can also filter a list of nodes like this: node.getChildren("modules").filterLoaded()
, to iterate only over the nodes that are included in your query.