# [BUAA OO]第三单元总结

## [BUAA OO]第三单元总结

### 一.写在前面

​ 数据的构造：完备性和边界性

​ 架构设计：基本图的实现与优化元素的维护

​ bug分析：性能与正确性

​ Network扩展

​ 学习体会

### 三.架构设计

public void insertNewLine(int id1,int id2,int value) {
ArrayList<Integer> newLine = new ArrayList<>();
......
if (sortedLine.size() == 0) {
} else {
int low = 0;
int high = sortedLine.size() - 1;
if (value <= getLineValue(0)) {
} else if (value >= getLineValue(high)) {
} else {
//小心死循环 和 二分的条件
while (true) {
//二分查找
}
}
}
}


public class JointSearchSet {

private HashMap<Integer,Integer> parent = new HashMap<>();
private HashMap<Integer,Integer> rootHeight = new HashMap<>();

public JointSearchSet() {}

public Integer numOfRoot() {
return rootHeight.size();
}

parent.put(id,id);
rootHeight.put(id,1);
}

public Integer find(Integer initialNode) {
int node = initialNode;
if (parent.get(node) == null) {
}
while (node != parent.get(node)) {
...
}
return node;
}

public void unionNode(int initialNode1,int initialNode2) {
int root1 = find(initialNode1);
int root2 = find(initialNode2);
if (root1 == root2) {
return;
}
if (rootHeight.get(root1) < rootHeight.get(root2)) {
...
}
}

public boolean isConnected(int node1,int node2) {
return Objects.equals(find(node1), find(node2));
}

}


 public static int kruskal(Integer createdLineNum,ArrayList<ArrayList<Integer>> sortedLineSet) {
JointSearchSet kruskalJointSet = new JointSearchSet();
int valueSum = 0;
int countLines = 0;
for (int i = 0;i < sortedLineSet.size();i++) {
if (countLines == createdLineNum) {
break;
}
int point1 = sortedLineSet.get(i).get(0);
int point2 = sortedLineSet.get(i).get(1);
if (!kruskalJointSet.isConnected(point1,point2)) {
valueSum = valueSum + sortedLineSet.get(i).get(2);
kruskalJointSet.unionNode(point1,point2);
countLines++;
}
}
return valueSum;
}


public static int dijkstra(int startId, int finalId, HashMap<Integer, Person> people,
JointSearchSet jointSearchSet) {
HashMap<Integer,Integer> marked = new HashMap<>();
HashMap<Integer,Integer> distance = new HashMap<>();
for (Integer personId : people.keySet()) {
if (jointSearchSet.isConnected(personId, startId)) {
marked.put(personId, 0);
distance.put(personId, -1);
}
}
return dijkstraExe(startId, finalId, distance, marked, people);
}

public static int dijkstraExe(int startId,int finalId,HashMap<Integer,Integer> distance,
HashMap<Integer,Integer> marked,
HashMap<Integer, Person> people) {
PriorityQueue<Node> priQueue = new PriorityQueue<>();
priQueue.offer(new Node(startId, 0));
distance.put(startId, 0);
while (priQueue.size() != 0) {
Node minNode;
do {
minNode = priQueue.poll();
if (minNode.getPersonId() == finalId) {
return minNode.getCost();
}
} while (marked.get(minNode.getPersonId()) == 1);
marked.put(minNode.getPersonId(), 1);
((MyPerson)people.get(minNode.getPersonId())).getAcquaintances();
...
}
return distance.get(finalId);
}


### 四.NetWork扩展

• Person：吃瓜群众，不发广告，不买东西，不卖东西

Product中记录产品的信息：id、所需金钱数目等

advertiseMessage是Advertiser给Customer发送的消息类型，用来告知对方可购买的产品

purchaseMessageAdvertiser发送给Producer的消息类型，用来告知对方Customer要购买的产品

Advertiser需要新增一个广告产品列表变量，记录广告的产品；新增一个addAdvertiseProduct方法，更新广告产品列表；

Producer需要新增一个可生产产品列表，记录可生产的产品；新增setProduct方法，修改可生产的产品；

Network需要让自己的sendMessagesendIndirectMessage能够发送新增的消息，能够新增产品，能够修改Advertiser、Producer、Customer中的产品列表，能够查询某Product的销售额、销售路径等等。

JML规格：

/*@ public normal_behavior
@ ensures (\old(getMessage(id)) instanceof AdvertiseMessage) ==>
@          (\exists int i; 0 <= i && i <= \old(getMessage(id)).getPerson2.exeAdvertiser.length - 1; \old(getMessage(id)).getPerson2.exeAdvertiser[i].equals(\old(getMessage(id)).getPerson1)
@ ensures (\old(getMessage(id)) instanceof AdvertiseMessage) ==>
@ (\forall int i; 0 <= i && i < \old(getMessage(id)).getPerson2.\old(exeAdvertiser).length;
@ ensures (\old(getMessage(id)) instanceof AdvertiseMessage) ==>
@          (\exists int i; 0 <= i && i <= \old(getMessage(id)).getPerson2.exeAdvertiser.length - 1 && \old(getMessage(id)).getPerson2.exeAdvertiser[i].equals(\old(getMessage(id)).getPerson1);(\forall Product j;\old(getMessage(id)).getPerson1.advertiseProducts.contains(i);\old(getMessage(id)).getPerson2.exeProducts[i].contains(j)))
@ ensures (\old(getMessage(id)) instanceof PurchaseMessage) ==>
@ (exists int i;0 <= i && i < producers.length;produces[i].equals(\old(getMessage(id)).getPerson2()))
@ \old(getMessage(id)).getPerson2().getProfis() =  \old(getMessage(id)).getPerson2().(\old(getProfits())) + ((PurchaseMessage)\old(getMessage(id))).getAllProductProfits()
@ ensures (\old(getMessage(id)) instanceof PurchaseMessage) ==>
@ successSellMessages.contains(\old(getMessage(id)))
@ (\forall Message i;(\old)successSellMessages.contains(i);successSellMessages.contains(i))
@ also
@ public exceptional_behavior
@ signals (AdvertiseDismatchException e) containsMessage(id) && (getMessage(id).getType == 1 || (getMessage(id).getType == 0 && (!getMessage(id).getPerson1 instanceof Advertiser || !getMessage(id).getPerson2 instaceof Customer)
@ signals (PurchaseDisMatchException e) containsMessage(id) && (getMessage(id).getType == 1 || (getMessage(id).getType == 0 && (!getMessage(id).getPerson1 instanceof Advertiser || !getMessage(id).getPerson2 instaceof Producer)
@*/
public void sendMessage(int id) throws


/*@ public normal_behavior
@ requires contains(id1)
@ ensures type == 1 ==>
@ getPerson(id1).getPriority().contains(product)
@ (\forall Product i;getPerson(id1).(\old)getPriority().contains(i); getPerson(id1).getPriority().contains(i))
@ ensures type == 0 ==>
@ !getPerson(id1).getPriority().contains(product)
@ (\forall Product i;getPerson(id1).getPriority().contains(i); getPerson(id1).(\old)getPriority().contains(i))
@ ensures type != 0 && type != 1 ==>
@ assignable \nothing;
@ also
@ public exceptional_behavior
@ signals (PersonIdNotFoundException e) !contains(id1);
@*/
public void setPersonPriority(int id1, int type, Product product) throws
PersonIdNotFoundException;


/*@ public normal_behavior
@ requires containsProduct(id)
@ ensures result == (\sum Message i;successSellMessages.contains(i) && ((PurchaseMessage) i).getPerchaseProducts().contains(id);getProduct(id).getProfits())
@ also
@ public exceptional_behavior
@ signals (ProductIdNotFoundException e) !containsProduct(id);
@*/
public int queryProductProfis(int id) throws
ProductIdNotFoundException;


### 五.学习体会

posted @ 2022-06-06 12:30  Jack_rbkd  阅读(15)  评论(0编辑  收藏  举报