BOX_COUNT_FILTER_TYPE = ComplexType(BOX_TYPE, ComplexType(NUM_TYPE, BOX_TYPE))
# This box filter returns boxes where a specified attribute is same or different
BOX_ATTRIBUTE_SAME_FILTER_TYPE = ComplexType(BOX_TYPE, BOX_TYPE)


ASSERT_COLOR_TYPE = ComplexType(OBJECT_TYPE, ComplexType(COLOR_TYPE, TRUTH_TYPE))
ASSERT_SHAPE_TYPE = ComplexType(OBJECT_TYPE, ComplexType(SHAPE_TYPE, TRUTH_TYPE))
ASSERT_BOX_COUNT_TYPE = ComplexType(BOX_TYPE, ComplexType(NUM_TYPE, TRUTH_TYPE))
ASSERT_OBJECT_COUNT_TYPE = ComplexType(OBJECT_TYPE, ComplexType(NUM_TYPE, TRUTH_TYPE))

BOX_EXISTS_TYPE = ComplexType(BOX_TYPE, TRUTH_TYPE)
OBJECT_EXISTS_TYPE = ComplexType(OBJECT_TYPE, TRUTH_TYPE)

BASIC_TYPES = {NUM_TYPE, BOX_TYPE, OBJECT_TYPE, COLOR_TYPE, SHAPE_TYPE}

name_mapper = NameMapper()  # pylint: disable=invalid-name

# Entities
name_mapper.map_name_with_signature("all_objects", OBJECT_TYPE)
name_mapper.map_name_with_signature("all_boxes", BOX_TYPE)
name_mapper.map_name_with_signature("color_black", COLOR_TYPE)
name_mapper.map_name_with_signature("color_blue", COLOR_TYPE)
name_mapper.map_name_with_signature("color_yellow", COLOR_TYPE)
name_mapper.map_name_with_signature("shape_triangle", SHAPE_TYPE)
name_mapper.map_name_with_signature("shape_square", SHAPE_TYPE)
name_mapper.map_name_with_signature("shape_circle", SHAPE_TYPE)


# Attribute function
name_mapper.map_name_with_signature("object_in_box", BOX_MEMBERSHIP_TYPE)
    def __init__(self, syntax: str) -> None:

        self.name_mapper = NameMapper()

        num_type = NamedBasicType("NUM")
        attr_type = NamedBasicType("ATTR")
        rdir_type = NamedBasicType("RDIR")
        world_type = NamedBasicType("WORLD")
        var_type = NamedBasicType("VAR")

        self.basic_types = {
            num_type, attr_type, rdir_type, world_type, var_type
        }

        if syntax == "quarel_friction":
            # attributes: <<QDIR, <WORLD, ATTR>>
            attr_function_type = ComplexType(
                rdir_type, ComplexType(world_type, attr_type))

            and_function_type = ComplexType(attr_type,
                                            ComplexType(attr_type, attr_type))

            # infer: <ATTR, <ATTR, <ATTR, NUM>>>
            infer_function_type = ComplexType(
                attr_type,
                ComplexType(attr_type, ComplexType(attr_type, num_type)))
            self.name_mapper.map_name_with_signature("infer",
                                                     infer_function_type)
            # Attributes
            self.name_mapper.map_name_with_signature("friction",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("smoothness",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("speed",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("heat",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("distance",
                                                     attr_function_type)

            # For simplicity we treat "high" and "low" as directions as well
            self.name_mapper.map_name_with_signature("high", rdir_type)
            self.name_mapper.map_name_with_signature("low", rdir_type)
            self.name_mapper.map_name_with_signature("and", and_function_type)

            self.curried_functions = {
                attr_function_type: 2,
                infer_function_type: 3,
                and_function_type: 2,
            }
        elif syntax in ("quarel_v1_attr_entities",
                        "quarel_friction_attr_entities"):
            # attributes: <<QDIR, <WORLD, ATTR>>
            attr_function_type = ComplexType(
                rdir_type, ComplexType(world_type, attr_type))

            and_function_type = ComplexType(attr_type,
                                            ComplexType(attr_type, attr_type))

            # infer: <ATTR, <ATTR, <ATTR, NUM>>>
            infer_function_type = ComplexType(
                attr_type,
                ComplexType(attr_type, ComplexType(attr_type, num_type)))
            self.name_mapper.map_name_with_signature("infer",
                                                     infer_function_type)
            # TODO: Remove this?
            self.name_mapper.map_name_with_signature("placeholder",
                                                     attr_function_type)

            # For simplicity we treat "high" and "low" as directions as well
            self.name_mapper.map_name_with_signature("high", rdir_type)
            self.name_mapper.map_name_with_signature("low", rdir_type)
            self.name_mapper.map_name_with_signature("and", and_function_type)

            self.curried_functions = {
                attr_function_type: 2,
                infer_function_type: 3,
                and_function_type: 2,
            }

        elif syntax == "quarel_v1":
            # attributes: <<QDIR, <WORLD, ATTR>>
            attr_function_type = ComplexType(
                rdir_type, ComplexType(world_type, attr_type))

            and_function_type = ComplexType(attr_type,
                                            ComplexType(attr_type, attr_type))

            # infer: <ATTR, <ATTR, <ATTR, NUM>>>
            infer_function_type = ComplexType(
                attr_type,
                ComplexType(attr_type, ComplexType(attr_type, num_type)))
            self.name_mapper.map_name_with_signature("infer",
                                                     infer_function_type)
            # Attributes
            self.name_mapper.map_name_with_signature("friction",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("smoothness",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("speed",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("heat",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("distance",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("acceleration",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("amountSweat",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("apparentSize",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("breakability",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("brightness",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("exerciseIntensity",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("flexibility",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("gravity",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("loudness",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("mass",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("strength",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("thickness",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("time",
                                                     attr_function_type)
            self.name_mapper.map_name_with_signature("weight",
                                                     attr_function_type)

            # For simplicity we treat "high" and "low" as directions as well
            self.name_mapper.map_name_with_signature("high", rdir_type)
            self.name_mapper.map_name_with_signature("low", rdir_type)
            self.name_mapper.map_name_with_signature("and", and_function_type)

            self.curried_functions = {
                attr_function_type: 2,
                infer_function_type: 3,
                and_function_type: 2,
            }

        else:
            raise Exception(f"Unknown LF syntax specification: {syntax}")

        self.name_mapper.map_name_with_signature("higher", rdir_type)
        self.name_mapper.map_name_with_signature("lower", rdir_type)

        self.name_mapper.map_name_with_signature("world1", world_type)
        self.name_mapper.map_name_with_signature("world2", world_type)

        # Hack to expose types
        self.world_type = world_type
        self.attr_function_type = attr_function_type
        self.var_type = var_type

        self.starting_types = {num_type}
# Numerical operations on numbers in the given column. "max", "min", "sum", "average" etc.
ROW_NUM_OP = ComplexType(ROW_TYPE, ComplexType(NUMBER_COLUMN_TYPE, NUMBER_TYPE))

# Numerical difference within the given column.
NUM_DIFF_WITH_COLUMN = ComplexType(ROW_TYPE, ComplexType(ROW_TYPE, ComplexType(NUMBER_COLUMN_TYPE,
                                                                               NUMBER_TYPE)))

# Date function takes three numbers and makes a date
DATE_FUNCTION_TYPE = ComplexType(NUMBER_TYPE, ComplexType(NUMBER_TYPE, ComplexType(NUMBER_TYPE,
                                                                                   DATE_TYPE)))


# We have one name mapper for all generic functions, and different ones for each of the column types
# because we will add those to the final name mapping only if needed, based on the table content.
generic_name_mapper = NameMapper()  # pylint: disable=invalid-name
string_column_name_mapper = NameMapper(alias_prefix="S")  # pylint: disable=invalid-name
number_column_name_mapper = NameMapper(alias_prefix="N")  # pylint: disable=invalid-name
date_column_name_mapper = NameMapper(alias_prefix="D")  # pylint: disable=invalid-name
comparable_column_name_mapper = NameMapper(alias_prefix="O")  # pylint: disable=invalid-name

generic_name_mapper.map_name_with_signature("all_rows", ROW_TYPE)

# <r,<g,s>>
generic_name_mapper.map_name_with_signature("select", SELECT_TYPE)
generic_name_mapper.map_name_with_signature("mode", SELECT_TYPE)

# <r,<c,r>>
comparable_column_name_mapper.map_name_with_signature("argmax", ROW_FILTER_WITH_COMPARABLE_COLUMN)
comparable_column_name_mapper.map_name_with_signature("argmin", ROW_FILTER_WITH_COMPARABLE_COLUMN)
# reverse
REVERSE_TYPE = ReverseType(ComplexType(ANY_TYPE, ANY_TYPE),
                           ComplexType(ANY_TYPE, ANY_TYPE))
# !=, fb:type.object.type
# fb:type.object.type takes a type and returns all objects of that type.
IDENTITY_TYPE = UnaryOpType()
# index
ROW_INDEX_TYPE = ComplexType(NUMBER_TYPE, ROW_TYPE)
# count
COUNT_TYPE = CountType(ANY_TYPE)
# and, or
CONJUNCTION_TYPE = BinaryOpType()
# argmax, argmin
ARG_EXTREME_TYPE = ArgExtremeType()

name_mapper = NameMapper(language_has_lambda=True)  # pylint: disable=invalid-name

# We hardcode some the names "V" and "X" to mean "var" and "x" in the DynamicTypeLogicParser to deal
# with these special types appropriately. So forcing their aliases here.
name_mapper.map_name_with_signature(name="var",
                                    signature=IDENTITY_TYPE,
                                    alias="V")
name_mapper.map_name_with_signature(name="x", signature=ANY_TYPE, alias="X")

name_mapper.map_name_with_signature("reverse", REVERSE_TYPE)
name_mapper.map_name_with_signature("argmax", ARG_EXTREME_TYPE)
name_mapper.map_name_with_signature("argmin", ARG_EXTREME_TYPE)
name_mapper.map_name_with_signature("max", UNARY_DATE_NUM_OP_TYPE)
name_mapper.map_name_with_signature("min", UNARY_DATE_NUM_OP_TYPE)
name_mapper.map_name_with_signature("and", CONJUNCTION_TYPE)
name_mapper.map_name_with_signature("or", CONJUNCTION_TYPE)
ROW_TO_ROW_TYPE = ComplexType(ROW_TYPE, ROW_TYPE)
# reverse
REVERSE_TYPE = ReverseType(ComplexType(ANY_TYPE, ANY_TYPE), ComplexType(ANY_TYPE, ANY_TYPE))
# !=, fb:type.object.type
# fb:type.object.type takes a type and returns all objects of that type.
IDENTITY_TYPE = UnaryOpType()
# index
ROW_INDEX_TYPE = ComplexType(NUMBER_TYPE, ROW_TYPE)
# count
COUNT_TYPE = CountType(ANY_TYPE)
# and, or
CONJUNCTION_TYPE = BinaryOpType()
# argmax, argmin
ARG_EXTREME_TYPE = ArgExtremeType()

name_mapper = NameMapper(language_has_lambda=True)  # pylint: disable=invalid-name

# We hardcode some the names "V" and "X" to mean "var" and "x" in the DynamicTypeLogicParser to deal
# with these special types appropriately. So forcing their aliases here.
name_mapper.map_name_with_signature(name="var", signature=IDENTITY_TYPE, alias="V")
name_mapper.map_name_with_signature(name="x", signature=ANY_TYPE, alias="X")

name_mapper.map_name_with_signature("reverse", REVERSE_TYPE)
name_mapper.map_name_with_signature("argmax", ARG_EXTREME_TYPE)
name_mapper.map_name_with_signature("argmin", ARG_EXTREME_TYPE)
name_mapper.map_name_with_signature("max", UNARY_DATE_NUM_OP_TYPE)
name_mapper.map_name_with_signature("min", UNARY_DATE_NUM_OP_TYPE)
name_mapper.map_name_with_signature("and", CONJUNCTION_TYPE)
name_mapper.map_name_with_signature("or", CONJUNCTION_TYPE)
name_mapper.map_name_with_signature("fb:row.row.next", ROW_TO_ROW_TYPE)
name_mapper.map_name_with_signature("number", NUMBER_FUNCTION_TYPE)
Esempio n. 6
0
PAS_COUNT_TYPE = ComplexType(PAS_TYPE, NUMBER_TYPE)

# Numerical operations on numbers within the given relation. "max", "min", "sum", "average",
# "count_entities", "extract_number".
PAS_NUM_OP = ComplexType(PAS_TYPE, ComplexType(RELATION_TYPE, NUMBER_TYPE))

# Numerical difference (diff)
NUM_DIFF_TYPE = ComplexType(NUMBER_TYPE, ComplexType(NUMBER_TYPE, NUMBER_TYPE))

# Date function takes four numbers and makes a date
DATE_FUNCTION_TYPE = ComplexType(
    NUMBER_TYPE,
    ComplexType(NUMBER_TYPE,
                ComplexType(NUMBER_TYPE, ComplexType(NUMBER_TYPE, DATE_TYPE))))

generic_name_mapper = NameMapper()  # pylint: disable=invalid-name

generic_name_mapper.map_name_with_signature("all_structures", PAS_TYPE)

# <p,<r,s>>
generic_name_mapper.map_name_with_signature("select", SELECT_TYPE)
generic_name_mapper.map_name_with_signature("mode", SELECT_TYPE)
generic_name_mapper.map_name_with_signature("extract_entity", SELECT_TYPE)

# <p,<r,p>>
generic_name_mapper.map_name_with_signature("argmax", PAS_FILTER_WITH_RELATION)
generic_name_mapper.map_name_with_signature("argmin", PAS_FILTER_WITH_RELATION)

# <p,<r,<n,p>>>
generic_name_mapper.map_name_with_signature(
    "filter_number_greater", PAS_FILTER_WITH_RELATION_AND_NUMBER)
    def __init__(self, syntax: str) -> None:

        self.name_mapper = NameMapper()

        num_type = NamedBasicType("NUM")
        attr_type = NamedBasicType("ATTR")
        rdir_type = NamedBasicType("RDIR")
        world_type = NamedBasicType("WORLD")
        var_type = NamedBasicType("VAR")

        self.basic_types = {num_type, attr_type, rdir_type, world_type, var_type}

        if syntax == "quarel_friction":
            # attributes: <<QDIR, <WORLD, ATTR>>
            attr_function_type = ComplexType(rdir_type,
                                             ComplexType(world_type, attr_type))

            and_function_type = ComplexType(attr_type, ComplexType(attr_type, attr_type))

            # infer: <ATTR, <ATTR, <ATTR, NUM>>>
            infer_function_type = ComplexType(attr_type,
                                              ComplexType(attr_type,
                                                          ComplexType(attr_type, num_type)))
            self.name_mapper.map_name_with_signature("infer", infer_function_type)
            # Attributes
            self.name_mapper.map_name_with_signature("friction", attr_function_type)
            self.name_mapper.map_name_with_signature("smoothness", attr_function_type)
            self.name_mapper.map_name_with_signature("speed", attr_function_type)
            self.name_mapper.map_name_with_signature("heat", attr_function_type)
            self.name_mapper.map_name_with_signature("distance", attr_function_type)

            # For simplicity we treat "high" and "low" as directions as well
            self.name_mapper.map_name_with_signature("high", rdir_type)
            self.name_mapper.map_name_with_signature("low", rdir_type)
            self.name_mapper.map_name_with_signature("and", and_function_type)

            self.curried_functions = {
                    attr_function_type: 2,
                    infer_function_type: 3,
                    and_function_type: 2
            }
        elif syntax == "quarel_v1_attr_entities" or syntax == "quarel_friction_attr_entities":
            # attributes: <<QDIR, <WORLD, ATTR>>
            attr_function_type = ComplexType(rdir_type,
                                             ComplexType(world_type, attr_type))

            and_function_type = ComplexType(attr_type, ComplexType(attr_type, attr_type))

            # infer: <ATTR, <ATTR, <ATTR, NUM>>>
            infer_function_type = ComplexType(attr_type,
                                              ComplexType(attr_type,
                                                          ComplexType(attr_type, num_type)))
            self.name_mapper.map_name_with_signature("infer", infer_function_type)
            # TODO: Remove this?
            self.name_mapper.map_name_with_signature("placeholder", attr_function_type)

            # For simplicity we treat "high" and "low" as directions as well
            self.name_mapper.map_name_with_signature("high", rdir_type)
            self.name_mapper.map_name_with_signature("low", rdir_type)
            self.name_mapper.map_name_with_signature("and", and_function_type)

            self.curried_functions = {
                    attr_function_type: 2,
                    infer_function_type: 3,
                    and_function_type: 2
            }

        elif syntax == "quarel_v1":
            # attributes: <<QDIR, <WORLD, ATTR>>
            attr_function_type = ComplexType(rdir_type,
                                             ComplexType(world_type, attr_type))

            and_function_type = ComplexType(attr_type, ComplexType(attr_type, attr_type))

            # infer: <ATTR, <ATTR, <ATTR, NUM>>>
            infer_function_type = ComplexType(attr_type,
                                              ComplexType(attr_type,
                                                          ComplexType(attr_type, num_type)))
            self.name_mapper.map_name_with_signature("infer", infer_function_type)
            # Attributes
            self.name_mapper.map_name_with_signature("friction", attr_function_type)
            self.name_mapper.map_name_with_signature("smoothness", attr_function_type)
            self.name_mapper.map_name_with_signature("speed", attr_function_type)
            self.name_mapper.map_name_with_signature("heat", attr_function_type)
            self.name_mapper.map_name_with_signature("distance", attr_function_type)
            self.name_mapper.map_name_with_signature("acceleration", attr_function_type)
            self.name_mapper.map_name_with_signature("amountSweat", attr_function_type)
            self.name_mapper.map_name_with_signature("apparentSize", attr_function_type)
            self.name_mapper.map_name_with_signature("breakability", attr_function_type)
            self.name_mapper.map_name_with_signature("brightness", attr_function_type)
            self.name_mapper.map_name_with_signature("exerciseIntensity", attr_function_type)
            self.name_mapper.map_name_with_signature("flexibility", attr_function_type)
            self.name_mapper.map_name_with_signature("gravity", attr_function_type)
            self.name_mapper.map_name_with_signature("loudness", attr_function_type)
            self.name_mapper.map_name_with_signature("mass", attr_function_type)
            self.name_mapper.map_name_with_signature("strength", attr_function_type)
            self.name_mapper.map_name_with_signature("thickness", attr_function_type)
            self.name_mapper.map_name_with_signature("time", attr_function_type)
            self.name_mapper.map_name_with_signature("weight", attr_function_type)

            # For simplicity we treat "high" and "low" as directions as well
            self.name_mapper.map_name_with_signature("high", rdir_type)
            self.name_mapper.map_name_with_signature("low", rdir_type)
            self.name_mapper.map_name_with_signature("and", and_function_type)

            self.curried_functions = {
                    attr_function_type: 2,
                    infer_function_type: 3,
                    and_function_type: 2
            }

        else:
            raise Exception(f"Unknown LF syntax specification: {syntax}")

        self.name_mapper.map_name_with_signature("higher", rdir_type)
        self.name_mapper.map_name_with_signature("lower", rdir_type)

        self.name_mapper.map_name_with_signature("world1", world_type)
        self.name_mapper.map_name_with_signature("world2", world_type)

        # Hack to expose types
        self.world_type = world_type
        self.attr_function_type = attr_function_type
        self.var_type = var_type

        self.starting_types = {num_type}