質問

挨拶、現在私がリファクタリングのプログラムは、この興味深い問題です。

私は転移オートマトン.遷移について語ってい開始状態と終了です。一部の推移していラベルは、符号化ある行動をとることを必要とするところにトラバーサルをサポートします。レーベルがないのです。一部の推移してい条件を満たすことが必要となるためにトラバースこの条件がなければ、そのままの状態に転移がありますが、基本的にはイプシロン-転移は、NFAまでを往来すことなく入力シンボルです。

さんありがとうございます。次の操作

  • チェックの場合の遷移のラベル
  • このラベル
  • 追加のラベルへの遷移
  • チェックの場合の遷移状態
  • この状態
  • チェック等

からの最初のポイント、この音のように明確なデコレータをベースに、転移とつデコレータ:識いたします。しかし、この方法は問題:二つの転移が等しい場合は等しいと見なされ始状態と終状態のラベルで推移して平等な存在しない)と両方の条件が同じなの既存の).と驚いて転移を標識("foo"は、条件付き("バー"、"baz","qux")))条件付き("bar"標識("foo"、"baz","qux")))必要とする非局所平等のデコレータが必要な全てのデータを収集するための移行は比較では収集したデータセット-ベース

class Transition(object):
    def __init__(self, start, end):
        self.start = start
        self.end = end
    def get_label(self):
        return None
    def has_label(self):
        return False
    def collect_decorations(self, decorations):
        return decorations
    def internal_equality(self, my_decorations, other):
        try:
            return (self.start == other.start
                    and self.end == other.end
                    and my_decorations = other.collect_decorations())
    def __eq__(self, other):
        return self.internal_equality(self.collect_decorations({}), other)

class Labeled(object):
    def __init__(self, label, base):
        self.base = base
        self.label = label
    def has_label(self):
        return True
    def get_label(self):
        return self.label
    def collect_decorations(self, decorations):
        assert 'label' not in decorations
        decorations['label'] = self.label
        return self.base.collect_decorations(decorations)
    def __getattr__(self, attribute):
        return self.base.__getattr(attribute)

このクリーンアプローチを考えていますか。私何かが足りない?

私は主に戸惑いを解決できること-長クラスの名前を用いた協調多重継承:

class Transition(object):
    def __init__(self, **kwargs):
        # init is pythons MI-madness ;-)
        super(Transition, self).__init__(**kwargs)
        self.start = kwargs['start']
        self.end = kwargs['end']
    def get_label(self):
        return None
    def get_condition(self):
        return None
    def __eq__(self, other):
        try:
            return self.start == other.start and self.end == other.end
        except AttributeError:
            return False

class LabeledTransition(Transition):
    def __init__(self, **kwargs):
        super(LabeledTransition).__init__(**kwargs)
        self.label = kwargs['label']
    def get_label(self):
        return self.label
    def __eq__(self):
        super_result = super(LabeledTransition, self).__eq__(other)
        try:
            return super_result and self.label == other.label
        except AttributeError:
            return False

class ConditionalTransition(Transition):
    def __init__(self, **kwargs):
        super(ConditionalTransition, self).__init__(**kwargs)
        self.condition = kwargs['condition']

    def get_condition(self):
        return self.condition

    def __eq__(self, other):
        super_result = super(ConditionalTransition, self).__eq__(other)
        try:
            return super_result and self.condition = other.condition
        except AttributeError:
            return False

# ConditionalTransition about the same, with get_condition
class LabeledConditionalTransition(LabeledTransition, ConditionalTransition):
    pass

クラスのLabledConditionalTransition動として期待されないコードが魅力のないものにマイルが混乱したこのサイズです。

もちろん、第三のオプションハンマーだけでも一つの遷移シェイクスピアの"マクベス"バンチのhas_label/has_transition.

なので---私はかなりの混乱をきたしている。私何かが足りない?実施に見えですか?どのような取り扱いは同様のケースでは、オブジェクトのようにデコレータを取り扱うことができ、その後は、このような非局所手法が。

編集:追加のConditionalTransition-クラスです。原則、このちょっとのように動作するデコレータ、マイナスのためのデコレータの移行をチェックの開始と終了いてのLabeledTransitionクラスのチェックのためのラベルが正しいとConditionalTransitionチェックのための条件が正しい。

役に立ちましたか?

解決

は思えない明るいの理解ご質問です。私はあなたがそのコンテキストで短くなります。一例として、"わらびもち"をここで一つの実施状態のパターンにpythonを勉強してくださいので、アイデアです。

class State(object):
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return self.name

class Automaton(object):
    def __init__(self, instance, start):
        self._state = start
        self.transitions = instance.transitions()

    def get_state(self):
        return self._state

    def set_state(self, target):
        transition = self.transitions.get((self.state, target))
        if transition:
            action, condition = transition
            if condition:
                if condition():
                    if action:
                        action()
                    self._state = target
            else:
                self._state = target
        else:
            self._state = target

    state = property(get_state, set_state)

class Door(object):
    open = State('open')
    closed = State('closed')

    def __init__(self, blocked=False):
        self.blocked = blocked

    def close(self):
        print 'closing door'

    def do_open(self):
        print 'opening door'

    def not_blocked(self):
        return not self.blocked

    def transitions(self):
        return {
            (self.open, self.closed):(self.close, self.not_blocked),
            (self.closed, self.open):(self.do_open, self.not_blocked),
        }

if __name__ == '__main__':
    door = Door()
    automaton = Automaton(door, door.open)

    print 'door is', automaton.state
    automaton.state = door.closed
    print 'door is', automaton.state
    automaton.state = door.open
    print 'door is', automaton.state
    door.blocked = True
    automaton.state = door.closed
    print 'door is', automaton.state

このprogrammすることはできない。

door is open
closing door
door is closed
opening door
door is open
door is open

他のヒント

のコードから投稿されたに違い転移を標識した遷移のget_lable()およびhas_label().この場合は圧縮できるこれら二つの単一のクラスを設定するラベルの属性を何よりも重要なもので、

return self.label is not None

のhas_label()機能です。

きのコードを ConditionalTransition クラス?と思うこうがより明確になる。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top