Ejemplo n.º 1
0
def build_size_relationships(
    relative_size_nodes: Tuple[Tuple[OntologyNode, ...], ...],
    *,
    relation_type: OntologyNode,
    opposite_type: OntologyNode,
):
    """
    Build a dictionary to represent opposite relation_types between OntologyNodes

    Used primarily to represent relative size_of relationships, this function takes a ranking of
    `OntologyNode` objects by *relative_size_nodes* which are then assigned the appropriate
    *relation_type* and *opposite_type* respectively.

    For use see GAILA_PHASE_1_ONTOLOGY.
    """
    node_to_relations: List[Relation[OntologyNode]] = []
    bigger: List[OntologyNode] = []
    for nodes in relative_size_nodes:
        for node in nodes:
            for entry in bigger:
                node_to_relations.append(
                    Relation(relation_type=opposite_type,
                             first_slot=node,
                             second_slot=entry))
                node_to_relations.append(
                    Relation(relation_type=relation_type,
                             first_slot=entry,
                             second_slot=node))
        bigger.extend(nodes)
    return node_to_relations
Ejemplo n.º 2
0
def located(
    arg1s: Union[_ObjectT, Iterable[_ObjectT]],
    arg2s: Union[_ObjectT, Iterable[_ObjectT]],
    *,
    distance: Distance,
    # We need to do `Direction[Any]` because we can't infer the generic type
    # when a GeonAxis is used as the relative_to_axis.
    # Using Any here let's use isolate the type:ignores to this function.
    direction: Direction[Any],
) -> Tuple[Relation[_ObjectT]]:
    """
    All *arg1s* are located with at the given `Distance` and `Direction`
    with respect to *args2*.

    It is usually better to use more specialized relations derived using
    `make_opposite_dsl_region_relation`, etc.,
    but then can be useful when you need, for example, to refer to particular concrete axes.
    """
    arg1s = _ensure_iterable(arg1s)
    arg2s = _ensure_iterable(arg2s)

    return flatten([
        tuple(
            Relation(IN_REGION, arg1,
                     Region(arg2, distance=distance, direction=direction))
            for arg1 in arg1s for arg2 in arg2s),
        tuple(
            Relation(
                IN_REGION,
                arg2,
                Region(arg1, distance=distance,
                       direction=direction.opposite()),
            ) for arg1 in arg1s for arg2 in arg2s),
    ])
Ejemplo n.º 3
0
def test_dynamic_perception_graph_instantiation():
    ball = ObjectPerception("ball", _BALL_SCHEMA.geon.copy())
    table = ObjectPerception("table", axes=_TABLE_SCHEMA.axes.copy())

    first_frame = DevelopmentalPrimitivePerceptionFrame(
        perceived_objects=[ball, table],
        relations=[
            above(ball, table),
            Relation(IN_REGION, ball,
                     Region(table, distance=EXTERIOR_BUT_IN_CONTACT)),
            Relation(IN_REGION, table,
                     Region(ball, distance=EXTERIOR_BUT_IN_CONTACT)),
        ],
    )

    second_frame = DevelopmentalPrimitivePerceptionFrame(
        perceived_objects=[ball, table],
        relations=[Relation(IN_REGION, ball, Region(table, distance=DISTAL))],
    )

    perception_graph = PerceptionGraph.from_dynamic_perceptual_representation(
        PerceptualRepresentation(frames=[first_frame, second_frame]))
    assert perception_graph.dynamic

    # Ensure we don't attempt to handle more than two frames yet.
    with pytest.raises(ValueError):
        PerceptionGraph.from_dynamic_perceptual_representation(
            PerceptualRepresentation(
                frames=[first_frame, second_frame, second_frame]))
Ejemplo n.º 4
0
 def dsl_opposite_function(
     arg1s: Union[_ObjectT, Iterable[_ObjectT]],
     arg2s: Union[_ObjectT, Iterable[_ObjectT]],
 ) -> Tuple[Relation[_ObjectT], ...]:
     arg1s = _ensure_iterable(arg1s)
     arg2s = _ensure_iterable(arg2s)
     return flatten([
         tuple(
             Relation(relation_type, arg1, arg2) for arg1 in arg1s
             for arg2 in arg2s),
         tuple(
             Relation(opposite_type, arg2, arg1) for arg1 in arg1s
             for arg2 in arg2s),
     ])
Ejemplo n.º 5
0
 def dsl_relation_function(
     arg1s: Union[_ObjectT, Iterable[_ObjectT]],
     arg2s: Union[_ObjectT, Iterable[_ObjectT]],
     **kw_args,
 ) -> Tuple["Relation[_ObjectT]", ...]:
     arg1s = _ensure_iterable(arg1s)
     arg2s = _ensure_iterable(arg2s)
     return flatten([
         tuple(
             Relation(IN_REGION, arg1, region_factory(arg2, **kw_args))
             for arg1 in arg1s for arg2 in arg2s),
         tuple(
             Relation(IN_REGION, arg2, region_factory(arg1, **kw_args))
             for arg1 in arg1s for arg2 in arg2s),
     ])
Ejemplo n.º 6
0
def test_relations():
    # ball on a table
    ball = ObjectPerception("ball", _BALL_SCHEMA.geon.copy())
    table = ObjectPerception("table", axes=_TABLE_SCHEMA.axes.copy())

    PerceptualRepresentation.single_frame(
        DevelopmentalPrimitivePerceptionFrame(
            perceived_objects=[ball, table],
            relations=[
                above(ball, table),
                Relation(IN_REGION, ball,
                         Region(table, distance=EXTERIOR_BUT_IN_CONTACT)),
                Relation(IN_REGION, table,
                         Region(ball, distance=EXTERIOR_BUT_IN_CONTACT)),
            ],
        ))
def test_objects_in_something_not_implcitly_grounded():
    box = situation_object(ontology_node=BOX)
    ball = situation_object(ontology_node=BALL)
    situation = HighLevelSemanticsSituation(
        ontology=GAILA_PHASE_1_ONTOLOGY,
        salient_objects=[box, ball],
        always_relations=[
            Relation(
                IN_REGION,
                ball,
                Region(
                    box,
                    distance=PROXIMAL,
                    direction=Direction(
                        positive=True,
                        relative_to_axis=HorizontalAxisOfObject(box, index=0),
                    ),
                ),
            )
        ],
    )

    perception = _PERCEPTION_GENERATOR.generate_perception(
        situation, chooser=RandomChooser.for_seed(0)
    )
    first_frame = perception.frames[0]
    ball_perception = perception_with_handle(first_frame, "**ball_0")
    ground_perception = perception_with_handle(first_frame, "the ground")
    box_perception = perception_with_handle(first_frame, "**box_0")

    first_frame_relations = first_frame.relations

    assert on(ball_perception, ground_perception)[0] in first_frame_relations
    assert on(box_perception, ground_perception)[0] in first_frame_relations
Ejemplo n.º 8
0
 def _get_single_size_relation(self, relation: Relation[Any],
                               relation_set: ImmutableSet[Relation[Any]]):
     single_size_relation: Optional[Tuple[Any, str, Any]] = None
     if relation.relation_type in self._opposite_size_relations:
         if (Relation(
                 self._opposite_size_relations[relation.relation_type],
                 relation.second_slot,
                 relation.first_slot,
         ) in relation_set):
             if relation.relation_type == SMALLER_THAN:
                 single_size_relation = (
                     relation.second_slot,
                     ">",
                     relation.first_slot,
                 )
             elif relation.relation_type == BIGGER_THAN:
                 single_size_relation = (
                     relation.first_slot,
                     ">",
                     relation.second_slot,
                 )
             elif relation.relation_type == MUCH_SMALLER_THAN:
                 single_size_relation = (
                     relation.second_slot,
                     ">>",
                     relation.first_slot,
                 )
             else:
                 single_size_relation = (
                     relation.first_slot,
                     ">>",
                     relation.second_slot,
                 )
     return single_size_relation
Ejemplo n.º 9
0
def make_bird_flies_over_a_house():
    bird = situation_object(BIRD)
    house = situation_object(HOUSE)
    situation = HighLevelSemanticsSituation(
        ontology=GAILA_PHASE_1_ONTOLOGY,
        salient_objects=[bird, house],
        actions=[
            Action(
                FLY,
                argument_roles_to_fillers=[(AGENT, bird)],
                during=DuringAction(
                    at_some_point=[
                        Relation(
                            IN_REGION,
                            bird,
                            Region(
                                reference_object=house,
                                distance=DISTAL,
                                direction=Direction(
                                    positive=True,
                                    relative_to_axis=GRAVITATIONAL_AXIS_FUNCTION,
                                ),
                            ),
                        )
                    ]
                ),
            )
        ],
    )
    return situation
Ejemplo n.º 10
0
 def dsl_relation_function(
     arg1s: Union[_ObjectT, Iterable[_ObjectT]],
     arg2s: Union[_ObjectT, Iterable[_ObjectT]],
 ) -> Tuple[Relation[_ObjectT], ...]:
     return tuple(
         Relation(relation_type, arg1, arg2)
         for arg1 in _ensure_iterable(arg1s)
         for arg2 in _ensure_iterable(arg2s))
Ejemplo n.º 11
0
 def dsl_relation_function(
     arg1s: Union[_ObjectT, Iterable[_ObjectT]],
     arg2s: Union[_ObjectT, Iterable[_ObjectT]],
     **kw_args,
 ) -> Tuple["Relation[_ObjectT]", ...]:
     return tuple(
         Relation(IN_REGION, arg1, region_factory(arg2, **kw_args))
         for arg1 in _ensure_iterable(arg1s)
         for arg2 in _ensure_iterable(arg2s))
Ejemplo n.º 12
0
def test_difference():
    ball = ObjectPerception("ball", _BALL_SCHEMA.geon.copy())
    cup = ObjectPerception("cup", _make_cup_schema().geon.copy())

    table = ObjectPerception("table", axes=_TABLE_SCHEMA.axes.copy())

    first_frame = DevelopmentalPrimitivePerceptionFrame(
        perceived_objects=[ball, table],
        relations=[
            above(ball, table),
            Relation(IN_REGION, ball,
                     Region(table, distance=EXTERIOR_BUT_IN_CONTACT)),
            Relation(IN_REGION, table,
                     Region(ball, distance=EXTERIOR_BUT_IN_CONTACT)),
        ],
    )

    second_frame = DevelopmentalPrimitivePerceptionFrame(
        perceived_objects=[ball, table, cup], relations=[above(ball, table)])

    diff = diff_primitive_perception_frames(before=first_frame,
                                            after=second_frame)
    assert len(diff.removed_relations) == 2
    assert not diff.added_relations
    assert len(diff.added_objects) == 1
    assert not diff.removed_objects
    assert diff.before_axis_info == first_frame.axis_info
    assert diff.after_axis_info == second_frame.axis_info
    assert not diff.added_property_assertions
    assert not diff.removed_property_assertions

    # Reversed
    diff_2 = diff_primitive_perception_frames(before=second_frame,
                                              after=first_frame)
    assert len(diff_2.added_relations) == 2
    assert not diff_2.removed_relations
    assert not diff_2.added_objects
    assert len(diff_2.removed_objects) == 1
    assert diff_2.before_axis_info == second_frame.axis_info
    assert diff_2.after_axis_info == first_frame.axis_info
    assert not diff_2.added_property_assertions
    assert not diff_2.removed_property_assertions
def test_perceive_relations_during():
    learner_perception = _PERCEPTION_GENERATOR.generate_perception(
        make_bird_flies_over_a_house(), chooser=RandomChooser.for_seed(0)
    )
    assert learner_perception.during

    bird = perception_with_handle(learner_perception.frames[0], "**bird_0")
    house = perception_with_handle(learner_perception.frames[0], "**house_0")

    bird_over_the_house = Relation(
        IN_REGION,
        bird,
        Region(reference_object=house, distance=DISTAL, direction=GRAVITATIONAL_UP),
    )

    assert bird_over_the_house in learner_perception.during.at_some_point
def test_liquid_in_and_out_of_container():
    juice = situation_object(JUICE)
    box = situation_object(ontology_node=BOX)
    table = situation_object(ontology_node=TABLE)
    two_d_situation = HighLevelSemanticsSituation(
        ontology=GAILA_PHASE_1_ONTOLOGY,
        salient_objects=[juice, table],
        always_relations=[on(juice, table)],
    )
    two_d_perception = _PERCEPTION_GENERATOR.generate_perception(
        two_d_situation, chooser=RandomChooser.for_seed(0)
    )

    three_d_situation = HighLevelSemanticsSituation(
        ontology=GAILA_PHASE_1_ONTOLOGY,
        salient_objects=[juice, box],
        always_relations=[
            Relation(IN_REGION, juice, Region(box, distance=INTERIOR, direction=None))
        ],
    )
    three_d_perception = _PERCEPTION_GENERATOR.generate_perception(
        three_d_situation, chooser=RandomChooser.for_seed(0)
    )

    two_perceived_objects = two_d_perception.frames[0].perceived_objects
    two_object_handles = set(obj.debug_handle for obj in two_perceived_objects)
    assert all(handle in two_object_handles for handle in {"**juice_0", "**table_0"})
    three_perceived_objects = three_d_perception.frames[0].perceived_objects
    three_object_handles = set(obj.debug_handle for obj in three_perceived_objects)
    assert all(handle in three_object_handles for handle in {"**juice_0", "**box_0"})

    assert any(
        isinstance(p, HasBinaryProperty)
        and perception_with_handle(two_d_perception.frames[0], "**juice_0")
        == p.perceived_object
        and p.binary_property == TWO_DIMENSIONAL
        for p in two_d_perception.frames[0].property_assertions
    )
    assert not any(
        isinstance(p, HasBinaryProperty)
        and perception_with_handle(three_d_perception.frames[0], "**juice_0")
        == p.perceived_object
        and p.binary_property == TWO_DIMENSIONAL
        for p in three_d_perception.frames[0].property_assertions
    )
def test_grounding_of_unsupported_objects():
    box = situation_object(ontology_node=BOX)
    ball = situation_object(ontology_node=BALL)
    situation = HighLevelSemanticsSituation(
        ontology=GAILA_PHASE_1_ONTOLOGY,
        salient_objects=[box, ball],
        always_relations=[Relation(IN_REGION, ball, Region(box, distance=INTERIOR))],
    )

    perception = _PERCEPTION_GENERATOR.generate_perception(
        situation, chooser=RandomChooser.for_seed(0)
    )
    first_frame = perception.frames[0]
    ball_perception = perception_with_handle(first_frame, "**ball_0")
    ground_perception = perception_with_handle(first_frame, "the ground")
    box_perception = perception_with_handle(first_frame, "**box_0")

    first_frame_relations = first_frame.relations

    assert on(ball_perception, ground_perception)[0] not in first_frame_relations
    assert on(box_perception, ground_perception)[0] in first_frame_relations
def test_person_put_ball_on_table():
    person = situation_object(ontology_node=PERSON)
    ball = situation_object(ontology_node=BALL)
    table = situation_object(ontology_node=TABLE)
    situation = HighLevelSemanticsSituation(
        ontology=GAILA_PHASE_1_ONTOLOGY,
        salient_objects=[person, ball, table],
        actions=[
            # What is the best way of representing the destination in the high-level semantics?
            # Here we represent it as indicating a relation which should be true.
            Action(
                PUT,
                (
                    (AGENT, person),
                    (THEME, ball),
                    (
                        GOAL,
                        Region(
                            reference_object=table,
                            distance=EXTERIOR_BUT_IN_CONTACT,
                            direction=GRAVITATIONAL_UP,
                        ),
                    ),
                ),
            )
        ],
    )

    perception = _PERCEPTION_GENERATOR.generate_perception(
        situation, chooser=RandomChooser.for_seed(0)
    )
    assert len(perception.frames) == 2
    first_frame = perception.frames[0]
    person_perception = perception_with_handle(first_frame, "**person_0")
    ball_perception = perception_with_handle(first_frame, "**ball_0")
    table_perception = perception_with_handle(first_frame, "**table_0")
    hand_perception = perception_with_handle(first_frame, "**hand_0")
    assert (
        HasBinaryProperty(person_perception, ANIMATE) in first_frame.property_assertions
    )
    assert (
        HasBinaryProperty(person_perception, SELF_MOVING)
        in first_frame.property_assertions
    )
    assert (
        HasBinaryProperty(ball_perception, INANIMATE) in first_frame.property_assertions
    )
    assert (
        HasBinaryProperty(table_perception, INANIMATE) in first_frame.property_assertions
    )

    first_frame_relations = first_frame.relations
    second_frame_relations = perception.frames[1].relations

    # assert we generate at least some relations in each frame
    assert first_frame_relations
    assert second_frame_relations

    assert (
        Relation(SMALLER_THAN, ball_perception, person_perception)
        in first_frame_relations
    )
    # Disabled because of https://github.com/isi-vista/adam/issues/673
    # assert Relation(PART_OF, hand_perception, person_perception) in first_frame_relations
    assert (
        Relation(
            IN_REGION,
            ball_perception,
            Region(reference_object=hand_perception, distance=EXTERIOR_BUT_IN_CONTACT),
        )
        in first_frame_relations
    )

    # continuing relations:
    assert (
        Relation(SMALLER_THAN, ball_perception, person_perception)
        in second_frame_relations
    )

    # new relations:
    ball_on_table_relation = Relation(
        IN_REGION,
        ball_perception,
        Region(
            table_perception, distance=EXTERIOR_BUT_IN_CONTACT, direction=GRAVITATIONAL_UP
        ),
    )
    assert ball_on_table_relation in second_frame_relations

    # removed relations:
    assert (
        Relation(
            IN_REGION,
            ball_perception,
            Region(reference_object=hand_perception, distance=EXTERIOR_BUT_IN_CONTACT),
        )
        not in second_frame_relations
    )

    # proto-role properties
    assert (
        HasBinaryProperty(person_perception, VOLITIONALLY_INVOLVED)
        in first_frame.property_assertions
    )
    assert (
        HasBinaryProperty(person_perception, CAUSES_CHANGE)
        in first_frame.property_assertions
    )
    assert (
        HasBinaryProperty(ball_perception, UNDERGOES_CHANGE)
        in first_frame.property_assertions
    )
    assert (
        HasBinaryProperty(table_perception, STATIONARY) in first_frame.property_assertions
    )