SequenceExtraction.java: work in progress, ignore it.
[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 import java.util.Map;
9
10 /**
11  * TODO add class documentation.
12  *
13  * @author Janus Varmarken
14  */
15 public class SequenceExtraction {
16
17
18     private final SequenceAlignment<Integer> mAlignmentAlg;
19
20
21     public SequenceExtraction() {
22         mAlignmentAlg = new SequenceAlignment<>(new AlignmentPricer<>((i1,i2) -> Math.abs(i1-i2), i -> 10));
23     }
24
25
26     public SequenceExtraction(SequenceAlignment<Integer> alignmentAlgorithm) {
27         mAlignmentAlg = alignmentAlgorithm;
28     }
29
30     // Initial
31 //    /**
32 //     *
33 //     * @param convsForAction A set of {@link Conversation}s known to be associated with a single type of user action.
34 //     */
35 //    public void extract(List<Conversation> convsForAction) {
36 //        int maxDifference = 0;
37 //
38 //        for (int i = 0; i < convsForAction.size(); i++) {
39 //            for (int j = i+1; j < convsForAction.size(); i++) {
40 //                Integer[] sequence1 = getPacketLengthSequence(convsForAction.get(i));
41 //                Integer[] sequence2 = getPacketLengthSequence(convsForAction.get(j));
42 //                int alignmentCost = mAlignmentAlg.calculateAlignment(sequence1, sequence2);
43 //                if (alignmentCost > maxDifference) {
44 //                    maxDifference = alignmentCost;
45 //                }
46 //            }
47 //        }
48 //
49 //    }
50
51
52 //    public void extract(Map<String, List<Conversation>> hostnameToConvs) {
53 //        int maxDifference = 0;
54 //
55 //        for (int i = 0; i < convsForAction.size(); i++) {
56 //            for (int j = i+1; j < convsForAction.size(); i++) {
57 //                Integer[] sequence1 = getPacketLengthSequence(convsForAction.get(i));
58 //                Integer[] sequence2 = getPacketLengthSequence(convsForAction.get(j));
59 //                int alignmentCost = mAlignmentAlg.calculateAlignment(sequence1, sequence2);
60 //                if (alignmentCost > maxDifference) {
61 //                    maxDifference = alignmentCost;
62 //                }
63 //            }
64 //        }
65 //
66 //    }
67
68     private Integer[] getPacketLengthSequence(Conversation c) {
69         List<PcapPacket> packets = c.getPackets();
70         Integer[] packetLengthSequence = new Integer[packets.size()];
71         for (int i = 0; i < packetLengthSequence.length; i++) {
72             packetLengthSequence[i] = packets.get(i).length();
73         }
74         return packetLengthSequence;
75     }
76 }