What's Covered
This tutorial illustrates how to use 51Degrees signature entities to derive a subset of signatures which match one or more property : value criteria. The following steps are covered:
- How to iterate through the signature entities.
- For each signature entity how to get a list of profiles belonging to the signature.
- How to compare the two profiles.
Code and Explanation
Example of filtering signatures to obtain a subset based on one or more property : value condition. The example starts with a full set of signatures available in the data file and narrows them down to only leave signatures that correspond to mobile devices running Android OS that use the Chrome browser. The example then prints out a list of device IDs for the remaining signatures and the rank for each signature.
Example covers:
-
Creating a dataset without provider
Dataset dataset = MemoryFactory.create( Shared.getLitePatternV32(), true);
-
Converting an Iterable to ArrayList using:
iterableToArrayList(dataset.getSignatures())
-
Using the
filterBy
method, that:- Performs null checks
- Retrieves Property object based on provided property name.
-
For each signature
for (Signature sig : listToFilter) {
Gets values for specified property:Values vals = sig.getValues(property);
Checks if signature in question contains provided property values:if (vals.get(propertyValue) != null)
filterResults.add(sig);
- Temporary list is then returned.
- Accessing deviceId and rank via signature object.
Every invocation of the
filterBy()
function will narrow down the
subset of signatures available based on the provided property name and value.
Eventually only a small subset will be left.
Dynamic filtering can be useful in a number of cases. For example: when
creating an interdependent menu where one choice narrows down the options
in a subsequent choice.
Summary
This tutorial covered how to work with the 51Degrees device Data Model to obtain a subset of devices that meet several conditions. Each condition in this case is a specific value for a chosen property. The result should be read as follows: this is a subset of devices where property1 has value1 and property2 has value2 and property3 has value3 and so on.
One real world application for dynamic filtering is building a set of interlinked menus where each choice will narrow down the available options for subsequent choices. This can be useful when part of the API is exposed to the end user:
- An ad agency could benefit from allowing their clients to target specific devices based on pre-defined criteria, such as DeviceType, ScreenInchesWidth or even PriceBand.
- A program that uses a 51Degrees API to generate/augment reports could be enhanced to allow the user to choose the report parameters. By p roviding a finite set of choices and avoiding arbitrary input the chance of errors occurring is reduced and user experience improved.
The Lite data file contains considerably fewer properties and values than the Premium or Enterprise files, making the usefulness of this tutorial slightly harder to appreciate. With Premium and Enterprise data files there are many more possibilities for creating subsets of devices. For example: using Premium device data you can generate a subset of all ' Samsung ' ' Smartphone ' devices that run ' Android ' OS version ' 4 ' or list browsers by popularity.