# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)

from .element import Entry, OutcomeType


class DataFlow(object):

    def __init__(self, rules):
        self.rules = rules
        self.entry_table = {}
        self.outcome_type_table = {}

    def _retrieve_or_record_element(self, table_name, element):
        element_table = getattr(self, table_name)
        if element.name in element_table:
            # retrieve.(ensure uniqueness)
            return element_table[element.name]
        else:
            # register.
            element_table[element.name] = element
            return element

    def get_entry(self, entry):
        return self._retrieve_or_record_element('entry_table', entry)

    def get_outcome_type(self, entry):
        return self._retrieve_or_record_element('outcome_type_table', entry)

    def build_DAG(self):
        for src, dst in self.rules:
            if isinstance(src, Entry) and isinstance(dst, Entry):
                src = self.get_entry(src)
                dst = self.get_entry(dst)
                # transform `EntryA --> EntryB` to
                # `EntryA --> [EntryA]` and `[EntryA] --> EntryB`.
                outcome_type = self.get_outcome_type(OutcomeType(src.name))
                src.add_outcome_type(outcome_type)
                outcome_type.add_entry(dst)
            elif isinstance(src, Entry) and isinstance(dst, OutcomeType):
                src = self.get_entry(src)
                dst = self.get_outcome_type(dst)
                # add outcome_type to entry.
                src.add_outcome_type(dst)
            elif isinstance(src, OutcomeType) and isinstance(dst, Entry):
                src = self.get_outcome_type(src)
                dst = self.get_entry(dst)
                # add entry to outcome_type.
                src.add_entry(dst)
            else:
                raise RuntimeError('build_DAG')

    def topology_sort(self):
        pass

@@ -332,113 +332,386 @@
 def 
-_retrieve_or_record_element(self, table_name, element):%0A        element_table = getattr(self, table_name)
+get_unique_element(self, element):%0A        # target table.%0A        element_table = None%0A        if isinstance(element, Entry):%0A            element_table = self.entry_table%0A        elif isinstance(element, OutcomeType):%0A            element_table = self.outcome_type_table%0A        else:%0A            raise RuntimeError('get_unique_element')%0A%0A        # retrieve or register element.
 %0A   
@@ -967,280 +967,154 @@
 def 
-get_entry(self, entry):%0A        return self._retrieve_or_record_element('entry_table', entry)%0A%0A    def get_outcome_type(self, entry):%0A        return self._retrieve_or_record_element('outcome_type_table', entry)%0A%0A    def build_DAG(self):%0A        for src,
+build_DAG(self):%0A        for src, dst in self.rules:%0A            src = self.get_unique_element(src)%0A           
  dst 
-in
+=
  self.
-rules:
+get_unique_element(dst)
 %0A   
@@ -1180,92 +1180,8 @@
 y):%0A
-                src = self.get_entry(src)%0A                dst = self.get_entry(dst)%0A
     
@@ -1334,28 +1334,30 @@
 elf.get_
-outcome_type
+unique_element
 (Outcome
@@ -1545,99 +1545,8 @@
 e):%0A
-                src = self.get_entry(src)%0A                dst = self.get_outcome_type(dst)%0A
     
@@ -1706,99 +1706,8 @@
 y):%0A
-                src = self.get_outcome_type(src)%0A                dst = self.get_entry(dst)%0A
     

Refactor analysis.py.