Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ key, names the variable to set, gives the page where a key is created, ends
with its "Find out more" section and exits with code 0. The tests named
`WithoutKey` run each one that way.

The TAC and native model examples need a key that includes
HardwarePlatform.Profiles. With one, they list each device as
`<vendor> <name> (<model>)` under the value looked up. With a key that does
not include it, they say so in one line that links to where a key is created
and to the contact page, make no lookup, end with their "Find out more"
section and exit with code 0. The published packages up to 4.5.130 cannot
build the pipeline for such a key, so with those the examples end with that
error instead. `TacCloudTest` and `NativeModelCloudTest` fail when the key in
`_51DEGREES_RESOURCE_KEY_BESPOKE` lacks the property, and
`HardwareLookupWithoutProfilesTest` checks the message against a stand-in for
the cloud service.

### Pitfalls

- **A data file that is not there.** `cxx/data` holds a `.link` for each file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
import fiftyone.devicedetection.examples.shared.FindOutMore;
import fiftyone.devicedetection.examples.shared.KeyHelper;
import fiftyone.devicedetection.cloud.data.MultiDeviceDataCloud;
import fiftyone.devicedetection.cloud.flowelements.HardwareProfileCloudEngine;
import fiftyone.devicedetection.cloud.flowelements.HardwareProfileCloudEngineBuilder;
import fiftyone.devicedetection.shared.Constants;
import fiftyone.devicedetection.shared.DeviceData;
import fiftyone.pipeline.cloudrequestengine.flowelements.CloudRequestEngine;
import fiftyone.pipeline.cloudrequestengine.flowelements.CloudRequestEngineBuilder;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.core.flowelements.Pipeline;
import fiftyone.pipeline.core.flowelements.PipelineBuilder;
import fiftyone.pipeline.engines.data.AspectPropertyMetaData;
import org.slf4j.LoggerFactory;

import java.io.OutputStream;
Expand All @@ -49,9 +52,19 @@ public class NativeModelCloud {
private static final String EXAMPLE_TAG =
"console-src-main-java-fiftyone-devicedetection-examples-console-nativemodelcloud.java";

/**
* The hardware engine's property that holds the matching devices.
*/
private static final String PROFILES = "Profiles";

/**
* The tag of the links given when the resource key lacks the lookup.
*/
private static final String NO_HARDWARE_LOOKUP = "no-hardware-lookup";

// Example values to use when looking up device details from native model names.
private static final String NATIVE_MODEL_1 = "SC-03L";
private static final String NATIVE_MODEL_2 = "iPhone11,8";
static final String NATIVE_MODEL_1 = "SC-03L";
static final String NATIVE_MODEL_2 = "iPhone11,8";
public static final String NATIVE_MODEL_EXAMPLE_RESOURCE_KEY_NAME = KeyHelper.RESOURCE_KEY_VARIABLE;

public static void main (String[] args) throws Exception {
Expand Down Expand Up @@ -103,10 +116,24 @@ public static void run(String resourceKey, OutputStream os) throws Exception {
// we are not closing them ourselves
.setAutoCloseElements(true)
.build()) {
// Pass a native model into the pipeline and list the matching devices.
if (lacksHardwareLookup(pipeline)) {
output.println("The resource key does not include " +
"HardwarePlatform.Profiles, the hardware lookup " +
"this example needs, so no devices are listed. " +
"Create a resource key that includes it at " +
FindOutMore.link(
KeyHelper.PAID_PROPERTIES_CONFIGURATION,
EXAMPLE_TAG, NO_HARDWARE_LOOKUP) +
", or contact us about it at " +
FindOutMore.link("contact-us", EXAMPLE_TAG,
NO_HARDWARE_LOOKUP) + ".");
} else {
// Pass a native model into the pipeline and list the
// matching devices.
analyseNativeModel(NATIVE_MODEL_1, pipeline, output);
// Repeat for an alternative native model name.
analyseNativeModel(NATIVE_MODEL_2, pipeline, output);
}
}
// The writer closes the stream it was given, so the section is
// written before it.
Expand All @@ -130,15 +157,41 @@ static void analyseNativeModel(String nativemodel, Pipeline pipeline, PrintWrite
// detection, so we have access to all the same properties.
output.format("Which devices are associated with the " +
"native model name '%s'?%n", nativemodel);
if (result.getProfiles().isEmpty()) {
output.println("\tNo devices are associated with this " +
"native model name.");
}
for (DeviceData device : result.getProfiles()) {
// output the result, taking care to deal with "no value" cases
output.format("\t%s; %s; %s%n", asString(device.getHardwareVendor()),
output.format("\t%s %s (%s)%n", asString(device.getHardwareVendor()),
asString(device.getHardwareName()),
asString(device.getHardwareModel()));

}
}
}

/**
* Whether the resource key lacks the hardware lookup. The hardware
* engine's properties come from the cloud's accessible properties for
* the key, and hold Profiles only when the key includes the lookup.
* When the accessible properties could not be fetched nothing is known
* to be missing, so the lookups run and report what went wrong.
*/
static boolean lacksHardwareLookup(Pipeline pipeline) {
if (pipeline.getElement(CloudRequestEngine.class)
.getPublicProperties() == null) {
return false;
}
for (AspectPropertyMetaData property : pipeline
.getElement(HardwareProfileCloudEngine.class)
.getProperties()) {
if (property.getName().equalsIgnoreCase(PROFILES)) {
return false;
}
}
return true;
}
}
/*!
* @example console/NativeModelCloud.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
import fiftyone.devicedetection.examples.shared.FindOutMore;
import fiftyone.devicedetection.examples.shared.KeyHelper;
import fiftyone.devicedetection.cloud.data.MultiDeviceDataCloud;
import fiftyone.devicedetection.cloud.flowelements.HardwareProfileCloudEngine;
import fiftyone.devicedetection.shared.Constants;
import fiftyone.devicedetection.shared.DeviceData;
import fiftyone.pipeline.cloudrequestengine.flowelements.CloudRequestEngine;
import fiftyone.pipeline.core.configuration.PipelineOptions;
import fiftyone.pipeline.core.configuration.PipelineOptionsFactory;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.core.flowelements.Pipeline;
import fiftyone.pipeline.engines.data.AspectPropertyMetaData;
import fiftyone.pipeline.engines.data.AspectPropertyValue;
import fiftyone.pipeline.engines.fiftyone.flowelements.FiftyOnePipelineBuilder;

Expand Down Expand Up @@ -63,6 +66,16 @@ public class TacCloud {
private static final String EXAMPLE_TAG =
"console-src-main-java-fiftyone-devicedetection-examples-console-taccloud.java";

/**
* The hardware engine's property that holds the matching devices.
*/
private static final String PROFILES = "Profiles";

/**
* The tag of the links given when the resource key lacks the lookup.
*/
private static final String NO_HARDWARE_LOOKUP = "no-hardware-lookup";

// Example values to use when looking up device details from TACs.
static final String TAC_1 = "35925406";
static final String TAC_2 = "86386802";
Expand Down Expand Up @@ -117,9 +130,23 @@ public static void run(String resourceKey, OutputStream os) throws Exception {
// Create the pipeline using the service provider and the configured options.
try (Pipeline pipeline = new FiftyOnePipelineBuilder()
.buildFromConfiguration(pipelineOptions)) {
// Pass a TAC into the pipeline and list the matching devices.
analyseTac(TAC_1, pipeline, writer);
analyseTac(TAC_2, pipeline, writer);
if (lacksHardwareLookup(pipeline)) {
writer.println("The resource key does not include " +
"HardwarePlatform.Profiles, the hardware lookup " +
"this example needs, so no devices are listed. " +
"Create a resource key that includes it at " +
FindOutMore.link(
KeyHelper.PAID_PROPERTIES_CONFIGURATION,
EXAMPLE_TAG, NO_HARDWARE_LOOKUP) +
", or contact us about it at " +
FindOutMore.link("contact-us", EXAMPLE_TAG,
NO_HARDWARE_LOOKUP) + ".");
} else {
// Pass a TAC into the pipeline and list the matching
// devices.
analyseTac(TAC_1, pipeline, writer);
analyseTac(TAC_2, pipeline, writer);
}
}
// The writer closes the stream it was given, so the section is
// written before it.
Expand All @@ -141,15 +168,40 @@ static void analyseTac(String tac, Pipeline pipeline, PrintWriter output) throws
// implementing 'DeviceData' which is the same interface used for standard device
// detection, so we have access to all the same properties.
output.format("Which devices are associated with the TAC '%s'?%n", tac);
if (result.getProfiles().isEmpty()) {
output.println("\tNo devices are associated with this TAC.");
}
for (DeviceData device : result.getProfiles()) {
AspectPropertyValue<String> vendor = device.getHardwareVendor();
AspectPropertyValue<List<String>> name = device.getHardwareName();
AspectPropertyValue<String> model = device.getHardwareModel();

// output the result, taking care to deal with "no value" cases
output.format("\t%s; %s; %s%n", asString(vendor), asString(name), asString(model));
output.format("\t%s %s (%s)%n", asString(vendor), asString(name), asString(model));
}
}
}

/**
* Whether the resource key lacks the hardware lookup. The hardware
* engine's properties come from the cloud's accessible properties for
* the key, and hold Profiles only when the key includes the lookup.
* When the accessible properties could not be fetched nothing is known
* to be missing, so the lookups run and report what went wrong.
*/
static boolean lacksHardwareLookup(Pipeline pipeline) {
if (pipeline.getElement(CloudRequestEngine.class)
.getPublicProperties() == null) {
return false;
}
for (AspectPropertyMetaData property : pipeline
.getElement(HardwareProfileCloudEngine.class)
.getProperties()) {
if (property.getName().equalsIgnoreCase(PROFILES)) {
return false;
}
}
return true;
}
}
/*!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */

package fiftyone.devicedetection.examples.console;

import fiftyone.devicedetection.examples.shared.ExampleProcess;
import fiftyone.devicedetection.examples.shared.KeyHelper;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
* Checks what the TAC and native model examples print for the values they
* look up, being a heading for each value with a line for each device under
* it, indented by a tab, in the form {@code <vendor> <name> (<model>)}.
*/
final class HardwareLookupOutput {

/**
* What the examples print when the resource key lacks the lookup.
*/
static final String NOT_INCLUDED =
"does not include HardwarePlatform.Profiles";

/**
* How the examples render a property with no value.
*/
private static final String NO_VALUE = "Unknown";

private HardwareLookupOutput() {
}

/**
* Fails unless the example listed at least one device with a vendor and
* a model for each value, and then ended with exit code 0.
* @param example the example that ran
* @param result what the example wrote and how it ended
* @param lookup the lookup the example makes, such as "TAC lookup"
* @param subject what each value is, such as "the TAC"
* @param values the values the example looks up
*/
static void requireDevices(Class<?> example,
ExampleProcess.Result result,
String lookup,
String subject,
String... values) {
String output = result.output;
if (output.contains(NOT_INCLUDED)) {
fail("The resource key in " +
KeyHelper.BESPOKE_RESOURCE_KEY_VARIABLE +
" does not include HardwarePlatform.Profiles, which the " +
lookup + " needs.\n" + output);
}
for (String value : values) {
String heading = "Which devices are associated with " +
subject + " '" + value + "'?";
if (listsDevice(output, heading) == false) {
fail("No device with a vendor and a model was listed " +
"for " + subject + " '" + value + "'.\n" + output);
}
}
assertEquals(example.getSimpleName() +
" should end with exit code 0.\n" + output,
0, result.exitCode);
}

/**
* Whether a tab indented line under the heading is a device with a
* vendor and a model.
*/
private static boolean listsDevice(String output, String heading) {
String[] lines = output.split("\r?\n");
for (int i = 0; i < lines.length; i++) {
if (lines[i].equals(heading)) {
for (int j = i + 1;
j < lines.length && lines[j].startsWith("\t"); j++) {
if (isDevice(lines[j].substring(1))) {
return true;
}
}
}
}
return false;
}

/**
* Whether the line has the form {@code <vendor> <name> (<model>)}, where
* the vendor is the text before the first space and the model is the
* text in the final parentheses, and neither is empty or a no value.
*/
static boolean isDevice(String line) {
if (line.endsWith(")") == false) {
return false;
}
// The parenthesis that opens the final one, counting any nested in
// the model.
int depth = 0;
int open = -1;
for (int i = line.length() - 1; i >= 0 && open < 0; i--) {
char c = line.charAt(i);
if (c == ')') {
depth++;
} else if (c == '(' && --depth == 0) {
open = i;
}
}
int space = line.indexOf(' ');
if (open < 1 || line.charAt(open - 1) != ' ' ||
space < 1 || space >= open - 1) {
return false;
}
return hasValue(line.substring(0, space)) &&
hasValue(line.substring(open + 1, line.length() - 1));
}

private static boolean hasValue(String text) {
String value = text.trim();
return value.isEmpty() == false && value.startsWith(NO_VALUE) == false;
}
}
Loading
Loading