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
19 changes: 13 additions & 6 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,19 @@ 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.
section and exit with code 0. A lookup that matches no device prints
`No devices are associated with this TAC.` or `... this native model name.`
under its heading. When the cloud's list of the key's properties cannot be
fetched, the examples end with the cloud service's error and a non-zero exit
code. The published packages up to 4.5.130 cannot build the pipeline for a
key without the lookup, so with those the examples log
`Aspect properties could not be loaded for fiftyone.devicedetection.cloud.flowelements.HardwareProfileCloudEngine`,
end with `java.lang.RuntimeException: Failed to load aspect properties for element 'hardware'`
and exit with code 1. `TacCloudTest` and `NativeModelCloudTest` fail when the
key in `_51DEGREES_RESOURCE_KEY_BESPOKE` lacks the property.
`HardwareLookupStandInTest` runs both examples against a stand-in for the
cloud service, for a key without the lookup, for lookups that match nothing,
and for a list of properties that fails once or every time.

### Pitfalls

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,20 @@ public static void run(String resourceKey, OutputStream os) throws Exception {
.setResourceKey(resourceKey)
.build())
// Create the hardware profile engine to process the response from the
// request engine - and add to the pipleine
// request engine - and add to the pipeline
.addFlowElement(new HardwareProfileCloudEngineBuilder(LoggerFactory.getILoggerFactory())
.build())
// very important to ask pipeline to close the engines we added since
// we are not closing them ourselves
.setAutoCloseElements(true)
.build()) {
// The pipeline asked the cloud for the key's accessible
// properties as it was built, so unless that request failed,
// a key without the lookup is found here, before any lookup.
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);
writeNoHardwareLookup(output);
} else if (analyseNativeModel(NATIVE_MODEL_1, pipeline,
output)) {
// Repeat for an alternative native model name.
analyseNativeModel(NATIVE_MODEL_2, pipeline, output);
}
Expand All @@ -142,14 +134,28 @@ public static void run(String resourceKey, OutputStream os) throws Exception {
}
}

static void analyseNativeModel(String nativemodel, Pipeline pipeline, PrintWriter output) throws Exception {
/**
* Lists the devices associated with the native model name, or says that
* the resource key does not include the hardware lookup.
* @return false if the key does not include the lookup
*/
static boolean analyseNativeModel(String nativemodel, Pipeline pipeline, PrintWriter output) throws Exception {
// Create the FlowData instance. This is wrapped in a try with resources to ensure
// that resources are disposed correctly.
try (FlowData data = pipeline.createFlowData()) {
// Add the native model key as evidence.
data.addEvidence(Constants.EVIDENCE_QUERY_NATIVE_MODEL_KEY, nativemodel);
// Process the supplied evidence.
// Process the supplied evidence. If the accessible properties
// could not be fetched as the pipeline was built, processing
// fetches them first, and throws the cloud service's error if
// that fails again.
data.process();
// So they are known now, and are checked before the result is
// used.
if (lacksHardwareLookup(pipeline)) {
writeNoHardwareLookup(output);
return false;
}
// Get result data from the flow data.
MultiDeviceDataCloud result = data.get(MultiDeviceDataCloud.class);
// The 'MultiDeviceDataCloud' object contains one or more instances
Expand All @@ -168,15 +174,32 @@ static void analyseNativeModel(String nativemodel, Pipeline pipeline, PrintWrite
asString(device.getHardwareModel()));

}
return true;
}
}

/**
* 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.
* Says that the resource key does not include the hardware lookup, with
* where to create a key that does.
*/
static void writeNoHardwareLookup(PrintWriter output) {
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) + ".");
}

/**
* Whether the resource key is known to lack 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. Until the accessible properties have been fetched nothing
* is known to be missing.
*/
static boolean lacksHardwareLookup(Pipeline pipeline) {
if (pipeline.getElement(CloudRequestEngine.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static void main (String[] args) throws Exception {
public static void run(String resourceKey, OutputStream os) throws Exception {
// In this example, we use the FiftyOnePipelineBuilder and configure it from a file.
// For a demonstration of how to do this in code instead, see the
// NativeModelLookup example.
// NativeModelCloud example.
// For more information about builders in general see the documentation at
// https://51degrees.com/documentation/_pipeline_api__concepts__configuration__builders__index.html?utm_source=code&utm_medium=example&utm_campaign=51degrees-java&utm_content=console-src-main-java-fiftyone-devicedetection-examples-console-taccloud.java&utm_term=run

Expand All @@ -130,21 +130,14 @@ 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)) {
// The pipeline asked the cloud for the key's accessible
// properties as it was built, so unless that request failed,
// a key without the lookup is found here, before any lookup.
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);
writeNoHardwareLookup(writer);
} else if (analyseTac(TAC_1, pipeline, writer)) {
// Pass the second TAC into the pipeline and list the
// matching devices.
analyseTac(TAC_2, pipeline, writer);
}
}
Expand All @@ -155,13 +148,27 @@ public static void run(String resourceKey, OutputStream os) throws Exception {
}
}

static void analyseTac(String tac, Pipeline pipeline, PrintWriter output) throws Exception {
/**
* Lists the devices associated with the TAC, or says that the resource
* key does not include the hardware lookup.
* @return false if the key does not include the lookup
*/
static boolean analyseTac(String tac, Pipeline pipeline, PrintWriter output) throws Exception {
// Create the FlowData instance.
try (FlowData data = pipeline.createFlowData()) {
// Add the TAC as evidence.
data.addEvidence(Constants.EVIDENCE_QUERY_TAC_KEY, tac);
// Process the supplied evidence.
// Process the supplied evidence. If the accessible properties
// could not be fetched as the pipeline was built, processing
// fetches them first, and throws the cloud service's error if
// that fails again.
data.process();
// So they are known now, and are checked before the result is
// used.
if (lacksHardwareLookup(pipeline)) {
writeNoHardwareLookup(output);
return false;
}
// Get result data from the flow data.
MultiDeviceDataCloud result = data.get(MultiDeviceDataCloud.class);
// The 'MultiDeviceDataCloud' object contains one or more instances
Expand All @@ -179,15 +186,32 @@ static void analyseTac(String tac, Pipeline pipeline, PrintWriter output) throws
// output the result, taking care to deal with "no value" cases
output.format("\t%s %s (%s)%n", asString(vendor), asString(name), asString(model));
}
return true;
}
}

/**
* 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.
* Says that the resource key does not include the hardware lookup, with
* where to create a key that does.
*/
static void writeNoHardwareLookup(PrintWriter output) {
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) + ".");
}

/**
* Whether the resource key is known to lack 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. Until the accessible properties have been fetched nothing
* is known to be missing.
*/
static boolean lacksHardwareLookup(Pipeline pipeline) {
if (pipeline.getElement(CloudRequestEngine.class)
Expand Down
Loading
Loading