Search in sources :

Example 11 with SumConstraint

use of easik.model.constraint.SumConstraint in project fql by CategoricalData.

the class EntityNode method getShadowEdges.

/**
 * This method returns the edges that will be "shadowed" in this entity for
 * allowing various types of constraints. The problem arises when we have
 * something like: A -> B -> C, where A is the summand of B, but B has
 * to be specified. In this case, the B to C edge will be returned as a
 * "shadow" edge. We handle this for other constraint types, too. For a
 * good, working, technical example, see the shadowEdges.easik sample
 * sketch.
 *
 * @return a set of edges that will be shadowed by this entity node.
 *
 *         Removing shadow edges completely. Started by Sarah Van der Laan
 *         continued by Federico Mora because a partial solution is worse
 *         than all or nothing
 *
 *         public LinkedHashSet<SketchEdge> getShadowEdges() { return
 *         getShadowEdges(new LinkedHashSet<EntityNode>(5), new
 *         LinkedHashSet<SketchEdge>(5)); }
 */
// Package-only implementation of the above that breaks recursion by
// ignoring
// shadowed nodes that we already know about.
/**
 * @param ignore
 * @param constraintEdges
 *
 * @return
 */
LinkedHashSet<SketchEdge> getShadowEdges(final Collection<EntityNode> ignore, final LinkedHashSet<SketchEdge> constraintEdges) {
    // These are the other entity node that we (potentially) need to shadow:
    final Collection<EntityNode> shadow = new LinkedHashSet<>(10);
    CONSTRAINT: for (final ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> c : getMModel().getConstraints().values()) {
        if (c instanceof SumConstraint) {
            final SumConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> s = (SumConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c;
            for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> path : s.getPaths()) {
                // for this entity, of course):
                if (path.getDomain() == this) {
                    shadow.addAll(path.getEntities());
                    constraintEdges.addAll(path.getEdges());
                    continue CONSTRAINT;
                }
            }
        } else if (c instanceof ProductConstraint) {
            final ProductConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> p = (ProductConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c;
            for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> path : p.getPaths()) {
                // codomains), along each product path
                if (path.getCoDomain() == this) {
                    for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> prodPath : p.getPaths()) {
                        // But we ignore all of the product path edges,
                        // since they will be automatically generated:
                        constraintEdges.addAll(prodPath.getEdges());
                        final Deque<EntityNode> pathNodes = new LinkedList<>();
                        pathNodes.addAll(prodPath.getEntities());
                        pathNodes.removeLast();
                        shadow.addAll(pathNodes);
                    }
                    continue CONSTRAINT;
                }
            }
        } else if (c instanceof EqualizerConstraint) {
            final EqualizerConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> e = (EqualizerConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c;
            // injective *edge* doesn't cause that problem).
            if (e.getSourceEntity() == this) {
                final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> projection = e.getProjection();
                shadow.addAll(projection.getEntities());
                // Ignore the projection edge itself:
                constraintEdges.addAll(projection.getEdges());
                continue CONSTRAINT;
            }
        } else if (c instanceof PullbackConstraint) {
            final PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> pb = (PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c;
            // WPBEDIT CF2012
            for (int i = 0; i < pb.getWidth(); i++) {
                ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> proj = pb.getProjectionPath(i);
                if (this == proj.getCoDomain()) {
                    for (int j = 0; j < pb.getWidth(); j++) {
                        proj = pb.getProjectionPath(j);
                        Deque<EntityNode> projNodes = new LinkedList<>(proj.getEntities());
                        projNodes.removeLast();
                        shadow.addAll(projNodes);
                        constraintEdges.addAll(proj.getEdges());
                    }
                    continue CONSTRAINT;
                }
            }
        } else if (c instanceof LimitConstraint) {
        // TRIANGLES TODO CF2012 incomplete
        }
    }
    final LinkedHashSet<SketchEdge> shadowEdges = new LinkedHashSet<>(20);
    // All of the ignore entities, plus everything we just found should be
    // ignored by any recursion:
    final Collection<EntityNode> toIgnore = new LinkedHashSet<>(3);
    toIgnore.add(this);
    toIgnore.addAll(ignore);
    toIgnore.addAll(shadow);
    for (final EntityNode node : shadow) {
        // it:
        if ((node == this) || ignore.contains(node)) {
            continue;
        }
        // Otherwise, shadow its non-partial edges, and all of its shadow
        // edges:
        shadowEdges.addAll(node.getShadowEdges(toIgnore, constraintEdges));
        shadowEdges.addAll(node.getNonPartialEdges());
        // Remove edges already
        shadowEdges.removeAll(constraintEdges);
    // involved in the sum
    }
    return shadowEdges;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LimitConstraint(easik.model.constraint.LimitConstraint) ModelConstraint(easik.model.constraint.ModelConstraint) 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) SketchFrame(easik.ui.SketchFrame) ProductConstraint(easik.model.constraint.ProductConstraint) SketchEdge(easik.sketch.edge.SketchEdge) ModelPath(easik.model.path.ModelPath) EqualizerConstraint(easik.model.constraint.EqualizerConstraint) Sketch(easik.sketch.Sketch)

Aggregations

ProductConstraint (easik.model.constraint.ProductConstraint)11 SumConstraint (easik.model.constraint.SumConstraint)11 CommutativeDiagram (easik.model.constraint.CommutativeDiagram)8 LimitConstraint (easik.model.constraint.LimitConstraint)8 PullbackConstraint (easik.model.constraint.PullbackConstraint)8 ModelConstraint (easik.model.constraint.ModelConstraint)7 Sketch (easik.sketch.Sketch)7 EqualizerConstraint (easik.model.constraint.EqualizerConstraint)6 SketchEdge (easik.sketch.edge.SketchEdge)6 SketchGraphModel (easik.sketch.util.graph.SketchGraphModel)6 SketchFrame (easik.ui.SketchFrame)6 EntityNode (easik.sketch.vertex.EntityNode)5 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)4 ModelPath (easik.model.path.ModelPath)3 LinkedHashSet (java.util.LinkedHashSet)3 LinkedList (java.util.LinkedList)3 Int (easik.database.types.Int)2 EntityAttribute (easik.model.attribute.EntityAttribute)2 ColumnEntry (easik.ui.datamanip.ColumnEntry)2 Component (java.awt.Component)2