Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fiftyone.generate.lists;
- import fiftyone.mobile.detection.Provider;
- import fiftyone.mobile.detection.entities.Signature;
- import fiftyone.mobile.detection.factories.MemoryFactory;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /* *********************************************************************
- * This Source Code Form is copyright of 51Degrees Mobile Experts Limited.
- * Copyright 2014 51Degrees Mobile Experts Limited, 5 Charlotte Close,
- * Caversham, Reading, Berkshire, United Kingdom RG4 7BY
- *
- * This Source Code Form is the subject of the following patent
- * applications, owned by 51Degrees Mobile Experts Limited of 5 Charlotte
- * Close, Caversham, Reading, Berkshire, United Kingdom RG4 7BY:
- * European Patent Application No. 13192291.6; and
- * United States Patent Application Nos. 14/085,223 and 14/085,301.
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0.
- *
- * If a copy of the MPL was not distributed with this file, You can obtain
- * one at http://mozilla.org/MPL/2.0/.
- *
- * This Source Code Form is "Incompatible With Secondary Licenses", as
- * defined by the Mozilla Public License, v. 2.0.
- * ********************************************************************* */
- /**
- * Class provides methods to create sequential lists based on device properties
- * derived from the 51Degrees data file.
- *
- * Tested with V3.2 51Degrees java API.
- */
- public class SequentialListGenerator {
- //51Degrees Provider - allows access to the dataset, used for matching.
- Provider provider;
- //The original list of all the Signatures.
- ArrayList<Signature> sortedSignatures;
- //This list will be modified as filtering takes place.
- ArrayList<Signature> filterSet;
- /**
- * Default constructor.
- * @param fiftyOneDataFile path to the 51Degrees data file.
- */
- public SequentialListGenerator(String fiftyOneDataFile) {
- try {
- //Use memory mode for this as Stream will take considerably longer.
- long startTime = System.nanoTime();
- this.provider = new Provider(
- MemoryFactory.create(fiftyOneDataFile, false)
- );
- long endTime = System.nanoTime();
- System.out.println("Provider initialised. Time elapsed: "
- +((endTime - startTime)/1000000)
- +"ms");
- } catch (IOException ex) {
- Logger.getLogger(SequentialListGenerator.class
- .getName()).log(Level.SEVERE, null, ex);
- }
- }
- /**
- * Test run that produces a list of Browser versions running on Android
- * Samsung IsSmartPhone devices.
- */
- public void testRunSamsungAndroidVersions() {
- long startTime, endTime;
- startTime = System.nanoTime();
- System.out.print("Creating a copy of the Signatures list. ");
- readSignaturesToRanked();
- endTime = System.nanoTime();
- System.out.println("Done. Time elapsed: "
- +((endTime - startTime)/1000000)
- +"ms");
- startTime = System.nanoTime();
- System.out.print("Creating a copy of the Original list. ");
- this.filterSet = new ArrayList<Signature>(this.sortedSignatures);
- endTime = System.nanoTime();
- System.out.println("Done. Time elapsed: "
- +((endTime - startTime)/1000000)
- +"ms");
- try {
- startTime = System.nanoTime();
- System.out.print("Filtering by Samsung vendor. ");
- filterBy("HardwareVendor", "Samsung");
- endTime = System.nanoTime();
- System.out.println("Done. Time elapsed: "
- +((endTime - startTime)/1000000)
- +"ms");
- startTime = System.nanoTime();
- System.out.print("Filtering by IsSmartPhone. ");
- filterBy("IsSmartPhone", "True");
- endTime = System.nanoTime();
- System.out.println("Done. Time elapsed: "
- +((endTime - startTime)/1000000)
- +"ms");
- startTime = System.nanoTime();
- System.out.print("Filtering by PlatformName. ");
- filterBy("PlatformName", "Android");
- endTime = System.nanoTime();
- System.out.println("Done. Time elapsed: "
- +((endTime - startTime)/1000000)
- +"ms");
- printValuesForProperty("BrowserVersion");
- } catch (IOException ex) {
- Logger.getLogger(SequentialListGenerator.class
- .getName()).log(Level.SEVERE, null, ex);
- }
- }
- /**
- * Print all unique values for a given property.
- * @param propertyName
- */
- public void printValuesForProperty(String propertyName) throws IOException {
- ArrayList<String> uniqueValues = new ArrayList<String>();
- //Set up the list of all possible values.
- for (Signature s : this.filterSet) {
- String value = s.getValues(propertyName).toString();
- if (!uniqueValues.contains(value)) {
- uniqueValues.add(value);
- }
- }
- //Print the list.
- for (String val : uniqueValues) {
- System.out.println(val);
- }
- }
- /**
- * Runs an example test with pre-defined filtering parameters.
- */
- public void testRunIsMobileIsTablet() {
- System.out.println("Total signatures in the data file: "
- +provider.dataSet.getSignatures().size());
- //Copy signatures in to master list.
- System.out.print("Signatures read into array: ");
- readSignaturesToRanked();
- System.out.println(this.sortedSignatures.size());
- //Create a working copy. To keep the master list intact.
- System.out.print("Signatures copied: ");
- this.filterSet = new ArrayList<Signature>(this.sortedSignatures);
- System.out.println(this.filterSet.size());
- //Filter by property:value
- System.out.print("Size after filtered by IsMobile:true ");
- try {
- //Get all signatures relating where IsMobile is True.
- filterBy("IsMobile", "True");
- } catch (IOException ex) {
- Logger.getLogger(SequentialListGenerator.class
- .getName()).log(Level.SEVERE, null, ex);
- }
- System.out.println(this.filterSet.size());
- //From the subset of signatures derived above now only leave those where
- //the IsTablet property is set to True.
- System.out.print("Size after filtered by IsTablet:true ");
- try {
- filterBy("IsTablet", "True");
- } catch (IOException ex) {
- Logger.getLogger(SequentialListGenerator.class
- .getName()).log(Level.SEVERE, null, ex);
- }
- System.out.println(this.filterSet.size());
- //Now cut off half the most unpopular signatures.
- System.out.print("Signatures after removing low ranked signatures: ");
- trimRank(50);
- System.out.println(this.filterSet.size());
- //Print the result.
- try {
- report();
- } catch (IOException ex) {
- Logger.getLogger(SequentialListGenerator.class
- .getName()).log(Level.SEVERE, null, ex);
- }
- }
- /**
- * Prints the content of the filtered signatures list.
- * @throws IOException
- */
- public void report() throws IOException {
- Collections.sort(filterSet, new RankComparator());
- for (Signature s : this.filterSet) {
- System.out.println("Device rank: "+s.getRank());
- System.out.println("Device Name: "+s.getValues("HardwareVendor")
- +" : "+s.getValues("HardwareFamily")
- +" : "+s.getValues("HardwareModel"));
- System.out.println("");
- System.out.println("");
- System.out.println("");
- }
- }
- /**
- * Cuts off X percent of the signatures based on signature rank.
- * @param thresholdPercentage
- */
- public void trimRank(int thresholdPercentage) {
- //Initially first rank is both highest and lowest.
- int smallest = this.filterSet.get(0).getRank();
- int largest = this.filterSet.get(0).getRank();
- //First pass.
- //Find min and max.
- for (int i = 0; i < this.filterSet.size(); i++) {
- if (smallest > this.filterSet.get(i).getRank()) {
- smallest = this.filterSet.get(i).getRank();
- }
- if (largest < this.filterSet.get(i).getRank()) {
- largest = this.filterSet.get(i).getRank();
- }
- }
- //Chop off the signatures that are below the threshold.
- int threshold = (largest - smallest) / 100 * thresholdPercentage;
- //Second pass.
- //only leave signatures with rank below the threshold.
- //the lower the rank the more popular the device.
- ArrayList<Signature> temp = new ArrayList<Signature>();
- for (Signature s : this.filterSet) {
- if (s.getRank() < threshold) {
- temp.add(s);
- }
- }
- this.filterSet = temp;
- }
- /**
- * Filter list of signatures by the property name and value. I.e. if you
- * want to see all IsMobile devices. Use several times to narrow down the
- * resulting set.
- * @param propertyName
- * @param propertyValue
- * @throws IOException
- */
- public void filterBy(String propertyName, String propertyValue)
- throws IOException {
- ArrayList<Signature> temp = new ArrayList<Signature>();
- for (Signature s : this.filterSet) {
- if (s.getValues(propertyName).toString().equals(propertyValue)) {
- temp.add(s);
- }
- }
- this.filterSet = temp;
- }
- /**
- * Copies Signatures in to a list.
- */
- public final void readSignaturesToRanked() {
- this.sortedSignatures = new ArrayList<Signature>();
- for (Signature s : provider.dataSet.getSignatures()) {
- this.sortedSignatures.add(s);
- }
- }
- /**
- * Class used when sorting the list by Signature rank.
- */
- public class RankComparator implements Comparator<Signature> {
- @Override
- public int compare(Signature o1, Signature o2) {
- int s1 = o1.getRank();
- int s2 = o2.getRank();
- return s1 < s2 ? -1 : s1 > s2 ? 1 : 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement