Exemplo n.º 1
0
    def __delitem__(self, key: str):
        """
		Remove the specified word to the vocabulary
		if it exists in the vocabulary.

		:param key: str, The name of the word to
			remove from the vocabulary.
		:return: None

		"""

        if self.__contains__(item=Word(name=key)):
            self.words.remove(Word(name=key))
Exemplo n.º 2
0
    def new_tape() -> Tape:
        """
		Creates a configured but empty tape.

		:return: Tape

		"""

        return Tape(
            vocab=Vocabulary(words={
                Word(name=Bit.BINARY_LABEL_0),
                Word(name=Bit.BINARY_LABEL_1)
            }),
            default=Word(name=Bit.BINARY_LABEL_0),
            data=[])
Exemplo n.º 3
0
    def indefinite_sequences(self) -> List[ControlSequence]:
        """
		Return a list of indefinite control sequences. With
		a dummy write action (writing the same value as currently)
		on the tape before transitioning to a terminal
		node.

		:return: List[ControlSequence]

		"""

        sequences, exceptions = list(), list()
        indefinites = self.indefinite_states()
        bits = len(list(self.entries)[0].source.identity) \
         - (StateSequence.MIN_STATE_SEQUENCE_LEN - 3)
        labeler = -1

        for e in self.entries:
            labeler = max(labeler, e.source.label, e.target.label)

        for indef in indefinites:
            labeler += 1
            sequences.append(
                ControlSequence(source=indef[0],
                                condition=BinarySequence(values=[indef[1]]),
                                target=StateSequence(
                                    identity=State(
                                        label=labeler,
                                        terminal=True,
                                        op_status=State.FAILURE).to_binary(
                                            label_size=bits),
                                    operation=Write(word=Word(
                                        name=indef[1].value)).to_binary())))

        return sequences
Exemplo n.º 4
0
    def indefinite_states(self) -> List[Tuple[State, Word]]:
        """
		Return a list of indefinite state-input pairs.
		These pairs indicate accessible states within the
		table, but ones that are not defined across all
		potential inputs/transitions.

		:return: List[Tuple[State, Word]]

		"""

        states = [s.label for s in self.states if not s.terminal]
        vocab = [w.name for w in self.vocab]
        indefinites = list()

        for s in states:
            for w in vocab:
                tmp_s = State(label=s)
                tmp_w = Word(name=w)
                e = Edge(source=tmp_s, condition=tmp_w, target=tmp_s)

                if e not in self.entries:
                    indefinites.append((tmp_s, tmp_w))

        return indefinites
Exemplo n.º 5
0
    def to_binary(self) -> BinaryTable:
        """
		Convert the table into a binary table
		controller.

		:return: BinaryTable

		"""

        bits = math.ceil(math.log(len(self.states), 2))
        sources, targets = list(), list()
        controls = list()

        for entry in self.entries:
            targets.append(
                StateSequence(identity=entry.target.to_binary(label_size=bits),
                              operation=entry.action.to_binary()))

        for entry in self.entries:
            found = False
            source = StateSequence(
                identity=entry.source.to_binary(label_size=bits),
                operation=entry.action.to_binary()  # just a placeholder
            )
            condition = entry.condition.to_binary()
            target = StateSequence(
                identity=entry.target.to_binary(label_size=bits),
                operation=entry.action.to_binary())

            for node in targets:
                if node.identity == source.identity:
                    found = True
                    source.operation = node.operation
                    controls.append(
                        ControlSequence(source=source,
                                        condition=condition,
                                        target=target))

            if not found and source.root:
                w = Word(name=condition.values[1].value)
                source.operation = Write(word=w).to_binary()
                controls.append(
                    ControlSequence(source=source,
                                    condition=condition,
                                    target=target))

        return BinaryTable(entries=set(controls))
Exemplo n.º 6
0
    def addition(a: int, b: int) -> Tape:
        """
		Configure the tape with the binary setup
		to process (a + b).

		:param a: int, First operand on the tape.
		:param b: int, Second operand on the tape.
		:return: Tape

		:raises ValueError If either operand is < 0.

		"""

        a = TapeGenerator.succession(a=a)
        a.data.append(Word(name=Bit.BINARY_LABEL_0))
        [a.data.append(c) for c in TapeGenerator.succession(a=b)]
        return a
Exemplo n.º 7
0
    def succession(a: int) -> Tape:
        """
		Configure the tape with the binary setup
		to process (a + 1).

		:param a: int, First operand on the tape.
		:return: Tape

		:raises ValueError If the operand is < 0.

		"""

        if a < 0:
            raise ValueError("Only non-negative operands allowed.")

        tape = TapeGenerator.new_tape()

        for i in range(0, (a + 1)):
            tape.data.append(Word(name=Bit.BINARY_LABEL_1))

        return tape