Skip to content

Instantly share code, notes, and snippets.

@fils
Created September 4, 2018 21:16
Show Gist options
  • Save fils/c5b20773f0a4e73a2e807412f492df75 to your computer and use it in GitHub Desktop.
Save fils/c5b20773f0a4e73a2e807412f492df75 to your computer and use it in GitHub Desktop.
package org.oceanleadership;
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.Property;
import org.apache.jena.rdf.model.RDFNode;
// import org.apache.jena.riot.RDFFormat;
import org.topbraid.jenax.util.JenaUtil;
import org.topbraid.shacl.util.ModelPrinter;
import org.topbraid.shacl.validation.ValidationUtil;
import java.io.FileInputStream;
import java.io.File;
import io.javalin.Javalin;
import org.apache.commons.io.FileUtils;
import java.io.IOException;
// import java.util.HashMap;
// import java.util.Map;
public class App {
public static void main(String[] args) {
Javalin app = Javalin.create().start(7000);
app.get("/", ctx -> ctx.html("<div>SHACL convertion requires POST (ref swagger docs)</div>"));
app.post("/", ctx -> {
File f = File.createTempFile("prefix-", "-suffix");
f.createNewFile();
f.deleteOnExit();
FileInputStream fis = FileUtils.openInputStream(f);
File f2 = File.createTempFile("prefix-", "-suffix");
f2.createNewFile();
f2.deleteOnExit();
FileInputStream fis2 = FileUtils.openInputStream(f2);
Model dataModel = JenaUtil.createMemoryModel();
Model shapesModel = JenaUtil.createMemoryModel();
ctx.uploadedFiles("files").forEach(file -> {
try {
FileUtils.copyInputStreamToFile(file.getContent(), f);
dataModel.read(fis, "urn:dummy", org.apache.jena.util.FileUtils.langTurtle);
} catch (IOException e) {
e.printStackTrace();
}
});
ctx.uploadedFiles("shape").forEach(file -> {
try {
FileUtils.copyInputStreamToFile(file.getContent(), f2);
shapesModel.read(fis2, "urn:dummy", org.apache.jena.util.FileUtils.langTurtle);
} catch (IOException e) {
e.printStackTrace();
}
});
// option
// The blank node for each sh:ValidationRecord comes from the constructor of ValidationEngine,
// which sets this.report to be reportModel.createResource(...) unless it is passed in.
// So you could call this constructor via the ValidationEngineFactory with an already given
// URI resource if you want to assign it a URI.
Resource report = ValidationUtil.validateModel(dataModel, shapesModel, false);
// String BASE = "http://datafacility.org/";
Model model = org.apache.jena.rdf.model.ModelFactory.createDefaultModel();
// model.setNsPrefix("", BASE);
// Resource r1 = model.createResource(BASE + "r1");
Property p1 = model.createProperty("http://www.w3.org/2004/02/skos/core#related");
// Property p2 = model.createProperty(BASE + "p2");
RDFNode v1 = model.createProperty("http://opencoredata.org/id/dataset/XYZ");
// RDFNode v1 = model.createTypedLiteral("http://opencoredata.org/id/dataset/XYZ", org.apache.jena.datatypes.xsd.XSDDatatype.XSDstring);
// RDFNode v2 = model.createTypedLiteral("2", org.apache.jena.datatypes.xsd.XSDDatatype.XSDinteger);
// r1.addProperty(p1, v1).addProperty(p1, v2);
report.addProperty(p1, v1);
// option, add metadata to the report object that holds my associated URI.
// System.out.println(ModelPrinter.get().print(report.getModel()));
ctx.result(ModelPrinter.get().print(report.getModel()));
});
}
}
@fils
Copy link
Author

fils commented Sep 4, 2018

Example output when running a test data and shape graph through them... I am NOT a java programmer anymore so most of this is copy and paste work from the intertubes....

@base          <http://example.org/random> .
[ a       <http://www.w3.org/ns/shacl#ValidationReport> ;
  <http://www.w3.org/2004/02/skos/core#related>
          <http://opencoredata.org/id/dataset/XYZ> ;
  <http://www.w3.org/ns/shacl#conforms>
          false ;
  <http://www.w3.org/ns/shacl#result>
          [ a       <http://www.w3.org/ns/shacl#ValidationResult> ;
            <http://www.w3.org/ns/shacl#focusNode>
                    <http://opencoredata.org/id/dataset/bcd15975-680c-47db-a062-ac0bb6e66816> ;
            <http://www.w3.org/ns/shacl#resultMessage>
                    "Less than 1^^http://www.w3.org/2001/XMLSchema#integer values" ;
            <http://www.w3.org/ns/shacl#resultPath>
                    <http://schema.org/citation> ;
            <http://www.w3.org/ns/shacl#resultSeverity>
                    <http://www.w3.org/ns/shacl#Violation> ;
            <http://www.w3.org/ns/shacl#sourceConstraintComponent>
                    <http://www.w3.org/ns/shacl#MinCountConstraintComponent> ;
            <http://www.w3.org/ns/shacl#sourceShape>
                    []
          ]
] .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment