def test_sanity(self):
   controller = MyController()
   c = Codelet(Foo, controller, 50, dict(x=3))
   self.assertEqual(50, c.urgency)
   self.assertEqual(18, c.Run())
   self.assertEqual(c, controller.most_recent_codelet)
   self.assertEqual(48, c.Run())
 def GetSimilarityAffordances(self, other, other_fringe, my_fringe,
                              controller):
     logging.debug('GetSimilarityAffordances called: [%s] and [%s] ', self,
                   other)
     if not isinstance(other, SAnchored):
         return ()
     left, right = sorted((self, other), key=lambda x: x.start_pos)
     # Do they overlap?
     if left.end_pos >= right.start_pos:
         # They overlap. So we will want to form a bigger group...
         from farg.apps.seqsee.codelet_families.overlapping_groups import CF_ActOnOverlappingGroups
         return [
             Codelet(CF_ActOnOverlappingGroups, controller, 100,
                     dict(left=left, right=right))
         ]
     else:
         # So both are anchored, and have overlapping fringes. Time for subspaces!
         urgency = 100.0 / (right.start_pos - left.end_pos
                            )  # denominator is at least 1.
         from farg.apps.seqsee.subspaces.get_mapping import CF_FindAnchoredSimilarity
         return [
             Codelet(
                 CF_FindAnchoredSimilarity, controller, urgency,
                 dict(left=left, right=right, seqsee_ltm=controller.ltm))
         ]
    def GetAffordances(self, controller):
        logging.debug('GetAffordances called for %s', self)
        codelets = []
        from farg.apps.seqsee.codelet_families.all import CF_FocusOn
        for relation in self.relations:
            # QUALITY TODO(Feb 10, 2012): Whether to add a relation-focusing codelet should depend
            # on its confidence, whether it is internal (part of a group), and other considerations.
            codelets.append(
                Codelet(CF_FocusOn, controller, 25, dict(focusable=relation)))
        my_node = controller.ltm.GetNode(content=self)
        outgoing_isa_edges = my_node.GetOutgoingEdgesOfTypeIsa()
        for edge in outgoing_isa_edges:
            # QUALITY TODO(Feb 10, 2012): Category activation and edge strength relevant here.
            category = edge.to_node.content

            # The following appears to repeatedly check; However, note that the link in the LTM
            # suggests that the categorization is possible.
            if not self.object.IsKnownAsInstanceOf(category):
                from farg.apps.seqsee.codelet_families.all import CF_DescribeAs
                codelets.append(
                    Codelet(CF_DescribeAs, controller, 25,
                            dict(item=self.object, category=category)))
        if self.object.underlying_mapping_set:
            from farg.apps.seqsee.codelet_families.extend_group import CF_ExtendGroup
            codelets.append(
                Codelet(CF_ExtendGroup, controller, 25, dict(item=self)))
        return codelets
示例#4
0
 def GetAffordances(self, controller):
     # TODO(# --- Jan 3, 2012): Too eager, tone this down later.
     from farg.apps.seqsee.codelet_families.all import CF_GroupFromRelation
     from farg.apps.seqsee.codelet_families.all import CF_IsThisInterlaced
     if self.AreEndsContiguous():
         return (Codelet(CF_GroupFromRelation, controller, 50,
                         dict(relation=self)), )
     else:
         distance_object = self.ChooseDistanceObject(controller)
         if controller.GetActivation(content=distance_object) > 0.8:
             return (Codelet(CF_IsThisInterlaced, controller, 50,
                             dict(distance=distance_object)), )
         else:
             return ()
示例#5
0
 def GetRemindingBasedActions(self, prior_overlapping):
     return [
         Codelet(
             family=CF_PrintOverlapWithPrior,
             controller=controller,
             urgency=100)
     ]
    def AddCodelet(self,
                   *,
                   family,
                   urgency,
                   arguments_dict=None,
                   parents=None,
                   msg=""):
        """Adds a codelet to the coderack.

    Keyword-only Args:
      family:
        Family of codelet. Subclass of
        :py:class:`~farg.core.codelet.CodeletFamily`.
      urgency:
        Number between 0 and 100 indicating urgency.
      arguments_dict:
        A dictionary of extra arguments to pass to the codelet. See details in
        the
        documentation of :py:class:`~farg.core.codelet.CodeletFamily`.
    """
        if arguments_dict is None:
            arguments_dict = {}
        codelet = Codelet(family=family,
                          controller=self,
                          urgency=urgency,
                          arguments_dict=arguments_dict)
        self.coderack.AddCodelet(codelet, parents=parents, msg=msg)
示例#7
0
    def test_force_next_codelet(self):
        """For testing, it is useful to mark the next codelet that GetCodelet() returns."""
        c = Coderack(10)
        controller = MyController()
        codelet = Codelet(Foo, controller, 20, dict(x=3))
        codelet2 = Codelet(Foo, controller, 30, dict(x=4))
        codelet3 = Codelet(Foo, controller, 30, dict(x=4))

        c.AddCodelet(codelet)
        c.AddCodelet(codelet2)

        self.assertEqual(None, c._forced_next_codelet)
        self.assertRaises(FargError, c.ForceNextCodelet,
                          codelet3)  # Not in coderack

        c.ForceNextCodelet(codelet2)
        self.assertEqual(codelet2, c._forced_next_codelet)

        self.assertEqual(codelet2, c.GetCodelet())
        self.assertEqual(None, c._forced_next_codelet)

        # Not in coderack any longer.
        self.assertRaises(FargError, c.ForceNextCodelet, codelet2)
 def SuggestActions(self, *, instance, logic, controller):
     """What actions could we take with things that are integers?"""
     # Are these categories? "I have tried extending right", "I should try extending right..."
     # I want to suggest the following only when it has not recently been tried. Perhaps the logic
     # can store some of this bookkeepinginfo?
     object_to_right = controller.workspace.GetObjectToRight(instance)
     if not object_to_right:
         return []
     return [
         Codelet(family=CF_DescribeRelationWithObject,
                 controller=controller,
                 urgency=100,
                 arguments_dict=dict(first=instance,
                                     second=object_to_right))
     ]
示例#9
0
    def test_basic(self):
        c = Coderack(max_capacity=10)
        controller = MyController()
        codelet = Codelet(Foo,
                          controller,
                          urgency=20,
                          arguments_dict=dict(x=3))

        assert 10 == c._max_capacity
        c.AddCodelet(codelet)
        assert 1 == c._codelet_count
        assert 20 == c._urgency_sum

        c._ExpungeSomeCodelet()
        assert 0 == c._codelet_count
        assert 0 == c._urgency_sum

        self.assertRaises(CoderackEmptyException, c.GetCodelet)
示例#10
0
    def _AddRoutineCodelets(self, force=False):
        """Add routine codelets to the coderack.

        The codelets are found in routine_codelets_to_add, which is a list of 3-tuples. Each
        3-tuple contains the codelet family, urgency, and probability with which to add that
        codelet.

        Args:
          force:
            If true, the third field of the 3-tuple ("probability of adding") is ignored.

        The codelets are added with a certain probability (specified in the third term of the
        tuple), but this can be over-ridden with force (or if the coderack is empty).
        """
        if self.coderack.IsEmpty():
            force = True
        if self.routine_codelets_to_add:
            for family, urgency, probability in self.routine_codelets_to_add:
                if force or Toss(probability):
                    cl = Codelet(family, self, urgency)
                    self.coderack.AddCodelet(cl, msg="Routine codelet")
示例#11
0
 def CalculateActions(self, ctrl):
     return [
         Codelet(family=CF_PrintIWasFocused,
                 controller=ctrl, urgency=100)
     ]