Skip to content

Commit cb5f908

Browse files
MetaDB: Map JSON configs to RDF (#618)
Start work on the mapping from structured RDF into the existing JSON config entries. Create a framework in which certain Apps are classified as _Structured application_, which means their JSON config entries are backed by RDF structure. Support pushing updates to the JSON back into the RDF, and regenerating the JSON when the RDF has changed. For now only _Object registration_, _General info_ and _Sparkplug address_ are supported as _Structured apps_, and their mappings are hard-coded. Long-term these should be generated from some form of schema entry. The regeneration code is rather simple-minded and regenerates all possible config entries on any change. This could be optimised in the future but the Jena ModelChangedListener facility is not as helpful as it could be so it will not be entirely straightforward, especially where config entries make use of the class structure. We must generate entries eagerly because we need to (eventually) support push notify.
2 parents a523086 + 9cff377 commit cb5f908

15 files changed

Lines changed: 1042 additions & 326 deletions

File tree

acs-metadb/src/main/java/uk/co/amrc/factoryplus/metadb/api/ErrorMapper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@
1414
import org.glassfish.hk2.api.MultiException;
1515
import org.glassfish.jersey.server.spi.ResponseErrorMapper;
1616

17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
19+
1720
import uk.co.amrc.factoryplus.metadb.db.Err;
1821

1922
public class ErrorMapper
2023
{
24+
public static final Logger log = LoggerFactory.getLogger(ErrorMapper.class);
25+
2126
public static Response clientError (Err.ClientError err)
2227
{
2328
/* XXX we should allow the error to include more fields here */
29+
log.info("Returning error: {}", err);
2430
var json = Json.createValue(err.getMessage());
2531
return Response.status(err.statusCode())
2632
.entity(json)

acs-metadb/src/main/java/uk/co/amrc/factoryplus/metadb/api/Sparql.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private static record QueryHandler (
126126
@Consumes("application/sparql-update")
127127
public void update (String update)
128128
{
129-
store.executeWrite(() ->
129+
store.requestExecute(req ->
130130
UpdateAction.parseExecute(update, store.dataset()));
131131
}
132132

@@ -143,7 +143,7 @@ public StreamingOutput sparql (String queryString) throws WebApplicationExceptio
143143

144144
var lang = handler.content().acceptLang(req);
145145

146-
return store.calculateRead(() -> {
146+
return store.requestRead(req -> {
147147
try (var qexec = QueryExecutionFactory.create(query, store.dataset())) {
148148
return handler.handle().apply(qexec, lang);
149149
}
@@ -226,7 +226,7 @@ public void graphPost (
226226
var lang = RDF_HANDLER.contentLang(type);
227227
var graph = resolveGraph(true);
228228

229-
store.executeWrite(() -> {
229+
store.requestExecute(req -> {
230230
readToGraph(graph, rdf, lang);
231231
/* This refreshes the inferences because we have been poking
232232
* around behind its back. Strictly this is only needed when
@@ -244,7 +244,7 @@ public void graphPut (
244244
var lang = RDF_HANDLER.contentLang(type);
245245
var graph = resolveGraph(true);
246246

247-
store.executeWrite(() -> {
247+
store.requestExecute(req -> {
248248
graph.removeAll();
249249
readToGraph(graph, rdf, lang);
250250
store.derived().rebind();

acs-metadb/src/main/java/uk/co/amrc/factoryplus/metadb/api/V2Config.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,21 @@ public class V2Config {
3535
@PathParam("app") UUID app;
3636
@PathParam("object") UUID obj;
3737

38-
private ConfigEntry configEntry ()
39-
{
40-
return store.configEntry(app, obj);
41-
}
42-
4338
@GET
4439
public Response get ()
4540
{
4641
log.info("Get config for {}/{}", app, obj);
47-
var entry = store.calculateRead(() -> {
48-
return configEntry().getValue();
42+
var entry = store.requestRead(req -> {
43+
return req.configEntry(app, obj).getValue();
4944
});
5045
return entry
51-
.map(e -> Response.ok(e.value())
52-
.tag(e.etag())
53-
.lastModified(Date.from(e.mtime()))
54-
.build())
46+
.map(e -> {
47+
var res = Response.ok(e.value())
48+
.tag(e.etag());
49+
e.mtime().ifPresent(t ->
50+
res.lastModified(Date.from(t)));
51+
return res.build();
52+
})
5553
.orElseThrow(() -> new WebApplicationException(404));
5654
}
5755

@@ -63,26 +61,26 @@ public Response get ()
6361
public void put (JsonValue config)
6462
{
6563
log.info("Put config for {}/{}", app, obj);
66-
store.executeWrite(() -> {
67-
configEntry().putValue(config);
64+
store.requestExecute(req -> {
65+
req.configEntry(app, obj).putValue(config);
6866
});
6967
}
7068

7169
@DELETE
7270
public void delete ()
7371
{
7472
log.info("Delete config for {}/{}", app, obj);
75-
store.executeWrite(() -> {
76-
configEntry().removeValue();
73+
store.requestExecute(req -> {
74+
req.configEntry(app, obj).removeValue();
7775
});
7876
}
7977

8078
@PATCH @Consumes("application/merge-patch+json")
8179
public void mergePatch (JsonValue json)
8280
{
8381
var patch = Json.createMergePatch(json);
84-
store.executeWrite(() -> {
85-
var entry = configEntry();
82+
store.requestExecute(req -> {
83+
var entry = req.configEntry(app, obj);
8684

8785
var o_conf = entry.getValue()
8886
.map(e -> e.value())

acs-metadb/src/main/java/uk/co/amrc/factoryplus/metadb/api/V2Objects.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public class V2Objects {
3030
@GET @Path("object")
3131
public JsonArray listObjects ()
3232
{
33-
var objs = db.objectStructure().listObjects();
33+
var objs = db.requestRead(req ->
34+
req.objectStructure().listObjects());
3435
return Json.createArrayBuilder(objs).build();
3536
}
3637

@@ -56,26 +57,28 @@ public JsonValue createObject (JsonObject spec)
5657
/* We don't accept any other parameters. The ServiceClient
5758
* doesn't pass any anyway. */
5859

59-
return db.objectStructure().createObject(klass, uuid);
60+
return db.requestWrite(req ->
61+
req.objectStructure().createObject(klass, uuid));
6062
}
6163

6264
@DELETE @Path("object/{object}")
6365
public void deleteObject (@PathParam("object") UUID uuid)
6466
{
65-
db.objectStructure().deleteObject(uuid);
67+
db.requestExecute(req -> req.objectStructure().deleteObject(uuid));
6668
}
6769

6870
@GET @Path("object/rank")
6971
public JsonArray listRanks ()
7072
{
71-
var ranks = db.objectStructure().listRanks();
73+
var ranks = db.requestRead(req -> req.objectStructure().listRanks());
7274
return Json.createArrayBuilder(ranks).build();
7375
}
7476

7577
private JsonArray listRelation (String graph, UUID uuid, String relation)
7678
{
77-
var members = db.objectStructure()
78-
.listRelation(graph, uuid, relation);
79+
var members = db.requestRead(req ->
80+
req.objectStructure()
81+
.listRelation(graph, uuid, relation));
7982

8083
return Json.createArrayBuilder(members).build();
8184
}
@@ -101,8 +104,9 @@ public JsonArray listDirectRelation (
101104

102105
private Response testRelation (String graph, UUID klass, String relation, UUID object)
103106
{
104-
var rv = db.objectStructure()
105-
.testRelation(graph, klass, relation, object);
107+
var rv = db.requestRead(req ->
108+
req.objectStructure()
109+
.testRelation(graph, klass, relation, object));
106110

107111
return Response.status(rv ? 204 : 404).build();
108112
}
@@ -131,7 +135,8 @@ public void putRelation (
131135
@PathParam("relation") String relation,
132136
@PathParam("object") UUID object)
133137
{
134-
db.objectStructure().putRelation(klass, relation, object);
138+
db.requestExecute(req ->
139+
req.objectStructure().putRelation(klass, relation, object));
135140
}
136141

137142
@DELETE @Path("class/{class}/direct/{relation}/{object}")
@@ -140,8 +145,8 @@ public void delRelation (
140145
@PathParam("relation") String relation,
141146
@PathParam("object") UUID object)
142147
{
143-
db.objectStructure().delRelation(klass, relation, object);
148+
db.requestExecute(req ->
149+
req.objectStructure().delRelation(klass, relation, object));
144150
}
145-
146151
}
147152

0 commit comments

Comments
 (0)