Пример #1
0
def switch(new_prop, fix_props, model):
    """new version of switch for dictionaries.
    First, tries to swap the object and subject if the relation is the
    opposite of the required relation.(find_rel_prop will return the
    relation between the subj and object.)
    Calls swap with the subject and the object. Checks if the resulting
    model has any conflicts, if not returns it. If there were any conflicts,
    set the new_mod to the result of move with the subject.
    move will change the position of the subject to make the premise true in
    the model. Returns new_mod if conflict-free.
    After that it tries the same thing with moving the object.
    If nothing works, returns None
    """
    if PRINT_VERIFICATION:
        print("switch with new_prop, fixprops, model: ", new_prop, fix_props, model)
    relation = helper.get_relation(new_prop)
    # just use the string of the item
    subj = helper.get_subject(new_prop)
    obj = helper.get_object(new_prop)
    # call new_find_item with a list!!!
    s_coord = helper.find_first_item(subj, [model])[0] # s_coord + o_coord are tuples
    o_coord = helper.find_first_item(obj, [model])[0]# only get the coordinates
    # check if the relation of subj and obj is converse to the relation of the proposition.
    if find_rel_prop(s_coord, o_coord) == helper.convert(relation):
        # only if first condition holds, try to swap the items.
        new_mod = modify.swap(subj, s_coord, obj, o_coord, model)
        if PRINT_VERIFICATION:
            print("switch: model, new_mod after swap:", model, new_mod)
        if new_mod != None:
            # if there are now conflicting props in the new model, return it
            if not conflict_props(fix_props, new_mod):
                if PRINT_VERIFICATION:
                    print("no conflicts found in the model")
                return new_mod
            if PRINT_VERIFICATION:
                print("model + new_model after swap+conflict:", model, new_mod)
    # move the subject and check if there are any conflicting propositions
    new_mod = modify.move(subj, s_coord, relation, o_coord, model)
    if PRINT_VERIFICATION:
        print("new_mod after move: ", new_mod)
    # revise condition!!!
    if new_mod and (new_mod != None) and not conflict_props(fix_props, new_mod):
        return new_mod
    if PRINT_VERIFICATION:
        print("model + new_model after move subject:", model, new_mod)
    # move the subject and check if there are any conflicting props
    new_mod = modify.move(obj, o_coord, helper.convert(relation), s_coord, model)
    if PRINT_VERIFICATION:
        print("new_mod after move: ", new_mod)
    if (new_mod != None) and not conflict_props(fix_props, new_mod):
        return new_mod
    return None  # nothing worked
Пример #2
0
def refers(item, prem):
    """
    Function returns True if item, which is a list (like ["A"]), is a
    subject or object of the premise, otherwise False.
    """
    prop = parser.Parser(False).parse(prem)
    subj = [helper.get_subject(prop)]
    obj = [helper.get_object(prop)]
    if not isinstance(item, list):
        item = [item]
    if ((item == subj) or (item == obj)):
        return True
    return False
Пример #3
0
def verify_spatial(proposition, model):
    """
    Extracts the relation, subject and object from the proposition. Then
    searches the subj + obj in the model. Returns the model if the relation
    between the subj and obj is correctly represented in the model. Iterates
    through the relation and checks the corresponding subj and obj coordinates
    for the axis of the relation.
    e.g. for relation[0] = 1, checks if the subj_coords and obj_coords at the
    corresponding index do satisfy the relation.
    If the relation is 0 but the obj and subj coordinates are different,
    verification fails aswell.
    Returns None if the relation does not hold.
    """
    if PRINT_VERIFICATION:
        print("call verify_spatial with prop, model: ", proposition, model)
    relation = helper.get_relation(proposition)
    subj = helper.get_subject(proposition)
    obj = helper.get_object(proposition)
    subj_coords = helper.find_first_item(subj, [model])[0]
    obj_coords = helper.find_first_item(obj, [model])[0]
    if PRINT_VERIFICATION:
        print("verify_spatial: subj_coords, obj_coords, relation",
              subj_coords, obj_coords, relation)
    # iterate through the relation and the coordinates of the objects.
    for index, value in enumerate(relation):
        # if the relation is != 0, check if the relation holds in this axis
        if (value > 0) and (subj_coords[index] <= obj_coords[index]):
            # if the subject coords are < than obj coords in rel axis, relation
            # does not hold!
            if PRINT_VERIFICATION:
                print("verify_spatial: relation does not hold, return None")
            return None
        if (value < 0) and (subj_coords[index] >= obj_coords[index]):
            # the same for the opposite relation, this is the case when verify_temporal fails
            if PRINT_VERIFICATION:
                print("verify_spatial: relation does not hold, return None")
            return None
        if (value == 0) and (subj_coords[index] != obj_coords[index]):
            # the items must not be at the same position!
            if PRINT_VERIFICATION:
                print("verify_spatial: objects are on a different line in another axis ")
            return None
    if PRINT_VERIFICATION:
        print("verify_spatial: succesfully verified, return the model")
    return model
Пример #4
0
def other_ref_than(term, prem):
    """
    Function returns reference from premise other than the given term, and None
    if the Term doesn´t occur in the premise.
    Example: For prem "A happens before B" and term ["B"], return ["A"].
    """
    if PRINT_BACKTRACK:
        print("function call - other_ref_than with term", term, "and prem",
              prem)
    prop = parser.Parser(False).parse(prem)
    subj = [helper.get_subject(prop)]
    obj = [helper.get_object(prop)]
    if not isinstance(term, list):
        term = [term]
    if term == subj:
        return obj
    if term == obj:
        return subj
    else:
        if PRINT_BACKTRACK:
            print("other_ref_than: Nothing found, return None")
        return None
Пример #5
0
def conflict(premises, model, spatial_parser):
    """
    Finds all premises that are conflicting with the given model.
    Iterates over premises and parses them each. If the premises can't be
    parsed or the subject and the object are in the model, try to verify_temporal the
    premise(prop) with verify_spatial.
    If it can't be verified, add the premise(prop) to the result list of
    conflicted props. Returns a list of conflicting premises.
    """
    if PRINT_VERIFICATION:
        print("conflict: prems, model: ", premises, model)
    if spatial_parser:
        pars = parser.Parser(True)
    else:
        pars = parser.Parser(False)
    if not premises:
        return None
    result_list = []
    for prem in premises:
        prop = pars.parse(prem)
        subj = helper.get_subject(prop)
        obj = helper.get_object(prop)
        if PRINT_VERIFICATION:
            print("conflict: subj, obj", subj, obj)
        #check if the premise can be parsed(should be always the case)
        # and the subject  and object are in the model.
        # call new_find_item with a list!!!
        if(prop is None) or ((helper.find_first_item(subj, [model]))
                             and (helper.find_first_item(obj, [model]))):
            #if subj + obj are in the model, try to verify_temporal. if verify_temporal
            # returns false, add the proposition to the conflicted props.
            if not verify_spatial(prop, model):
                if PRINT_VERIFICATION:
                    print("conflicted premise in prems: with model", prop, model)
                result_list.append(prop)
    return result_list