X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FOntologyGenerator.java;fp=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FOntologyGenerator.java;h=f98a85f5673785e292347149b5865cde0454f392;hb=d788a05e77b983199f0982a0e56629e7fc30c691;hp=741e197f0e2ff50bf9bb6b41c619fe94fd78471e;hpb=fb01c21839aea801224aeb82b64bd261120619d5;p=simantics%2Finterop.git diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java index 741e197..f98a85f 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java @@ -11,9 +11,11 @@ import java.util.Set; import javax.xml.namespace.QName; import org.simantics.utils.datastructures.BijectionMap; +import org.simantics.xml.sax.SchemaConversionBase.Inheritance; +import org.simantics.xml.sax.SchemaConversionBase.InheritanceType; +import org.simantics.xml.sax.SchemaConversionBase.RefType; import org.simantics.xml.sax.SchemaObject.ObjectType; import org.simantics.xml.sax.configuration.AttributeComposition; -import org.simantics.xml.sax.configuration.Configuration; import org.simantics.xml.sax.configuration.IDReference; import org.w3._2001.xmlschema.Annotated; import org.w3._2001.xmlschema.Attribute; @@ -22,15 +24,21 @@ import org.w3._2001.xmlschema.Element; import org.w3._2001.xmlschema.LocalComplexType; import org.w3._2001.xmlschema.LocalSimpleType; import org.w3._2001.xmlschema.NamedAttributeGroup; +import org.w3._2001.xmlschema.NamedGroup; import org.w3._2001.xmlschema.Restriction; import org.w3._2001.xmlschema.Schema; import org.w3._2001.xmlschema.SimpleType; -import org.w3._2001.xmlschema.TopLevelAttribute; -public class OntologyGenerator extends SchemaConversionBase { +//public class OntologyGenerator extends SchemaConversionBase { +public class OntologyGenerator implements SchemaConversionComponent { + SchemaConversionBase base; - public OntologyGenerator(Configuration configuration) { - super(configuration); + public OntologyGenerator(SchemaConverter converter, SchemaConversionBase base) { + this.base = base; + this.converter = converter; + this.schema = base.schema; + this.ontologyUri = base.ontologyURI; + this.className = base.className; } String ontRoot = "ONT."; @@ -44,30 +52,7 @@ public class OntologyGenerator extends SchemaConversionBase { PrintWriter writer = null; - - - - - public void createOntology(Schema schema, String ontologyName, String className, SchemaConverter converter) throws FileNotFoundException { - this.schema = schema; - this.converter = converter; - this.ontologyUri = ontologyName; - this.className = className; - -// for (OpenAttrs attrs : schema.getIncludeOrImportOrRedefine()) { -// if (attrs instanceof Annotation) { -// Annotation ann = (Annotation)attrs; -// for (Object o : ann.getAppinfoOrDocumentation()) { -// if (o instanceof Documentation) { -// Documentation doc = (Documentation)o; -// } else if (o instanceof Appinfo) { -// Appinfo info = (Appinfo)o; -// } -// } -// } -// } - - + public void createOntology() throws FileNotFoundException { StringWriter stringWriter = null; if (converter.getOntologyFile() == null) { stringWriter = new StringWriter(); @@ -76,7 +61,7 @@ public class OntologyGenerator extends SchemaConversionBase { writer = new PrintWriter(converter.getOntologyFile()); } - handle(schema); + handle(); writer.flush(); writer.close(); @@ -84,10 +69,8 @@ public class OntologyGenerator extends SchemaConversionBase { System.out.println(stringWriter.toString()); } - protected void handle(Schema schema) { - String parts[] = ontologyUri.split("/"); - String name = parts[parts.length-1]; - ontRoot = name.substring(0, Math.min(3, name.length())).toUpperCase(); + protected void handle() { + ontRoot = converter.shortName; for (String s : converter.getHeader()) { writer.println(commentTag + " " + s); @@ -109,18 +92,18 @@ public class OntologyGenerator extends SchemaConversionBase { writer.println(commentTag + " Interpreted from schema"); writer.println(); - super.handle(schema); + base.handle(this); } protected String getType(QName qtype) { - String ontType = getL0TypeFromPrimitiveType(qtype); + String ontType = base.getL0TypeFromPrimitiveType(qtype); if (ontType != null) return ontType; - else if (isComplexTypeRef(qtype.getLocalPart())) + else if (base.isComplexTypeRef(qtype.getLocalPart())) return ontRoot+getComplexTypePrefix()+qtype.getLocalPart(); - else if (isSimpleTypeRef(qtype.getLocalPart())) + else if (base.isSimpleTypeRef(qtype.getLocalPart())) return ontRoot+qtype.getLocalPart(); - else if (isElementRef(qtype.getLocalPart())) + else if (base.isElementRef(qtype.getLocalPart())) return ontRoot+qtype.getLocalPart(); else if (qtype.getPrefix() != null && qtype.getPrefix().length() > 0) { return ontRoot+qtype.getPrefix()+qtype.getLocalPart(); @@ -128,19 +111,6 @@ public class OntologyGenerator extends SchemaConversionBase { throw new RuntimeException("Reference to unknown type " + qtype.getLocalPart()); } - @Override - protected void handle(TopLevelAttribute topLevelAttribute) { - super.handle(topLevelAttribute); - writer.println(); - } - - @Override - protected void handleSimpleType(SchemaObject topLevelSimpleType) { - super.handleSimpleType(topLevelSimpleType); - writer.println(); - } - - public String getComplexTypePrefix() { return "ComplexTypes."; } @@ -149,94 +119,85 @@ public class OntologyGenerator extends SchemaConversionBase { return "AttributeGroups."; } - @Override - protected void handle(SchemaObject parent, SchemaElement indicator, List elements) { - if (indicator.getType() == SchemaElement.ElementType.SEQUENCE || (indicator.getType() == SchemaElement.ElementType.CHOICE && indicator.getRestriction().many())) { - for (SchemaElement e : elements) { - handle(parent, indicator, e); - } - } else if (indicator.getType() == SchemaElement.ElementType.CHOICE) { - String name = getChoiceName(elements); - boolean single = true; - for (SchemaElement e : elements) { - if (e.getRestriction().many()) { - single = false; - break; - } + public void handleChoice(SchemaObject parent, SchemaElement indicator, java.util.List elements, String name) { + boolean single = true; + for (SchemaElement e : elements) { + if (e.getRestriction().many()) { + single = false; + break; } - String relationName = getName(parent)+".has"+name; - writer.print(relationName); + } + String relationName = getName(parent)+".has"+name; + writer.print(relationName); + + List types = new ArrayList(); + for (SchemaElement e : elements) { + Element localElement = e.getElement(); + QName refType = null; + String type = null; - List types = new ArrayList(); - for (SchemaElement e : elements) { - Element localElement = e.getElement(); - QName refType = null; - String type = null; - - if (localElement.getName() != null) { - refType = localElement.getType(); - type = getL0TypeFromPrimitiveType(refType); - } else if (localElement.getRef() != null) { - refType = localElement.getRef(); - type = getL0TypeFromPrimitiveType(refType); - } - if (type == null) { - SchemaObject obj = getWithName(parent, refType.getLocalPart()); - types.add(getName(obj,"has")); - } + if (localElement.getName() != null) { + refType = localElement.getType(); + type = base.getL0TypeFromPrimitiveType(refType); + } else if (localElement.getRef() != null) { + refType = localElement.getRef(); + type = base.getL0TypeFromPrimitiveType(refType); } - if (types.size() > 0) { - for (String type : types) { - writer.print(" 0) { + for (String type : types) { + writer.print(" " + type); - } + } else { + writer.print(" " + type); + } + } + if (!single) { + writer.println(ontRoot+name+ "List " + ontRoot+name+"List"); + writer.println(" : L0.FunctionalRelation"); } + writer.println(" --> " + ontRoot+name+"List"); } - - } + }; @Override - protected void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType) { + public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType) { if (refType != refType.Element) { QName referenceType = null; if (refType == RefType.Type) { referenceType = element.getElement().getType(); //refName = element.getElement().getName() - SchemaObject eObj = elements.get(element.getElement()); + SchemaObject eObj = base.elements.get(element.getElement()); if (refName == null) refName = eObj.getName(); } else { @@ -244,10 +205,10 @@ public class OntologyGenerator extends SchemaConversionBase { if (refName == null) refName = referenceType.getLocalPart(); } - String type = getL0TypeFromPrimitiveType(referenceType); + String type = base.getL0TypeFromPrimitiveType(referenceType); SchemaObject obj = null; if (type == null) { - obj = getWithName(parent, referenceType.getLocalPart()); + obj = base.getWithName(parent, referenceType.getLocalPart()); writer.println(getName(parent)+".has"+refName + " " + getName(obj)); @@ -256,7 +217,7 @@ public class OntologyGenerator extends SchemaConversionBase { writer.println(" --> " + getType(referenceType)); } - if (useElementList(parent, indicator,element, refType == RefType.Reference, refName, referenceType)) { + if (base.useElementList(parent, indicator,element, refType == RefType.Reference, refName, referenceType)) { if (type == null) { writer.println(getName(parent)+"."+refName + "List " + getName(obj)); - if (useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) { + if (base.useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) { writer.println(getName(parent)+"."+refName + "List element = (JAXBElement)facetWrap; @@ -348,7 +315,7 @@ public class OntologyGenerator extends SchemaConversionBase { } @Override - protected void handleAttributes(SchemaObject simpleTypeObj) { + public void handleAttributes(SchemaObject simpleTypeObj) { // SchemaObject parent = simpleTypeObj.getParent(); // SimpleType simpleType = simpleTypeObj.getSimpleType(); // Restriction restriction = simpleType.getRestriction(); @@ -357,7 +324,7 @@ public class OntologyGenerator extends SchemaConversionBase { } @Override - protected void handle(SchemaObject parent, AttributeGroup attributeGroup) { + public void handle(SchemaObject parent, AttributeGroup attributeGroup) { if (parent == null) { NamedAttributeGroup group = (NamedAttributeGroup)attributeGroup; writer.println(ontRoot+getAttributeGroupPrefix()+group.getName()+ " attributes) { + public void handleAttributeComposition(SchemaObject parent, AttributeComposition composition, BijectionMap attributes) { Attribute compositionAttribute = new Attribute(); compositionAttribute.setName(composition.getName()); - QName type = new QName(CONVERSION_NS, composition.getType()); + QName type = new QName(SchemaConversionBase.CONVERSION_NS, composition.getType()); compositionAttribute.setType(type); handle(parent, compositionAttribute); } @Override - protected void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { + public void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { SimpleType simpleType = simpleTypeObj.getSimpleType(); String name = simpleType.getName(); @@ -398,27 +366,75 @@ public class OntologyGenerator extends SchemaConversionBase { relationName = getName(parent)+".has"+name; writer.println(relationName+ " " + ontType); } else { - Restriction restriction = simpleType.getRestriction(); - if (restriction == null || simpleType.getUnion() != null || simpleType.getId() != null) - throw new RuntimeException(simpleType.getName() + " restriction error"); - QName base = restriction.getBase(); - String relationName = ontRoot+"has"+name; if (parent != null) relationName = getName(parent)+".has"+name; writer.println(relationName+ " " + ontType); + QName base = this.base.getSimpleTypeBase(simpleType); + Inheritance inheritance = new Inheritance(""); + this.base.getAtomicTypeInheritance(base, inheritance); + if (inheritance.atomicType == null) + throw new RuntimeException("Could not locate atomic type for SimpleType " + simpleType.getName()); + writer.println(" --> " + inheritance.atomicType.l0Type); + +// Restriction restriction = simpleType.getRestriction(); +// if (restriction != null) { +// +// QName base = restriction.getBase(); +// String ontType = getL0Type(base); +// writer.println(" --> " + ontType); +// } else if (simpleType.getId() != null) { +// throw new RuntimeException(simpleType.getName() + " restriction error"); +// } else if (simpleType.getUnion() != null) { +// Union union = simpleType.getUnion(); +// String ontType = null; +// if (union.getMemberTypes().size() > 0) { +// for (QName type : union.getMemberTypes()) { +// String sType = null; +// TypeEntry entry = getTypeEntry(type); +// if (entry == null) { +// SchemaObject obj = simpleTypeName.get(type.getLocalPart()); +// Inheritance inheritance = new Inheritance(""); +// getAtomicTypeInheritance(type, obj, inheritance); +// sType = inheritance.atomicType.l0Type; +// } else { +// sType = entry.l0Type; +// } +// if (ontType == null) +// ontType = sType; +// else if (!ontType.equals(sType)) +// throw new RuntimeException(simpleType.getName() + " union has incompatible member types"); +// } +// } else { +// if (union.getSimpleType().size() == 0) +// throw new RuntimeException(simpleType.getName() + " union error"); +// for (SimpleType s : union.getSimpleType()) { +// if (restriction == null) +// restriction = s.getRestriction(); +// else { +// Restriction r = s.getRestriction(); +// if (!r.getBase().equals(restriction.getBase())) +// throw new RuntimeException(simpleType.getName() + " union has incompatible restriction bases"); +// } +// } +// QName base = restriction.getBase(); +// ontType = getL0Type(base); +// } +// writer.println(" --> " + ontType); +// } else { +// throw new RuntimeException(simpleType.getName() + " restriction error"); +// } + } } @Override - protected void handleComplexType(SchemaObject topLevelComplexType) { + public void handleComplexType(SchemaObject topLevelComplexType) { String name = getName(topLevelComplexType); // if (topLevelComplexType.getName().equals("Reference")) // System.out.println(); @@ -440,7 +456,7 @@ public class OntologyGenerator extends SchemaConversionBase { // if (base != null) { // baseType = getType(base); // } - Inheritance inheritance = getInheritance(topLevelComplexType); + Inheritance inheritance = base.getInheritance(topLevelComplexType); // writer.println(name+ " references = getIDReferences(element); + List references = this.base.getIDReferences(element); for (IDReference ref : references) { writer.println(name+"."+ref.getReference().getName()+ "