Пример #1
0
 def __str__(self):
     s = [
         op_to_opstr(self.op),
         "(",
         str(self.left),
         ")",
     ]
     return "".join(s)
    def from_uncoalesced_constraints(table_name, unc_bcs, unc_gcs, objective):
        """
        This method creates a new PackageQuery from sets of uncoalesced constraints and an objective.
        """
        bc_query = "SELECT * FROM {} {}".format(
            table_name, "WHERE true" if len(unc_bcs) > 0 else "")
        for attr, op, n in unc_bcs:
            bc_query += " AND {a} {o} {b}".format(a=attr,
                                                  o=op_to_opstr(op),
                                                  b=n)

        gc_queries = []
        gc_ranges = []
        for (aggr, attr), op, n in unc_gcs:
            gc_query = "SELECT {aggr}({attr}) FROM memory_representations".format(
                aggr=aggr, attr=attr)
            if op == operator.le:
                # gc_range = (-sys.maxint, n)
                gc_range = (-float("inf"), n)
            elif op == operator.ge:
                # gc_range = (n, sys.maxint)
                gc_range = (n, float("inf"))
            elif op == operator.eq:
                gc_range = (n, n)
            else:
                raise Exception("Operator '{}' not supported yet.".format(op))
            gc_queries.append(gc_query)
            gc_ranges.append(gc_range)

        return PackageQuery({
            "bc":
            bc_query,
            "gc":
            map(lambda x: (x[0], x[1][0], x[1][1]), zip(gc_queries,
                                                        gc_ranges)),
            "objective":
            objective,
        })
Пример #3
0
 def __str__(self):
     if self.ugc is not None:
         return "Linear Constraint {}: {} {} {}".format(
             self.cid, self.ugc, op_to_opstr(self.ugc.op), self.ugc.rhs)
     else:
         return "Linear Constraint {}".format(self.cid)
    def get_paql_str(self, redo=False, recompute_gcs=True, coalesced=False):
        if redo or self._paql_query_str is None or self._paql_query_str_stale:

            if recompute_gcs:
                self.coalesced_gcs = get_coalesced_global_constraints(
                    self.gc_queries, self.gc_ranges)
                self.uncoalesced_gcs = get_uncoalesced_global_constraints(
                    self.coalesced_gcs)
                self.coalesced_bcs = get_coalesced_base_constraints(
                    self.bc_query)
                self.uncoalesced_bcs = get_uncoalesced_base_constraints(
                    self.coalesced_bcs)

            if self.rel_namespace is None:
                # raise Exception("rel_namespace is None")
                # return ""
                self.rel_namespace = {"R": self.table_name}

            bcs_str = []
            gcs_str = []
            obj_str = None

            if not coalesced:
                if len(self.uncoalesced_bcs) > 0:
                    for attr, op, n in self.uncoalesced_bcs:
                        bcs_str.append("{} {} {}".format(
                            attr, op_to_opstr(op), n))

                if len(self.uncoalesced_gcs) > 0:
                    for (aggr, attr), op, n in self.uncoalesced_gcs:
                        gcs_str.append("{}({}) {} {}".format(
                            aggr, attr, op_to_opstr(op), n))

            else:
                if len(self.coalesced_bcs) > 0:
                    for attr, (lb, ub) in self.coalesced_bcs.iteritems():
                        if float(lb) == -float("inf") and float(ub) == float(
                                "inf"):
                            continue
                        elif float(ub) == float("inf"):
                            bcs_str.append("{} {} {}".format(
                                attr, op_to_opstr(operator.ge), lb))
                        elif float(lb) == -float("inf"):
                            bcs_str.append("{} {} {}".format(
                                attr, op_to_opstr(operator.le), ub))
                        elif lb == ub:
                            bcs_str.append("{} {} {}".format(
                                attr, op_to_opstr(operator.eq), ub))
                        else:
                            bcs_str.append("{} BETWEEN {} AND {}".format(
                                attr, lb, ub))

                if len(self.coalesced_gcs) > 0:
                    for (aggr, attr), (lb,
                                       ub) in self.coalesced_gcs.iteritems():
                        if aggr.lower() == "count":
                            lb, ub = int(lb), int(ub)

                        uaggr = aggr.upper()

                        if float(lb) == -float("inf") and float(ub) == float(
                                "inf"):
                            continue
                        elif float(ub) == float("inf"):
                            gcs_str.append("{}({}) {} {}".format(
                                uaggr, attr, op_to_opstr(operator.ge), lb))
                        elif float(lb) == -float("inf"):
                            gcs_str.append("{}({}) {} {}".format(
                                uaggr, attr, op_to_opstr(operator.le), ub))
                        elif lb == ub:
                            gcs_str.append("{}({}) {} {}".format(
                                uaggr, attr, op_to_opstr(operator.eq), ub))
                        else:
                            gcs_str.append("{}({}) BETWEEN {} AND {}".format(
                                uaggr, attr, lb, ub))

            if self.objective is not None:
                if self.objective["type"] == "maximize":
                    obj_str = "MAXIMIZE "
                elif self.objective["type"] == "minimize":
                    obj_str = "MINIMIZE "
                else:
                    raise
                obj_str += self.objective["func"].get_str()

            self._paql_query_str = \
                "SELECT \n\tPACKAGE({pack}) \n" \
                "FROM \n\t{tables} {bcs}{gcs}{obj};".format(
                pack=", ".join(self.rel_namespace.keys()),
                tables=", ".join("{} {}".format(name, alias) for alias, name in self.rel_namespace.iteritems()),
                bcs="\nWHERE \n\t{} ".format(" AND\n\t".join(bcs_str)) if bcs_str else "",
                gcs="\nSUCH THAT \n\t{} ".format(" AND\n\t".join(gcs_str)) if gcs_str else "",
                obj="\n{}".format(obj_str) if obj_str is not None else "")

            self._paql_query_str_stale = False

        return self._paql_query_str
Пример #5
0
 def __str__(self):
     return " ".join([str(self.expr), op_to_opstr(self.op), str(self.rhs)])