use of catdata.Ctx in project fql by CategoricalData.
the class AqlViewer method viewDP.
/*public static <Ty, En, Sym, Fk, Att> JComponent viewSchema2(Schema<Ty, En, Sym, Fk, Att> schema) {
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
Ctx<Chc<En,Ty>, Object> nodes = new Ctx<>();
for (En en : schema.ens) {
Object v1 = graph.insertVertex(parent, null, en.toString(), 20, 20, 80, 30);
nodes.put(Chc.inLeft(en), v1);
}
for (Ty ty : schema.typeSide.tys) {
Object v1 = graph.insertVertex(parent, null, ty.toString(), 20, 20, 80, 30);
nodes.put(Chc.inRight(ty), v1);
}
for (Att att : schema.atts.keySet()) {
graph.insertEdge(parent, null, att.toString(), nodes.get(Chc.inLeft(schema.atts.get(att).first)), nodes.get(Chc.inRight(schema.atts.get(att).second)));
}
for (Fk fk : schema.fks.keySet()) {
graph.insertEdge(parent, null, fk.toString(), nodes.get(Chc.inLeft(schema.fks.get(fk).first)), nodes.get(Chc.inLeft(schema.fks.get(fk).second)));
}
mxGraphLayout layout = new mxOrganicLayout(graph); // or whatever layouting algorithm
layout.execute(parent);
mxGraphComponent graphComponent = new mxGraphComponent(graph);
// Map<String, Object> style = graph.getStylesheet().getDefaultEdgeStyle();
// style.put(mxConstants.STYLE_ROUNDED, true);
// style.put(mxConstants.STYLE_EDGE, mxConstants.EDGESTYLE_ENTITY_RELATION);
return graphComponent;
} */
private static <Ty, En, Sym, Fk, Att, Gen, Sk> JComponent viewDP(DP<Ty, En, Sym, Fk, Att, Gen, Sk> dp, Collage col, AqlJs js) {
CodeTextPanel input = new CodeTextPanel("Input (either equation-in-ctx or term-in-ctx)", "");
CodeTextPanel output = new CodeTextPanel("Output", "");
JButton eq = new JButton("Decide Equation-in-ctx");
JButton nf = new JButton("Normalize Term-in-ctx");
/*
* if (!dp.hasNFs()) { nf.setEnabled(false); }
*/
JButton print = new JButton("Show Info");
JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
buttonPanel.add(eq);
buttonPanel.add(nf);
buttonPanel.add(print);
// TODO: aql does not position correctly
Split split = new Split(.5, JSplitPane.VERTICAL_SPLIT);
split.add(input);
split.add(output);
JPanel main = new JPanel(new BorderLayout());
main.add(split, BorderLayout.CENTER);
main.add(buttonPanel, BorderLayout.NORTH);
print.addActionListener(x -> output.setText(dp.toStringProver()));
eq.addActionListener(x -> {
try {
Triple<List<Pair<String, String>>, RawTerm, RawTerm> y = AqlParser.getParser().parseEq(input.getText());
Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> z = RawTerm.infer2(y.first, y.second, y.third, col, js);
boolean isEq = dp.eq(z.first, z.second, z.third);
output.setText(Boolean.toString(isEq));
} catch (Exception ex) {
ex.printStackTrace();
output.setText(ex.getMessage());
}
});
nf.addActionListener(x -> {
try {
Pair<List<Pair<String, String>>, RawTerm> y = AqlParser.getParser().parseTermInCtx(input.getText());
Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> z = RawTerm.infer2(y.first, y.second, y.second, col, js);
Term<Ty, En, Sym, Fk, Att, Gen, Sk> w = dp.nf(z.first, z.second);
output.setText(w.toString());
} catch (Exception ex) {
ex.printStackTrace();
output.setText(ex.getMessage());
}
});
return main;
}
use of catdata.Ctx in project fql by CategoricalData.
the class AqlViewer method viewMorphism.
private static <Ty, En1, Sym, Fk1, Att1, Gen1, Sk1, En2, Fk2, Att2, Gen2, Sk2> JComponent viewMorphism(Morphism m, AqlJs js) {
CodeTextPanel input = new CodeTextPanel("Input term-in-ctx", "");
CodeTextPanel output = new CodeTextPanel("Output term-in-ctx", "");
JButton nf = new JButton("Translate");
JPanel buttonPanel = new JPanel(new GridLayout(1, 1));
buttonPanel.add(nf);
// TODO: aql does not position correctly
Split split = new Split(.5, JSplitPane.VERTICAL_SPLIT);
split.add(input);
split.add(output);
JPanel main = new JPanel(new BorderLayout());
main.add(split, BorderLayout.CENTER);
main.add(buttonPanel, BorderLayout.NORTH);
nf.addActionListener(x -> {
try {
Pair<List<Pair<String, String>>, RawTerm> y = AqlParser.getParser().parseTermInCtx(input.getText());
Triple<Ctx<Var, Chc<Ty, En1>>, Term<Ty, En1, Sym, Fk1, Att1, Gen1, Sk1>, Term<Ty, En1, Sym, Fk1, Att1, Gen1, Sk1>> z = RawTerm.infer2(y.first, y.second, y.second, m.src(), js);
Pair<Ctx<Var, Chc<Ty, En2>>, Term<Ty, En2, Sym, Fk2, Att2, Gen2, Sk2>> a = m.translate(z.first, z.second);
output.setText(a.first.toString() + a.second);
} catch (Exception ex) {
ex.printStackTrace();
output.setText(ex.getMessage());
}
});
return main;
}
use of catdata.Ctx in project fql by CategoricalData.
the class InstExpJdbcQuotient method eval.
@Override
public Instance<Ty, En, Sym, Fk, Att, Gen, Sk, ID, Chc<Sk, Pair<ID, Att>>> eval(AqlEnv env) {
Instance<Ty, En, Sym, Fk, Att, Gen, Sk, X, Y> J = I.eval(env);
Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col = new Collage<>(J.collage());
AqlOptions strat = new AqlOptions(options, col, env.defaults);
String toGet = jdbcString;
String driver = clazz;
if (clazz.trim().isEmpty()) {
driver = (String) strat.getOrDefault(AqlOption.jdbc_default_class);
Util.checkClass(driver);
}
if (jdbcString.trim().isEmpty()) {
toGet = (String) strat.getOrDefault(AqlOption.jdbc_default_string);
}
Set<Pair<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>>> eqs0 = new HashSet<>(J.eqs());
try (Connection conn = DriverManager.getConnection(toGet)) {
for (Entry<LocStr, String> q : queries.entrySet()) {
if (!J.schema().ens.contains(new En(q.getKey().str))) {
throw new LocException(q.getKey().loc, "Not an entity: " + q.getKey().str + ", expected one of " + J.schema().ens);
}
Statement stmt = conn.createStatement();
stmt.execute(q.getValue());
ResultSet rs = stmt.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
if (columnsNumber != 2) {
stmt.close();
rs.close();
throw new LocException(q.getKey().loc, "Expected 2 columns but received " + columnsNumber);
}
while (rs.next()) {
// input instance need not be jdbc, so dont call toGen from import here
// (Gen) ;
Gen gen1 = new Gen(rs.getObject(1).toString());
// (Gen) rs.getObject(2).toString();
Gen gen2 = new Gen(rs.getObject(2).toString());
if (gen1 == null || gen2 == null) {
stmt.close();
rs.close();
throw new LocException(q.getKey().loc, "Encountered a NULL generator");
} else if (!J.gens().containsKey(gen1)) {
throw new LocException(q.getKey().loc, "Cannot import record linkage: " + gen1 + " is not a generator in the input instance");
} else if (!J.gens().containsKey(gen2)) {
throw new LocException(q.getKey().loc, "Cannot import record linkage: " + gen2 + " is not a generator in the input instance");
}
Term<Ty, En, Sym, Fk, Att, Gen, Sk> l = Term.Gen(gen1);
Term<Ty, En, Sym, Fk, Att, Gen, Sk> r = Term.Gen(gen2);
eqs0.add(new Pair<>(l, r));
col.eqs.add(new Eq<>(new Ctx<>(), l, r));
}
stmt.close();
rs.close();
}
// } catch (SQLException exn) {
// exn.printStackTrace();
// throw new RuntimeException(/*"JDBC exception: " + */ exn /*.getMessage() */);
} catch (Throwable thr) {
// thr.printStackTrace();
throw new RuntimeException(thr);
}
InitialAlgebra<Ty, En, Sym, Fk, Att, Gen, Sk, ID> initial0 = new InitialAlgebra<>(strat, J.schema(), col, new It(), Object::toString, Object::toString);
return new LiteralInstance<>(J.schema(), col.gens.map, col.sks.map, eqs0, initial0.dp(), initial0, (Boolean) strat.getOrDefault(AqlOption.require_consistency), (Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe));
}
use of catdata.Ctx in project fql by CategoricalData.
the class InstExpQuotient method eval.
@Override
public Instance<Ty, En, Sym, Fk, Att, Gen, Sk, ID, Chc<Sk, Pair<ID, Att>>> eval(AqlEnv env) {
Instance<Ty, En, Sym, Fk, Att, Gen, Sk, X, Y> J = I.eval(env);
Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col = new Collage<>(J.collage());
Set<Pair<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>>> eqs0 = new HashSet<>();
for (Pair<RawTerm, RawTerm> eq : eqs) {
try {
Map<String, Chc<Ty, En>> ctx = Collections.emptyMap();
Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> eq0 = RawTerm.infer1x(ctx, eq.first, eq.second, null, col, "", J.schema().typeSide.js).first3();
if (J.type(eq0.second).left) {
throw new RuntimeException("Attempt to equate values at type in quotient: " + eq0.second + " at type " + J.type(eq0.second).l);
}
if (J.type(eq0.third).left) {
throw new RuntimeException("Attempt to equate values at type in quotient: " + eq0.third + " at type " + J.type(eq0.third).l);
}
eqs0.add(new Pair<>(eq0.second, eq0.third));
col.eqs.add(new Eq<>(new Ctx<>(), eq0.second, eq0.third));
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(find("equations", eq), "In equation " + eq.first + " = " + eq.second + ", " + ex.getMessage());
}
}
AqlOptions strat = new AqlOptions(options, col, env.defaults);
InitialAlgebra<Ty, En, Sym, Fk, Att, Gen, Sk, ID> initial = new InitialAlgebra<>(strat, J.schema(), col, new It(), Object::toString, Object::toString);
return new LiteralInstance<>(J.schema(), col.gens.map, col.sks.map, eqs0, initial.dp(), initial, (Boolean) strat.getOrDefault(AqlOption.require_consistency), (Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe));
}
use of catdata.Ctx in project fql by CategoricalData.
the class InstExpRandom method eval.
// not exactly the smartest way
@Override
public SaturatedInstance<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>, Pair<Integer, En>, Pair<Integer, Att>> eval(AqlEnv env) {
int seed = (Integer) new AqlOptions(options, null, env.defaults).getOrDefault(AqlOption.random_seed);
Random rand = new Random(seed);
Schema<Ty, En, Sym, Fk, Att> schema = sch.eval(env);
Ctx<En, Collection<Pair<Integer, En>>> ens0 = new Ctx<>();
Ctx<Ty, Collection<Pair<Integer, Att>>> tys = new Ctx<>();
Ctx<Pair<Integer, En>, Ctx<Fk, Pair<Integer, En>>> fks = new Ctx<>();
Ctx<Pair<Integer, En>, Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Pair<Integer, Att>>>> atts = new Ctx<>();
for (Ty ty : schema.typeSide.tys) {
tys.put(ty, new LinkedList<>());
}
for (String en : ens.keySet()) {
List<Pair<Integer, En>> l = new LinkedList<>();
int size = ens.get(en);
for (int i = 0; i < size; i++) {
l.add(new Pair<>(i, new En(en)));
Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Pair<Integer, Att>>> ctx = new Ctx<>();
for (Att att : schema.attsFrom(new En(en))) {
ctx.put(att, Term.Sk(new Pair<>(i, att)));
tys.get(schema.atts.get(att).second).add(new Pair<>(i, att));
}
atts.put(new Pair<>(i, new En(en)), ctx);
Ctx<Fk, Pair<Integer, En>> ctx0 = new Ctx<>();
for (Fk fk : schema.fksFrom(new En(en))) {
int size0 = ens.get(schema.fks.get(fk).second.str);
Integer k = rand.nextInt(size0);
ctx0.put(fk, new Pair<>(k, schema.fks.get(fk).second));
}
fks.put(new Pair<>(i, new En(en)), ctx0);
}
ens0.put(new En(en), l);
}
ImportAlgebra<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> alg = new ImportAlgebra<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>>(schema, ens0, tys, fks, atts, x -> x.toString(), x -> x.toString(), true);
DP<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> dp = new DP<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>>() {
@Override
public String toStringProver() {
return "Random";
}
@Override
public boolean eq(Ctx<Var, Chc<Ty, En>> ctx, Term<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> lhs, Term<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> rhs) {
if (!ctx.isEmpty()) {
Util.anomaly();
}
return lhs.equals(rhs);
}
};
return new SaturatedInstance<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>, Pair<Integer, En>, Pair<Integer, Att>>(alg, dp, false, true, false, new Ctx<>());
}
Aggregations