actor binary tree lab4
forward 与 ! (tell) 的差异,举个例子:
Main(当前actor): topNode ! Insert(requester, id=1, ele = 2)
topNode: root ! Insert(requester, id=1, ele = 2)
对于root而言,Insert消息的来源是topNode
假如第二行替换为
topNode: root forward Insert(requester, id=1, ele = 2)
此时,对于root而言,Insert消息的来源是Main
从上面的例子可以看出,! 和 forward之间是有差别的,但是差别本身比较tricky,会增加理解的成本,所以传递消息时,把消息的actorRef作为参数传递会简单很多。
在lab4中,就需要考虑到!与forward的异同点
testcase 的 code
test("proper inserts and lookups") {
val topNode = system.actorOf(Props[BinaryTreeSet])
topNode ! Contains(testActor, id = , )
expectMsg(ContainsResult(, false))
topNode ! Insert(testActor, id = , )
topNode ! Contains(testActor, id = , )
expectMsg(OperationFinished())
expectMsg(ContainsResult(, true))
}
结合topNode,与root的处理逻辑
val normal: Receive = {
// 这个forward非常重要,不能写成 !,不然第一个testcase都不过去
case operation: Operation => root ! operation
case GC => {
//garbageCollecting需要先变,因为节点的复制可能会很久
val newRoot = createRoot
context.become(garbageCollecting(newRoot))
root ! CopyTo(newRoot)
}
case _ => println("unknown operation")
}
// root 的处理逻辑
case Insert(requester, id, elem) =>
if(this.elem == elem) {
if(this.removed) this.removed = false
requester ! OperationFinished(id)
} else {
val child = if(this.elem > elem) Left else Right
if(subtrees contains child) subtrees(child) ! Insert(requester, id, elem)
else {
subtrees += child -> context.actorOf(props(elem, false))
requester ! OperationFinished(id)
}
}
在source code中,topNode收到消息后,把消息传递给root,root认为消息的来源是topNode。假如消息不绑定requester参数,那么通过sender获得actor是tiopNode,而不是main。我们在testcase中做assertion的话,肯定就是错的。
另外,上例中用到了testActor,它是implicit变量,用于处理发送到main中的消息。主要是测试方便。
一个debug了5个小时的bug
case msg: Operation =>
pendingQueue = pendingQueue.enqueue(msg)
pendingQueue.enqueue(msg) 并不能更新pendingQueue自己,必须重新赋值才行。
actor binary tree lab4的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- Hibernate注解方式一对多自关联关系映射
MySQL版数据库表结构 DROP TABLE IF EXISTS SYS_DICT_ITEM; CREATE TABLE SYS_DICT_ITEM( ITEM_CODE ) NOT NULL, ...
- ASP.NET Core 2.0使用Log4net实现记录日志功能
一.安装Log4net 1.使用Nuget包进行安装 在依赖项上面右键,选择“管理NuGet程序包”,如下图所示: 在浏览界面输入log4net,然后点击安装,如下图所示: 2.使用程序包管理器控制台 ...
- NetBeans Support Weblog
https://blogs.oracle.com/NetBeansSupport/entry/tomcat_log_files ———————————————————————————————————— ...
- PHP数组缓存:三种方式JSON、序列化和var_export的比较
使用PHP的站点系统,在面对大数据量的时候不得不引入缓存机制.有一种简单有效的办法是将PHP的对象缓存到文件里.下面我来对这3种缓存方法进行说明和比较. 第一种方法:JSONJSON缓存变量的方式主要 ...
- qt之QAbstractSocket
这QAbstractSocket 类提供了整个socket的类型,是QTcpSocket和QUdpSocket的基类 创建一个本体套接字,可以调用QAbstractSocket 和 setSocket ...
- vb.net与c#相互转换工具
vb.net与c#相互转换工具: http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx http://www.dotne ...
- e741. 将标签的焦点置于关联的文本框上面
This example associates a label with a text field using setLabelFor(). A mnemonic is set on the labe ...
- Sql:主表与子表的最新记录级联查询
SELECT * FROM MainTable mLEFT JOIN (SELECT d.* FROM (SELECT MAX(clc.Id) AS id FROM ChildTable AS clc ...
- Android多任务切换与Activity启动模式SingleTask之间关系的分析
这里会以多个场景列子进行分析,在分析之前先了解一下基本的概念. Task任务:一系列Activity的集合,这些Activity以栈的形式进行排列(后进先出). 那在什么时候系统会新建一个Task任务 ...
- Keystone中间件WSGI环境变量总结
OpenStack keystonemiddleware接收前一个WSGI过滤器传来的WSGI环境信息,进行验证工作后传递给下一个中间件,本文探讨keystone中间件究竟有哪些WSGI环境变量. 说 ...