Search in sources :

Example 71 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class OplParser method toGraph.

private static OplExp toGraph(Tuple3 yyy, Tuple3 xxx) {
    List<String> c = (List<String>) yyy.b;
    List<Tuple5> d = (List<Tuple5>) xxx.b;
    List<Triple<String, String, String>> l = new LinkedList<>();
    for (Tuple5 t : d) {
        l.add(new Triple<>((String) t.a, (String) t.c, (String) t.e));
    }
    return new OplGraph<>(c, l);
}
Also used : Triple(catdata.Triple) Tuple5(org.jparsec.functors.Tuple5) List(java.util.List) LinkedList(java.util.LinkedList) OplGraph(catdata.opl.OplExp.OplGraph) LinkedList(java.util.LinkedList)

Example 72 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class OplToKB method convert.

@SuppressWarnings({ "unchecked", "rawtypes" })
private OplKB<C, V> convert(OplSig<S, C, V> s) {
    if (s.prec.keySet().size() != new HashSet<>(s.prec.values()).size()) {
        throw new RuntimeException("Cannot duplicate precedence: " + s.prec);
    }
    // if (!Collections.disjoint(Arrays.asList(KBHorn.reserved), s.symbols.keySet())) {
    // throw new RuntimeException("Theory contains reserved symbol, one of " + Arrays.toString(KBHorn.reserved));
    // }
    Map<C, Pair<List<S>, S>> symbols = new HashMap<>(s.symbols);
    List one = new LinkedList();
    one.add(new Unit());
    List two = new LinkedList();
    two.add(new Unit());
    two.add(new Unit());
    symbols.put((C) KBHorn._eq, new Pair(two, new Unit()));
    symbols.put((C) KBHorn._or, new Pair(two, new Unit()));
    symbols.put((C) KBHorn._not, new Pair(one, new Unit()));
    symbols.put((C) KBHorn._true, new Pair(new LinkedList(), new Unit()));
    symbols.put((C) KBHorn._false, new Pair(new LinkedList(), new Unit()));
    Function<Pair<C, C>, Boolean> gt = x -> {
        Integer l = s.prec.get(x.first);
        Integer r = s.prec.get(x.second);
        if (l != null && r != null) {
            return l > r;
        }
        if (l == null && r != null) {
            return false;
        }
        if (l != null) {
            return true;
        }
        String lx = x.first.toString();
        String rx = x.second.toString();
        if (!symbols.containsKey(x.first)) {
            throw new RuntimeException("Missing: " + x.first);
        }
        int la = symbols.get(x.first).first.size();
        int ra = symbols.get(x.second).first.size();
        if (la == ra) {
            if (lx.length() == rx.length()) {
                return lx.compareTo(rx) < 0;
            }
            return lx.length() < rx.length();
        }
        if (la >= 3 && ra >= 3) {
            return la > ra;
        }
        if (la == 0 && ra > 0) {
            return false;
        }
        if (la == 1 && (ra == 0 || ra == 2)) {
            return true;
        }
        if (la == 1 && ra > 2) {
            return false;
        }
        if (la == 2 && ra == 0) {
            return true;
        }
        if (la == 2 && (ra == 1 || ra > 2)) {
            return false;
        }
        if (la >= 3 || ra >= 3) {
            // added Aug 3 16
            return la > ra;
        }
        throw new RuntimeException("Bug in precedence, report to Ryan: la=" + la + ", ra=" + ra + ", l=null r=null");
    // function symbols: arity-0 < arity-2 < arity-1 < arity-3 < arity-4
    };
    Set<Pair<KBExp<C, V>, KBExp<C, V>>> eqs = new HashSet<>();
    for (Triple<?, OplTerm<C, V>, OplTerm<C, V>> eq : s.equations) {
        eqs.add(new Pair<>(convert(eq.second), convert(eq.third)));
    }
    Set<Pair<KBExp<C, V>, KBExp<C, V>>> rs = new HashSet<>();
    for (Triple<?, List<Pair<OplTerm<C, V>, OplTerm<C, V>>>, List<Pair<OplTerm<C, V>, OplTerm<C, V>>>> impl : s.implications) {
        rs.addAll(convert(impl.second, impl.third));
    }
    KBOptions options = new KBOptions(DefunctGlobalOptions.debug.opl.opl_prover_unfailing, DefunctGlobalOptions.debug.opl.opl_prover_sort, DefunctGlobalOptions.debug.opl.opl_allow_horn && !s.implications.isEmpty(), DefunctGlobalOptions.debug.opl.opl_prover_ac, DefunctGlobalOptions.debug.opl.opl_prover_timeout, DefunctGlobalOptions.debug.opl.opl_prover_reduction_limit, DefunctGlobalOptions.debug.opl.opl_prover_filter_subsumed, /* NEWDEBUG.debug.opl.simplify, */
    DefunctGlobalOptions.debug.opl.opl_prover_compose, false);
    return new OplKB(eqs, KBOrders.lpogt(DefunctGlobalOptions.debug.opl.opl_allow_horn && !s.implications.isEmpty(), gt), fr, rs, options);
}
Also used : OplJavaInst(catdata.opl.OplExp.OplJavaInst) KBExp(catdata.provers.KBExp) Chc(catdata.Chc) HashMap(java.util.HashMap) KBOptions(catdata.provers.KBOptions) Function(java.util.function.Function) KBOrders(catdata.provers.KBOrders) OplSig(catdata.opl.OplExp.OplSig) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) KBHorn(catdata.provers.KBHorn) Map(java.util.Map) LinkedList(java.util.LinkedList) Pair(catdata.Pair) Iterator(java.util.Iterator) KBApp(catdata.provers.KBExp.KBApp) Collection(java.util.Collection) Util(catdata.Util) Set(java.util.Set) Unit(catdata.Unit) Collectors(java.util.stream.Collectors) FinSet(catdata.fqlpp.cat.FinSet) KBVar(catdata.provers.KBExp.KBVar) List(java.util.List) Invocable(javax.script.Invocable) DoNotIgnore(catdata.opl.OplParser.DoNotIgnore) Triple(catdata.Triple) Collections(java.util.Collections) DefunctGlobalOptions(catdata.ide.DefunctGlobalOptions) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Unit(catdata.Unit) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) List(java.util.List) KBOptions(catdata.provers.KBOptions) HashSet(java.util.HashSet) Pair(catdata.Pair)

Example 73 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class OplWarehouse method computeColimit.

// /////////////////////////////////////////////////////////////////////////
private static void computeColimit(Map<String, OplExp> env, OplGraph<String, String> shape, String base) {
    OplUnion u0 = new OplUnion(new LinkedList<>(shape.nodes), base);
    OplObject u1 = union(env, u0);
    OplSCHEMA0<String, String, String> u = (OplSCHEMA0<String, String, String>) u1;
    Map<String, Set<String>> equivs = new HashMap<>();
    Map<String, String> equivs0 = new HashMap<>();
    for (String schname : shape.nodes) {
        if (!(env.get(schname) instanceof OplSCHEMA0)) {
            throw new RuntimeException("Not a SCHEMA: " + schname);
        }
        OplSCHEMA0<String, String, String> sch = (OplSCHEMA0<String, String, String>) env.get(schname);
        for (String ename : sch.entities) {
            Set<String> set = new HashSet<>();
            set.add(schname + "_" + ename);
            equivs.put(schname + "_" + ename, set);
        }
    }
    // TODO: aql type check colimit
    for (String mname : shape.edges.keySet()) {
        Pair<String, String> mt = shape.edges.get(mname);
        String s = mt.first;
        String t = mt.second;
        OplSCHEMA0<String, String, String> s0 = (OplSCHEMA0<String, String, String>) env.get(s);
        OplMapping<String, String, String, String, String> m0 = (OplMapping<String, String, String, String, String>) env.get(mname);
        if (!m0.src0.equals(s)) {
            throw new RuntimeException("Source of " + m0 + " is " + m0.src + " and not " + s + "as expected");
        }
        if (!m0.dst0.equals(t)) {
            throw new RuntimeException("Target of " + m0 + " is " + m0.dst + " and not " + t + "as expected");
        }
        for (String ob : s0.entities) {
            String ob0 = m0.sorts.get(ob);
            Set<String> set1 = equivs.get(s + "_" + ob);
            Set<String> set2 = equivs.get(t + "_" + ob0);
            set1.addAll(set2);
            equivs.put(s + "_" + ob, set1);
            equivs.put(t + "_" + ob0, set1);
        }
    }
    for (String k : equivs.keySet()) {
        List<String> v = new LinkedList<>(equivs.get(k));
        v.sort(String.CASE_INSENSITIVE_ORDER);
        equivs0.put(k, Util.sep(v, "__"));
    }
    Set<String> entities = new HashSet<>(equivs0.values());
    Map<String, Pair<List<String>, String>> edges = new HashMap<>();
    Map<String, Pair<List<String>, String>> attrs = new HashMap<>();
    List<Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>>> pathEqs = new LinkedList<>();
    List<Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>>> obsEqs = new LinkedList<>();
    Function<String, String> fun = x -> {
        if (equivs0.containsKey(x)) {
            return equivs0.get(x);
        }
        return x;
    };
    for (String edge : u.edges.keySet()) {
        Pair<List<String>, String> ty = u.edges.get(edge);
        edges.put(edge, new Pair<>(ty.first.stream().map(fun).collect(Collectors.toList()), fun.apply(ty.second)));
    }
    for (String attr : u.attrs.keySet()) {
        Pair<List<String>, String> ty = u.attrs.get(attr);
        attrs.put(attr, new Pair<>(ty.first.stream().map(fun).collect(Collectors.toList()), fun.apply(ty.second)));
    }
    for (Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>> eq : u.pathEqs) {
        OplCtx<String, String> ctx = new OplCtx<>(eq.first.values2().stream().map(x -> new Pair<>(x.first, fun.apply(x.second))).collect(Collectors.toList()));
        pathEqs.add(new Triple<>(ctx, OplOps.fun2(equivs0, eq.second), OplOps.fun2(equivs0, eq.third)));
    }
    for (Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>> eq : u.obsEqs) {
        OplCtx<String, String> ctx = new OplCtx<>(eq.first.values2().stream().map(x -> new Pair<>(x.first, fun.apply(x.second))).collect(Collectors.toList()));
        obsEqs.add(new Triple<>(ctx, OplOps.fun2(equivs0, eq.second), OplOps.fun2(equivs0, eq.third)));
    }
    for (String mname : shape.edges.keySet()) {
        Pair<String, String> mt = shape.edges.get(mname);
        String s = mt.first;
        String t = mt.second;
        OplSCHEMA0<String, String, String> s0 = (OplSCHEMA0<String, String, String>) env.get(s);
        // OplSchema<String, String, String> t0 = (OplSchema<String, String, String>) ENV.get(t);
        OplMapping<String, String, String, String, String> m0 = (OplMapping<String, String, String, String, String>) env.get(mname);
        for (String edge : s0.edges.keySet()) {
            Pair<OplCtx<String, String>, OplTerm<String, String>> edge2 = m0.symbols.get(edge);
            List<OplTerm<String, String>> args = edge2.first.vars0.keySet().stream().map((Function<String, OplTerm<String, String>>) OplTerm::new).collect(Collectors.toList());
            OplTerm<String, String> lhs = OplOps.fun2(equivs0, new OplTerm<>(s + "_" + edge, args));
            OplCtx<String, String> ctx = new OplCtx<>(edge2.first.values2().stream().map(x -> new Pair<>(x.first, fun.apply(s + "_" + x.second))).collect(Collectors.toList()));
            OplTerm<String, String> rhs = OplOps.fun2(equivs0, OplOps.prepend(t, edge2.second));
            pathEqs.add(new Triple<>(ctx, lhs, rhs));
        }
        for (String edge : s0.attrs.keySet()) {
            Pair<OplCtx<String, String>, OplTerm<String, String>> edge2 = m0.symbols.get(edge);
            List<OplTerm<String, String>> args = edge2.first.vars0.keySet().stream().map((Function<String, OplTerm<String, String>>) OplTerm::new).collect(Collectors.toList());
            OplTerm<String, String> lhs = OplOps.fun2(equivs0, new OplTerm<>(s + "_" + edge, args));
            OplCtx<String, String> ctx = new OplCtx<>(edge2.first.values2().stream().map(x -> new Pair<>(x.first, fun.apply(s + "_" + x.second))).collect(Collectors.toList()));
            OplTerm<String, String> rhs = OplOps.fun2(equivs0, OplOps.prepend(t, edge2.second));
            obsEqs.add(new Triple<>(ctx, lhs, rhs));
        }
    }
    OplSCHEMA0<String, String, String> ret = new OplSCHEMA0<>(new HashMap<>(), entities, edges, attrs, pathEqs, obsEqs, base);
    env.putIfAbsent(SCHEMA + "_Colimit", ret);
    for (String schname : shape.nodes) {
        OplSCHEMA0<String, String, String> sch = (OplSCHEMA0<String, String, String>) env.get(schname);
        Map<String, String> inj_sorts = new HashMap<>();
        Map<String, Pair<OplCtx<String, String>, OplTerm<String, String>>> inj_symbols = new HashMap<>();
        for (String ename : sch.entities) {
            inj_sorts.put(ename, fun.apply(schname + "_" + ename));
        }
        for (String c1 : sch.attrs.keySet()) {
            Pair<List<String>, String> t = sch.attrs.get(c1);
            List<Pair<String, String>> l = new LinkedList<>();
            List<OplTerm<String, String>> vs = new LinkedList<>();
            for (String s1 : t.first) {
                String v = new VIt().next();
                vs.add(new OplTerm<>(v));
                l.add(new Pair<>(v, fun.apply(schname + "_" + s1)));
            }
            OplCtx<String, String> ctx = new OplCtx<>(l);
            OplTerm<String, String> value = OplOps.fun2(equivs0, new OplTerm<>(schname + "_" + c1, vs));
            inj_symbols.put(c1, new Pair<>(ctx, value));
        }
        for (String c1 : sch.edges.keySet()) {
            Pair<List<String>, String> t = sch.edges.get(c1);
            List<Pair<String, String>> l = new LinkedList<>();
            List<OplTerm<String, String>> vs = new LinkedList<>();
            for (String s1 : t.first) {
                String v = new VIt().next();
                vs.add(new OplTerm<>(v));
                l.add(new Pair<>(v, fun.apply(schname + "_" + s1)));
            }
            OplCtx<String, String> ctx = new OplCtx<>(l);
            OplTerm<String, String> value = OplOps.fun2(equivs0, new OplTerm<>(schname + "_" + c1, vs));
            inj_symbols.put(c1, new Pair<>(ctx, value));
        }
        OplMapping<String, String, String, String, String> mapping = new OplMapping<>(inj_sorts, inj_symbols, schname, SCHEMA + "_Colimit");
        env.putIfAbsent(MAPPING + "_" + schname + "_colimit", mapping);
    }
}
Also used : OplPres(catdata.opl.OplExp.OplPres) JSplitPane(javax.swing.JSplitPane) HashMap(java.util.HashMap) Function(java.util.function.Function) OplSig(catdata.opl.OplExp.OplSig) GridLayout(java.awt.GridLayout) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) WizardModel(catdata.ide.WizardModel) OplPresTrans(catdata.opl.OplExp.OplPresTrans) Map(java.util.Map) JTabbedPane(javax.swing.JTabbedPane) VIt(catdata.opl.OplParser.VIt) ChangeListener(javax.swing.event.ChangeListener) LinkedList(java.util.LinkedList) JComboBox(javax.swing.JComboBox) Pair(catdata.Pair) ChangeEvent(javax.swing.event.ChangeEvent) CodeTextPanel(catdata.ide.CodeTextPanel) Util(catdata.Util) Set(java.util.Set) BorderFactory(javax.swing.BorderFactory) Collectors(java.util.stream.Collectors) OplSCHEMA0(catdata.opl.OplExp.OplSCHEMA0) List(java.util.List) OplUnion(catdata.opl.OplExp.OplUnion) OplGraph(catdata.opl.OplExp.OplGraph) Program(catdata.Program) Triple(catdata.Triple) OplInst0(catdata.opl.OplExp.OplInst0) OplSigma(catdata.opl.OplExp.OplSigma) OplMapping(catdata.opl.OplExp.OplMapping) JPanel(javax.swing.JPanel) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Function(java.util.function.Function) LinkedList(java.util.LinkedList) List(java.util.List) HashSet(java.util.HashSet) Pair(catdata.Pair) VIt(catdata.opl.OplParser.VIt) OplUnion(catdata.opl.OplExp.OplUnion) OplSCHEMA0(catdata.opl.OplExp.OplSCHEMA0) LinkedList(java.util.LinkedList) Triple(catdata.Triple) OplMapping(catdata.opl.OplExp.OplMapping)

Example 74 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class SqlChecker method toPath.

@SuppressWarnings("rawtypes")
private static Triple<String, List<Pair<String, List<Pair<String, String>>>>, List<Pair<String, String>>> toPath(Object ox) {
    Tuple3 o = (Tuple3) ox;
    String start = (String) o.a;
    List l = (List) o.b;
    List<Pair<String, List<Pair<String, String>>>> x = new LinkedList<>();
    for (Object a : l) {
        x.add(toEdge(a));
    }
    Set<String> seen = new HashSet<>();
    List<Pair<String, String>> y = null;
    org.jparsec.functors.Pair qq = (org.jparsec.functors.Pair) o.c;
    if (qq != null) {
        y = new LinkedList<>();
        List z = (List) qq.b;
        for (Object q : z) {
            Tuple3 q2 = (Tuple3) q;
            Pair<String, String> pair = new Pair<>(((String) q2.a).toUpperCase(), ((String) q2.c).toUpperCase());
            if (seen.contains(pair.second)) {
                throw new RuntimeException("Duplicate col: " + pair.second);
            }
            seen.add(pair.second);
            y.add(pair);
        }
    }
    return new Triple<>(start.toUpperCase(), x, y);
}
Also used : LinkedList(java.util.LinkedList) Triple(catdata.Triple) Tuple3(org.jparsec.functors.Tuple3) List(java.util.List) LinkedList(java.util.LinkedList) JList(javax.swing.JList) Pair(catdata.Pair) HashSet(java.util.HashSet)

Example 75 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class SqlChecker method path.

private Triple<String, Set<String>, String> path(String start, List<Pair<String, List<Pair<String, String>>>> path, List<Pair<String, String>> last, SqlSchema info) {
    String init = start;
    String v = next();
    String init_v = v;
    Set<String> from = new HashSet<>();
    Set<String> where = new HashSet<>();
    Set<String> ret = new HashSet<>();
    from.add(start + " AS " + v);
    info.getTable(start);
    for (Pair<String, List<Pair<String, String>>> edge : path) {
        String target = edge.first;
        info.getTable(target);
        if (!match(start, target, edge.second)) {
            String exn = pr(edge) + " is a not declared as a foreign key from " + start + " to " + target;
            ret.add(exn);
            if (haltOnErrors.isSelected()) {
                throw new RuntimeException(exn);
            }
        }
        ret.addAll(typeCheck(start, target, edge));
        if (!targetIsPK(start, target, edge)) {
            String exn = pr(edge) + " does not target the primary key of " + target;
            ret.add(exn);
            if (haltOnErrors.isSelected()) {
                throw new RuntimeException(exn);
            }
        }
        String v2 = next();
        from.add(target + " AS " + v2);
        for (Pair<String, String> p : edge.second) {
            where.add(v + "." + p.first + " = " + v2 + "." + p.second);
        }
        v = v2;
        start = target;
    }
    Set<String> select = new HashSet<>();
    for (SqlColumn col : info.getTable(init).columns) {
        select.add(init_v + "." + col.name + " AS " + "I_" + col.name);
    }
    if (last != null) {
        for (Pair<String, String> col : last) {
            info.getTable(start).getColumn(col.first);
            select.add(v + "." + col.first + " AS " + "O_" + col.second);
        }
    } else {
        for (SqlColumn col : info.getTable(start).columns) {
            select.add(v + "." + col.name + " AS " + "O_" + col.name);
        }
    }
    // TODO: aql must check end is the same in path eq too
    String str = "SELECT DISTINCT " + Util.sep(select, ", ") + "\nFROM " + Util.sep(from, ", ") + (where.isEmpty() ? "" : "\nWHERE " + Util.sep(where, " AND "));
    return new Triple<>(str, ret, start);
}
Also used : Triple(catdata.Triple) List(java.util.List) LinkedList(java.util.LinkedList) JList(javax.swing.JList) SqlColumn(catdata.sql.SqlColumn) HashSet(java.util.HashSet)

Aggregations

Triple (catdata.Triple)116 Pair (catdata.Pair)93 LinkedList (java.util.LinkedList)84 List (java.util.List)75 HashMap (java.util.HashMap)65 Map (java.util.Map)49 HashSet (java.util.HashSet)47 LinkedHashMap (java.util.LinkedHashMap)36 Set (java.util.Set)28 Chc (catdata.Chc)22 Util (catdata.Util)18 En (catdata.aql.exp.SchExpRaw.En)18 Ty (catdata.aql.exp.TyExpRaw.Ty)18 Ctx (catdata.Ctx)17 Sym (catdata.aql.exp.TyExpRaw.Sym)17 Collectors (java.util.stream.Collectors)17 Att (catdata.aql.exp.SchExpRaw.Att)16 Fk (catdata.aql.exp.SchExpRaw.Fk)16 Tuple3 (org.jparsec.functors.Tuple3)16 Quad (catdata.Quad)13