package net.timbusproject.populator.modules.rcaapconverter;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.PrefixManager;
import pt.caixamagica.owl.owlapi.Ontology;
import pt.caixamagica.owl.owlapi.OntologyFormat;
import pt.caixamagica.owl.owlapi.Prefix;
import pt.caixamagica.owl.owlapi.exceptions.OntologyCreationException;
import pt.caixamagica.owl.owlapi.exceptions.OntologyFormatStorageException;
import pt.caixamagica.owl.owlapi.impl.axioms.ClassAxiom;
import pt.caixamagica.owl.owlapi.impl.axioms.DataPropertyAxiom;
import pt.caixamagica.owl.owlapi.impl.axioms.ObjectPropertyAxiom;
import java.io.ByteArrayOutputStream;
public class Engine {
private Ontology ontology = null;
private OWLOntology owlOntology = null;
private OWLOntologyManager manager = null;
private OWLDataFactory factory = null;
private PrefixManager prefixManager = null;
public String run(String json) throws OntologyCreationException, JSONException, IllegalAccessException, OntologyFormatStorageException, InstantiationException {
ontology = Ontology.createOntology("http://timbus.teco.edu/ontologies/examples/rcaap.owl");
ontology.addPrefix(
new Prefix("cudf", "http://timbus.teco.edu/ontologies/DSOs/CUDF.owl"),
new Prefix("dio", "http://timbus.teco.edu/ontologies/DIO.owl")
);
initializeOntology(ontology);
JSONArray array = new JSONArray(json);
//parseArray
parseArray(array);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ontology.print(OntologyFormat.RDFXML,out);
return new String(out.toByteArray());
}
private void parseArray(JSONArray array) throws JSONException {
String mainRepository = "RCAAP";
ontology.addAxiom(true,
new ClassAxiom(mainRepository, "cudf:Package"),
new DataPropertyAxiom<String>(mainRepository, "cudf:hasName", "Open Access Repository of Scientific Articles ")
);
String repoName;
String homepage;
String directory;
String metadata;
String country;
JSONArray tags_pt;
for(int i = 0; i < array.length(); i++){
repoName = array.getJSONObject(i).getJSONObject("repository").getString("name").replace(" ","");
homepage = array.getJSONObject(i).getJSONObject("repository").getString("homepage");
country = array.getJSONObject(i).getJSONObject("repository").getString("country");
metadata = array.getJSONObject(i).getJSONObject("repository").getString("metadata_format");
directory = array.getJSONObject(i).getJSONObject("repository").getString("directory");
ontology.addAxiom(true,
new ClassAxiom(repoName, "cudf:Package"),
new ObjectPropertyAxiom(mainRepository, "cudf:realizedBy", repoName),
new DataPropertyAxiom<String>(repoName, "cudf:hasName", repoName),
new DataPropertyAxiom<String>(repoName, "hasHomePage", homepage),
new DataPropertyAxiom<String>(repoName, "hasMetadataFormat", metadata),
new DataPropertyAxiom<String>(repoName, "hasDirectory", directory)
);
tags_pt = array.getJSONObject(i).getJSONArray("tags_pt");
String [] tags = new String[0];
for(int j = 0; j < tags_pt.length(); j++){
tags = tags_pt.getString(j).split(" ");
}
for(int k = 0; k < tags.length;k++){
if(!tags[k].isEmpty())
ontology.addAxiom(true,
new DataPropertyAxiom<String>(repoName, "isType", tags[k]) );
}
}
}
private String[] getTags(JSONArray tags_pt) throws JSONException {
String [] tags = new String[0];
for(int i = 0; i < tags_pt.length(); i++){
tags = tags_pt.getString(i).split(" ");
}
return tags;
}
public Ontology getOntology() {
return ontology;
}
private void initializeOntology(Ontology ontology) {
owlOntology = ontology.getOntology();
manager = owlOntology.getOWLOntologyManager();
factory = manager.getOWLDataFactory();
prefixManager = ontology.getPrefixManager();
}
}