Advertisement
CodeFerret

Jena Ontology Loading Tests

Apr 22nd, 2020
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. import java.io.FileWriter;
  2.  
  3. import org.apache.jena.ontology.OntDocumentManager;
  4. import org.apache.jena.ontology.OntModel;
  5. import org.apache.jena.ontology.OntModelSpec;
  6. import org.apache.jena.rdf.model.Model;
  7. import org.apache.jena.rdf.model.ModelFactory;
  8. import org.apache.jena.rdfconnection.RDFConnection;
  9. import org.apache.jena.rdfconnection.RDFConnectionFuseki;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12.  
  13. public class OntTestLoading {
  14.  
  15.     public static Logger logger = LoggerFactory.getLogger(OntTestLoading.class);
  16.  
  17.     private static final String ONT_POLICY = "https://raw.githubusercontent.com/buda-base/editor-templates/master/ont-policy.rdf";
  18.     private static final String OUT = <local directory path for output ttl files>;
  19.     private static final String DS = <dataset URL for putting and fetching models/graphs>;
  20.    
  21.     private static RDFConnection fuConn;
  22.    
  23.     private static final String BDG = "http://purl.bdrc.io/graph/";
  24.     private static final String PERSON_SHAPES = "http://purl.bdrc.io/shapes/core/PersonShapes/";
  25.     private static final String PERSON_UI_SHAPES = "http://purl.bdrc.io/shapes/core/PersonUIShapes/";
  26.    
  27.     private static OntModelSpec oms;
  28.     private static OntDocumentManager odm;
  29.    
  30.     private static void initOdm() {
  31.         oms = new OntModelSpec(OntModelSpec.OWL_MEM);        
  32.         odm = new OntDocumentManager(ONT_POLICY);        
  33.         oms.setDocumentManager(odm);
  34.     }
  35.    
  36.     private static void writeTtl(Model m, String nm) {
  37.         try {
  38.             m.write(new FileWriter(OUT+nm+".ttl"), "TTL");
  39.         } catch (Exception ex) {
  40.             ex.printStackTrace();
  41.         }        
  42.     }
  43.    
  44.     private static void doTest(String ontUri, boolean processImports, String graphLocalName) {
  45.         doTest(ontUri, processImports, graphLocalName, false);
  46.     }
  47.    
  48.     private static void doTest(String ontUri, boolean processImports, String graphLocalName, boolean loadImp) {
  49.         doTest(oms, odm, ontUri, processImports, graphLocalName, loadImp);
  50.     }
  51.    
  52.     private static void doTest(OntModelSpec oms, OntDocumentManager odm, String ontUri, boolean processImports, String graphLocalName, boolean loadImp) {
  53.         logger.info("processing {} imports {}", graphLocalName, processImports);
  54.        
  55.         odm.setProcessImports(processImports);
  56.        
  57.         // fetch OntModel
  58.         OntModel om = odm.getOntology(ontUri, oms);      
  59.         writeTtl(om, graphLocalName+"_ONT_MOD_GET_ONT");
  60.        
  61.         // send OntModel to Fuseki and retrieve Model by graph name
  62.         String graphName = BDG+graphLocalName;
  63.         fuConn.put(graphName, om);
  64.         Model mFmFu = fuConn.fetch(graphName);
  65.         writeTtl(mFmFu, graphLocalName+"_MODEL_FM_FUSEKI");
  66.  
  67.         // explicitly load
  68.         if (loadImp) {
  69.             om.loadImports();
  70.             writeTtl(om, graphLocalName+"_ONT_MOD_LOAD_IMP");
  71.         }
  72.     }
  73.    
  74.     public static void main(String[] args){
  75.        
  76.         fuConn = RDFConnectionFuseki.create().destination(DS).build();        
  77.  
  78.         // PROCESS IMPORTS FALSE
  79.         initOdm();
  80.         doTest(PERSON_SHAPES, false, "PersonShapes_NI10");        
  81.  
  82.         // PROCESS IMPORTS TRUE
  83.         initOdm();        
  84.         doTest(PERSON_SHAPES, true, "PersonShapes_WI10", true);
  85.  
  86.         // PROCESS IMPORTS FALSE
  87.         initOdm();
  88.         doTest(PERSON_UI_SHAPES, false, "PersonUIhapes_NI10");        
  89.  
  90.         // PROCESS IMPORTS TRUE
  91.         initOdm();        
  92.         doTest(PERSON_UI_SHAPES, true, "PersonUIShapes_WI10");
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement