Search in sources :

Example 6 with OplInst

use of catdata.opl.OplExp.OplInst in project fql by CategoricalData.

the class OplChase method chase.

static OplInst chase(OplInst I, List<OplQuery> EDs, int limit) {
    OplInst ret = I;
    for (int i = 0; i < limit; i++) {
        boolean changed = false;
        for (OplQuery ed : EDs) {
            OplPushout ret2 = step(ret, ed);
            if (ret2 != null) {
                ret = (OplInst) ret2.pushout().first;
                changed = true;
            }
        }
        if (!changed) {
            return ret;
        }
    }
    throw new RuntimeException("Limit exceeded, last instance:\n\n" + ret);
}
Also used : OplPushout(catdata.opl.OplExp.OplPushout) OplInst(catdata.opl.OplExp.OplInst)

Example 7 with OplInst

use of catdata.opl.OplExp.OplInst in project fql by CategoricalData.

the class OplOps method visit.

@Override
public OplObject visit(Program<OplExp> env, OplPivot e) {
    OplObject o = ENV.get(e.I0);
    if (o instanceof OplInst) {
        OplInst I = (OplInst) o;
        e.validate(I);
        return e;
    }
    throw new RuntimeException("Not an instnce: " + e.I0);
}
Also used : OplInst(catdata.opl.OplExp.OplInst)

Example 8 with OplInst

use of catdata.opl.OplExp.OplInst in project fql by CategoricalData.

the class OplOps method visit.

@Override
public OplObject visit(Program<OplExp> env, OplPresTrans e) {
    OplObject src = ENV.get(e.src0);
    OplObject dst = ENV.get(e.dst0);
    if (src instanceof OplPres && dst instanceof OplPres) {
        OplPres src0 = (OplPres) src;
        OplPres dst0 = (OplPres) dst;
        for (Object k2 : e.imports) {
            String k = (String) k2;
            OplExp o = env.exps.get(k);
            if (!(o instanceof OplPresTrans)) {
                throw new RuntimeException("Not a typed mapping: " + k);
            }
            OplPresTrans a = (OplPresTrans) o;
            for (Object z : a.pre_map.keySet()) {
                if (!e.pre_map.containsKey(z)) {
                    e.pre_map.put(z, new HashMap());
                }
                Map u = (Map) a.pre_map.get(z);
                Map v = (Map) e.pre_map.get(z);
                Util.putAllSafely(v, u);
            }
        }
        // ?
        e.validateNotReally(src0, dst0);
        // e.toMapping(); redundant
        return e;
    } else if (src instanceof OplInst && dst instanceof OplInst) {
        OplInst src0 = (OplInst) src;
        OplInst dst0 = (OplInst) dst;
        for (Object k2 : e.imports) {
            String k = (String) k2;
            OplExp o = env.exps.get(k);
            if (!(o instanceof OplPresTrans)) {
                throw new RuntimeException("Not a typed mapping: " + k);
            }
            OplPresTrans a = (OplPresTrans) o;
            for (Object z : a.pre_map.keySet()) {
                if (!e.pre_map.containsKey(z)) {
                    e.pre_map.put(z, new HashMap());
                }
                Map u = (Map) a.pre_map.get(z);
                Map v = (Map) e.pre_map.get(z);
                Util.putAllSafely(v, u);
            }
        }
        // ?
        e.validateNotReally(src0, dst0);
        // e.toMapping(); redundant
        return e;
    }
    throw new RuntimeException("Source or target is not a presentation or instance in " + e);
}
Also used : OplPresTrans(catdata.opl.OplExp.OplPresTrans) HashMap(java.util.HashMap) OplPres(catdata.opl.OplExp.OplPres) Map(java.util.Map) HashMap(java.util.HashMap) OplInst(catdata.opl.OplExp.OplInst)

Example 9 with OplInst

use of catdata.opl.OplExp.OplInst in project fql by CategoricalData.

the class OplOps method visit.

@Override
public OplObject visit(Program<OplExp> env, OplChaseExp e) {
    OplObject I0 = ENV.get(e.I);
    List<OplQuery> EDs = new LinkedList<>();
    if (!(I0 instanceof OplInst)) {
        throw new RuntimeException("Not an instance: " + e.I);
    }
    OplInst I = (OplInst) I0;
    for (String ed : e.EDs) {
        OplObject ed0 = ENV.get(ed);
        if (!(ed0 instanceof OplQuery)) {
            throw new RuntimeException("Not a query: " + ed0);
        }
        EDs.add((OplQuery) ed0);
    }
    return OplChase.chase(I, EDs, e.limit);
}
Also used : LinkedList(java.util.LinkedList) OplInst(catdata.opl.OplExp.OplInst)

Example 10 with OplInst

use of catdata.opl.OplExp.OplInst in project fql by CategoricalData.

the class SqlToOpl method convert.

// : happy medium between CNF and non-CNF? ignore as many columns as possible?
// ////////////////////////////////////////////////////////////////////////////////////////////////////
private static Pair<OplSchema<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, OplInst<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String>> convert(SqlSchema info, SqlInstance inst, String S0, String I0) {
    Set<Chc<SqlType, SqlTable>> sorts = new HashSet<>();
    Set<Chc<SqlType, SqlTable>> entities = new HashSet<>();
    Map<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, Pair<List<Chc<SqlType, SqlTable>>, Chc<SqlType, SqlTable>>> symbols = new HashMap<>();
    List<Triple<OplCtx<Chc<SqlType, SqlTable>, String>, OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>>> equations = new LinkedList<>();
    for (SqlType type : info.types) {
        sorts.add(Chc.inLeft(type));
    }
    for (SqlTable table : info.tables) {
        sorts.add(Chc.inRight(table));
        entities.add(Chc.inRight(table));
        for (SqlColumn col : table.columns) {
            symbols.put(Chc.inRight(Chc.inLeft(col)), new Pair<>(Util.singList(Chc.inRight(table)), Chc.inLeft(col.type)));
        }
    }
    for (SqlForeignKey fk : info.fks) {
        symbols.put(Chc.inRight(Chc.inRight(fk)), new Pair<>(Util.singList(Chc.inRight(fk.source)), Chc.inRight(fk.target)));
        OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> head = new OplTerm<>("x");
        OplCtx<Chc<SqlType, SqlTable>, String> ctx = new OplCtx<>(Util.singList(new Pair<>("x", Chc.inRight(fk.source))));
        OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> rhs0 = new OplTerm<>(Chc.inRight(Chc.inRight(fk)), Util.singList(head));
        for (SqlColumn tcol : fk.map.keySet()) {
            SqlColumn scol = fk.map.get(tcol);
            Chc<SqlColumn, SqlForeignKey> l = Chc.inLeft(scol);
            Chc<SqlColumn, SqlForeignKey> r = Chc.inLeft(tcol);
            OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> lhs = new OplTerm<>(Chc.inRight(l), Util.singList(head));
            OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> rhs = new OplTerm<>(Chc.inRight(r), Util.singList(rhs0));
            equations.add(new Triple<>(ctx, lhs, rhs));
        }
    }
    OplSchema<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> sch = new OplSchema<>(S0, entities);
    OplSig<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> sig = new OplSig<>(VIt.vit, new HashMap<>(), sorts, symbols, equations);
    sch.validate(sig);
    OplInst<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String> I = new OplInst<>(S0, I0, "none");
    Map<String, Chc<SqlType, SqlTable>> gens = new HashMap<>();
    List<Pair<OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String>, OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String>>> eqs = new LinkedList<>();
    int fr = 0;
    if (inst != null) {
        Map<SqlTable, Map<Map<SqlColumn, Optional<Object>>, String>> iso1 = new HashMap<>();
        for (SqlTable table : info.tables) {
            Set<Map<SqlColumn, Optional<Object>>> tuples = inst.get(table);
            Map<Map<SqlColumn, Optional<Object>>, String> i1 = new HashMap<>();
            // Map<String, Map<SqlColumn, Optional<Object>>> i2 = new HashMap<>();
            for (Map<SqlColumn, Optional<Object>> tuple : tuples) {
                String i = "v" + (fr++);
                i1.put(tuple, i);
                // i2.put(i, tuple);
                gens.put(i, Chc.inRight(table));
                for (SqlColumn col : table.columns) {
                    SqlType ty = col.type;
                    Optional<Object> val = tuple.get(col);
                    if (!val.isPresent()) {
                        continue;
                    }
                    symbols.put(Chc.inLeft(val.get()), new Pair<>(new LinkedList<>(), Chc.inLeft(ty)));
                    OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> rhs = new OplTerm<>(Chc.inLeft(Chc.inLeft(val.get())), new LinkedList<>());
                    OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> lhs = new OplTerm<>(Chc.inLeft(Chc.inRight(Chc.inLeft(col))), Util.singList(new OplTerm<>(Chc.inRight(i), new LinkedList<>())));
                    eqs.add(new Pair<>(lhs, rhs));
                }
            }
            iso1.put(table, i1);
        // iso2.put(table, i2);
        }
        for (SqlForeignKey fk : info.fks) {
            for (Map<SqlColumn, Optional<Object>> in : inst.get(fk.source)) {
                Map<SqlColumn, Optional<Object>> out = inst.follow(in, fk);
                String tgen = iso1.get(fk.target).get(out);
                String sgen = iso1.get(fk.source).get(in);
                OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> rhs = new OplTerm<>(Chc.inRight(tgen), new LinkedList<>());
                OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> lhs = new OplTerm<>(Chc.inLeft(Chc.inRight(Chc.inRight(fk))), Util.singList(new OplTerm<>(Chc.inRight(sgen), new LinkedList<>())));
                eqs.add(new Pair<>(lhs, rhs));
            }
        }
    }
    OplPres<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String> P = new OplPres<>(new HashMap<>(), S0, sig, gens, eqs);
    P.toSig();
    I.validate(sch, P, null);
    return new Pair<>(sch, I);
}
Also used : HashMap(java.util.HashMap) OplSchema(catdata.opl.OplExp.OplSchema) OplInst(catdata.opl.OplExp.OplInst) OplPres(catdata.opl.OplExp.OplPres) SqlType(catdata.sql.SqlType) HashSet(java.util.HashSet) Pair(catdata.Pair) Optional(java.util.Optional) OplSig(catdata.opl.OplExp.OplSig) LinkedList(java.util.LinkedList) Triple(catdata.Triple) SqlForeignKey(catdata.sql.SqlForeignKey) SqlTable(catdata.sql.SqlTable) SqlColumn(catdata.sql.SqlColumn) HashMap(java.util.HashMap) Map(java.util.Map) Chc(catdata.Chc)

Aggregations

OplInst (catdata.opl.OplExp.OplInst)12 OplPres (catdata.opl.OplExp.OplPres)7 OplSchema (catdata.opl.OplExp.OplSchema)5 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 Pair (catdata.Pair)4 Triple (catdata.Triple)4 OplSig (catdata.opl.OplExp.OplSig)4 HashSet (java.util.HashSet)4 Chc (catdata.Chc)3 OplInst0 (catdata.opl.OplExp.OplInst0)3 OplPresTrans (catdata.opl.OplExp.OplPresTrans)3 Map (java.util.Map)3 OplGround (catdata.opl.OplExp.OplGround)2 OplMapping (catdata.opl.OplExp.OplMapping)2 OplPushout (catdata.opl.OplExp.OplPushout)2 OplSCHEMA0 (catdata.opl.OplExp.OplSCHEMA0)2 OplTyMapping (catdata.opl.OplExp.OplTyMapping)2 SqlColumn (catdata.sql.SqlColumn)2 SqlForeignKey (catdata.sql.SqlForeignKey)2