//
// OntModel combined = ModelFactory.createOntologyModel(spec, base);
//
// Model m = new ModelImplJena22(combined);
Model m = RDF2Go.getModelFactory().createModel(Reasoning.owl);
m.open();
m.addAll(schemaDataModel.iterator());
// Reasoner reasoner = ReasonerRegistry.getOWLMicroReasoner();//
// miniReasoner();
// reasoner = reasoner.bindSchema(schemaDataModel);
// InfModel jenaModel = ModelFactory.createInfModel(reasoner,
// schemaDataModel);
// Model m = new ModelImplJena24(null, jenaModel);
log.debug("de-anonymizing");
ModelUtils.deanonymize(m);
log.debug("after inferencing, found " + m.size() + " statements");
// // DEBUG
// File debugOut = new File(outDir, packagename + "/schema.nt");
// debugOut.mkdir();
// jenaModel.write(new FileWriter(debugOut), "N-TRIPLES");
JPackage jp = new JPackage(packagename);
// bootstrapping?
JModel jm = Semantics.getbuiltIns_OWL();
jm.addPackage(jp);
/**
* local ontology root
*/
JClass localRoot = new JClass(jp, "Thing1", OWL.Class);
localRoot
.setComment("This class acts as a catch-all for all properties, for which no domain has specified.");
localRoot.addSuperclass(jm.getRoot());
jm.setRoot(localRoot);
// get all classes and assign to package
Set<org.ontoware.rdfreactor.schema.bootstrap.OwlClass> owlClasses = new HashSet<org.ontoware.rdfreactor.schema.bootstrap.OwlClass>();
Set<String> usedClassnames = new HashSet<String>();
for(org.ontoware.rdfreactor.schema.bootstrap.OwlClass oc : org.ontoware.rdfreactor.schema.bootstrap.OwlClass
.getAllInstance_as(m).asList()) {
log.debug("Found owl:Class " + oc.getResource() + " (have " + owlClasses.size()
+ " already)");
org.ontoware.rdf2go.model.node.Resource classURI = oc.getResource();
// check if restriction or real class
if(m.contains(classURI, RDF.type, OWL.Restriction)) {
log.debug("skipping restriction " + classURI);
} else if(skipbuiltins && jm.hasMapping(classURI)) {
log.debug("skipping known class " + classURI);
// TODO add all XSD classes to default JModels and remove this
// check
} else if(classURI.toString().startsWith(Semantics.NS_XSD)) {
log.debug("skipping XML Schema class " + classURI);
// TODO: what is the purpose of this?
} else if(oc.getResource() instanceof BlankNode) {
log.debug("skipping blank class " + classURI);
} else {
log.debug("owl:Class : " + classURI);
owlClasses.add(oc);
// TODO better classname guessing
String classname = JavaNamingUtils.toBeanName(oc, usedClassnames);
assert classname != null;
usedClassnames.add(classname);
log.debug("generating class " + classname + " for " + classURI + " ...");
JClass jc = new JClass(jp, classname, (URI)oc.getResource());
jc.setComment(oc.getAllComment_asList().get(0));
jm.addMapping(oc.getResource(), jc);
}
}
log.debug("dealing with " + owlClasses.size() + " 'real' classes");
log.debug(">>>> Inheritance");
// get all classes and link superclasses
for(org.ontoware.rdfreactor.schema.bootstrap.OwlClass oc : owlClasses) {
log.debug("owl:Class " + oc.getResource());
JClass jc = jm.getMapping(oc.getResource());
for(org.ontoware.rdfreactor.schema.bootstrap.OwlClass superclass : TypeUtils
.getAllRealSuperclasses(oc, owlClasses))
jc.addSuperclass(jm.getMapping(superclass.getResource()));
}
jm.flattenInheritanceHierarchy(jp);
// get all properties
log.info(">>> Processing properties ...");
// this uniqueness constraint can be weakened,
// property names need only to be unique within a class,
// but this might be more consistent anyways
Set<String> usedPropertynames = new HashSet<String>();
for(Property rp : Property.getAllInstance_as(m).asList()) {
log.debug("> Processing property " + rp.getResource());
// name it
String propertyName = JavaNamingUtils.toBeanName(rp, usedPropertynames);
usedPropertynames.add(propertyName);
assert propertyName != null;
List<Class> domains = rp.getAllDomain_asList();
// no domain = no generated property
if(domains == null || domains.size() == 0) {
log.warn("Property " + rp.getResource() + " has no domain, so we ignore it");
} else {
for(Class domain : domains) {
if(!owlClasses.contains(domain)) {
// log.debug("ignored");
} else {
JClass domainClass = jm.getMapping(domain.getResource());
assert domainClass != null : "found no JClass for "
+ rp.getAllDomain_asList().get(0).getResource();
JProperty jprop = new JProperty(domainClass, propertyName,
(URI)rp.getResource());
// wire
log.debug("Adding property '" + jprop.getName() + "' to '"
+ domainClass.getName() + "'");
jprop.getJClass().getProperties().add(jprop);
jprop.setComment(rp.getAllComment_asList().get(0));
for(Class range : rp.getAllRange_asList()) {
if(owlClasses.contains(range
.castTo(org.ontoware.rdfreactor.schema.owl.OwlClass.class)))
jprop.addType(jm.getMapping(range.getResource()));
}
jprop.fixRanges(jm);
// figure out cardinality
ClosableIterator<Statement> it = m.findStatements(Variable.ANY,
OWL.onProperty, rp.getResource());
while(it.hasNext()) {
Statement stmt = it.next();
org.ontoware.rdf2go.model.node.Resource restrictionResource = stmt
.getSubject();
OWL_Protege_NRL_Restriction restriction = OWL_Protege_NRL_Restriction
.getInstance(m, restrictionResource);
int min = restriction.getAllMinCardinality_asList().get(0);
log.debug("Found minrestriction on " + rp + " minCard = " + min);
if(min != -1)
jprop.setMinCardinality(min);
int max = restriction.getAllMaxCardinality_asList().get(0);
log.debug("Found maxrestriction on " + rp + " maxCard = " + max);
if(max != -1)
jprop.setMaxCardinality(max);
}
it.close();
}
}
}
}
// // prune
// log.debug(">>>>>> Pruning");
// for (JClass jc : jp.getClasses()) {
// // FIXME: this is too simple: if no properties: remove
// if (jc.getProperties().size() == 0) {
// log.debug(jc.getName() + " has no properties, removing");
// jp.getClasses().remove(jc);
// }
// }
m.close();
return jm;
}