From 3cc065bd7f3225ae6f4e0474fae1f24989c87a77 Mon Sep 17 00:00:00 2001 From: Calum Mackervoy Date: Fri, 9 Jul 2021 14:54:50 +0200 Subject: [PATCH 1/5] utility function serializing request to model --- .../java/com/mackervoy/calum/mud/AbstractMUDController.java | 6 ++++++ .../com/mackervoy/calum/mud/content/ContentController.java | 6 ++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/mackervoy/calum/mud/AbstractMUDController.java b/src/main/java/com/mackervoy/calum/mud/AbstractMUDController.java index e8c9e27..87d1b8b 100644 --- a/src/main/java/com/mackervoy/calum/mud/AbstractMUDController.java +++ b/src/main/java/com/mackervoy/calum/mud/AbstractMUDController.java @@ -1,11 +1,17 @@ package com.mackervoy.calum.mud; import java.io.ByteArrayOutputStream; +import java.io.StringReader; import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.ModelFactory; public abstract class AbstractMUDController { + public Model serializeTurtleRequestToModel(String requestBody) { + return ModelFactory.createDefaultModel().read(new StringReader(requestBody), "", "TURTLE"); + } + public String serializeModelToTurtle(Model m) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); m.write(baos, "Turtle"); diff --git a/src/main/java/com/mackervoy/calum/mud/content/ContentController.java b/src/main/java/com/mackervoy/calum/mud/content/ContentController.java index d006481..9e9b887 100644 --- a/src/main/java/com/mackervoy/calum/mud/content/ContentController.java +++ b/src/main/java/com/mackervoy/calum/mud/content/ContentController.java @@ -28,8 +28,7 @@ public class ContentController extends AbstractMUDController { //NOTE: the ContentContoller POST must receives objects with RDF type set, or it will ignore them @POST public Response post(String requestBody) { - final Model request = ModelFactory.createDefaultModel(); - request.read(new StringReader(requestBody), "", "TURTLE"); + final Model request = this.serializeTurtleRequestToModel(requestBody); //build the result model by iterating over each object in the scene and annotating a description //TODO: for now we are just describing everything, later we will want to be able to optimise what is described @@ -37,8 +36,7 @@ public Response post(String requestBody) { ResIterator resources = request.listResourcesWithProperty(RDF.type); while(resources.hasNext()) { Resource res = resources.next(); - Model m = ModelFactory.createDefaultModel(); - m.read(res.getURI()); + Model m = ModelFactory.createDefaultModel().read(res.getURI()); final Resource r = m.getResource(res.getURI()); System.out.println(r); From 9b4a834f55b2af02eedff091bd4b68069c2b45fe Mon Sep 17 00:00:00 2001 From: Calum Mackervoy Date: Fri, 9 Jul 2021 14:55:20 +0200 Subject: [PATCH 2/5] ActionController implements Discovery method --- .../calum/mud/behaviour/ActionController.java | 49 +++++++++++++++++++ .../mud/behaviour/task/TaskActorFactory.java | 8 ++- 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/mackervoy/calum/mud/behaviour/ActionController.java diff --git a/src/main/java/com/mackervoy/calum/mud/behaviour/ActionController.java b/src/main/java/com/mackervoy/calum/mud/behaviour/ActionController.java new file mode 100644 index 0000000..c5e2abd --- /dev/null +++ b/src/main/java/com/mackervoy/calum/mud/behaviour/ActionController.java @@ -0,0 +1,49 @@ +package com.mackervoy.calum.mud.behaviour; + +import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.ModelFactory; +import org.apache.jena.rdf.model.Resource; +import org.apache.jena.rdf.model.ResIterator; +import org.apache.jena.vocabulary.RDF; + +import java.io.StringReader; +import java.util.Set; + +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Response; + +import com.mackervoy.calum.mud.AbstractMUDController; +import com.mackervoy.calum.mud.behaviour.task.TaskActorFactory; + +/** + * @author Calum Mackervoy + * Provides Action Discovery (https://multi-user-domain.github.io/docs/05-action-server.html) + */ + +@Path("/mud/act/discover/") +public class ActionController extends AbstractMUDController { + // pass a URI in the query string for an object + // expect to get back the configured endpoints for actions and tasks on this object + @POST + public Response discoverActions(String requestBody) { + // TODO: analyse the objects in the request (the scene), and return those Actions which have matching shapes for them + // https://github.com/Multi-User-Domain/mud-jena/issues/44 + // final Model request = this.serializeTurtleRequestToModel(requestBody); + + Model result = ModelFactory.createDefaultModel(); + + Set keys = TaskActorFactory.keySet(); + + for(String key : keys) { + Model m = ModelFactory.createDefaultModel().read(key, "TURTLE"); + Resource r = m.getResource(key); + result.add(r, RDF.type, r.getPropertyResourceValue(RDF.type)); + } + + String responseData = result.isEmpty() ? null : serializeModelToTurtle(result); + return Response.ok(responseData).build(); + } + +} diff --git a/src/main/java/com/mackervoy/calum/mud/behaviour/task/TaskActorFactory.java b/src/main/java/com/mackervoy/calum/mud/behaviour/task/TaskActorFactory.java index 86c8afb..872da94 100644 --- a/src/main/java/com/mackervoy/calum/mud/behaviour/task/TaskActorFactory.java +++ b/src/main/java/com/mackervoy/calum/mud/behaviour/task/TaskActorFactory.java @@ -1,9 +1,9 @@ package com.mackervoy.calum.mud.behaviour.task; import java.lang.reflect.Constructor; -import java.util.Dictionary; import java.util.Hashtable; import java.util.Optional; +import java.util.Set; import javax.ws.rs.BadRequestException; @@ -16,7 +16,7 @@ * Resolves at runtime a TaskActor class for a requested RDF class */ public class TaskActorFactory { - private static Dictionary> taskActors = + private static Hashtable> taskActors = new Hashtable>(); /* @@ -62,4 +62,8 @@ public Optional getActorWithExistingTask(Resource task) { return Optional.empty(); } } + + public static Set keySet() { + return taskActors.keySet(); + } } From bc8dc07bb442526de09b01d07fdd8affb88ed744 Mon Sep 17 00:00:00 2001 From: Calum Mackervoy Date: Fri, 9 Jul 2021 14:55:50 +0200 Subject: [PATCH 3/5] added Action Discovery endpoint to configuration --- src/main/webapp/WEB-INF/mudserver.ttl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/webapp/WEB-INF/mudserver.ttl b/src/main/webapp/WEB-INF/mudserver.ttl index 532ba22..cbce191 100644 --- a/src/main/webapp/WEB-INF/mudserver.ttl +++ b/src/main/webapp/WEB-INF/mudserver.ttl @@ -5,4 +5,5 @@ :configuration a mud:Configuration ; mud:worldEndpoint ; mudcontent:SceneDescriptionEndpoint ; - mudcontent:SimpleObjectDescriptionEndpoint . + mudcontent:SimpleObjectDescriptionEndpoint ; + mudlogic:actionDiscoveryEndpoint . From 999d9a447c953f7bc54ad7299a7994ab637a9848 Mon Sep 17 00:00:00 2001 From: Calum Mackervoy Date: Fri, 9 Jul 2021 19:15:23 +0200 Subject: [PATCH 4/5] added mudlogic:actedAt to serialization in ActionController --- .../calum/mud/behaviour/ActionController.java | 11 ++++++++--- .../mackervoy/calum/mud/vocabularies/MUDLogic.java | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/mackervoy/calum/mud/behaviour/ActionController.java b/src/main/java/com/mackervoy/calum/mud/behaviour/ActionController.java index c5e2abd..effb389 100644 --- a/src/main/java/com/mackervoy/calum/mud/behaviour/ActionController.java +++ b/src/main/java/com/mackervoy/calum/mud/behaviour/ActionController.java @@ -3,19 +3,18 @@ import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.ResIterator; import org.apache.jena.vocabulary.RDF; -import java.io.StringReader; import java.util.Set; import javax.ws.rs.POST; import javax.ws.rs.Path; -import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import com.mackervoy.calum.mud.AbstractMUDController; +import com.mackervoy.calum.mud.MUDApplication; import com.mackervoy.calum.mud.behaviour.task.TaskActorFactory; +import com.mackervoy.calum.mud.vocabularies.MUDLogic; /** * @author Calum Mackervoy @@ -24,6 +23,11 @@ @Path("/mud/act/discover/") public class ActionController extends AbstractMUDController { + private String getActAtURL(Resource action) { + String base = MUDApplication.getSiteUrl() + "mud/act/"; + return action.getPropertyResourceValue(RDF.type) == MUDLogic.Task ? base + "task/" : base; + } + // pass a URI in the query string for an object // expect to get back the configured endpoints for actions and tasks on this object @POST @@ -40,6 +44,7 @@ public Response discoverActions(String requestBody) { Model m = ModelFactory.createDefaultModel().read(key, "TURTLE"); Resource r = m.getResource(key); result.add(r, RDF.type, r.getPropertyResourceValue(RDF.type)); + result.add(r, MUDLogic.actAt, this.getActAtURL(r)); } String responseData = result.isEmpty() ? null : serializeModelToTurtle(result); diff --git a/src/main/java/com/mackervoy/calum/mud/vocabularies/MUDLogic.java b/src/main/java/com/mackervoy/calum/mud/vocabularies/MUDLogic.java index 002539f..8d42d57 100644 --- a/src/main/java/com/mackervoy/calum/mud/vocabularies/MUDLogic.java +++ b/src/main/java/com/mackervoy/calum/mud/vocabularies/MUDLogic.java @@ -30,6 +30,7 @@ protected static final Property property( String local ) public final static Resource Action = resource( "Action" ); public final static Resource Task = resource( "Task" ); public final static Resource Transit = resource( "Transit" ); + public final static Property actAt = property( "actAt" ); public final static Property taskImplements = property( "taskImplements" ); public final static Property isComplete = property( "isComplete" ); public final static Property endState = property( "endState" ); From e27e1da9c5bd08a08ce69e86076e9e2c7e8cd4c1 Mon Sep 17 00:00:00 2001 From: Calum Mackervoy Date: Fri, 16 Jul 2021 13:11:42 +0200 Subject: [PATCH 5/5] fix undefined prefix mudserver configuration --- src/main/webapp/WEB-INF/mudserver.ttl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/webapp/WEB-INF/mudserver.ttl b/src/main/webapp/WEB-INF/mudserver.ttl index cbce191..f5e3ac0 100644 --- a/src/main/webapp/WEB-INF/mudserver.ttl +++ b/src/main/webapp/WEB-INF/mudserver.ttl @@ -1,3 +1,4 @@ +@prefix : <#>. @prefix mud: . @prefix mudcontent: . @prefix mudlogic: . @@ -6,4 +7,5 @@ mud:worldEndpoint ; mudcontent:SceneDescriptionEndpoint ; mudcontent:SimpleObjectDescriptionEndpoint ; - mudlogic:actionDiscoveryEndpoint . + mudlogic:actionDiscoveryEndpoint ; + mudlogic:Transit .