CodeFerret

ShaclName_validateGraph.java

May 28th, 2020
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. import org.apache.jena.graph.Graph;
  2. import org.apache.jena.rdf.model.Literal;
  3. import org.apache.jena.rdf.model.Model;
  4. import org.apache.jena.rdf.model.ModelFactory;
  5. import org.apache.jena.rdf.model.Property;
  6. import org.apache.jena.rdf.model.RDFNode;
  7. import org.apache.jena.rdf.model.Resource;
  8. import org.apache.jena.rdf.model.ResourceFactory;
  9. import org.apache.jena.rdf.model.Statement;
  10. import org.apache.jena.rdf.model.StmtIterator;
  11. import org.apache.jena.riot.Lang;
  12. import org.apache.jena.riot.RDFDataMgr;
  13. import org.apache.jena.shacl.ShaclValidator;
  14. import org.apache.jena.shacl.Shapes;
  15. import org.apache.jena.shacl.ValidationReport;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18.  
  19. public class ShaclName_validateGraph {
  20.    
  21.     public static Logger logger = LoggerFactory.getLogger(ShaclName_validateGraph.class);
  22.    
  23.     static final String BDR = "http://purl.bdrc.io/resource/";
  24.     static final String SH = "http://www.w3.org/ns/shacl#";
  25.     static final String SHAPES = "ShapeName_01.ttl";
  26.     static final String ONT_GRAPH = "http://purl.bdrc.io/graph/ontologySchema.ttl";
  27.     static final String REZ_NM = "P707";
  28.     static final String DATA = REZ_NM+"_nameErrs02.ttl";
  29.    
  30.     static final Property SH_CONFORMS = ResourceFactory.createProperty(SH+"conforms");
  31.     static final Property SH_RESULT = ResourceFactory.createProperty(SH+"result");
  32.     static final Property SH_VALUE = ResourceFactory.createProperty(SH+"value");
  33.     static final Literal FALSE = ModelFactory.createDefaultModel().createTypedLiteral(false);
  34.     static final ShaclValidator sv = ShaclValidator.get();
  35.    
  36.     static Model process(Shapes shapes, Graph dataGraph, Resource rez) {
  37.         logger.info("Validating Node {} with {}", rez.getLocalName(), SHAPES);
  38.         ValidationReport report = sv.validate(shapes, dataGraph, rez.asNode());
  39.         return report.getModel();
  40.     }
  41.  
  42.     // for violations in top-level report validate the sh:value node, if any
  43.     static Model completeReport(Shapes shapes, Graph dataGraph, Model top) {
  44.         Model complete = ModelFactory.createDefaultModel();
  45.         complete.add(top);
  46.  
  47.         if (top.contains((Resource) null, SH_CONFORMS, FALSE)) {
  48.             StmtIterator valItr = top.listStatements((Resource) null, SH_VALUE, (RDFNode) null);
  49.             while (valItr.hasNext()) {
  50.                 Statement valStmt = valItr.removeNext();
  51.                 RDFNode valNode = valStmt.getObject();
  52.  
  53.                 if (valNode.isResource()) {
  54.                     Model subReport = process(shapes, dataGraph, (Resource) valNode);
  55.                     complete.add(subReport);
  56.                 }
  57.             }
  58.         }
  59.  
  60.         return complete;
  61.     }
  62.    
  63.     public static void main(String ...args) {
  64.         Graph shapesGraph = RDFDataMgr.loadGraph(SHAPES);
  65.         Shapes shapes = Shapes.parse(shapesGraph);
  66.         // merge ontGraph and targetGraph        
  67.         Graph ontGraph = RDFDataMgr.loadGraph(ONT_GRAPH);
  68.         Graph targetGraph = RDFDataMgr.loadGraph(DATA);
  69.         Model datasetModel = ModelFactory.createModelForGraph(ontGraph);
  70.         datasetModel.add(ModelFactory.createModelForGraph(targetGraph));
  71.         Graph dataGraph = datasetModel.getGraph();
  72.  
  73.         Resource rez = ResourceFactory.createResource(BDR+REZ_NM);
  74.         Model topReport = process(shapes, dataGraph, rez);
  75.  
  76.         Model finalReport = completeReport(shapes, dataGraph, topReport);
  77.         RDFDataMgr.write(System.out, finalReport, Lang.TTL);
  78.     }
  79. }
Add Comment
Please, Sign In to add comment