Skip to content

Commit

Permalink
some fixes to OWL importing
Browse files Browse the repository at this point in the history
  • Loading branch information
GEGlobalResearch committed Feb 21, 2017
1 parent d7d77cd commit ff7534b
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,7 @@ else if(constant.equals("None")){
}

private boolean isVariable(TypeCheckInfo tci) {
if (tci == null) return false;
ConceptIdentifier ci = tci.getTypeCheckType();
if (ci instanceof ConceptName && ((ConceptName)ci).getType() != null && ((ConceptName)ci).getType().equals(ConceptType.VARIABLE)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.ge.research.sadl.jena.JenaBasedSadlModelProcessor;
import com.ge.research.sadl.jena.inference.SadlJenaModelGetterPutter;
import com.ge.research.sadl.preferences.SadlPreferences;
import com.ge.research.sadl.processing.SadlConstants;
import com.ge.research.sadl.reasoner.ConfigurationException;
import com.ge.research.sadl.utils.ResourceManager;
import com.hp.hpl.jena.datatypes.RDFDatatype;
Expand Down Expand Up @@ -132,6 +133,7 @@ public class OwlToSadl {
public class ModelConcepts {
private List<Ontology> ontologies = new ArrayList<Ontology>();
private List<OntClass> classes = new ArrayList<OntClass>();
private List<OntClass> anonClasses = new ArrayList<OntClass>();
private List<ObjectProperty> objProperties = new ArrayList<ObjectProperty>();
private List<DatatypeProperty> dtProperties = new ArrayList<DatatypeProperty>();
private List<Property> rdfProperties = new ArrayList<Property>();
Expand Down Expand Up @@ -347,6 +349,14 @@ public void addErrorMessage(String errorMessage) {
errorMessages.add(errorMessage);
}
}

private List<OntClass> getAnonClasses() {
return anonClasses;
}

private void addAnonClass(OntClass anonClass) {
this.anonClasses.add(anonClass);
}
}

/**
Expand Down Expand Up @@ -561,7 +571,7 @@ private void process() throws Exception {
alias = qNamePrefixes.get(baseUri + "#");
}
}
if (alias != null) {
if (alias != null && alias.length() > 0) {
sadlModel.append(" alias ");
sadlModel.append(alias);
}
Expand Down Expand Up @@ -641,6 +651,9 @@ private void process() throws Exception {
String impUri;
try {
impUri = imp.asOntology().getURI(); //getNameSpace();
if (isImplicitUri(impUri)) {
continue;
}
}
catch (Exception e) {
impUri = imp.toString();
Expand Down Expand Up @@ -869,6 +882,22 @@ private void process() throws Exception {
}
}

private boolean isImplicitUri(String impUri) {
if (impUri.equals(SadlConstants.SADL_BASE_MODEL_URI)) {
return true;
}
else if (impUri.equals(SadlConstants.SADL_BUILTIN_FUNCTIONS_URI)) {
return true;
}
else if (impUri.equals(SadlConstants.SADL_IMPLICIT_MODEL_URI)) {
return true;
}
else if (impUri.equals(SadlConstants.SADL_LIST_MODEL_URI)) {
return true;
}
return false;
}

private void addEndOfStatement(StringBuilder sb, int numLineFeeds) {
if (sb.length() > 1 && Character.isDigit(sb.charAt(sb.length() - 1))) {
sb.append(" .");
Expand Down Expand Up @@ -1232,8 +1261,8 @@ private List<Triple> rdfListToList(Node lst, List<Triple> trLst) {
}
return trLst;
}

private String individualToSadl(ModelConcepts concepts, Individual inst) {
private String individualNameAndAnnotations(ModelConcepts concepts, Individual inst) {
StringBuilder sb = new StringBuilder();
if (!inst.getNameSpace().equals(baseUri+'#')) {
if (qNamePrefixes.containsKey(inst.getNameSpace())) {
Expand All @@ -1254,6 +1283,12 @@ private String individualToSadl(ModelConcepts concepts, Individual inst) {
sb.append(uriToSadlString(concepts, inst));
}
addNotesAndAliases(sb, inst);
return sb.toString();
}

private String individualToSadl(ModelConcepts concepts, Individual inst) {
StringBuilder sb = new StringBuilder();
sb.append(individualNameAndAnnotations(concepts, inst));
if (isNewLineAtEndOfBuffer(sb)) {
sb.append(" ");
}
Expand Down Expand Up @@ -1282,7 +1317,7 @@ private String individualToSadl(ModelConcepts concepts, Individual inst) {
StmtIterator insitr = inst.listProperties();
while (insitr.hasNext()) {
Statement s = insitr.next();
if (s.getPredicate().equals(RDF.type)) {
if (s.getPredicate().equals(RDF.type) || s.getPredicate().equals(RDFS.label) || s.getPredicate().equals(RDFS.comment)) {
continue;
}
sb.append("\n has ");
Expand Down Expand Up @@ -1390,6 +1425,15 @@ else if (!spcls.equals(OWL.Thing)) {
else {
sb.append(" is a class");
}
OntClass eqcls = cls.getEquivalentClass();
if (eqcls != null) {
if (concepts.getAnonClasses().contains(eqcls)) {
concepts.getAnonClasses().remove(eqcls);
String str = uriToSadlString(concepts, eqcls);
sb.append(" must be ");
sb.append(str);
}
}
// add properties with this class in the domain
ExtendedIterator<OntProperty> eitr2 = cls.listDeclaredProperties(true);
while (eitr2.hasNext()) {
Expand Down Expand Up @@ -1443,8 +1487,8 @@ else if (!spcls.equals(OWL.Thing)) {
private void addNotesAndAliases(StringBuilder sb, Resource rsrc) {
StmtIterator sitr = rsrc.listProperties(RDFS.label);
if (sitr.hasNext()) {
addNewLineIfNotAtEndOfBuffer(sb);
sb.append(" (alias ");
// addNewLineIfNotAtEndOfBuffer(sb);
sb.append(" (alias ");
int cntr = 0;
while (sitr.hasNext()) {
RDFNode alias = sitr.nextStatement().getObject();
Expand Down Expand Up @@ -1643,11 +1687,13 @@ private String rdfNodeToSadlString(ModelConcepts concepts, RDFNode object, boole
}
else if (object.isLiteral()) {
String dturi = object.asLiteral().getDatatypeURI();
forceQuotes = forceQuotes ? true : isRDFDatatypeString(dturi, object);
forceQuotes = forceQuotes ? true : (dturi != null ? isRDFDatatypeString(dturi, object) : true);
if (object.asLiteral().getDatatypeURI() == null) {
String lf = object.asLiteral().getLexicalForm();
if (forceQuotes || lf.contains(" ")) {
return "\"" + object.asLiteral().getLexicalForm() + "\"";
if (forceQuotes || lf.contains(" ") || lf.contains("\"")) {
String s = object.asLiteral().getLexicalForm();
s = s.replace("\"", "\\\"");
return "\"" + s + "\"";
}
return object.asLiteral().getLexicalForm();
}
Expand All @@ -1672,10 +1718,17 @@ private String uriToSadlString(ModelConcepts concepts, Resource rsrc) {
if (rsrc.getNameSpace().equals(XSD.getURI())) {
return rsrc.getLocalName();
}
if (qNamePrefixes.containsKey(rsrc.getNameSpace())) {
return qNamePrefixes.get(rsrc.getNameSpace()) + ":" + checkLocalnameForKeyword(rsrc.getLocalName());
String ns = rsrc.getNameSpace();
String trimmedNs = ns.endsWith("#") ? ns.substring(0, ns.length() - 1) : null;
if (qNamePrefixes.containsKey(ns)) {
return qNamePrefixes.get(ns) + ":" + checkLocalnameForKeyword(rsrc.getLocalName());
}
else {
if (trimmedNs != null) {
if (qNamePrefixes.containsKey(trimmedNs)) {
return qNamePrefixes.get(trimmedNs) + ":" + checkLocalnameForKeyword(rsrc.getLocalName());
}
}
return rsrc.getURI();
}
}
Expand All @@ -1687,6 +1740,47 @@ private String uriToSadlString(ModelConcepts concepts, Resource rsrc) {
else if (rsrc.canAs(Restriction.class)) {
return restrictionToString(concepts, null, rsrc.as(Restriction.class));
}
else if (rsrc instanceof OntClass) {
// OntClass eqcls = ((OntClass)rsrc).getEquivalentClass();
// if (eqcls != null) {
// return uriToSadlString(concepts, eqcls);
// }
EnumeratedClass enumcls = ((OntClass)rsrc).asEnumeratedClass();
if (enumcls != null) {
ExtendedIterator<? extends OntResource> eitr = enumcls.listInstances();
while (eitr.hasNext()) {
OntResource en = eitr.next();
en.toString();
}
ExtendedIterator<RDFNode> eitr2 = enumcls.listIsDefinedBy();
while (eitr2.hasNext()) {
RDFNode en = eitr2.next();
en.toString();
}
RDFList oneoflst = enumcls.getOneOf();
List<RDFNode> nodeLst = oneoflst.asJavaList();
if (nodeLst != null && nodeLst.size() > 0) {
StringBuilder sb = new StringBuilder();
sb.append("one of {");
int cntr = 0;
for (int i = 0; i < nodeLst.size(); i++) {
RDFNode n = nodeLst.get(i);
if (cntr > 0) sb.append(", ");
sb.append("\n ");
if (n.canAs(Individual.class)&& concepts.getInstances().contains(n)) {
sb.append(individualNameAndAnnotations(concepts, n.as(Individual.class)));
concepts.getInstances().remove(n);
}
else {
sb.append(rdfNodeToSadlString(concepts, n, false));
}
cntr++;
}
sb.append("}");
return sb.toString();
}
}
}
}
return rsrc.toString();
}
Expand All @@ -1702,8 +1796,14 @@ private String checkLocalnameForKeyword(String localName) {
private boolean addResourceToList(ModelConcepts concepts, OntResource ontRsrc) {
Resource type = ontRsrc.getRDFType();
if (type.equals(OWL.Class)) {
concepts.addClass(ontRsrc.as(OntClass.class));
return true;
if (ontRsrc.isAnon() && ontRsrc.canAs(OntClass.class)) {
concepts.addAnonClass(ontRsrc.as(OntClass.class));
return true;
}
else {
concepts.addClass(ontRsrc.as(OntClass.class));
return true;
}
}
else if (type.equals(OWL.ObjectProperty)) {
concepts.addObjProperty(ontRsrc.asObjectProperty());
Expand Down Expand Up @@ -1734,8 +1834,13 @@ else if (type.equals(OWL.AnnotationProperty)) {
return true;
}
else if (type.equals(OWL.Ontology)) {
concepts.addOntology(ontRsrc.asOntology());
return true;
if (!isImplicitUri(ontRsrc.getURI())) {
concepts.addOntology(ontRsrc.asOntology());
return true;
}
else {
return false;
}
}
else if (type.equals(RDFS.Datatype)) {
if (ontRsrc.isAnon()) {
Expand Down

0 comments on commit ff7534b

Please sign in to comment.