ddf8010497c3696dc62d1d304111376126591088
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / SequenceExtraction.java
1 package edu.uci.iotproject;
2
3 import edu.uci.iotproject.comparison.seqalignment.AlignmentPricer;
4 import edu.uci.iotproject.comparison.seqalignment.SequenceAlignment;
5 import org.pcap4j.core.PcapPacket;
6
7 import java.util.List;
8
9 /**
10  * TODO add class documentation.
11  *
12  * @author Janus Varmarken
13  */
14 public class SequenceExtraction {
15
16
17     private final SequenceAlignment<Integer> mAlignmentAlg;
18
19
20     public SequenceExtraction() {
21         mAlignmentAlg = new SequenceAlignment<>(new AlignmentPricer<>((i1,i2) -> Math.abs(i1-i2), i -> 10));
22     }
23
24
25     public SequenceExtraction(SequenceAlignment<Integer> alignmentAlgorithm) {
26         mAlignmentAlg = alignmentAlgorithm;
27     }
28
29     /**
30      *
31      * @param convsForAction A set of {@link Conversation}s known to be associated with a single type of user action.
32      */
33     public void extract(List<Conversation> convsForAction) {
34         int maxDifference = 0;
35
36         for (int i = 0; i < convsForAction.size(); i++) {
37             for (int j = i+1; j < convsForAction.size(); i++) {
38                 Integer[] sequence1 = getPacketLengthSequence(convsForAction.get(i));
39                 Integer[] sequence2 = getPacketLengthSequence(convsForAction.get(j));
40                 int alignmentCost = mAlignmentAlg.calculateAlignment(sequence1, sequence2);
41                 if (alignmentCost > maxDifference) {
42                     maxDifference = alignmentCost;
43                 }
44             }
45         }
46
47     }
48
49     private Integer[] getPacketLengthSequence(Conversation c) {
50         List<PcapPacket> packets = c.getPackets();
51         Integer[] packetLengthSequence = new Integer[packets.size()];
52         for (int i = 0; i < packetLengthSequence.length; i++) {
53             packetLengthSequence[i] = packets.get(i).length();
54         }
55         return packetLengthSequence;
56     }
57 }