X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FOntologyGenerator.java;h=3aea73436367a3ab596f94a5e9854f83c3bb8dc5;hb=HEAD;hp=741e197f0e2ff50bf9bb6b41c619fe94fd78471e;hpb=09fb8eafbc9046ead17f0529eebad0b0caf55e2b;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..3aea734 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 @@ -1,611 +1,640 @@ -package org.simantics.xml.sax; - -import java.io.FileNotFoundException; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.ArrayList; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; - -import javax.xml.namespace.QName; - -import org.simantics.utils.datastructures.BijectionMap; -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; -import org.w3._2001.xmlschema.AttributeGroup; -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.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 OntologyGenerator(Configuration configuration) { - super(configuration); - } - - String ontRoot = "ONT."; - String commentTag = "//"; - - Schema schema; - String ontologyUri; - String className; - - SchemaConverter converter; - - 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; -// } -// } -// } -// } - - - StringWriter stringWriter = null; - if (converter.getOntologyFile() == null) { - stringWriter = new StringWriter(); - writer = new PrintWriter(stringWriter); - } else { - writer = new PrintWriter(converter.getOntologyFile()); - } - - handle(schema); - - writer.flush(); - writer.close(); - if (stringWriter != null) - 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(); - - for (String s : converter.getHeader()) { - writer.println(commentTag + " " + s); - } - writer.println(); - writer.println("L0 = "); - writer.println("XML = "); - writer.println(); - writer.println(ontRoot + " = <" + ontologyUri +"> : L0.Ontology"); - writer.println(" @L0.new"); - writer.println(" L0.HasResourceClass \"" + className +"\""); - writer.println(); - writer.println(); - - ontRoot += "."; - writer.println(ontRoot+"ComplexTypes : L0.Library"); - writer.println(ontRoot+"AttributeGroups : L0.Library"); - writer.println(); - writer.println(commentTag + " Interpreted from schema"); - writer.println(); - - super.handle(schema); - } - - protected String getType(QName qtype) { - String ontType = getL0TypeFromPrimitiveType(qtype); - if (ontType != null) - return ontType; - else if (isComplexTypeRef(qtype.getLocalPart())) - return ontRoot+getComplexTypePrefix()+qtype.getLocalPart(); - else if (isSimpleTypeRef(qtype.getLocalPart())) - return ontRoot+qtype.getLocalPart(); - else if (isElementRef(qtype.getLocalPart())) - return ontRoot+qtype.getLocalPart(); - else if (qtype.getPrefix() != null && qtype.getPrefix().length() > 0) { - return ontRoot+qtype.getPrefix()+qtype.getLocalPart(); - } - 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."; - } - - public String getAttributeGroupPrefix() { - 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; - } - } - 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; - - 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 (types.size() > 0) { - for (String type : types) { - writer.print(" " + type); - } - } - if (!single) { - writer.println(ontRoot+name+ "List " + ontRoot+name+"List"); - } - } - - } - - - - @Override - protected 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()); - if (refName == null) - refName = eObj.getName(); - } else { - referenceType = element.getElement().getRef(); - if (refName == null) - refName = referenceType.getLocalPart(); - } - String type = getL0TypeFromPrimitiveType(referenceType); - SchemaObject obj = null; - if (type == null) { - obj = getWithName(parent, referenceType.getLocalPart()); - - writer.println(getName(parent)+".has"+refName + " " + getName(obj)); - } else { - writer.println(getName(parent)+".has"+refName + " " + getType(referenceType)); - } - - if (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()))) { - writer.println(getName(parent)+"."+refName + "List element = (JAXBElement)facetWrap; -// QName elementName = element.getName(); -// Facet facet = (Facet)element.getValue(); -// } - } - - - - writer.println(relationName+ " " + ontType); - } else if (primitiveType != null) { - writer.println(" attributes) { - Attribute compositionAttribute = new Attribute(); - compositionAttribute.setName(composition.getName()); - QName type = new QName(CONVERSION_NS, composition.getType()); - compositionAttribute.setType(type); - handle(parent, compositionAttribute); - } - - @Override - protected void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { - SimpleType simpleType = simpleTypeObj.getSimpleType(); - String name = simpleType.getName(); - - org.w3._2001.xmlschema.List list = simpleType.getList(); - if (list != null) { - // TODO : process restriction in lists - String relationName = ontRoot+"has"+name; - if (parent != null) - 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); - } - } - - @Override - protected void handleComplexType(SchemaObject topLevelComplexType) { - String name = getName(topLevelComplexType); -// if (topLevelComplexType.getName().equals("Reference")) -// System.out.println(); - - String relationName = getName(topLevelComplexType,"has");//ontRoot+"has"+name; - - writer.println(relationName+ " " + ontRoot+getComplexTypePrefix()+name); - writer.println(" --> " + name); - writer.println(); -// String baseType = "XML.ComplexType"; -// -// QName base = getComplexTypeBase(topLevelComplexType.getComplexType()); -// if (base != null) { -// baseType = getType(base); -// } -// base = getSimpleTypeBase(topLevelComplexType.getSimpleType()); -// if (base != null) { -// baseType = getType(base); -// } - Inheritance inheritance = getInheritance(topLevelComplexType); - -// writer.println(name+ " types = new LinkedHashSet(); - if (element.getType() != null) { - types.add(getType(element.getType())); - } - QName base = getElementBase(element); - if (base != null) { - if (base.getNamespaceURI().equals(SCHEMA_NS)) { - String l0Type = getL0Type(base); - if (l0Type == null) - throw new RuntimeException("Cannot get L0 type for " + base.getLocalPart()); - types.add(l0Type); - } else if (isElementRef(base.getLocalPart())) - types.add(ontRoot+base.getLocalPart()); - else - types.add(ontRoot+getComplexTypePrefix()+base.getLocalPart()); - } - QName substitution = element.getSubstitutionGroup(); - if (substitution != null) { - if (isElementRef(substitution.getLocalPart())) - types.add(ontRoot+substitution.getLocalPart()); - else - types.add( ontRoot+getComplexTypePrefix()+substitution.getLocalPart()); - } - for (String t : types) { - type += " references = getIDReferences(element); - - for (IDReference ref : references) { - writer.println(name+"."+ref.getReference().getName()+ " "); + writer.println("XML = "); + writer.println(); + if (converter.isPrimary()) { + writer.println(ontRoot + " = <" + ontologyUri +"> : L0.Ontology"); + writer.println(" @L0.new"); + writer.println(" L0.HasResourceClass \"" + className +"\""); + } else { + writer.println(ontRoot + " = <" + ontologyUri +">"); + } + writer.println(); + writer.println(); + + Set children = new HashSet<>(); + Deque stack = new ArrayDeque<>(); + stack.addAll(converter.getSubConverters()); + while (!stack.isEmpty()) { + SchemaConverter sc = stack.pop(); + if (children.contains(sc)) + continue; + children.add(sc); + stack.addAll(sc.getSubConverters()); + } + children.remove(converter); + for (SchemaConverter sc : children) { + writer.println(sc.shortName + " = <" + sc.ontologyUri +">"); + } + + writer.println(); + + ontRoot += "."; + writer.println(ontRoot+"SimpleTypes : L0.Library"); + writer.println(ontRoot+"ComplexTypes : L0.Library"); + writer.println(ontRoot+"AttributeGroups : L0.Library"); + writer.println(); + writer.println(commentTag + " Interpreted from schema"); + writer.println(); + + + + base.handle(this); + } + + protected String getType(QName qtype, String rel) { + String ontType = base.getL0TypeFromPrimitiveType(qtype); + if (ontType != null) + return ontType; + else { + if (base.isComplexTypeRef(qtype.getLocalPart())) + return getName(base.getComplexType(qtype), rel); + else if (base.isSimpleTypeRef(qtype.getLocalPart())) + return getName(base.getSimpleType(qtype), rel); + else if (base.isElementRef(qtype.getLocalPart())) + return getName(base.getElement(qtype), rel); + } + throw new RuntimeException("Reference to unknown type " + qtype.getLocalPart()); + } + + public String getSimpleTypePrefix() { + return "SimpleTypes."; + } + + public String getComplexTypePrefix() { + return "ComplexTypes."; + } + + public String getAttributeGroupPrefix() { + return "AttributeGroups."; + } + + public String 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); + writer.print(" " + elementListType); + } + + return relationName; + }; + + + + @Override + public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType, String baseRelationName) { + if (refType != RefType.Element) { + QName referenceType = null; + if (refType == RefType.Type) { + referenceType = element.getElement().getType(); + //refName = element.getElement().getName() + SchemaObject eObj = base.getElement(element.getElement());//base.elements.get(element.getElement()); + if (refName == null) + refName = eObj.getName(); + } else { + referenceType = element.getElement().getRef(); + if (refName == null) + refName = base.getName(referenceType); + } + String type = base.getL0TypeFromPrimitiveType(referenceType); + SchemaObject obj = null; + if (type == null) { + obj = base.getWithName(referenceType); + writer.print(getName(parent)+".has"+refName + " " + getType(referenceType, "")); + } + + if (base.useElementList(parent, indicator,element, refType == RefType.Reference, refName, referenceType)) { + + if (type == null) { + writer.println(getName(parent)+"."+refName + "List " + getName(obj)); + if (base.useElementList(parent, indicator,element, false, refName, new QName(obj.getName()))) { + writer.println(getName(parent)+"."+refName + "_List element = (JAXBElement)facetWrap; +// QName elementName = element.getName(); +// Facet facet = (Facet)element.getValue(); +// } + } + + + + writer.println(relationName+ " " + ontType); + } else if (primitiveType != null) { + writer.println(" attributes) { + Attribute compositionAttribute = new Attribute(); + compositionAttribute.setName(composition.getName()); + QName type = new QName(SchemaConversionBase.CONVERSION_NS, composition.getType()); + compositionAttribute.setType(type); + handle(parent, compositionAttribute); + } + + @Override + public void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) { + SimpleType simpleType = simpleTypeObj.getSimpleType(); + String name = simpleType.getName(); + + org.w3._2001.xmlschema.List list = simpleType.getList(); + if (list != null) { + // TODO : process restriction in lists + String relationName = getName(simpleTypeObj, "has"); + writer.println(relationName+ " " + ontType); + } else { + QName base = this.base.getSimpleTypeBase(simpleType); + writer.println(getName(simpleTypeObj) + " " + getName(simpleTypeObj)); + +// 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 + public void handleComplexType(SchemaObject topLevelComplexType) { + String name = getName(topLevelComplexType); +// if (topLevelComplexType.getName().equals("Reference")) +// System.out.println(); + +// String baseType = "XML.ComplexType"; +// +// QName base = getComplexTypeBase(topLevelComplexType.getComplexType()); +// if (base != null) { +// baseType = getType(base); +// } +// base = getSimpleTypeBase(topLevelComplexType.getSimpleType()); +// if (base != null) { +// baseType = getType(base); +// } + Inheritance inheritance = base.getInheritance(topLevelComplexType); + +// writer.println(name+ " " + name); + writer.println(relationName+ "_List " + name + "_List"); + writer.println(); + + // Attributes +// if (!baseType.equals(inheritance.baseClass)) +// System.out.println(); + //super.handleComplexType(topLevelComplexType); + base.handleComplexTypeAttributes(topLevelComplexType); + base.handleComplexTypeExtension(topLevelComplexType); + base.handleExtensionAttributes(topLevelComplexType); + writer.println(); + } + + @Override + public void handleElement(SchemaObject elementObj) { + Element element = elementObj.getElement(); + String name = getName(elementObj);//element.getName(); + + if (name.contains("Canvas")) + System.out.println(); + + String type = "XML.Element"; + Set types = new LinkedHashSet(); + if (element.getType() != null) { + types.add(getType(element.getType(), "")); + } + QName base = this.base.getElementBase(element); + if (base != null) { + if (base.getNamespaceURI().equals(SchemaConversionBase.SCHEMA_NS)) { + String l0Type = this.base.getL0Type(base); + if (l0Type == null) + throw new RuntimeException("Cannot get L0 type for " + base.getLocalPart()); + types.add(l0Type); + } else if (this.base.isElementRef(base.getLocalPart())) + types.add(ontRoot+this.base.getName(base)); + else + types.add(ontRoot+getComplexTypePrefix()+this.base.getName(base)); + } + QName substitution = element.getSubstitutionGroup(); + if (substitution != null) { + if (this.base.isElementRef(substitution.getLocalPart())) + types.add(ontRoot+this.base.getName(substitution)); + else + types.add( ontRoot+getComplexTypePrefix()+this.base.getName(substitution)); + } + for (String t : types) { + type += " references = this.base.getIDReferences(element); + + for (IDReference ref : references) { + writer.println(name+"."+ref.getReference().getName()+ "