예제 #1
0
def test_come(language_mode, learner):
    movee = standard_object(
        "movee",
        required_properties=[SELF_MOVING],
        banned_properties=[IS_SPEAKER, IS_ADDRESSEE],
    )
    learner_obj = standard_object("leaner_0", LEARNER)
    speaker = standard_object(
        "speaker",
        PERSON,
        banned_properties=[IS_SPEAKER, IS_ADDRESSEE],
        added_properties=[IS_SPEAKER],
    )
    object_ = standard_object("object_0",
                              THING,
                              banned_properties=[IS_SPEAKER, IS_ADDRESSEE])
    ground = standard_object("ground", root_node=GROUND)

    come_to_speaker = Phase1SituationTemplate(
        "come-to-speaker",
        salient_object_variables=[movee, speaker],
        actions=[
            Action(COME,
                   argument_roles_to_fillers=[(AGENT, movee), (GOAL, speaker)])
        ],
    )
    come_to_learner = Phase1SituationTemplate(
        "come-to-leaner",
        salient_object_variables=[movee],
        actions=[
            Action(COME,
                   argument_roles_to_fillers=[(AGENT, movee),
                                              (GOAL, learner_obj)])
        ],
    )
    come_to_object = Phase1SituationTemplate(
        "come-to-object",
        salient_object_variables=[movee, object_],
        actions=[
            Action(COME,
                   argument_roles_to_fillers=[(AGENT, movee), (GOAL, object_)])
        ],
    )
    for situation_template in [
            _make_come_down_template(movee, object_, speaker, ground,
                                     immutableset()),
            come_to_speaker,
            come_to_learner,
            come_to_object,
    ]:
        run_verb_test(
            learner(language_mode),
            situation_template,
            language_generator=phase1_language_generator(language_mode),
        )
예제 #2
0
def test_matching_static_vs_dynamic_graphs():
    target_object = BOX
    train_obj_object = object_variable("obj-with-color", target_object)
    obj_template = Phase1SituationTemplate(
        "colored-obj-object", salient_object_variables=[train_obj_object])
    template = all_possible(obj_template,
                            chooser=PHASE1_CHOOSER_FACTORY(),
                            ontology=GAILA_PHASE_1_ONTOLOGY)
    train_curriculum = phase1_instances("all obj situations",
                                        situations=template)
    perceptual_representation = only(train_curriculum.instances())[2]

    perception_graph = graph_without_learner(
        PerceptionGraph.from_frame(perceptual_representation.frames[0]))
    temporal_perception_graph = perception_graph.copy_with_temporal_scopes(
        temporal_scopes=[TemporalScope.AFTER])

    perception_pattern = PerceptionGraphPattern.from_graph(
        perception_graph).perception_graph_pattern

    temporal_perception_pattern = perception_pattern.copy_with_temporal_scopes(
        required_temporal_scopes=[TemporalScope.AFTER])

    # Test runtime error for matching static pattern against dynamic graph and vice versa

    with pytest.raises(RuntimeError):
        perception_pattern.matcher(temporal_perception_graph,
                                   match_mode=MatchMode.NON_OBJECT)

    with pytest.raises(RuntimeError):
        temporal_perception_pattern.matcher(perception_graph,
                                            match_mode=MatchMode.NON_OBJECT)
예제 #3
0
def _under_template(
    figure: TemplateObjectVariable,
    ground: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
    *,
    is_training: bool,
    is_distal: bool,
    syntax_hints: Iterable[str] = [],
) -> Phase1SituationTemplate:
    handle = "training" if is_training else "testing"
    # TODO: currently this hack keeps old implementation for English that hasn't solved https://github.com/isi-vista/adam/issues/802
    # and returns new implementation for Chinese that does solve this
    return Phase1SituationTemplate(
        f"preposition-{handle}-{figure.handle}-under-{ground.handle}",
        salient_object_variables=[figure, ground],
        background_object_variables=background,
        asserted_always_relations=[
            strictly_under(ground,
                           figure,
                           dist=DISTAL if is_distal else PROXIMAL)
        ],
        constraining_relations=[bigger_than(ground, figure)],
        gazed_objects=[figure],
        syntax_hints=syntax_hints,
    )
예제 #4
0
def _in_front_template(
    figure: TemplateObjectVariable,
    ground: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
    *,
    is_training: bool,
    is_near: bool,
    speaker_root_node: OntologyNode = PERSON,
) -> Phase1SituationTemplate:
    handle = "training" if is_training else "testing"
    direction = Direction(positive=True,
                          relative_to_axis=FacingAddresseeAxis(ground))
    speaker = standard_object("speaker",
                              speaker_root_node,
                              added_properties=[IS_SPEAKER])
    addressee = standard_object("addressee",
                                LEARNER,
                                added_properties=[IS_ADDRESSEE])
    computed_background = [speaker, addressee]
    computed_background.extend(background)
    return Phase1SituationTemplate(
        f"preposition-{handle}-{figure.handle}-behind-{ground.handle}",
        salient_object_variables=[figure, ground],
        background_object_variables=computed_background,
        asserted_always_relations=[
            near(figure, ground, direction=direction)
            if is_near else far(figure, ground, direction=direction)
        ],
        gazed_objects=[figure],
    )
예제 #5
0
def _under_template(
    figure: TemplateObjectVariable,
    ground: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
    *,
    is_training: bool,
    is_distal: bool,
    syntax_hints: Iterable[str] = immutableset(),
    background_relations: Iterable[TemplateObjectVariable] = immutableset(),
) -> Phase1SituationTemplate:
    handle = "training" if is_training else "testing"
    relations = [
        negate(on(figure, GROUND_OBJECT_TEMPLATE)),
        strictly_under(ground, figure, dist=DISTAL if is_distal else PROXIMAL),
    ]
    relations.extend(background_relations)  # type: ignore
    return Phase1SituationTemplate(
        f"preposition-{handle}-{figure.handle}-under-{ground.handle}",
        salient_object_variables=[figure, ground],
        background_object_variables=background,
        asserted_always_relations=flatten_relations(relations),
        constraining_relations=[bigger_than(ground, figure)],
        gazed_objects=[figure],
        syntax_hints=syntax_hints,
    )
예제 #6
0
def _go_under_template(
    agent: TemplateObjectVariable,
    goal_object: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
    *,
    is_distal: bool,  # pylint:disable=unused-argument
) -> Phase1SituationTemplate:
    return Phase1SituationTemplate(
        f"go_under-{agent.handle}-under-{goal_object.handle}",
        salient_object_variables=[agent, goal_object],
        background_object_variables=background,
        actions=[
            Action(
                GO,
                argument_roles_to_fillers=[
                    (AGENT, agent),
                    (
                        GOAL,
                        Region(
                            goal_object, distance=PROXIMAL, direction=GRAVITATIONAL_DOWN
                        ),
                    ),
                ],
            )
        ],
        before_action_relations=[negate(on(goal_object, GROUND_OBJECT_TEMPLATE))],
        asserted_always_relations=[negate(on(goal_object, GROUND_OBJECT_TEMPLATE))],
        after_action_relations=[
            negate(on(goal_object, GROUND_OBJECT_TEMPLATE)),
            near(agent, goal_object),
        ],
        constraining_relations=flatten_relations(bigger_than(goal_object, agent)),
    )
예제 #7
0
def _beside_template(
    figure: TemplateObjectVariable,
    ground: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
    *,
    is_right: bool,
    is_training: bool,
) -> Phase1SituationTemplate:
    direction_str = "right" if is_right else "left"
    handle = "training" if is_training else "testing"
    return Phase1SituationTemplate(
        f"preposition-{handle}-{figure.handle}-beside-{ground.handle}-{direction_str}",
        salient_object_variables=[figure, ground],
        background_object_variables=background,
        asserted_always_relations=[
            near(
                figure,
                ground,
                direction=Direction(
                    positive=is_right,
                    relative_to_axis=HorizontalAxisOfObject(ground, index=0),
                ),
            )
        ],
        gazed_objects=[figure],
    )
예제 #8
0
def test_two_objects():
    two_object_template = Phase1SituationTemplate(
        "two-objects",
        salient_object_variables=[
            object_variable("person", root_node=_PERSON),
            object_variable("toy_vehicle", required_properties=[_TOY_VEHICLE]),
        ],
    )

    reference_object_sets = {
        immutableset(["mom", "toy_truck"]),
        immutableset(["dad", "toy_truck"]),
        immutableset(["learner", "toy_truck"]),
        immutableset(["mom", "toy_car"]),
        immutableset(["dad", "toy_car"]),
        immutableset(["learner", "toy_car"]),
    }

    generated_object_sets = set(
        immutableset(situation_object.ontology_node.handle
                     for situation_object in situation.salient_objects)
        for situation in all_possible(
            two_object_template,
            ontology=_TESTING_ONTOLOGY,
            chooser=RandomChooser.for_seed(0),
            default_addressee_node=_LEARNER,
        ))

    assert generated_object_sets == reference_object_sets
예제 #9
0
def test_before_after_relations_asserted():
    ball = object_variable("ball", root_node=BALL)
    box = object_variable("box", root_node=BOX)
    ground = object_variable("ground", root_node=GROUND)

    template_action = Phase1SituationTemplate(
        "Before/After Relation",
        salient_object_variables=[ball, box],
        background_object_variables=[ground],
        actions=[
            Action(
                ROLL,
                argument_roles_to_fillers=[(AGENT, ball)],
                auxiliary_variable_bindings=[(ROLL_SURFACE_AUXILIARY, ground)],
            )
        ],
        before_action_relations=flatten_relations([on(ball, box)]),
        after_action_relations=flatten_relations([far(ball, box)]),
    )

    situation_with_relations = tuple(
        sampled(
            template_action,
            ontology=GAILA_PHASE_1_ONTOLOGY,
            chooser=RandomChooser.for_seed(0),
            max_to_sample=1,
        ))

    assert situation_with_relations[0].before_action_relations
    assert situation_with_relations[0].after_action_relations
예제 #10
0
def _put_on_body_part_template(
    # X puts Y on body part
    agent: TemplateObjectVariable,
    theme: TemplateObjectVariable,
    goal_reference: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
) -> Phase1SituationTemplate:
    return Phase1SituationTemplate(
        f"{agent.handle}-puts-{theme.handle}-on-{goal_reference.handle}",
        salient_object_variables=[agent, theme, goal_reference],
        background_object_variables=background,
        actions=[
            Action(
                PUT,
                argument_roles_to_fillers=[
                    (AGENT, agent),
                    (THEME, theme),
                    (
                        GOAL,
                        Region(
                            goal_reference,
                            distance=EXTERIOR_BUT_IN_CONTACT,
                            direction=GRAVITATIONAL_UP,
                        ),
                    ),
                ],
            )
        ],
        constraining_relations=flatten_relations(
            bigger_than([agent, goal_reference], theme)
        ),
        asserted_always_relations=flatten_relations(has(agent, goal_reference)),
    )
예제 #11
0
def _make_multiple_object_template(
    target: TemplateObjectVariable, background: Iterable[TemplateObjectVariable]
) -> Phase1SituationTemplate:
    return Phase1SituationTemplate(
        "object-falls",
        salient_object_variables=[target],
        background_object_variables=background,
    )
예제 #12
0
def test_successfully_extending_partial_match():
    """
    Tests whether we can match a perception pattern against a perception graph
    when initializing the search from a partial match.
    """

    target_object = BOX
    # Create train and test templates for the target objects
    train_obj_object = object_variable("obj-with-color", target_object)

    obj_template = Phase1SituationTemplate(
        "colored-obj-object", salient_object_variables=[train_obj_object])
    template = all_possible(obj_template,
                            chooser=PHASE1_CHOOSER_FACTORY(),
                            ontology=GAILA_PHASE_1_ONTOLOGY)

    train_curriculum = phase1_instances("all obj situations",
                                        situations=template)

    perceptual_representation = only(train_curriculum.instances())[2]

    # Original perception graph
    perception = PerceptionGraph.from_frame(
        perceptual_representation.frames[0])

    # Create a perception pattern for the whole thing
    # and also a perception pattern for a subset of the whole pattern
    whole_perception_pattern = PerceptionGraphPattern.from_graph(
        perception).perception_graph_pattern

    partial_digraph = whole_perception_pattern.copy_as_digraph()
    partial_digraph.remove_nodes_from([
        node for node in partial_digraph.nodes
        if isinstance(node, IsColorNodePredicate)
    ])
    partial_perception_pattern = PerceptionGraphPattern(partial_digraph)

    # get our initial match by matching the partial pattern
    matcher = partial_perception_pattern.matcher(
        perception, match_mode=MatchMode.NON_OBJECT)

    partial_match: PerceptionGraphPatternMatch = first(
        matcher.matches(use_lookahead_pruning=True))
    partial_mapping = partial_match.pattern_node_to_matched_graph_node

    # Try to extend the partial mapping, to create a complete mapping
    matcher_2 = whole_perception_pattern.matcher(
        perception, match_mode=MatchMode.NON_OBJECT)
    complete_match: PerceptionGraphPatternMatch = first(
        matcher_2.matches(initial_partial_match=partial_mapping,
                          use_lookahead_pruning=True),
        None,
    )
    complete_mapping = complete_match.pattern_node_to_matched_graph_node
    assert len(complete_mapping) == len(perception.copy_as_digraph().nodes)
    assert len(complete_mapping) == len(
        whole_perception_pattern.copy_as_digraph().nodes)
예제 #13
0
def _make_drink_cups_curriculum(
    num_samples: Optional[int],
    noise_objects: Optional[int],
    language_generator: LanguageGenerator[
        HighLevelSemanticsSituation, LinearizedDependencyTree
    ],
) -> Phase1InstanceGroup:

    templates = []
    for cup in [CUP, CUP_2, CUP_3, CUP_4]:
        cup_obj = standard_object("cup", cup)
        liquid_0 = object_variable("liquid_0", required_properties=[LIQUID])
        person_0 = standard_object(
            "person_0", PERSON, banned_properties=[IS_SPEAKER, IS_ADDRESSEE]
        )

        templates.append(
            Phase1SituationTemplate(
                "drink-cup",
                salient_object_variables=[liquid_0, person_0, cup_obj],
                background_object_variables=make_noise_objects(noise_objects),
                actions=[
                    Action(
                        DRINK,
                        argument_roles_to_fillers=[(AGENT, person_0), (THEME, liquid_0)],
                        auxiliary_variable_bindings=[(DRINK_CONTAINER_AUX, cup_obj)],
                    )
                ],
                asserted_always_relations=[inside(liquid_0, cup_obj)],
            )
        )

    return phase2_instances(
        "drink - cup",
        chain(
            *[
                sampled(
                    cup_template,
                    chooser=PHASE1_CHOOSER_FACTORY(),
                    ontology=GAILA_PHASE_2_ONTOLOGY,
                    max_to_sample=num_samples,
                    block_multiple_of_the_same_type=True,
                )
                if num_samples
                else all_possible(
                    cup_template,
                    chooser=PHASE1_CHOOSER_FACTORY(),
                    ontology=GAILA_PHASE_2_ONTOLOGY,
                )
                for cup_template in templates
            ]
        ),
        perception_generator=GAILA_PHASE_2_PERCEPTION_GENERATOR,
        language_generator=language_generator,
    )
def _big_x_template(theme: TemplateObjectVariable,
                    noise_objects: Optional[int]) -> Phase1SituationTemplate:
    learner = learner_template_factory()
    computed_background = [learner]
    computed_background.extend(make_noise_objects(noise_objects))
    return Phase1SituationTemplate(
        f"big-{theme.handle}",
        salient_object_variables=[theme],
        background_object_variables=computed_background,
        asserted_always_relations=[bigger_than(theme, learner)],
    )
예제 #15
0
 def single_object_described_template(
     target: TemplateObjectVariable,
     *,
     background_objects: Iterable[TemplateObjectVariable] = immutableset(),
     background_relations: Iterable[Relation[Any]] = immutableset(),
 ) -> Phase1SituationTemplate:
     return Phase1SituationTemplate(
         name=f"single-object-{target.handle}",
         salient_object_variables=[target],
         background_object_variables=background_objects,
         asserted_always_relations=background_relations,
         syntax_hints=[IGNORE_COLORS],
     )
def _short_x_template(theme: TemplateObjectVariable,
                      noise_objects: Optional[int]) -> Phase1SituationTemplate:
    learner = learner_template_factory()
    computed_background = [learner]
    computed_background.extend(make_noise_objects(noise_objects))

    # TODO: This difference should be an axis size but we can't yet
    # implement that. See: https://github.com/isi-vista/adam/issues/832
    return Phase1SituationTemplate(
        f"tall-{theme.handle}",
        salient_object_variables=[theme],
        background_object_variables=computed_background,
        asserted_always_relations=[bigger_than(learner, theme)],
        syntax_hints=[USE_VERTICAL_MODIFIERS],
    )
예제 #17
0
def _on_template(
    figure: TemplateObjectVariable,
    ground: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
    *,
    is_training: bool,
) -> Phase1SituationTemplate:
    handle = "training" if is_training else "testing"
    return Phase1SituationTemplate(
        f"preposition-{handle}-{figure.handle}-on-{ground.handle}",
        salient_object_variables=[figure, ground],
        background_object_variables=background,
        asserted_always_relations=[on(figure, ground)],
        gazed_objects=[figure],
    )
예제 #18
0
 def object_with_color(
     target_with_color: TemplateObjectVariable,
     *,
     background_objects: Iterable[TemplateObjectVariable] = immutableset(),
     background_relations: Iterable[Relation[Any]] = immutableset(),
 ) -> Phase1SituationTemplate:
     return Phase1SituationTemplate(
         name=f"single-attribute-color-{target_with_color.handle}",
         salient_object_variables=[target_with_color],
         background_object_variables=background_objects
         if add_noise
         else immutableset(),
         asserted_always_relations=background_relations
         if add_noise
         else immutableset(),
     )
예제 #19
0
def _on_template(
    figure: TemplateObjectVariable,
    ground: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
    *,
    is_training: bool,
    background_relations: Iterable[Relation[Any]] = immutableset(),
) -> Phase1SituationTemplate:
    handle = "training" if is_training else "testing"
    relations = [on(figure, ground)]
    relations.extend(background_relations)  # type: ignore
    return Phase1SituationTemplate(
        f"preposition-{handle}-{figure.handle}-on-{ground.handle}",
        salient_object_variables=[figure, ground],
        background_object_variables=background,
        asserted_always_relations=flatten_relations(relations),
        gazed_objects=[figure],
    )
예제 #20
0
def test_perception_graph_post_init_edge_cases():
    target_object = BOX
    train_obj_object = object_variable("obj-with-color", target_object)
    obj_template = Phase1SituationTemplate(
        "colored-obj-object", salient_object_variables=[train_obj_object])
    template = all_possible(obj_template,
                            chooser=PHASE1_CHOOSER_FACTORY(),
                            ontology=GAILA_PHASE_1_ONTOLOGY)
    train_curriculum = phase1_instances("all obj situations",
                                        situations=template)
    perceptual_representation = only(train_curriculum.instances())[2]
    perception_graph = graph_without_learner(
        PerceptionGraph.from_frame(perceptual_representation.frames[0]))
    temporal_perception_graph = perception_graph.copy_with_temporal_scopes(
        temporal_scopes=[TemporalScope.AFTER])
    temporal_digraph = temporal_perception_graph.copy_as_digraph()
    # Test valid edge label
    # The only feasible test seems to be the instation, since creating a corrupt instance throws the same RuntimeError
    with pytest.raises(RuntimeError):
        TemporallyScopedEdgeLabel(None)

    # In a dynamic graph, all edge labels must be wrapped in TemporallyScopedEdgeLabel
    new_graph = DiGraph()
    for (source, target) in temporal_digraph.edges():
        new_graph.add_edge(source, target)
        new_graph[source][target]["label"] = None
    with pytest.raises(RuntimeError):
        PerceptionGraph(new_graph, dynamic=True)

    # TemporallyScopedEdgeLabels may not appear in a static graph
    new_graph = DiGraph()
    for (source, target) in temporal_digraph.edges():
        new_graph.add_edge(source, target)
        new_graph[source][target]["label"] = TemporallyScopedEdgeLabel(
            "attribute", [TemporalScope.AFTER])
    with pytest.raises(RuntimeError):
        PerceptionGraph(new_graph)

    # Every edge in a PerceptionGraph must have a 'label
    new_graph = DiGraph()
    for (source, target) in temporal_digraph.edges():
        new_graph.add_edge(source, target)
    with pytest.raises(RuntimeError):
        PerceptionGraph(new_graph)
예제 #21
0
def test_recognize_in_transfer_of_possession(language_mode):
    dad = object_variable("person_0", DAD)
    baby = object_variable("person_1", BABY)
    chair = object_variable("give_object_0", CHAIR)

    giving_template = Phase1SituationTemplate(
        "dad-transfer-of-possession",
        salient_object_variables=[dad, baby, chair],
        actions=[
            Action(
                GIVE,
                argument_roles_to_fillers=[(AGENT, dad), (GOAL, baby),
                                           (THEME, chair)],
            )
        ],
        syntax_hints=[PREFER_DITRANSITIVE],
    )

    (_, _, perception) = first(
        phase1_instances(
            "foo",
            sampled(
                giving_template,
                max_to_sample=1,
                chooser=PHASE1_CHOOSER_FACTORY(),
                ontology=GAILA_PHASE_1_ONTOLOGY,
                block_multiple_of_the_same_type=True,
            ),
        ).instances())

    perception_graph = PerceptionGraph.from_dynamic_perceptual_representation(
        perception)
    perception_semantic_alignment = PerceptionSemanticAlignment.create_unaligned(
        perception_graph)
    (_, description_to_matched_semantic_node
     ) = LANGUAGE_MODE_TO_OBJECT_RECOGNIZER[language_mode].match_objects(
         perception_semantic_alignment)
    assert len(description_to_matched_semantic_node) == 4
    assert (language_mode == LanguageMode.ENGLISH and
            ("Dad", ) in description_to_matched_semantic_node) or (
                language_mode == LanguageMode.CHINESE and
                ("ba4 ba4", ) in description_to_matched_semantic_node)
예제 #22
0
def _fly_over_template(
    # A bird flies over a ball
    agent: TemplateObjectVariable,
    object_in_path: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
) -> Phase1SituationTemplate:
    return Phase1SituationTemplate(
        f"{agent.handle}-flies-over-{object_in_path.handle}",
        salient_object_variables=[agent, object_in_path],
        background_object_variables=background,
        actions=[
            Action(
                FLY,
                argument_roles_to_fillers=[(AGENT, agent)],
                during=DuringAction(
                    at_some_point=flatten_relations(strictly_above(agent, object_in_path))
                ),
            )
        ],
    )
예제 #23
0
def _go_in_template(
    agent: TemplateObjectVariable,
    goal_object: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
) -> Phase1SituationTemplate:
    return Phase1SituationTemplate(
        f"go_in-{agent.handle}-in-{goal_object.handle}",
        salient_object_variables=[agent, goal_object],
        background_object_variables=background,
        actions=[
            Action(
                GO,
                argument_roles_to_fillers=[
                    (AGENT, agent),
                    (GOAL, Region(goal_object, distance=INTERIOR)),
                ],
            )
        ],
        constraining_relations=flatten_relations(bigger_than(goal_object, agent)),
        after_action_relations=[inside(agent, goal_object)],
    )
예제 #24
0
def _go_to_template(
    agent: TemplateObjectVariable,
    goal_object: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
) -> Phase1SituationTemplate:
    return Phase1SituationTemplate(
        f"go_to-{agent.handle}-to-{goal_object.handle}",
        salient_object_variables=[agent, goal_object],
        background_object_variables=background,
        actions=[
            Action(
                GO,
                argument_roles_to_fillers=[
                    (AGENT, agent),
                    (GOAL, Region(goal_object, distance=PROXIMAL)),
                ],
            )
        ],
        after_action_relations=[near(agent, goal_object)],
        gazed_objects=[agent],
    )
예제 #25
0
def _jump_over_template(
    # "Mom jumps over a ball"
    agent: TemplateObjectVariable,
    object_in_path: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
) -> Phase1SituationTemplate:
    return Phase1SituationTemplate(
        f"{agent.handle}-jumps-over-{object_in_path.handle}",
        salient_object_variables=[agent, object_in_path],
        background_object_variables=background,
        actions=[
            Action(
                JUMP,
                argument_roles_to_fillers=[(AGENT, agent)],
                during=DuringAction(
                    at_some_point=flatten_relations(
                        strictly_above(agent, object_in_path)
                    ),
                    objects_to_paths=[
                        (
                            agent,
                            SpatialPath(
                                operator=VIA,
                                reference_source_object=Region(
                                    object_in_path,
                                    direction=GRAVITATIONAL_UP,
                                    distance=DISTAL,
                                ),
                                reference_destination_object=GROUND_OBJECT_TEMPLATE,
                            ),
                        )
                    ],
                ),
                auxiliary_variable_bindings=[
                    (JUMP_INITIAL_SUPPORTER_AUX, GROUND_OBJECT_TEMPLATE)
                ],
            )
        ],
        asserted_always_relations=[negate(on(agent, GROUND_OBJECT_TEMPLATE))],
    )
예제 #26
0
def test_copy_with_temporal_scopes_content():
    """
    Tests whether copy_with_temporal_scopes converts graphs to be dynamic as intended
    """

    # We use a situation to generate the perceptual representation
    # for a box with color.
    target_object = BOX
    train_obj_object = object_variable("obj-with-color", target_object)
    obj_template = Phase1SituationTemplate(
        "colored-obj-object", salient_object_variables=[train_obj_object])
    template = all_possible(obj_template,
                            chooser=PHASE1_CHOOSER_FACTORY(),
                            ontology=GAILA_PHASE_1_ONTOLOGY)

    train_curriculum = phase1_instances("all obj situations",
                                        situations=template)

    perceptual_representation = only(train_curriculum.instances())[2]

    perception_graph = graph_without_learner(
        PerceptionGraph.from_frame(perceptual_representation.frames[0]))
    temporal_perception_graph = perception_graph.copy_with_temporal_scopes(
        temporal_scopes=[TemporalScope.AFTER])
    for (source, target) in perception_graph.copy_as_digraph().edges():
        assert not isinstance(
            perception_graph.copy_as_digraph()[source][target]["label"],
            TemporallyScopedEdgeLabel,
        )
    for (source,
         target) in temporal_perception_graph.copy_as_digraph().edges():
        # Check type, and then the content
        label = temporal_perception_graph.copy_as_digraph(
        )[source][target]["label"]
        assert isinstance(label, TemporallyScopedEdgeLabel)
        assert (label.attribute == perception_graph.copy_as_digraph()[source]
                [target]["label"])
        assert all(specifier in [TemporalScope.AFTER]
                   for specifier in label.temporal_specifiers)
예제 #27
0
def _put_in_template(
    agent: TemplateObjectVariable,
    theme: TemplateObjectVariable,
    goal_reference: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
) -> Phase1SituationTemplate:
    return Phase1SituationTemplate(
        f"{agent.handle}-puts-{theme.handle}-in-{goal_reference.handle}",
        salient_object_variables=[agent, theme, goal_reference],
        background_object_variables=background,
        actions=[
            Action(
                PUT,
                argument_roles_to_fillers=[
                    (AGENT, agent),
                    (THEME, theme),
                    (GOAL, Region(goal_reference, distance=INTERIOR)),
                ],
            )
        ],
        constraining_relations=flatten_relations(bigger_than(goal_reference, theme)),
    )
예제 #28
0
def _fly_under_template(
    # A bird flies under a chair
    agent: TemplateObjectVariable,
    object_in_path: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
) -> Phase1SituationTemplate:
    return Phase1SituationTemplate(
        f"{agent.handle}-flies-under-{object_in_path.handle}",
        salient_object_variables=[agent, object_in_path],
        background_object_variables=background,
        actions=[
            Action(
                FLY,
                argument_roles_to_fillers=[(AGENT, agent)],
                during=DuringAction(
                    at_some_point=flatten_relations(strictly_above(object_in_path, agent))
                ),
            )
        ],
        asserted_always_relations=[negate(on(object_in_path, GROUND_OBJECT_TEMPLATE))],
        before_action_relations=[negate(on(object_in_path, GROUND_OBJECT_TEMPLATE))],
        after_action_relations=[negate(on(object_in_path, GROUND_OBJECT_TEMPLATE))],
        constraining_relations=flatten_relations(bigger_than(object_in_path, agent)),
    )
예제 #29
0
def _in_front_template(
    figure: TemplateObjectVariable,
    ground: TemplateObjectVariable,
    background: Iterable[TemplateObjectVariable],
    *,
    is_training: bool,
    is_near: bool,
    background_relations: Iterable[Relation[Any]] = immutableset(),
) -> Phase1SituationTemplate:
    handle = "training" if is_training else "testing"
    direction = Direction(positive=True,
                          relative_to_axis=FacingAddresseeAxis(ground))
    relations = [
        near(figure, ground, direction=direction)
        if is_near else far(figure, ground, direction=direction)
    ]
    relations.extend(background_relations)  # type: ignore
    return Phase1SituationTemplate(
        f"preposition-{handle}-{figure.handle}-behind-{ground.handle}",
        salient_object_variables=[figure, ground],
        background_object_variables=background,
        asserted_always_relations=flatten_relations(relations),
        gazed_objects=[figure],
    )
예제 #30
0
def test_learner_as_default_addressee():
    learner = object_variable("learner", root_node=LEARNER)
    ball = object_variable("ball", root_node=BALL)
    template_with_learner = Phase1SituationTemplate(
        "template with learner",
        salient_object_variables=[learner, ball],
        asserted_always_relations=[near(learner, ball)],
    )

    template_with_out_learner = Phase1SituationTemplate(
        "template with out learner",
        salient_object_variables=[object_variable("ball", root_node=BALL)],
    )

    template_with_addressee = Phase1SituationTemplate(
        "template with addressee",
        salient_object_variables=[
            object_variable("mom",
                            root_node=MOM,
                            added_properties=[IS_ADDRESSEE])
        ],
    )

    situation_with_learner = tuple(
        sampled(
            template_with_learner,
            ontology=GAILA_PHASE_1_ONTOLOGY,
            chooser=RandomChooser.for_seed(0),
            max_to_sample=1,
        ))

    situation_with_out_learner = tuple(
        sampled(
            template_with_out_learner,
            ontology=GAILA_PHASE_1_ONTOLOGY,
            chooser=RandomChooser.for_seed(0),
            max_to_sample=1,
        ))

    situation_with_addressee = tuple(
        sampled(
            template_with_addressee,
            ontology=GAILA_PHASE_1_ONTOLOGY,
            chooser=RandomChooser.for_seed(0),
            max_to_sample=1,
        ))

    for object_ in situation_with_learner[0].all_objects:
        if object_.ontology_node == LEARNER:
            assert IS_ADDRESSEE in object_.properties
            break

    assert situation_with_learner[0].axis_info
    assert situation_with_learner[0].axis_info.addressee

    assert len(situation_with_out_learner[0].all_objects) == 2

    for object_ in situation_with_out_learner[0].all_objects:
        if object_.ontology_node == LEARNER:
            assert IS_ADDRESSEE in object_.properties
            break

    assert situation_with_out_learner[0].axis_info
    assert situation_with_out_learner[0].axis_info.addressee

    for object_ in situation_with_addressee[0].all_objects:
        if object_.ontology_node == LEARNER:
            assert False

    assert situation_with_addressee[0].axis_info
    assert situation_with_addressee[0].axis_info.addressee