📄 count.java
字号:
package com.count;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class Count {
private ArrayList<String> routetrace;// 链接路径表
private ArrayList<Integer> acs;// 攻击计数表
private ArrayList<Integer> tcs;// 链接计数表
private HashMap<String, Double> pxy;// 依赖概率表
private int xn;// 终端总数
private HashMap<String, Double> xmap;// 终端攻击概率表
private HashMap<String, Double> xrmap;// 端点路径p(xr)表
private Double py;// 终点被攻击概率表
private HashMap<String, Double> routeP;
// 得到一对节点间的依赖概率
private Double getPxy(String x, String y) {
if (pxy.containsKey(x + y)) {
return pxy.get(x + y);
} else {
int ac = 0;
int tc = 0;
for (int i = 0; i < routetrace.size(); i++) {
String sl[] = (routetrace.get(i)).split("->");
for (int j = 0; j < sl.length; j++) {
if (sl[j].equals(x)) {
if (sl[j + 1].equals(y)) {
ac += acs.get(i);
tc += tcs.get(i);
break;
} else {
break;
}
}
}
}
double xy = (double) ac / (double) tc;
pxy.put(x + y, xy);
return xy;
}
}
// 初始化
public void init(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
String line = null;
int totalcount = 0;
xmap = new HashMap<String, Double>();
xrmap = new HashMap<String, Double>();
routetrace = new ArrayList<String>();
acs = new ArrayList<Integer>();
tcs = new ArrayList<Integer>();
pxy = new HashMap<String, Double>();
routeP = new HashMap<String, Double>();
int ac = 0;
int tc = 0;
while ((line = br.readLine()) != null) {
String sl[] = (line.trim()).split("=");
routetrace.add(sl[0]);
xrmap. put(sl[0],Double.parseDouble(sl[2]));
acs.add(Integer.parseInt(sl[1]));
ac += Integer.parseInt(sl[1]);
tcs.add(Integer.parseInt(sl[2]));
tc += Integer.parseInt(sl[2]);
String key = sl[0].substring(0, sl[0].indexOf("->"));
if (xmap.containsKey(key)) {
continue;
} else {
xmap.put(key, 0.0);
}
}
Set<String>keyss=xrmap.keySet();
ArrayList<String>keys=new ArrayList<String>();
for(String key:keyss)
{
keys.add(key);
}
for(String key:keys)
{
Double tmp=xrmap.get(key);
tmp=tmp/tc;
xrmap.remove(key);
xrmap.put(key, tmp);
}
py = (double) ac / (double) tc;
xn = xmap.size();
}
// 得到整条路径的攻击概率/p(x),记得要乘p(x)
public Double getRouteP(String routetrace) {
if (routeP.containsKey(routetrace)) {
return routeP.get(routetrace);
} else {
String sl[] = routetrace.split("->");
// double px=1.0/xn;
double pattack = 1.0;
for (int i = 0; i < sl.length - 1; i++) {
pattack *= getPxy(sl[i], sl[i + 1]);
}
pattack*=xrmap.get(routetrace);
pattack /= py;
routeP.put(routetrace, pattack);
return pattack;
}
}
public Double getxp(String x) {
if (xmap.containsKey(x) && xmap.get(x) != 0.0) {
return xmap.get(x);
} else {
double xp = 0.0;
for (int i = 0; i < acs.size(); i++) {
String sl[] = routetrace.get(i).split("->");
if (x.equals(sl[0]))
xp += getRouteP(routetrace.get(i));
}
xmap.put(x, xp);
return xp;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -