Model m = RDF2Go.getModelFactory().createModel(Reasoning.rdfs);
m.open();
m.addAll(modelWithSchemaData.iterator());
// prepare JModel
JModel jm = Semantics.getbuiltIns_RDFS();
JPackage jp = new JPackage(packagename);
jm.getPackages().add(jp);
// set local ontology root
JClass localRoot = new JClass(jp, "Thing1", RDFS.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);
// process
log.debug("de-anonymizing (replacing bnodes with random uris");
ModelUtils.deanonymize(m);
// analysis (triggers also inferencing)
List<? extends Class> rdfclasses = Class.getAllInstance_as(m).asList();
log.info("Got " + rdfclasses.size() + " rdfs:Classes");
for(Class c : rdfclasses) {
log.debug("Found class: " + c.getResource());
}
Property[] rdfproperties = Property.getAllInstance_as(m).asArray();
for(Property p : rdfproperties) {
log.debug("Found property: " + p.getResource());
}
log.info("Got " + rdfproperties.length + " rdfs:Properties");
log.debug("Found " + m.size() + " statements in schema after inferencing.");
// get all classes and assign to package
Set<String> usedClassnames = new HashSet<String>();
usedClassnames.add(jm.getRoot().getName());
Set<Class> rdfsClasses = new HashSet<Class>();
for(Class rc : Class.getAllInstance_as(m).asList()) {
if(skipbuiltins && jm.hasMapping(rc.getResource())) {
log.debug("CLASS " + rc + " is known -> skipping generation");
} else if(!(rc.getResource() instanceof URI)) {
log.warn("A Class with a blank node ID makes not much sense -> ignored");
} else {
rdfsClasses.add(rc);
// TODO better classname guessing
String classname = JavaNamingUtils.toBeanName(rc, usedClassnames);
assert classname != null;
usedClassnames.add(classname);
log.debug("CLASS " + classname + " generated for " + rc.getResource() + " ...");
assert rc.getResource() instanceof URI : "A Class with a blank node ID makes not much sense";
JClass jc = new JClass(jp, classname, (URI)rc.getResource());
jc.setComment(Utils.toJavaComment(rc.getAllComment_asList())); // might
// be
// null,
// ok.
jm.addMapping(rc.getResource(), jc);
}
}
log.debug(">>>> Inheritance");
// get all classes and link superclasses
for(org.ontoware.rdfreactor.schema.bootstrap.Class rc : rdfsClasses) {
log.debug("rdfs:Class " + rc.getResource());
JClass jc = jm.getMapping(rc.getResource());
for(org.ontoware.rdfreactor.schema.bootstrap.Class superclass : rc
.getAllSubClassOf_asList())
jc.addSuperclass(jm.getMapping(superclass.getResource()));
}
log.info("-------------- PROPERTIES ...");
for(Property rp : Property.getAllInstance_as(m).asList()) {
log.info("PROPERTY " + rp.getResource());
if(skipbuiltins && jm.knownProperties.contains(rp.getResource().asURI())) {
// do nothing
log.debug("Skipping built-in property " + rp.getResource().asURI().toSPARQL());
} else if(DeprecatedProperty.hasInstance(rp.getModel(), rp.getResource().asURI())) {
log.info("Skipping deprecated property " + rp
+ " (as indicated by owl:DeprecatedProperty)");
} else {
// inspect domains
List<Class> domains = rp.getAllDomain_asList();
// TODO: ignore if already in higher level
if(domains == null || domains.size() == 0) {
log.warn("PROPERTY " + rp.getResource() + " has no domain, using root");
handleProperty(m, jm, jm.getRoot(), rp);
} else {
for(Resource domain : domains) {
log.info("PROPERTY " + rp.getResource() + " has domain " + domain);
JClass domainClass = jm.getMapping(domain.getResource());
assert domainClass != null : "found no JClass for "
+ rp.getAllDomain_asList().get(0).getResource();
// domainclass might be a built-in, redirect to root
if(Semantics.getbuiltIns_RDFS().containsJClass(domainClass)) {
log.info("domain " + domainClass
+ " is a built-in, hence we attach the property to the root ("
+ jm.getRoot() + ")");
domainClass = jm.getRoot();
}
handleProperty(m, jm, domainClass, rp);
}
}
}
jm.flattenInheritanceHierarchy(jp);
jm.materialiseMissingProperties(jp, skipbuiltins);
}
m.close();
m = null;
return jm;