Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #45 from Multi-User-Domain/33-action-discovery
Browse files Browse the repository at this point in the history
Action discovery
  • Loading branch information
calummackervoy authored Jul 27, 2021
2 parents f2d4b9b + e27e1da commit 7eedf47
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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.vocabulary.RDF;

import java.util.Set;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
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
* Provides Action Discovery (https://multi-user-domain.github.io/docs/05-action-server.html)
*/

@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
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<String> 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));
result.add(r, MUDLogic.actAt, this.getActAtURL(r));
}

String responseData = result.isEmpty() ? null : serializeModelToTurtle(result);
return Response.ok(responseData).build();
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -16,7 +16,7 @@
* Resolves at runtime a TaskActor class for a requested RDF class
*/
public class TaskActorFactory {
private static Dictionary<String, Class<? extends ITaskActor>> taskActors =
private static Hashtable<String, Class<? extends ITaskActor>> taskActors =
new Hashtable<String, Class<? extends ITaskActor>>();

/*
Expand Down Expand Up @@ -62,4 +62,8 @@ public Optional<ITaskActor> getActorWithExistingTask(Resource task) {
return Optional.empty();
}
}

public static Set<String> keySet() {
return taskActors.keySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@ 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
Model result = ModelFactory.createDefaultModel();
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand Down
5 changes: 4 additions & 1 deletion src/main/webapp/WEB-INF/mudserver.ttl
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
@prefix : <#>.
@prefix mud: <https://raw.githubusercontent.com/Multi-User-Domain/vocab/main/mud.ttl#>.
@prefix mudcontent: <https://raw.githubusercontent.com/Multi-User-Domain/vocab/main/mudcontent.ttl#>.
@prefix mudlogic: <https://raw.githubusercontent.com/Multi-User-Domain/vocab/main/mudlogic.ttl#>.

:configuration a mud:Configuration ;
mud:worldEndpoint <http://localhost:8080/mud/world/> ;
mudcontent:SceneDescriptionEndpoint <http://localhost:8080/mud/content/> ;
mudcontent:SimpleObjectDescriptionEndpoint <http://localhost:8080/mud/content/> .
mudcontent:SimpleObjectDescriptionEndpoint <http://localhost:8080/mud/content/> ;
mudlogic:actionDiscoveryEndpoint <http://localhost:8000/mud/act/discover/> ;
mudlogic:Transit <http://localhost:8000/mud/act/task/> .

0 comments on commit 7eedf47

Please sign in to comment.