package eu.pericles.spinengine;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.util.FileUtils;
import org.topbraid.spin.inference.SPINInferences;
import org.topbraid.spin.system.SPINModuleRegistry;
import org.topbraid.spin.util.JenaUtil;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
import java.io.StringWriter;

@Provider
@Path("/")
public class RESTService {


    @GET
    @Path("/test")
    @Produces("text/plain")
    public String test() {
        return "test";
    }



    @POST
    @Path("/runInferences")
    @Consumes("application/x-www-form-urlencoded")
    @Produces ("text/plain")
    public String runInferences (@FormParam("baseURI")  final String baseURI,@FormParam("document")  final String document) {

        // Initialize system functions and templates
        SPINModuleRegistry.get().init();

        Model baseModel = ModelFactory.createDefaultModel();
        baseModel.read(document);

        // Create OntModel with imports
        OntModel ontModel = JenaUtil.createOntologyModel(OntModelSpec.OWL_MEM,baseModel);

        // Register locally defined functions
        SPINModuleRegistry.get().registerAll(ontModel, null);

        // Create and add Model for inferred triples
        Model newTriples = ModelFactory.createDefaultModel();
        newTriples.setNsPrefixes(ontModel);
        ontModel.addSubModel(newTriples);

        // Perform inferencing
        SPINInferences.run(ontModel, newTriples, null, null, false, null);

        // Create results model
        StringWriter w = new StringWriter();
        // Output results in Turtle
        newTriples.write(w, FileUtils.langTurtle);

        return w.toString();
    }
    @GET
    @Path("/runInferencesGet")
    @Produces ("text/plain")
    public String runInferencesGet (@FormParam("baseURI")  final String baseURI,@FormParam("document")  final String document) {

        // Initialize system functions and templates
        SPINModuleRegistry.get().init();

        Model baseModel = ModelFactory.createDefaultModel();
        baseModel.read(document);

        // Create OntModel with imports
        OntModel ontModel = JenaUtil.createOntologyModel(OntModelSpec.OWL_MEM,baseModel);

        // Register locally defined functions
        SPINModuleRegistry.get().registerAll(ontModel, null);

        // Create and add Model for inferred triples
        Model newTriples = ModelFactory.createDefaultModel();
        newTriples.setNsPrefixes(ontModel);
        ontModel.addSubModel(newTriples);

        // Perform inferencing
        SPINInferences.run(ontModel, newTriples, null, null, false, null);

        // Create results model
        StringWriter w = new StringWriter();
        // Output results in Turtle
        newTriples.write(w, FileUtils.langTurtle);

        return w.toString();
    }
}
@@ -864,19 +864,16 @@
 ;%0A    %7D%0A
-%0A%0A%0A
     @POS
@@ -1112,940 +1112,47 @@
 ) %7B%0A
-%0A
     
-    // Initialize system functions and templates%0A        SPINModuleRegistry.get().init();%0A%0A        Model baseModel = ModelFactory.createDefaultModel();%0A        baseModel.read(document);%0A%0A        // Create OntModel with imports%0A        OntModel ontModel = JenaUtil.createOntologyModel(OntModelSpec.OWL_MEM,baseModel);%0A%0A        // Register locally defined functions%0A        SPINModuleRegistry.get().registerAll(ontModel, null);%0A%0A        // Create and add Model for inferred triples%0A        Model newTriples = ModelFactory.createDefaultModel();%0A        newTriples.setNsPrefixes(ontModel);%0A        ontModel.addSubModel(newTriples);%0A%0A        // Perform inferencing%0A        SPINInferences.run(ontModel, newTriples, null, null, false, null);%0A%0A        // Create results model%0A        StringWriter w = new StringWriter();%0A        // Output results in Turtle%0A        newTriples.write(w, FileUtils.langTurtle);%0A%0A        return w.toString(
+return internalRun(baseURI,document
 );%0A 
@@ -1262,20 +1262,21 @@
 esGet (@
-Form
+Query
 Param(%22b
@@ -1299,36 +1299,37 @@
 String baseURI,@
-Form
+Query
 Param(%22document%22
@@ -1357,16 +1357,135 @@
 ent) %7B%0A%0A
+        return internalRun(baseURI,document);%0A    %7D%0A%0A    private String internalRun(String baseURI, String document) %7B%0A
         
@@ -1624,32 +1624,113 @@
 DefaultModel();%0A
+        if (baseURI!=null)%0A            baseModel.read(baseURI);%0A        else%0A    
         baseMode
@@ -1747,17 +1747,16 @@
 ument);%0A
-%0A
         

Update to the rest service