2121import org .apache .jena .ontology .OntModel ;
2222import org .apache .jena .ontology .OntProperty ;
2323import org .apache .jena .ontology .OntResource ;
24- import org .apache .jena .query .Query ;
25- import org .apache .jena .query .QueryExecution ;
26- import org .apache .jena .query .QueryExecutionFactory ;
27- import org .apache .jena .query .QueryFactory ;
28- import org .apache .jena .query .ResultSet ;
24+ import org .apache .jena .query .*;
25+ import org .apache .jena .rdf .model .Literal ;
2926import org .apache .jena .rdf .model .ModelFactory ;
3027import org .apache .jena .rdf .model .Property ;
3128import org .apache .jena .vocabulary .XSD ;
3532 */
3633public class CSV2RDF {
3734 OntModel instances ;
35+ OntModel variables ;
3836 OntModel mcOntology ;
3937 String instance_URI = "https://w3id.org/okn/i/mint/" ;
4038 JsonObject unitDictionary ;
@@ -45,13 +43,15 @@ public class CSV2RDF {
4543 */
4644 public CSV2RDF (String unitDictionaryFile , String scientificVariableFile ) throws FileNotFoundException {
4745 instances = ModelFactory .createOntologyModel ();
46+ variables = ModelFactory .createOntologyModel ();
4847 //create class
4948 instances .createClass ("https://w3id.org/okn/o/sd/#SoftwareConfiguration" );
50- //we read all the variables, as they will be part of the model catalog .
51- instances .read (scientificVariableFile );
49+ //we read all the variables separately , as we will use them to link contents .
50+ variables .read (scientificVariableFile );
5251 mcOntology = ModelFactory .createOntologyModel ();
5352 mcOntology .read ("https://w3id.org/okn/o/sdm#" );
5453 mcOntology .read ("https://w3id.org/okn/o/sd#" );
54+ mcOntology .createObjectProperty ("http://www.w3.org/2002/07/owl#sameAs" );
5555 //for some reason it's not doing the right redirect
5656// mcOntology.read("https://mintproject.github.io/Mint-ModelCatalog-Ontology/release/1.4.0/ontology.xml");
5757// mcOntology.read("http://ontosoft.org/ontology/software/ontosoft-v1.0.owl");//for some reason, it doesn't do the negotiation rihgt.
@@ -106,29 +106,31 @@ private void checkFile (String path){
106106 * @param rowValue
107107 */
108108 private void processSVO (Individual ind , Property p , String rowValue , boolean index ){
109+ Individual mint_svo = instances .createClass ("https://w3id.org/okn/o/sd#StandardVariable" ).createIndividual (instance_URI +encode (rowValue ));
110+ mint_svo .addLabel (rowValue , null );
111+ //assign the variable to target node.
112+ ind .addProperty (p , mint_svo );
109113 /**
110114 read label, try to find one in the svo file (there should be).
111- * If found, then use that URI. IF not found, then create one .
115+ * If found, then copy description and label into instance, and add owl:sameAs .
112116 **/
113- Query query = QueryFactory .create ("select ?var where {"
114- + "?var <http://www.w3.org/2000/01/rdf-schema#label> \" " +rowValue +"\" }" );
115- // Execute the query and obtain results
116- QueryExecution qe = QueryExecutionFactory .create (query , instances );
117- ResultSet rs = qe .execSelect ();
118- if (rs .hasNext ()){
119- ind .addProperty (p , rs .next ().getResource ("?var" ));
120- }else {
121- //no variable found: create variable, add label
122- Individual userInstance ;
123- //if(index){
124- // userInstance = instances.createClass("https://w3id.org/okn/o/sdm#NumericalIndex").createIndividual(instance_URI+encode(rowValue));
125- //}else{
126- userInstance = instances .createClass ("http://www.geoscienceontology.org/svo/svu#Variable" ).createIndividual (instance_URI +encode (rowValue ));
127- //}
128- userInstance .addLabel (rowValue , null );
129- //assign the variable to target node.
130- ind .addProperty (p , userInstance );
131- }
117+ Query query = QueryFactory .create ("select ?var ?desc where {"
118+ + "?var <http://www.w3.org/2000/01/rdf-schema#label> \" " +rowValue +"\" " +
119+ "optional {?var <https://schema.org/description> ?desc}}" );
120+ QueryExecution qe = QueryExecutionFactory .create (query , variables );
121+ ResultSet rs = qe .execSelect ();
122+ // if found in SVO, copy description and owl:sameAs
123+ if (rs .hasNext ()){
124+ QuerySolution qs = rs .next ();
125+ OntProperty pSameAs = (OntProperty )mcOntology .getProperty ("http://www.w3.org/2002/07/owl#sameAs" );
126+ mint_svo .addProperty (pSameAs , qs .getResource ("?var" ));
127+ Literal desc = qs .getLiteral ("?desc" );
128+ if (!"" .equals (desc ) && desc != null ){
129+ //svo comes with html tags. remove them
130+ mint_svo .addProperty (mcOntology .getOntProperty ("https://w3id.org/okn/o/sd#description" ),
131+ desc .getString ().replace ("<p>" ,"\n " ).replaceAll ("\\ <[^>]*>" ,"" ));
132+ }
133+ }
132134 }
133135
134136 private void processFile (String path ) throws Exception {
@@ -176,10 +178,10 @@ private void processFile(String path) throws Exception{
176178 System .err .println ("ERROR: " +rowValue +" is not a URI. Ind: " +ind .getLocalName ());
177179 }
178180 break ;
179- case "http://www.w3.org/2001/XMLSchema#int " :
181+ case "http://www.w3.org/2001/XMLSchema#integer " :
180182 try {
181183 Integer .parseInt (rowValue );
182- ind .addProperty (p , rowValue ,XSDDatatype .XSDint );
184+ ind .addProperty (p , rowValue ,XSDDatatype .XSDinteger );
183185 }catch (NumberFormatException e ){
184186 System .err .println ("ERROR: " +rowValue +" is not an Integer. Ind: " +ind .getLocalName ());
185187 }
@@ -189,7 +191,7 @@ private void processFile(String path) throws Exception{
189191 Float .parseFloat (rowValue );
190192 ind .addProperty (p , rowValue ,XSDDatatype .XSDfloat );
191193 }catch (NumberFormatException e ){
192- System .err .println ("ERROR: " +rowValue +" is not an Integer . Ind: " +ind .getLocalName ());
194+ System .err .println ("ERROR: " +rowValue +" is not a float . Ind: " +ind .getLocalName ());
193195 }
194196 break ;
195197 default :
@@ -248,9 +250,6 @@ private void processFile(String path) throws Exception{
248250 }else if (p .toString ().contains ("hasStandardVariable" )){
249251 processSVO (ind , p , rowValue ,false );
250252 }
251- //else if(p.toString().contains("usefulForCalculatingIndex")){
252- // processSVO(ind, p, rowValue,true);
253- //}
254253 else if (p .toString ().contains ("hasSoftwareImage" )){
255254 //seaparated because here we will do the link to Dockerpedia when appropriate.
256255 //at the moment just create a URI and link with label
@@ -366,10 +365,16 @@ public static void main(String[] args){
366365// String pathToTransformationsDataFolder = "C:\\Users\\dgarijo\\Documents\\GitHub\\ModelCatalog\\Data\\Transformations";
367366
368367 //MINT
369- String version = "1.6.0" ;
370- String pathToInstancesDataFolder = "C:\\ Users\\ dgarijo\\ Documents\\ GitHub\\ ModelCatalog\\ Data\\ MINT\\ " +version ;
368+ String version = "1.6_to_1.7" ;
369+ // Unix
370+ String pathToGitFolder = "/home/dgarijo/Documents/GitHub/ModelCatalog" ;
371+
372+ // WIN
373+ // String pathToGitFolder = "C:\\Users\\dgarijo\\Documents\\GitHub";
374+ String pathToInstancesDataFolder = pathToGitFolder +"/Data/MINT/" +version ;
371375 String [] graphs = {"mint@isi.edu" };//, "texas@isi.edu", "coertel@mitre.org", "brandon@starsift.com","hvarg@isi.edu"};
372- //end mint
376+ //end MINT
377+
373378 //COVID models example
374379// pathToInstancesDataFolder = "C:\\Users\\dgarijo\\Documents\\GitHub\\ModelCatalog\\Data\\COVID";
375380// processDataFolder(pathToInstancesDataFolder, false, catalog);
@@ -382,11 +387,12 @@ public static void main(String[] args){
382387 //end wifire
383388 for (String graph :graphs ){
384389 //a new catalog should be made per graph; otherwise graphs will be aggregated.
385- CSV2RDF catalog = new CSV2RDF ("C: \\ Users \\ dgarijo \\ Documents \\ GitHub \\ ModelCatalog \\ Data\\ Units\\ dict.json" ,
386- "C: \\ Users \\ dgarijo \\ Documents \\ GitHub \\ ModelCatalog \\ Data\\ SVO\\ variable-23-10-2019.ttl" );
387- processDataFolder (pathToInstancesDataFolder +"\\ " +graph , false , catalog );
388- exportRDFFile ("modelCatalog_" +graph +".ttl" , catalog .instances , "TTL" );
390+ CSV2RDF catalog = new CSV2RDF (pathToGitFolder + "/ Data/ Units/ dict.json" ,
391+ pathToGitFolder + "/ Data/ SVO/ variable-23-10-2019.ttl" );
392+ processDataFolder (pathToInstancesDataFolder +"/ " +graph , false , catalog );
393+ exportRDFFile ("modelCatalog_" +graph +"_" + version + " .ttl" , catalog .instances , "TTL" );
389394 }
395+ System .out .println ("Import process finished successfully" );
390396 }catch (Exception e ){
391397 System .err .println ("Error: " +e );
392398 }
0 commit comments