Search in sources :

Example 31 with SketchEdge

use of easik.sketch.edge.SketchEdge in project fql by CategoricalData.

the class XSDExporter method createConstraint.

/**
 * Add an annotation explaining the product.
 * <p/>
 * Today, this is simply done by creating an annotation. For example in the
 * standard product constraint in constraints.easik gives this annotation:
 *
 * <pre>
 * &lt;xs:annotation>
 *  &lt;xs:documentation>
 *     ForAll.elem1 in (P1), ForAll.elem2 in (P2)
 *     Exists.p=(elem1,elem2) in Product
 *   &lt;/xs:documentation>
 * &lt;/xs:annotation>
 * </pre>
 *
 * @param prod
 *            the product diagram constraint.
 */
private void createConstraint(final ProductConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> prod) {
    final List<ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> paths = prod.getPaths();
    final EntityNode dom = paths.get(0).getDomain();
    final XSDType domType = dom.getXsdType();
    final List<String> elts = new ArrayList<>(paths.size());
    int id = 0;
    @SuppressWarnings("unused") final String keyrefName = Easik.getInstance().getSettings().getProperty("xml_keyref_name");
    final List<String> values = new ArrayList<>(paths.size());
    for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> path : paths) {
        id++;
        final LinkedList<SketchEdge> tmpPath = new LinkedList<>(path.getEdges());
        tmpPath.removeFirst();
        final String elem = "elem" + id;
        elts.add(elem);
        if (tmpPath.size() == 0) {
            values.add("ForAll." + elem + " in (" + path.getCoDomain().getName() + ')');
        } else {
            values.add("ForAll." + elem + " in " + xmlJoinPath(tmpPath, true));
        }
    }
    final String documentation = EasikTools.join(", ", values);
    final String elements = "Exists.p=(" + EasikTools.join(",", elts) + ") in " + dom.getName();
    domType.addAnnotation(new XSDAnnotation(documentation + lineSep + elements));
}
Also used : ArrayList(java.util.ArrayList) LimitConstraint(easik.model.constraint.LimitConstraint) EqualizerConstraint(easik.model.constraint.EqualizerConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) SumConstraint(easik.model.constraint.SumConstraint) PullbackConstraint(easik.model.constraint.PullbackConstraint) ModelConstraint(easik.model.constraint.ModelConstraint) LinkedList(java.util.LinkedList) EntityNode(easik.sketch.vertex.EntityNode) XSDType(easik.xml.xsd.nodes.types.XSDType) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) SketchFrame(easik.ui.SketchFrame) SketchEdge(easik.sketch.edge.SketchEdge) ModelPath(easik.model.path.ModelPath) Sketch(easik.sketch.Sketch) XSDAnnotation(easik.xml.xsd.nodes.XSDAnnotation)

Example 32 with SketchEdge

use of easik.sketch.edge.SketchEdge in project fql by CategoricalData.

the class XSDExporter method xmlJoinPath.

/**
 * protected method to assist in building "path" strings for the constraint
 * annotations
 *
 * @param inEdges
 *            List of sketchedges
 * @param includeLast
 *            Include the last or not
 * @return path like string
 */
protected String xmlJoinPath(final List<SketchEdge> inEdges, final boolean includeLast) {
    final LinkedList<SketchEdge> edges = new LinkedList<>(inEdges);
    final StringBuilder joinClause = new StringBuilder("; ");
    joinClause.append(quoteId(edges.get(0).getSourceEntity().getName()));
    if (!includeLast && !edges.isEmpty()) {
        edges.removeLast();
    }
    for (final SketchEdge e : edges) {
        final EntityNode target = e.getTargetEntity();
        joinClause.append('(').append(e.getName()).append(") ;").append(target.getName());
    }
    return joinClause.toString();
}
Also used : SketchEdge(easik.sketch.edge.SketchEdge) LinkedList(java.util.LinkedList) EntityNode(easik.sketch.vertex.EntityNode)

Example 33 with SketchEdge

use of easik.sketch.edge.SketchEdge in project fql by CategoricalData.

the class JDBCExporter method joinPath.

/**
 * Constructs a join clause for this path. For instance, if the path is a
 * -&gt; b -&gt; c -&gt; d, this would return
 * <code>a JOIN b ON b.a_id = a.id JOIN c ON c.b_id = b.id JOIN d ON d.c_id = c.id</code>
 * suitable for use in a select query.
 *
 * @param inEdges
 *            the path for which the selection is being made
 * @param includeLast
 *            true if the last path target should be included. If false, the
 *            last path will be omitted.
 * @param initialOn
 *            if non-null, "ON " + initialOn will be added after the path
 *            source table name
 * @return the string, suitable for use in a SELECT query.
 */
protected String joinPath(final List<SketchEdge> inEdges, final boolean includeLast, final String initialOn) {
    final LinkedList<SketchEdge> edges = new LinkedList<>(inEdges);
    final StringBuilder joinClause = new StringBuilder(quoteId(edges.get(0).getSourceEntity().getName()));
    if (initialOn != null) {
        joinClause.append(" ON ").append(initialOn);
    }
    if (!includeLast && !edges.isEmpty()) {
        edges.removeLast();
    }
    for (final SketchEdge e : edges) {
        final EntityNode target = e.getTargetEntity();
        joinClause.append(" JOIN ").append(quoteId(target)).append(" ON ").append(qualifiedFK(e)).append(" = ").append(qualifiedPK(target));
    }
    return joinClause.toString();
}
Also used : SketchEdge(easik.sketch.edge.SketchEdge) LinkedList(java.util.LinkedList) EntityNode(easik.sketch.vertex.EntityNode)

Example 34 with SketchEdge

use of easik.sketch.edge.SketchEdge in project fql by CategoricalData.

the class JDBCExporter method createConstraints.

/**
 * Generates db constraints from the sketch constraints. This method is not
 * generally overridden by drivers: the individual createConstraint(...)
 * methods are called for each existing constraint.
 *
 * @return list of queries to create the constraints.
 */
protected List<String> createConstraints() {
    final List<String> constraintSQL = new LinkedList<>();
    final List<ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> constraints = new ArrayList<>(sketch.getConstraints().values());
    int id = 0;
    for (final ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> c : constraints) {
        id++;
        if (c instanceof CommutativeDiagram) {
            constraintSQL.addAll(createConstraint((CommutativeDiagram<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
        } else if (c instanceof ProductConstraint) {
            constraintSQL.addAll(createConstraint((ProductConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
        } else if (c instanceof PullbackConstraint) {
            constraintSQL.addAll(createConstraint((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
        } else if (c instanceof EqualizerConstraint) {
            constraintSQL.addAll(createConstraint((EqualizerConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
        } else if (c instanceof SumConstraint) {
            constraintSQL.addAll(createConstraint((SumConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
        } else if (c instanceof LimitConstraint) {
            constraintSQL.addAll(createConstraint((LimitConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
        } else {
            System.err.println("Unknown constraint type encountered: " + c.getClass());
        }
    }
    return constraintSQL;
}
Also used : LimitConstraint(easik.model.constraint.LimitConstraint) ModelConstraint(easik.model.constraint.ModelConstraint) ArrayList(java.util.ArrayList) PullbackConstraint(easik.model.constraint.PullbackConstraint) SumConstraint(easik.model.constraint.SumConstraint) LinkedList(java.util.LinkedList) LimitConstraint(easik.model.constraint.LimitConstraint) EqualizerConstraint(easik.model.constraint.EqualizerConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) SumConstraint(easik.model.constraint.SumConstraint) PullbackConstraint(easik.model.constraint.PullbackConstraint) ModelConstraint(easik.model.constraint.ModelConstraint) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) SketchFrame(easik.ui.SketchFrame) ProductConstraint(easik.model.constraint.ProductConstraint) SketchEdge(easik.sketch.edge.SketchEdge) EqualizerConstraint(easik.model.constraint.EqualizerConstraint) Sketch(easik.sketch.Sketch) CommutativeDiagram(easik.model.constraint.CommutativeDiagram)

Example 35 with SketchEdge

use of easik.sketch.edge.SketchEdge in project fql by CategoricalData.

the class JDBCExporter method createTables.

/**
 * Recursive method to create table definitions. Each iteration exhaustively
 * determines and generates all unconnected EntityNodes, then, if any
 * EntityNodes remain, arbitrarily removes one (deferring creation of its
 * references), and recurses, until all tables have been created.
 *
 * @param tables
 *            the sql for the tables
 * @param entities
 *            sketch entities to create
 * @param created
 *            sketch entites already created
 */
private void createTables(final List<String> tables, final Set<EntityNode> entities, final Set<EntityNode> created) {
    NON_EMPTY: while (!entities.isEmpty()) {
        // entities
        ENTITIES: for (final EntityNode n : entities) {
            for (final SketchEdge e : n.getOutgoingEdges()) {
                final EntityNode target = e.getTargetEntity();
                // vertex (for now)
                if (!created.contains(target)) {
                    continue ENTITIES;
                }
            }
            // Also check shadow edges:
            /**
             * Removing all traces of shadow edges
             *
             * for (final SketchEdge e : n.getShadowEdges()) { final
             * EntityNode target = e.getTargetEntity();
             *
             * // if an EntityNode target isn't already created, skip this
             * vertex (for now) if (!created.contains(target)) { continue
             * ENTITIES; } }
             */
            // If we got here, all the entity's targets exist, so we can go
            // ahead
            // with the table creation.
            tables.addAll(createTable(n, true));
            entities.remove(n);
            created.add(n);
            // Restart the non-empty loop, since we've
            continue NON_EMPTY;
        // changed entities.
        }
        // or (even simpler), a self-reference (A -> A).
        break;
    }
    if (!entities.isEmpty()) {
        final EntityNode someNode = entities.iterator().next();
        tables.addAll(createTable(someNode, false));
        entities.remove(someNode);
        created.add(someNode);
        // Recurse, creating the rest of the tables
        createTables(tables, entities, created);
        // When the remaining tables are created, we are able to create the
        // reference:
        tables.addAll(createReferences(someNode));
    }
}
Also used : SketchEdge(easik.sketch.edge.SketchEdge) EntityNode(easik.sketch.vertex.EntityNode)

Aggregations

SketchEdge (easik.sketch.edge.SketchEdge)51 EntityNode (easik.sketch.vertex.EntityNode)45 Sketch (easik.sketch.Sketch)31 SketchGraphModel (easik.sketch.util.graph.SketchGraphModel)31 SketchFrame (easik.ui.SketchFrame)30 LinkedList (java.util.LinkedList)22 LimitConstraint (easik.model.constraint.LimitConstraint)17 ProductConstraint (easik.model.constraint.ProductConstraint)16 PullbackConstraint (easik.model.constraint.PullbackConstraint)16 SumConstraint (easik.model.constraint.SumConstraint)16 EqualizerConstraint (easik.model.constraint.EqualizerConstraint)13 ModelConstraint (easik.model.constraint.ModelConstraint)13 ArrayList (java.util.ArrayList)11 ModelPath (easik.model.path.ModelPath)9 LinkedHashSet (java.util.LinkedHashSet)9 InjectiveEdge (easik.sketch.edge.InjectiveEdge)7 UniqueIndexable (easik.model.keys.UniqueIndexable)5 XSDAnnotation (easik.xml.xsd.nodes.XSDAnnotation)5 XSDType (easik.xml.xsd.nodes.types.XSDType)5 EasikType (easik.database.types.EasikType)4