我正在Scala中实现来自Jade的ContractNetInitiator,我需要重写此方法:
protected void handleAllResponses(java.util.Vector responses,java.util.Vector acceptances)
并像这样实现它:
override def handleAllResponses(responses: Vector[_], acceptances: Vector[_]): Unit = {
var acceptProposal: ACLMessage = null
var bestProposal = Integer.MAX_VALUE
var bestProposer = ""
// Find best proposal and reject all proposal
val e = responses.elements()
while (e.hasMoreElements) {
val response: ACLMessage = e.nextElement().asInstanceOf[ACLMessage]
if (response.getPerformative == ACLMessage.PROPOSE) {
val reply = response.createReply()
reply.setPerformative(ACLMessage.REJECT_PROPOSAL)
acceptances.addElement(reply) // Can't add the reply : "Type mismatch, expected: _$1, actual: ACLMessage"
if (response.getUserDefinedParameter("Value").toInt < bestProposal) {
bestProposal = response.getUserDefinedParameter("Value").toInt
bestProposer = response.getSender.getLocalName
acceptProposal = reply
}
}
}
// Accept proposal
if (acceptProposal != null) {
acceptProposal.setPerformative(ACLMessage.ACCEPT_PROPOSAL)
}
}
但是当我尝试添加对接受的答复时,会得到Type mismatch
。
我尝试将“ acceptances:Vector [_]”更改为“ acceptances:Vector [ACLMessage]”和“ acceptances:Vector [Any]”,但由于它与超类不对应,因此无法正常工作
是否可以将元素添加到接受中?
答案 0 :(得分:3)
您需要投射它:
acceptances.asInstanceOf[Vector[ACLMessage]].addElement(reply)
通常需要避免,但是在这种情况下,使用raw types并仅记录有效的类型参数完全是库的错。