mx.states

Flex状态管理
本文介绍Flex框架中的状态管理机制,利用States属性实现不同视图状态之间的切换。通过设置currentState属性,可以展示不同的视图内容,类似于页面间的跳转效果。

 

文档上这样写的:

The State class defines a view state, a particular view of a component. For example, a product thumbnail could have two view states; a base view state with minimal information, and a rich view state with additional information. The overrides property specifies a set of child classes to add or remove from the base view state, and properties, styles, and event handlers to set when the view state is in effect.

You use the State class in the states property of Flex components. You can only specify a states property at the root of an application or a custom control, not on child controls.

You enable a view state by setting a component's currentState property.

 

可以这样理解:

 

Flex没有页面跳转,但是不能总在一个页面玩啊,通过states 就可以模拟实现。

<mx:states>
 <mx:State name="1">
<!--想要显示什么,想要隐藏什么-->
</mx:State>
 <mx:State name="2">
</mx:State>
 <mx:State name="3">
</mx:State>
</mx:states>

  通过设置

currentState='1' 或者currentState='2' currentState='3'

来达到显示1,2或者3里的内容。类似于通过javascript来设置HTML页面某些控件显示或者某些隐藏来达到显示不同的view功能。

 

例子看:http://livedocs.adobe.com/flex/3/langref/mx/states/State.html#mxmlSyntaxSummary

        http://blog.minidx.com/2008/10/14/1521.html

 

 

def _visualize_automaton(self, fa=None, title="有限自动机"): if fa is None: fa = self.current_fa if not fa: return self.figure.clear() ax = self.figure.add_subplot(111) G = nx.MultiDiGraph() # 1. 构建图结构(节点 + 边) for state in fa.states: G.add_node(state) edge_data = [] for from_state, symbol_dict in fa.transitions.items(): for symbol, to_states in symbol_dict.items(): for to_state in to_states: edge_data.append((from_state, to_state, symbol)) for from_state, to_state, symbol in edge_data: G.add_edge(from_state, to_state, symbol=symbol) # 2. 布局优化 try: pos = nx.kamada_kawai_layout(G) except: pos = nx.spring_layout(G, k=0.5, iterations=50) # 3. 绘制节点(含终结状态双圈) nx.draw_networkx_nodes( G, pos, node_size=800, node_color='white', edgecolors='black', ax=ax ) # 标记终结状态(双圈) for state in fa.final_states: if state in pos: x, y = pos[state] outer = plt.Circle((x, y), 0.15, fill=False, edgecolor='black', linewidth=2) inner = plt.Circle((x, y), 0.12, fill=True, color='white', edgecolor='black', linewidth=2) ax.add_artist(outer) ax.add_artist(inner) ax.set_aspect('equal') # 4. 绘制边(优化弯曲、颜色区分重叠边) edges = G.edges(data=True, keys=True) # 用字典记录 <起点-终点> 对应的边符号列表,避免重复处理 edge_symbol_map = defaultdict(list) for u, v, _, data in edges: edge_symbol_map[(u, v)].append(data["symbol"]) # 为每组边分配不同颜色(可选,区分重叠边) color_map = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd'] for idx, (u, v) in enumerate(edge_symbol_map.keys()): group_idx = idx % len(color_map) # 绘制当前 <u-v> 所有边(按颜色循环) for symbol in edge_symbol_map[(u, v)]: nx.draw_networkx_edges( G, pos, edgelist=[(u, v)], width=2, alpha=0.8, arrows=True, arrowsize=20, connectionstyle='arc3,rad=0.3', edge_color=color_map[group_idx], ax=ax ) # 5. 绘制边标签(优化版:动态避让重叠) for (u, v), symbols in edge_symbol_map.items(): total_edges = len(symbols) # 计算边的基础弧度(用于标签偏移) x0, y0 = pos[u] x1, y1 = pos[v] dx = x1 - x0 dy = y1 - y0 base_rad = 0.3 if total_edges == 1 else 0.2 for i, symbol in enumerate(symbols): # 计算边中点 mx, my = (x0 + x1) / 2, (y0 + y1) / 2 # 计算边的角度(弧度制) angle = math.atan2(dy, dx) # 多条边时,分散标签位置(基于边索引调整弧度) if total_edges > 1: # 让标签沿边的垂直方向分散 rad_offset = 0.1 * (i - (total_edges - 1) / 2) else: rad_offset = 0 # 计算最终偏移(结合基础弧度和分散偏移) offset_x = (base_rad + rad_offset) * math.sin(angle) offset_y = -(base_rad + rad_offset) * math.cos(angle) # 自环边特殊处理(标签放在环外侧) if u == v: offset_x = 0.2 # 固定右侧偏移 offset_y = 0.1 # 微调垂直位置 ax.text( mx + offset_x, my + offset_y, symbol, fontsize=12, fontfamily='SimHei', ha='center', va='center', bbox=dict(facecolor='white', alpha=0.9, edgecolor='none', pad=0.2) ) # 6. 绘制节点标签、起始箭头 nx.draw_networkx_labels( G, pos, font_size=12, font_family='SimHei', ax=ax ) if fa.start_state and fa.start_state in pos: start_x, start_y = pos[fa.start_state] ax.arrow( start_x, start_y + 0.35, 0, -0.2, head_width=0.05, head_length=0.05, fc='black', ec='black', linewidth=2, zorder=5 ) ax.set_title(f"{title} 可视化", fontsize=16) ax.axis('off') self.canvas.draw()帮我重新写这个绘图函数,目的是为了图形化NFA,DFA
07-03
为了优化有限自动机(NFA/DFA)的可视化函数,可以采用图形库如 `graphviz` 来生成更清晰的图形化表示。以下是一个改进后的 Python 代码示例,用于展示 NFA 和 DFA 的状态转移结构,并通过 `graphviz` 生成可视化图。 ### 改进目标 - 使用 `graphviz` 渲染状态转移图。 - 清晰地标注初始状态、接受状态和状态转移。 - 支持 NFA 和 DFA 的通用表示。 ```python from graphviz import Digraph class FiniteAutomaton: def __init__(self): self.states = set() self.alphabet = set() self.transitions = {} self.start_state = None self.accept_states = set() def add_state(self, state): self.states.add(state) if state not in self.transitions: self.transitions[state] = {} def add_transition(self, from_state, symbol, to_state): if symbol not in self.alphabet: self.alphabet.add(symbol) if symbol not in self.transitions[from_state]: self.transitions[from_state][symbol] = [] self.transitions[from_state][symbol].append(to_state) def set_start_state(self, state): self.start_state = state def set_accept_state(self, state): self.accept_states.add(state) def visualize(self, filename="automaton", view=True): dot = Digraph(comment="Finite Automaton") # 添加所有状态节点 for state in self.states: shape = "doublecircle" if state in self.accept_states else "circle" dot.node(state, state, shape=shape) # 初始状态添加一个不可见节点并指向它 if self.start_state: dot.node("start", "", shape="none") dot.edge("start", self.start_state) # 添加转移边 for from_state, transitions in self.transitions.items(): for symbol, to_states in transitions.items(): for to_state in to_states: dot.edge(from_state, to_state, label=symbol) dot.render(filename, format="png", cleanup=True, view=view) ``` ### 示例:构造一个简单的 DFA 并可视化 ```python # 创建 DFA 实例 dfa = FiniteAutomaton() # 添加状态 dfa.add_state("q0") dfa.add_state("q1") dfa.add_state("q2") # 设置初始状态和接受状态 dfa.set_start_state("q0") dfa.set_accept_state("q2") # 添加转移规则 dfa.add_transition("q0", "a", "q1") dfa.add_transition("q0", "b", "q0") dfa.add_transition("q1", "a", "q2") dfa.add_transition("q1", "b", "q1") dfa.add_transition("q2", "a", "q2") dfa.add_transition("q2", "b", "q2") # 可视化 DFA dfa.visualize("dfa_output") ``` ### 输出说明 上述代码将生成一个名为 `dfa_output.png` 的图片文件,其中: - 圆圈代表普通状态。 - 双层圆圈代表接受状态。 - 箭头指示状态之间的转移。 - 起始状态前有一个空节点指向它。 这种方法能够清晰地展示 NFA/DFA 的结构,适用于教学演示或调试分析。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值