예제 #1
0
    def get(cls, a, config_name, config_base=None):
        cls.__initialize(a)

        config_base = cls.__get_config_concept_node(config_base)

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

        exist_set = None
        while True:
            exist_set = cls.__execute_wrap("GET", config_dict)
            if len(exist_set.out) > 0:
                break

            parent_set = cls.__execute_wrap("INHERITANCE", config_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:
            ret = None
            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]
                log.warn("TODO: Currently, config have to keep in unique." +
                         "Trying to use first element..." + str(config_name) +
                         ' in ' + str(config_base) + ' = ' +
                         str(exist_set.out))

            cls.a.remove(exist_set)
            return ret
예제 #2
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
예제 #3
0
    def get(cls, a, config_name, config_base=None):
        """Get config.

        Get value of config that has config_name, starts find from
        config_base. If config_base was not given, then it finds in default
        config_base(Top of config system).

        Args:
            a: An instance of AtomSpace to find Atom.
            config_name: A config to update.
            config_base: A base of config system.
            :param a: AtomSpace
            :param config_name: str | unicode | Node
            :param config_base: str | unicode | Node
        Returns:
            ret: A found config value node.
            :rtype : ConceptNode
        """
        cls.__initialize(a)

        config_base = cls.__get_config_concept_node(config_base)
        config_name = cls.__get_config_name_schema_node(config_name)

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

        exist_set = cls.__get_exist_config_set(config_dict)

        ret = None
        if exist_set is not None:
            if len(exist_set.out) == 1:
                ret = exist_set.out[0]
            elif len(exist_set.out) > 1:
                ret = exist_set.out[0]
                log.warn(
                    "TODO: Currently, config have to keep in unique." +
                    "Trying to use first element..." +
                    str(config_name) + ' in ' +
                    str(config_base) + ' = ' +
                    str(exist_set.out)
                )
            cls.a.remove(exist_set)

        return ret
예제 #4
0
    def get(cls, a, config_name, config_base=None):
        cls.__initialize(a)

        config_base = cls.__get_config_concept_node(config_base)

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

        exist_set = None
        while True:
            exist_set = cls.__execute_wrap("GET", config_dict)
            if len(exist_set.out) > 0:
                break

            parent_set = cls.__execute_wrap("INHERITANCE", config_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:
            ret = None
            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]
                log.warn(
                    "TODO: Currently, config have to keep in unique." +
                    "Trying to use first element..." +
                    str(config_name) + ' in ' +
                    str(config_base) + ' = ' +
                    str(exist_set.out)
                )

            cls.a.remove(exist_set)
            return ret
예제 #5
0
    def run(self, focus_atoms=None, config_base=None):
        """Execute conceptual blending.new_blended_atoms

        TODO: Write detailed description.

        Args:
            focus_atoms:
            config_base:
            :param focus_atoms: list[Atom]
            :param config_base: Atom

        Returns:
            The blended atom(s). For example:

            [(ConceptNode "car-man"),
             (ConceptNode "man-car"),
             ...]

            If a list is empty, then blender can't find proper blend atoms.
            :rtype : list[Atom]
        """
        self.last_status = self.Status.IN_PROCESS
        self.ret = None

        if focus_atoms is None:
            focus_atoms = []

        if config_base is None:
            config_base = self.a.add_node(types.ConceptNode,
                                          BlendConfig().name)

        try:
            self.make_workers(config_base)

            # Give interface to each blenders to suitable prepare works.
            # eg. caching, ...
            self.prepare_hook(config_base)

            # Choose nodes to blending.
            self.chosen_atoms = \
                self.chooser.atom_choose(focus_atoms, config_base)

            # Decide whether or not to execute blending and prepare.
            self.decided_atoms = \
                self.decider.blending_decide(self.chosen_atoms, config_base)

            # Initialize the new blend node.
            self.new_blended_atom = \
                self.maker.new_blend_make(self.decided_atoms, config_base)

            # Make the links between exist nodes and newly blended node.
            # Check the severe conflict links in each node and remove.
            # Detect and improve conflict links in newly blended node.
            self.new_blended_atoms = self.connector.link_connect(
                self.decided_atoms, self.new_blended_atom, config_base)

            # Give interface to each blenders to finish works.
            self.finish_hook(self.new_blended_atoms, config_base)

            # If all task finished successfully, save new atoms to return.
            self.ret = self.new_blended_atoms

        except UserWarning as e:
            log.warn("Skipping blend, caused by '" + str(e) + "'")
            log.warn("Last status is '" +
                     self.Status.reverse_mapping[self.last_status] + "'")

        if self.last_status == self.Status.IN_PROCESS:
            self.last_status = self.Status.SUCCESS_BLEND

        if self.ret is None:
            self.ret = []

        return self.ret
예제 #6
0
    def __connect_duplicate_links(self, duplicate_links, dst_node):
        """
        Connect duplicate links.

        Make the links between exist nodes, with detect and improve conflict
        links in newly blended node.

        :param dict(Handle, list(EqualLinkKey)) duplicate_links: List of
        duplicated link expressed by EqualLinkKey list.
        :param Atom dst_node: node to connect new links.
        """
        conflict_links = []
        non_conflict_links = []

        for links in duplicate_links:
            if self.__is_conflict(links):
                conflict_links.append(links)
            else:
                non_conflict_links.append(links)

        # 1. Make all available new blended atoms.
        # Number of new atoms is expected to 2^k, if there exists k conflicts.
        if self.viable_atoms_count_threshold is not None:
            if self.viable_atoms_count_threshold < 2**len(conflict_links):
                # TODO: Control if expected result atoms count
                # is bigger than threshold
                log.warn("ConnectConflictAllViable: Too many atoms! ->" +
                         str(2**len(conflict_links)) + " atoms will produce.")

        # 1-a. Prepare 2^k new blend atoms.
        new_blended_atoms = [dst_node]
        for i in xrange(1, 2**len(conflict_links)):
            new_blended_atoms.append(
                self.a.add_node(dst_node.t, dst_node.name + '-' + str(i),
                                dst_node.tv))

        # 1-b. Prepare cartesian product iterator.
        # if number of conflict_links is 3, this iterator produces:
        # (0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), ... (1, 1, 1)
        cartesian_binary_iterator = \
            itertools.product([0, 1], repeat=len(conflict_links))

        # 1-c. Connect to each viable atoms.
        for i, viable_case_binary in enumerate(cartesian_binary_iterator):
            for j, selector in enumerate(viable_case_binary):
                make_link_from_equal_link_key(self.a,
                                              conflict_links[j][selector],
                                              new_blended_atoms[i],
                                              conflict_links[j][selector].tv)

        # 2. Others, evaluate the weighted average of truth value between
        # the duplicate links.
        for new_blend_atom in new_blended_atoms:
            for links in non_conflict_links:
                # array elements are same except original node information.
                link_key_sample = links[0]

                make_link_from_equal_link_key(self.a, link_key_sample,
                                              new_blend_atom,
                                              get_weighted_tv(links))

        self.ret = new_blended_atoms