Ejemplo n.º 1
0
    def get(cls, a, config_name, config_base=None):
        cls.__initialize(a)

        config_base = cls.__wrap_config_name(config_base)

        config_dict = {"config_name": config_name, "config_base": config_base}

        exist_set = None
        while True:
            get_link_dict = cls.__create_execute_link("GET", config_dict)
            exist_set = PyCogExecute().execute(a,
                                               get_link_dict['execute_link'])
            cls.__clean_up_obsolete_dict(get_link_dict)

            if len(exist_set.out) > 0:
                break

            inh_link_dict = cls.__create_execute_link("INHERITANCE",
                                                      config_dict)
            parent_set = PyCogExecute().execute(a,
                                                inh_link_dict['execute_link'])
            cls.__clean_up_obsolete_dict(inh_link_dict)

            if len(parent_set.out) == 0:
                exist_set = None
                break
            else:
                config_dict["config_base"] = parent_set.out[0]

        if exist_set is None:
            return None

        ret = None
        try:
            if len(exist_set.out) < 1:
                ret = None
            elif len(exist_set.out) == 1:
                ret = exist_set.out[0]
            elif len(exist_set.out) > 1:
                ret = exist_set.out[0]
                raise UserWarning(
                    "TODO: Currently, config have to keep in unique." +
                    "Trying to use first element..." + str(config_name) +
                    ' in ' + str(config_base) + ' = ' + str(exist_set.out))
        except UserWarning as e:
            log.warn(str(e))

        cls.a.remove(exist_set)
        return ret
Ejemplo n.º 2
0
    def update(cls, a, config_name, config, config_base=None):
        cls.__initialize(a)

        config = cls.__wrap_config_name(config)
        config_base = cls.__wrap_config_name(config_base)

        config_dict = {
            "config_name": config_name,
            "config_base": config_base,
            "config": config
        }

        exist_set = None
        while True:
            get_link_dict = cls.__create_execute_link("GET", config_dict)
            exist_set = PyCogExecute().execute(a,
                                               get_link_dict['execute_link'])
            cls.__clean_up_obsolete_dict(get_link_dict)

            if len(exist_set.out) > 0:
                break

            inh_link_dict = cls.__create_execute_link("INHERITANCE",
                                                      config_dict)
            parent_set = PyCogExecute().execute(a,
                                                inh_link_dict['execute_link'])
            cls.__clean_up_obsolete_dict(inh_link_dict)

            if len(parent_set.out) == 0:
                exist_set = None
                break
            else:
                config_dict["config_base"] = parent_set.out[0]

        if exist_set is not None:
            config_dict["config"] = exist_set.out[0]
            del_link_dict = cls.__create_execute_link("DEL", config_dict)
            PyCogExecute().execute(a, del_link_dict['execute_link'])
            cls.__clean_up_obsolete_dict(del_link_dict)

        config_dict["config"] = config
        put_link_dict = cls.__create_execute_link("PUT", config_dict)
        PyCogExecute().execute(a, put_link_dict['execute_link'])
        cls.__clean_up_obsolete_dict(put_link_dict)

        if exist_set is not None:
            cls.a.remove(exist_set)
Ejemplo n.º 3
0
def find_inheritance_nodes(a, decided_atoms):
    """Find the inheritance nodes from decided atoms list.

    Args:
        a: An instance of AtomSpace.
        decided_atoms: The source atoms to make new atom.
        :param a: opencog.atomspace.AtomSpace
        :param decided_atoms: list[Atom]
    Returns:
        ret: Nodes inherited to decided_atoms.
        :rtype : list[Atom]
    """
    free_var = a.add_node(types.VariableNode, "$")

    inheritance_link = a.add_link(
        types.InheritanceLink, [free_var, decided_atoms]
    )
    """
    GetLink
        InheritanceLink
            VariableNode $
            ConceptNode <Decided Atom>
    """
    get_link = a.add_link(types.GetLink, [inheritance_link])
    """
    SetLink
        ConceptNode <Related node 1>
        ConceptNode <Related node 2>
        ...
        ConceptNode <Related node N>
    """
    inheritance_node_set = PyCogExecute().execute(a, get_link)

    ret = inheritance_node_set.out

    # Delete temp links.
    BlendConfig().exe_factory.clean_up(inheritance_node_set)
    BlendConfig().exe_factory.clean_up(get_link)
    BlendConfig().exe_factory.clean_up(inheritance_link)
    BlendConfig().exe_factory.clean_up(free_var)
    return ret
try:
    __import__("blending.blend")
except ImportError:
    import unittest
    raise unittest.SkipTest(
        "ImportError exception: " +
        "Can't find Python Conceptual Blender. " +
        "make sure the required dependencies are installed."
    )
else:
    from blending.blend import ConceptualBlending

try:
    from blending.util.py_cog_execute import PyCogExecute
    PyCogExecute().load_scheme()
except (ImportError, RuntimeError):
    import unittest

    raise unittest.SkipTest(
        "Can't load Scheme." +
        "make sure the you installed atomspace to /usr/local/share/opencog."
    )


# noinspection PyArgumentList, PyTypeChecker
class TestNewBlendAtomMaker(TestConceptualBlendingBase):
    """
    2.3 New Blend Atom Maker tests.
    """
Ejemplo n.º 5
0
 def __execute_wrap(cls, call_type, config_dict):
     link_dict = cls.execute_link_factory.create(call_type, config_dict)
     result = PyCogExecute().execute(cls.a, link_dict['execute_link'])
     cls.execute_link_factory.clean_up(link_dict)
     return result