Child: [ea0fe7] (diff)

Download this file

Engine.java    108 lines (91 with data), 4.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
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();
}
}