nerru86

51Degrees Java build interdependant menu from data file

Mar 23rd, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.85 KB | None | 0 0
  1. /*
  2.  * Program lets user choose device type which then displays all
  3.  * HardwareVendors for this device type. User can then choose
  4.  * hardware name for chosen vendor. Then choose possible OS based
  5.  * on the 3 previous factors. Finally a list of possible OS combinations
  6.  * is displayed for the hardware name of the hardware vendor with
  7.  * a chosen platform name.
  8.  *
  9.  * Requires premium or enterprise data. Can adopt for use with Lite but
  10.  * with different properties.
  11.  */
  12. package sttest;
  13.  
  14. import fiftyone.mobile.detection.Provider;
  15. import fiftyone.mobile.detection.entities.Component;
  16. import fiftyone.mobile.detection.entities.Profile;
  17. import fiftyone.mobile.detection.entities.Property;
  18. import fiftyone.mobile.detection.entities.Signature;
  19. import fiftyone.mobile.detection.entities.Value;
  20. import fiftyone.mobile.detection.factories.StreamFactory;
  21. import java.io.BufferedReader;
  22. import java.io.IOException;
  23. import java.io.InputStreamReader;
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26.  
  27. /**
  28.  *
  29.  * @author mike
  30.  */
  31. public class Test {
  32.  
  33.     private static Provider p;
  34.     private static BufferedReader br;
  35.    
  36.     /**
  37.      * @param args the command line arguments
  38.      */
  39.     public static void main(String[] args) throws IOException {
  40.         //Init program.
  41.         br = new BufferedReader(new InputStreamReader(System.in));
  42.         initDataFile();
  43.        
  44.         //Init parameters, used to fine-tune filtering at every stage.
  45.         String deviceType = "";
  46.         String hardwareVendor = "";
  47.         String hardwareName = "";
  48.         String platformName = "";
  49.         System.out.println("***** Starting Choice Menu *****");
  50.         for (int i = 0; i < 5; i++) {
  51.             switch(i) {
  52.                 case 0:
  53.                     //Print all values for DeviceType
  54.                     System.out.println("DEVICE TYPES:");
  55.                     ArrayList<String> deviceTypes = getDeviceTypes();
  56.                     printArrayList(deviceTypes);
  57.                     //User chooses device type.
  58.                     System.out.print("PLEASE ENTER VALID DEVICE TYPE: ");
  59.                     //Make sure device type is valid
  60.                     do {
  61.                         deviceType = getUserInput();
  62.                     } while(!deviceTypes.contains(deviceType));
  63.                     System.out.println();
  64.                     break;
  65.                 case 1:
  66.                     //Device type selected. Now get hardware vendors based on device type.
  67.                     ArrayList<String> hardwareVendors = getHardwareVendors(deviceType);
  68.                     printArrayList(hardwareVendors);
  69.                     System.out.print("PLEASE ENTER VALID HARDVARE VENDOR: ");
  70.                     //Make sure hardware vendor is valid.
  71.                     do {
  72.                         hardwareVendor = getUserInput();
  73.                     } while(!hardwareVendors.contains(hardwareVendor));
  74.                     System.out.println();
  75.                     break;
  76.                 case 2:
  77.                     //Hardware vendor chosen, get hardware models.
  78.                     ArrayList<String> hardwareNames = getHardwareNames(hardwareVendor, deviceType);
  79.                     printArrayList(hardwareNames);
  80.                     System.out.print("PLEASE ENTER VALID HARDWARE NAME: ");
  81.                     //Make sure hardware models are valid.
  82.                     do {
  83.                         hardwareName = getUserInput();
  84.                     } while(!hardwareNames.contains(hardwareName));
  85.                     System.out.println();
  86.                     break;
  87.                 case 3:
  88.                     /* HardwareModel selected, get all platform names to do with it. */
  89.                     ArrayList<String> platformNames = getPlatformNames(hardwareName ,hardwareVendor, deviceType);
  90.                     printArrayList(platformNames);
  91.                     System.out.print("PLEASE ENTER VALID PLATFORM NAME: ");
  92.                     //Make sure platform name is valid.
  93.                     do {
  94.                         platformName = getUserInput();
  95.                     } while(!platformNames.contains(platformName));
  96.                     System.out.println();
  97.                     break;
  98.                 case 4:
  99.                     /* Finally, get all OS versions valid for that hardware and platform combination. */
  100.                     ArrayList<String> platformVersions = getPlatformVersions(platformName, hardwareName ,hardwareVendor, deviceType);
  101.                     System.out.println("The operating system"+platformName+" on "+hardwareName+" "+hardwareVendor+
  102.                             " can have the following versions:");
  103.                     Collections.sort(platformVersions);
  104.                     printArrayList(platformVersions);
  105.                     break;
  106.                 default: System.out.println("Sorry, something went wrong.");
  107.                     break;
  108.             }
  109.         }
  110.        
  111.         System.exit(0);
  112.     }
  113.    
  114.     /**
  115.      * Function initialised data file and signature ranking. Prints basic info.
  116.      * @throws IOException
  117.      */
  118.     private static void initDataFile() throws IOException {
  119.         //Initialise dataset, print basic info.
  120.         System.out.print("Initialising provider. ");
  121.         p = new Provider(StreamFactory.create("C:\\path\\to\\51degrees.dat"));
  122.         System.out.println("Done.");
  123.         System.out.println("***** Data file info *****");
  124.         System.out.println("Data file: "+p.dataSet.getName());
  125.         System.out.println("Published: "+p.dataSet.published);
  126.         System.out.println("Proeprties: "+p.dataSet.getProperties().size());
  127.         System.out.println("**************************");
  128.         System.out.print("Initialising signatures. ");
  129.         //NEW: initialise signatures.
  130.         p.dataSet.initSignatureRanks();
  131.         System.out.print("Done");
  132.         System.out.println();
  133.     }
  134.    
  135.     /**
  136.      * Read in user input.
  137.      * @return String
  138.      */
  139.     private static String getUserInput() throws IOException {
  140.         return br.readLine();
  141.     }
  142.    
  143.     /**
  144.      * Prints an array list of strings.
  145.      * @param toPrint ArrayList of strings.
  146.      */
  147.     private static void printArrayList(ArrayList<String> toPrint) {
  148.         if(!toPrint.isEmpty()) {
  149.             for (int i = 0; i < toPrint.size(); i++) {
  150.                 System.out.println(toPrint.get(i));
  151.             }
  152.         }
  153.     }
  154.    
  155.     /**
  156.      * Return the possible device types.
  157.      * @return ArrayList of strings, one tor each device type.
  158.      * @throws IOException
  159.      */
  160.     private static ArrayList<String> getDeviceTypes() throws IOException {
  161.         ArrayList<String> result = new ArrayList<>();
  162.         for (Value v : p.dataSet.get("DeviceType").getValues()) {
  163.             result.add(v.getName());
  164.         }
  165.         return result;
  166.     }
  167.    
  168.     /**
  169.      * Returns a list of all hardware vendors for a given device type.
  170.      * @param deviceType String to filter by device type.
  171.      * @return ArrayList of Strings with unique hardware vendors.
  172.      * @throws IOException
  173.      */
  174.     private static ArrayList<String> getHardwareVendors(String deviceType) throws IOException {
  175.         ArrayList<String> distinctHardwareVendors = new ArrayList<>();
  176.        
  177.         //Loop through all profiles in the Hardware components.
  178.         for (Profile pr : p.dataSet.getHardware().getProfiles()){
  179.             //Get all values with given DeviceType parameter.
  180.             String val = pr.getValues("DeviceType").get(0).getName();
  181.            
  182.             if(val.equals(deviceType)) {
  183.                 //Add distinct related Profiles to the list.
  184.                 ArrayList<Profile> relatedProfiles = new ArrayList<>();
  185.                
  186.                 //Calculate max possible rank.
  187.                 int max = 0;
  188.                 int threshold = 75;
  189.                 for (Signature s : pr.Signatures()) {
  190.                     if (max < s.getRank())
  191.                         max = s.getRank();
  192.                 }
  193.                 //Cut off everything above the threshold.
  194.                 int maxRank = (max/100)*threshold;
  195.                
  196.                 for (Signature s : pr.Signatures()) {
  197.                    
  198.                     if (s.getRank() < maxRank) {
  199.                        
  200.                         for (Profile p1 : s.getProfiles()) {
  201.                             if(!relatedProfiles.contains(p1))
  202.                                 relatedProfiles.add(p1);
  203.                         }
  204.                     }
  205.                 }
  206.                
  207.                 //Go through each profile in the relatedProfiles list to filter
  208.                 //by some variable.
  209.                 for (Profile p2 : relatedProfiles) {
  210.                     if(p2.getComponent().getName().equals("HardwarePlatform")) {
  211.                         String osName = p2.getValues("HardwareVendor").get(0).getName();
  212.                        
  213.                         if(!distinctHardwareVendors.contains(osName)) {
  214.                             distinctHardwareVendors.add(osName);
  215.                         }
  216.                     }
  217.                 }
  218.             }
  219.         }
  220.         return distinctHardwareVendors;
  221.     }
  222.    
  223.     /**
  224.      * Function returns the list of strings derived from filtering by HardwareVendor and DeviceType
  225.      * @param hv String representing HardwareVendor filter criteria.
  226.      * @param dt String representing DeviceType filter criteria.
  227.      * @return ArrayList of Strings with distinct Hardware Names.
  228.      * @throws IOException
  229.      */
  230.     private static ArrayList<String> getHardwareNames(String hv, String dt) throws IOException {
  231.         ArrayList<String> distinctHardwareNames = new ArrayList<>();
  232.        
  233.         //Loop through all profiles in the Hardware components.
  234.         for (Profile pr : p.dataSet.getHardware().getProfiles()){
  235.             String hardwareVendor = pr.getValues("HardwareVendor").get(0).getName();
  236.             String deviceType = pr.getValues("DeviceType").get(0).getName();
  237.            
  238.             //Filter by hardware vendor and device type.
  239.             if(hardwareVendor.equals(hv) && deviceType.equals(dt)) {
  240.                 //Add distinct related Profiles to the list.
  241.                 ArrayList<Profile> relatedProfiles = new ArrayList<>();
  242.                
  243.                 //Calculate max possible rank to cut off some of the values.
  244.                 int max = 0;
  245.                 int threshold = 100;
  246.                 for (Signature s : pr.Signatures()) {
  247.                     if (max < s.getRank())
  248.                         max = s.getRank();
  249.                 }
  250.                 //Cut off everything above the threshold.
  251.                 int maxRank = (max/100)*threshold;
  252.                
  253.                 for (Signature s : pr.Signatures()) {
  254.                     if (s.getRank() < maxRank) {
  255.                         for (Profile p1 : s.getProfiles()) {
  256.                             if(!relatedProfiles.contains(p1))
  257.                                 relatedProfiles.add(p1);
  258.                         }
  259.                     }
  260.                 }
  261.                
  262.                 //Go through each profile in the relatedProfiles list to filter
  263.                 //by some variable.
  264.                 for (Profile p2 : relatedProfiles) {
  265.                     if(p2.getComponent().getName().equals("HardwarePlatform")) {
  266.                         String resultHardwareName = "";
  267.                         for (int i = 0; i < p2.getValues("HardwareName").size(); i++) {
  268.                             resultHardwareName = p2.getValues("HardwareName").get(i).getName();
  269.                            
  270.                             //Add unique browser name to the list to be dispalyed.
  271.                             if(!distinctHardwareNames.contains(resultHardwareName)) {
  272.                                 distinctHardwareNames.add(resultHardwareName);
  273.                             }
  274.                         }
  275.                     }
  276.                 }
  277.             }
  278.  
  279.         }
  280.         return distinctHardwareNames;
  281.     }
  282.    
  283.     /**
  284.      * Function returns the list of platform names based on HardwareName, HardwareVendor and DeviceType.
  285.      * @param hn String HardwareName filter
  286.      * @param hv String HardwareVendor filter.
  287.      * @param dt String DeviceType filter.
  288.      * @return ArrayList of Strings with PlatformName values
  289.      * @throws IOException
  290.      */
  291.     private static ArrayList<String> getPlatformNames(String hn, String hv, String dt) throws IOException {
  292.         ArrayList<String> distinctPlatformNames = new ArrayList<>();
  293.        
  294.         //Loop through all profiles in the Hardware components.
  295.         for (Profile pr : p.dataSet.getHardware().getProfiles()){
  296.  
  297.             String hardwareName = pr.getValues("HardwareName").get(0).getName();
  298.             String hardwareVendor = pr.getValues("HardwareVendor").get(0).getName();
  299.             String deviceType = pr.getValues("DeviceType").get(0).getName();
  300.            
  301.             //Find all for chosen devie type.
  302.             if(hardwareName.equals(hn) && hardwareVendor.equals(hv) && deviceType.equals(dt)) {
  303.                 //DEBUG: System.out.println(hn+" "+hv+" "+dt);
  304.                 //Add distinct related Profiles to the list.
  305.                 ArrayList<Profile> relatedProfiles = new ArrayList<>();
  306.  
  307.                                 //Calculate max possible rank to cut off some of the values.
  308.                 int max = 0;
  309.                 int threshold = 100;
  310.                 for (Signature s : pr.Signatures()) {
  311.                     if (max < s.getRank())
  312.                         max = s.getRank();
  313.                 }
  314.                 //Cut off everything above the threshold.
  315.                 int maxRank = (max/100)*threshold;
  316.                
  317.                 for (Signature s : pr.Signatures()) {
  318.                     if (hn.startsWith("iPhone")) {
  319.                         for (Profile p1 : s.getProfiles()) {
  320.                             if(!relatedProfiles.contains(p1))
  321.                                 relatedProfiles.add(p1);
  322.                         }
  323.                     } else {
  324.                         if (s.getRank() <= maxRank) {
  325.                             //System.out.println("Test");
  326.                             for (Profile p1 : s.getProfiles()) {
  327.                                 if(!relatedProfiles.contains(p1))
  328.                                     relatedProfiles.add(p1);
  329.                             }
  330.                         }
  331.                     }
  332.                 }
  333.                
  334.                
  335.                 //Go through each profile in the relatedProfiles list to filter
  336.                 //by some variable.
  337.                 for (Profile p2 : relatedProfiles) {
  338.                     if(p2.getComponent().getName().equals("SoftwarePlatform")) {
  339.                        
  340.                         String tempPlatformName = p2.getValues("PlatformName").get(0).getName();
  341.                        
  342.                         //Add unique browser name to the list to be dispalyed.
  343.                         if(!distinctPlatformNames.contains(tempPlatformName)) {
  344.                             distinctPlatformNames.add(tempPlatformName);
  345.                         }
  346.                     }
  347.                 }
  348.             }
  349.         }
  350.         return distinctPlatformNames;
  351.     }
  352.    
  353.     /**
  354.      * Returns a list of Platform Names for HardwareVendor's HardwareValue running a specific PlatformName OS.
  355.      * @param pn String PlatformName
  356.      * @param hn String HardwareName
  357.      * @param hv String HardwareVendor
  358.      * @param dt String DeviceType
  359.      * @return Returns a list of Strings with OS versions
  360.      * @throws IOException
  361.      */
  362.     private static ArrayList<String> getPlatformVersions(String pn, String hn, String hv, String dt) throws IOException {
  363.         ArrayList<String> distinctPlatformVersions = new ArrayList<>();
  364.        
  365.         //Loop through all profiles in the Hardware components.
  366.         for (Profile pr : p.dataSet.getHardware().getProfiles()){
  367.  
  368.             String hardwareName = pr.getValues("HardwareName").get(0).getName();
  369.             String hardwareVendor = pr.getValues("HardwareVendor").get(0).getName();
  370.             String deviceType = pr.getValues("DeviceType").get(0).getName();
  371.            
  372.             if(hardwareName.equals(hn) && hardwareVendor.equals(hv) && deviceType.equals(dt)) {
  373.                 //Add distinct related Profiles to the list.
  374.                 ArrayList<Profile> relatedProfiles = new ArrayList<>();
  375.  
  376.                 for (Signature s : pr.Signatures()) {
  377.                     //if (s.getRank() < maxRank) {
  378.                        
  379.                         for (Profile p1 : s.getProfiles()) {
  380.                             if(!relatedProfiles.contains(p1))
  381.                                 relatedProfiles.add(p1);
  382.                         }
  383.                     //}
  384.                 }
  385.                
  386.                 //Go through each profile in the relatedProfiles list to filter
  387.                 //by some variable.
  388.                 for (Profile p2 : relatedProfiles) {
  389.                     if(p2.getComponent().getName().equals("SoftwarePlatform")) {
  390.                         String osName = p2.getValues("PlatformName").get(0).getName();
  391.                        
  392.                         //Now, check that name of the OS corresponds to the one we want.
  393.                         if(osName.equals(pn)) {
  394.                            
  395.                             String tempPlatformVersion = p2.getValues("PlatformVersion").get(0).getName();
  396.                            
  397.                             //Add unique browser name to the list to be dispalyed.
  398.                             if(!distinctPlatformVersions.contains(tempPlatformVersion)) {
  399.                                 distinctPlatformVersions.add(tempPlatformVersion);
  400.                             }
  401.                         }
  402.                     }
  403.                 }
  404.             }
  405.         }
  406.         return distinctPlatformVersions;
  407.     }
  408. }
Advertisement
Add Comment
Please, Sign In to add comment