Search in sources :

Example 1 with XSDChoiceCompositor

use of easik.xml.xsd.nodes.elements.XSDChoiceCompositor in project fql by CategoricalData.

the class XSDExporter method createConstraint.

/**
 * Add an annotation explaining the sum constraint and effect the isAs.
 * <p/>
 * Today, this has two parts. First the annotation is truly just
 * documentation in this case. The second part of the constraint creation,
 * where the isA parts are added as elements of the type suffices to explain
 * everything that is needed. In the standard sum constraint in
 * constraints.easik gives this annotation:
 *
 * <pre>
 * &lt;xs:annotation>
 *  &lt;xs:documentation>
 *     Sum is a disjoint generalization of Summand1 and Summand2 and Summand3
 *   &lt;/xs:documentation>
 * &lt;/xs:annotation>
 * </pre>
 * <p/>
 * Each of the Summand entities have an "isA" relationship to the Sum entity
 * at the start of the diagram, so this is reflected in the typing. First,
 * an element of each Summand type is added to the target of the "isA"
 * relationship. Second, since there is no need for a separate element in
 * the schema for the Summand entity, its element is removed from the sketch
 * entity.
 *
 * @param sum
 *            the summand diagram constraint.
 * @todo Why not do this with standard isA relationships as well?
 */
private static void createConstraint(final SumConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> sum) {
    final Map<String, StringBuilder> codMap = new HashMap<>(10);
    final Map<String, XSDType> codToXMLType = new HashMap<>(10);
    XSDChoiceCompositor summands = new XSDChoiceCompositor(new ArrayList<XSDAbstractElement>(sum.getPaths().size()));
    String cdTypeName = "";
    XSDComplexType lastCodomainType = null;
    for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> path : sum.getPaths()) {
        final EntityNode coDomain = path.getCoDomain();
        final EntityNode dom = path.getDomain();
        final String coDomainName = coDomain.getName();
        final StringBuilder coDomainAnnotation;
        if (codMap.containsKey(coDomainName)) {
            coDomainAnnotation = codMap.get(coDomainName);
            coDomainAnnotation.append(" and ").append(dom.getName());
        } else {
            coDomainAnnotation = new StringBuilder(100);
            coDomainAnnotation.append(coDomain.getName()).append(" is a disjoint generalization of ").append(dom.getName());
        }
        final XSDComplexType codomainType = (XSDComplexType) coDomain.getXsdType();
        if ((null != lastCodomainType) && !cdTypeName.equals(codomainType.getName())) {
            if (!summands.isEmpty()) {
                lastCodomainType.addAtom(summands);
                summands = new XSDChoiceCompositor(new ArrayList<XSDAbstractElement>(sum.getPaths().size()));
            }
        }
        lastCodomainType = codomainType;
        cdTypeName = codomainType.getName();
        codToXMLType.put(coDomainName, codomainType);
        codMap.put(coDomainName, coDomainAnnotation);
        final XSDElement element = dom.getXsdElement();
        element.setParent(coDomain.getXsdElement());
        summands.addSubElement(element);
        dom.setXsdElement(null);
    }
    if (null != lastCodomainType) {
        if (!summands.isEmpty()) {
            lastCodomainType.addAtom(summands);
            summands = null;
        }
    }
    for (final Map.Entry<String, StringBuilder> entry : codMap.entrySet()) {
        final XSDType typ = codToXMLType.get(entry.getKey());
        typ.addAnnotation(new XSDAnnotation(entry.getValue().toString()));
    }
}
Also used : HashMap(java.util.HashMap) XSDAbstractElement(easik.xml.xsd.nodes.elements.XSDAbstractElement) ArrayList(java.util.ArrayList) XSDElement(easik.xml.xsd.nodes.elements.XSDElement) XSDType(easik.xml.xsd.nodes.types.XSDType) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) SketchFrame(easik.ui.SketchFrame) SketchEdge(easik.sketch.edge.SketchEdge) Sketch(easik.sketch.Sketch) XSDChoiceCompositor(easik.xml.xsd.nodes.elements.XSDChoiceCompositor) XSDAnnotation(easik.xml.xsd.nodes.XSDAnnotation) HashMap(java.util.HashMap) Map(java.util.Map) XSDComplexType(easik.xml.xsd.nodes.types.XSDComplexType)

Aggregations

Sketch (easik.sketch.Sketch)1 SketchEdge (easik.sketch.edge.SketchEdge)1 SketchGraphModel (easik.sketch.util.graph.SketchGraphModel)1 EntityNode (easik.sketch.vertex.EntityNode)1 SketchFrame (easik.ui.SketchFrame)1 XSDAnnotation (easik.xml.xsd.nodes.XSDAnnotation)1 XSDAbstractElement (easik.xml.xsd.nodes.elements.XSDAbstractElement)1 XSDChoiceCompositor (easik.xml.xsd.nodes.elements.XSDChoiceCompositor)1 XSDElement (easik.xml.xsd.nodes.elements.XSDElement)1 XSDComplexType (easik.xml.xsd.nodes.types.XSDComplexType)1 XSDType (easik.xml.xsd.nodes.types.XSDType)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1