//针对某一个节点的不同事件可信度,计算其可信度 static IOTNodeTrustResult DeduceNodeTrustByDefault(int node, List<IOTEventTrustResult> events, float interval) { //将事件按照类型分类 Dictionary<IOTEventCategoryType, List<IOTEventTrustResult>> temp = new Dictionary<IOTEventCategoryType, List<IOTEventTrustResult>>(); foreach (IOTEventTrustResult e in events) { if (!temp.ContainsKey(e.category)) temp.Add(e.category, new List<IOTEventTrustResult>()); temp[e.category].Add(e); } //计算每个类型的可信度,每个类型的可信度由多次该类型的事件组成,可使用正交的方法进行推导 List<IOTEventTrustCategoryResult> combinedCategories = new List<IOTEventTrustCategoryResult>(); foreach (KeyValuePair<IOTEventCategoryType, List<IOTEventTrustResult>> pair in temp) { IOTEventCategoryType category = pair.Key; List<IOTEventTrustResult> events1 = pair.Value; IOTEventTrustCategoryResult r = CombineToCategory(node, node + "-" + category, category, events1); combinedCategories.Add(r); } //分析每个类型的可信度,以及每个类型事件的频率,来推导节点的可信度 //先分析频率,更新类型的可信度 HashDSClass hashedCaterogies = new HashDSClass(IOTEventTrust.pow(IOTEventType.COUNT)); int[] confirmedCount = new int[(int)IOTEventType.COUNT]; int[] totalCount = new int[(int)IOTEventCategoryType.COUNT]; for (int i = 0; i < combinedCategories.Count; i++) { IOTEventTrustCategoryResult r = combinedCategories[i]; int start = IOTEventTrustResult.GetStartTypeByCategory(r.category); int count = IOTEventTrustResult.GetCountByCategory(r.category); totalCount[i] = r.totalEventCount; for (int j = 0; j < count; j++) confirmedCount[start + j] = r.confirmedEventNums[j]; MergeToHashDS(hashedCaterogies, r); } hashedCaterogies.Normalize(); IOTNodeTrustResult nodeTrust = DeduceNodeTrustByCategories(node, combinedCategories[0].app, hashedCaterogies, confirmedCount, totalCount); return nodeTrust; }
static void MergeToHashDS(HashDSClass ds, IOTEventTrustCategoryResult r) { int start = IOTEventTrustResult.GetStartTypeByCategory(r.category); for (int i = start; i < r.ds.length; i++) ds.SetM(i, r.ds.m[i]); }
//根据节点事件的类型来推导节点的可信度 static IOTNodeTrustResult DeduceNodeTrustByCategories(int node, int app, HashDSClass hashedCaterogies, int[] confirmedCount, int[] totalCount) { IOTGlobal global = (IOTGlobal)IOTGlobal.getInstance(); IOTNodeTrustResult nodeTrust = new IOTNodeTrustResult(node, app); double c1 = DSClass.OR(hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.NotDropPacket)), hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.NotModifyPacket))); //Normal double c2 = DSClass.OR(hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.NotDropPacketButNotReceivePacket)), hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.ModifyPacketDueToNodeFaulty))); //Node faulty double c3 = DSClass.OR(hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.DropPacketDueToBandwith)), hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.NotModifyPacketButNetworkFaulty))); //Env faulty double c4 = DSClass.OR(hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.DropPacketMaliciously)), hashedCaterogies.GetM(IOTEventTrust.pow(IOTEventType.ModifyPacketMaliciously))); //Maliciously DSClass ds = new DSClass(pow(IOTNodeType.COUNT)); ds.SetM(DSClass.pow((int)IOTNodeType.NORMAL), c1); ds.SetM(DSClass.pow((int)IOTNodeType.NODE_FAULTY), c2); ds.SetM(DSClass.pow((int)IOTNodeType.ENV_FAULTY), c3); ds.SetM(DSClass.pow((int)IOTNodeType.MALICIOUS), c4); ds.Normalize(); ds.Cal(); nodeTrust.ds = ds; for (int i = 0; i < totalCount.Length; i++) { IOTEventCategoryType categoryType = (IOTEventCategoryType)i; int start = IOTEventTrustResult.GetStartTypeByCategory(categoryType); int count = IOTEventTrustResult.GetCountByCategory(categoryType); for (int j = 0; j < count; j++) { int iType = start + j; IOTEventType eventType = (IOTEventType)iType; int confirmed = confirmedCount[iType]; if (!nodeTypeMapping.ContainsKey(eventType)) throw new Exception("No such a event type defined"); if (totalCount[i] == 0) continue; nodeTrust.confirmed[(int)nodeTypeMapping[eventType]] += confirmed; if (ds.b[DSClass.pow(iType)] > global.BeliefThrehold && ds.p[DSClass.pow(iType)] > global.PlausibilityThrehold) nodeTrust.confirmedRate[iType] = (double)confirmed / totalCount[i]; } } return nodeTrust; }