CodeFerret

ShaclName_validateGraphTQ.java

May 29th, 2020
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. import org.apache.jena.graph.Graph;
  2. import org.apache.jena.rdf.model.Model;
  3. import org.apache.jena.rdf.model.ModelFactory;
  4. import org.apache.jena.rdf.model.Resource;
  5. import org.apache.jena.rdf.model.ResourceFactory;
  6. import org.apache.jena.riot.Lang;
  7. import org.apache.jena.riot.RDFDataMgr;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.topbraid.shacl.validation.ValidationEngine;
  11. import org.topbraid.shacl.validation.ValidationEngineConfiguration;
  12. import org.topbraid.shacl.validation.ValidationUtil;
  13.  
  14. public class ShaclName_validateGraphTQ {
  15.  
  16.     public static Logger logger = LoggerFactory.getLogger(ShaclName_validateGraphTQ.class);
  17.  
  18.     static final String BDR = "http://purl.bdrc.io/resource/";
  19.     static final String SHAPES = "ShapeName_01.ttl";
  20.     static final String DATA = "DATA_P707.ttl";
  21.  
  22.     static ValidationEngine engine;
  23.     static Graph dataGraph;
  24.     static Model dataModel;
  25.     static Graph shapesGraph;
  26.     static Model shapesModel;
  27.    
  28.     static void initEngine() {
  29.         try {
  30.             ValidationEngineConfiguration config = new ValidationEngineConfiguration().setValidateShapes(true);
  31.             engine = ValidationUtil.createValidationEngine(dataModel, shapesModel, config);
  32.             engine.applyEntailments();
  33.         } catch(InterruptedException ex) {
  34.             ex.printStackTrace();
  35.         }    
  36.     }
  37.  
  38.     static {
  39.         dataGraph = RDFDataMgr.loadGraph(DATA);
  40.         dataModel = ModelFactory.createModelForGraph(dataGraph);
  41.         shapesGraph = RDFDataMgr.loadGraph(SHAPES);
  42.         shapesModel = ModelFactory.createModelForGraph(shapesGraph);
  43.         initEngine();
  44.     }
  45.  
  46.     public static void process(Resource focus) throws InterruptedException {
  47.         logger.info("Validating Node {} with {}", focus.getLocalName(), SHAPES);
  48.         Resource report = engine.validateNode(focus.asNode());
  49.         RDFDataMgr.write(System.out, report.getModel(), Lang.TTL);
  50.     }
  51.  
  52.     public static void main(String ...args) {
  53.         try {
  54.             process(ResourceFactory.createResource(BDR+"P707"));
  55.             initEngine();
  56.             process(ResourceFactory.createResource(BDR+"NMC2A097019ABA499F"));
  57.             initEngine();
  58.             process(ResourceFactory.createResource(BDR+"NM0895CB6787E8AC6E"));
  59.             initEngine();
  60.             process(ResourceFactory.createResource(BDR+"NM2463D933BA1F9A38"));
  61.             initEngine();
  62.             process(ResourceFactory.createResource(BDR+"NMEA2B380AF0BBFB1B"));        
  63.         } catch(InterruptedException ex) {
  64.             ex.printStackTrace();
  65.         }    
  66.     }
  67. }
Add Comment
Please, Sign In to add comment