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>
* <xs:annotation>
* <xs:documentation>
* ForAll.elem1 in (P1), ForAll.elem2 in (P2)
* Exists.p=(elem1,elem2) in Product
* </xs:documentation>
* </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));
}
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();
}
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
* -> b -> c -> 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();
}
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;
}
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));
}
}
Aggregations