Example #1
0
 def __init__(self, *, ui=None, controller_depth=0,
              parent_controller=None, workspace_arguments=None,
              stopping_condition=None):
     History.AddArtefact(self, ObjectType.CONTROLLER, "Created controller")
     #: How deeply in the stack this controller is. The top-level controller has a depth
     #: of 0, Subspaces it spawns 1, and so forth.
     self.controller_depth = controller_depth
     #: If this is a controller of a subspace, this points to parent_controller.
     self.parent_controller = parent_controller
     #: The coderack.
     self.coderack = self.coderack_class()
     #: The stream.
     self.stream = self.stream_class(self)
     if self.workspace_class:
         if workspace_arguments is None:
             workspace_arguments = dict()
         #: Workspace, constructed if workspace_class is defined. The workspace is constructed
         #: using workspace_arguments.
         self.workspace = self.workspace_class(
             **workspace_arguments)  # pylint: disable=E1102
     if self.ltm_name:
         #: LTM, if any
         self.ltm = LTMManager.GetLTM(self.ltm_name)
     else:
         self.ltm = None
     #: Number of steps taken
     self.steps_taken = 0
     #: The UI running this controller. May be a GUI or a Guided UI (which knows how to
     #: answer questions that'd normally be answered by a user in a GUI). Any subspace
     #: spawned by this space shall inherit the ui.
     self.ui = ui
     #: Stopping condition (for SxS and batch modes).
     self.stopping_condition = stopping_condition
     # Add any routine codelets...
     self._AddRoutineCodelets(force=True)
    def __init__(self):
        #: Variables are a superset of Attributes: they additionally contain internal, attribute-like
        #: things, that don't need to be made public. There are no constraints on the values for
        #: non-attribute variables.
        self._Variables = set()  # We will add to these.
        self._Variables.update(self._Attributes)
        #: Compiled rules are obtained from the string versions by parsing and seeking out variables.
        self._CompiledRules = []
        for rule in self._Rules:
            target, rest = rule.split(sep=':', maxsplit=1)
            rule_obj = Rule(target=target.strip(),
                            expression=rest.lstrip(),
                            ctx=self._Context)
            self._Variables.add(target.strip())
            self._Variables.update(x for x in rule_obj.GetVars())
            self._CompiledRules.append(rule_obj)

        #: Compiled checks. These are expressed as rules, but there is no target.
        self._CompiledChecks = []
        for rule in self._Checks:
            rule_obj = Rule(target=None,
                            expression=rule.strip(),
                            ctx=self._Context)
            self._Variables.update(x for x in rule_obj.GetVars())
            self._CompiledChecks.append(rule_obj)

        self.SanityCheck()
        History.AddArtefact(self, artefact_type=ObjectType.CATEGORY)
Example #3
0
    def _PlonkIntoPlace(self, group, *, parents=None):
        """Anchors the group into the workspace. Assumes that conflicts have already been checked
           for.
        """
        groups_at_this_location = list(self.GetGroupsWithSpan(Exactly(group.start_pos),
                                                              Exactly(group.end_pos)))
        if groups_at_this_location:
            # Merge current group into the group at this location, as it were. This merging only
            # adds the underlying mapping. However, if we extend groups to make multiple
            # underlying mappings possible, this needs to be updated too.
            group_at_this_location = groups_at_this_location[
                0]  # There can be only 1
            if (group.object.underlying_mapping_set and
                    not group_at_this_location.object.underlying_mapping_set):
                group_at_this_location.object.underlying_mapping_set = set(
                    group.object.underlying_mapping_set)
            # We should also merge all pieces of the group into the
            # corresponding existing pieces.
            for x in group.items:
                self._PlonkIntoPlace(x)  # Ignore output, we don't need it.
            group_at_this_location.object.AddCategoriesFrom(group.object)
            return group_at_this_location

        # Group does not exist, create one.
        pieces = [self._PlonkIntoPlace(x) for x in group.items]
        new_object = SAnchored.Create(pieces,
                                      underlying_mapping_set=set(
                                          group.object.underlying_mapping_set))
        new_object.object.AddCategoriesFrom(group.object)
        self.groups.add(new_object)
        History.AddArtefact(new_object,
                            ObjectType.WS_GROUP, "Initial creation: [%d, %d]" % (new_object.start_pos,
                                                                                 new_object.end_pos),
                            parents=parents)
        return new_object
 def __init__(self, *, log_msg='', parents=[]):
     PSFocusable.__init__(self)
     self.relations = dict()
     self._span = None
     History.AddArtefact(item=self,
                         artefact_type=ObjectType.WS_GROUP,
                         log_msg=log_msg,
                         parents=parents)
 def __init__(self, *, first, second, parents=[]):
     self.first = first
     self.second = second
     Categorizable.__init__(self)
     if History._is_history_on:
         roles = {first._hid: "first end", second._hid: "second end"}
         History.AddArtefact(self, ObjectType.WS_RELN, "", parents, roles)
         History.Note("Created relation")
Example #6
0
 def AddCodelet(self, codelet, *, msg="", parents=[]):
     """Adds codelet to coderack. Removes some existing codelet if needed."""
     kLogger.debug('Codelet added: %s', str(codelet.family))
     if self._codelet_count == self._max_capacity:
         self._ExpungeSomeCodelet()
     self._codelets.add(codelet)
     self._codelet_count += 1
     self._urgency_sum += codelet.urgency
     History.AddArtefact(codelet, ObjectType.CODELET,
                         "Codelet %s %s" % (codelet.family.__name__, msg),
                         parents)
Example #7
0
 def __init__(self, first, second, *, mapping_set):
     #: Typically, the object on the left.
     self.first = first
     #: Typically, the object on the right.
     self.second = second
     #: The set of mappings any of which transform the left object to the right object.
     self.mapping_set = mapping_set
     History.AddArtefact(self,
                         ObjectType.WS_RELN,
                         "",
                         parents=(self.first, self.second))
Example #8
0
 def InsertElement(self, element):
     """Insert an element beyond the last element."""
     assert isinstance(element, SElement)
     anchored = SAnchored(sobj=element,
                          items=[],
                          start_pos=self.num_elements,
                          end_pos=self.num_elements,
                          is_sequence_element=True)
     self.num_elements += 1
     History.AddArtefact(anchored, ObjectType.WS_GROUP, "Initial creation")
     History.Note("Element Inserted")
     self.elements.append(anchored)
Example #9
0
 def AddCodelet(self, codelet, *, msg="", parents=[]):
     """Adds codelet to coderack. Removes some existing codelet if needed."""
     kLogger.debug("Codelet added: %s", str(codelet.family))
     if self._codelet_count == self._max_capacity:
         self._ExpungeSomeCodelet()
     self._codelets.add(codelet)
     self._codelet_count += 1
     self._urgency_sum += codelet.urgency
     roles = {}
     for k, v in codelet.args.items():
         if hasattr(v, "_hid"):
             roles[v._hid] = "Argument %s" % k
     History.AddArtefact(codelet,
                         ObjectType.CODELET,
                         msg,
                         parents,
                         roles=roles)
 def __init__(self,
              parent_controller,
              *,
              nsteps=5,
              workspace_arguments=None,
              parents=None,
              msg=""):
     """Initializes the subspace by just storing the arguments."""
     self.parent_controller = parent_controller
     self.nsteps = nsteps
     self.workspace_arguments = workspace_arguments
     effective_parents = [parent_controller]
     if parents:
         effective_parents.extend(parents)
     History.AddArtefact(self,
                         ObjectType.SUBSPACE,
                         msg,
                         parents=effective_parents)
Example #11
0
 def __init__(self, *, msg='', parents=[]):
     PSFocusable.__init__(self)
     self.relations = dict()
     self._span = None
     History.AddArtefact(self, ObjectType.WS_GROUP, "EltOrGp %s" % msg,
                         parents)