-
Notifications
You must be signed in to change notification settings - Fork 2.9k
migrate code from googleapis/java-optimization #7409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 37 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
df956c3
feat: initial generation
Neenu1995 5707cb1
test(deps): update dependency com.google.truth:truth to v1.1.3 (#11)
renovate-bot 83077b2
test(deps): update dependency junit:junit to v4.13.2 (#5)
renovate-bot 106779c
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot 85ad3d9
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot 4ada54b
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot 715bbb3
docs: add async api code snippet (#25)
changsongd c75d5f0
docs: add a code snippet for the sync api (#24)
changsongd 82829d4
samples: add long timeout sample (#27)
changsongd 3281436
docs: add get operation code snippet (#28)
changsongd 32742a5
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot 305ccf3
chore(deps): update dependency com.google.cloud:libraries-bom to v25.…
renovate-bot de6c638
chore(deps): update dependency com.google.cloud:libraries-bom to v25.…
renovate-bot 2bdd021
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot 3796253
chore(deps): update dependency com.google.cloud:libraries-bom to v25.…
renovate-bot b826951
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot 4a6ae0b
chore(deps): update dependency com.google.cloud:libraries-bom to v26 …
renovate-bot 9f476df
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot ad1a584
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot 7ea146c
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot 1b5f21e
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot ecf5061
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot 1374b66
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot 0eab931
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot aab9327
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot 5cee42c
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot 9cfb234
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot 3522fde
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot 07aec77
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot a565f99
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot 99e1ec3
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot c38cdc1
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot 125eda1
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot 7a7934f
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot 936bb0b
Merge remote-tracking branch 'migration/main' into java-optimization-…
charlieyu1996 e5dfb3e
Update pom.xml
charlieyu1996 f9c9c4b
Update pom.xml
charlieyu1996 ca722ba
Update pom.xml
charlieyu1996 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>optimization-ai-snippets</artifactId> | ||
<packaging>pom</packaging> | ||
<name>Google Cloud Fleet Routing Samples Parent</name> | ||
<url>https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/optimization</url> | ||
<description> | ||
Java idiomatic client for Google Cloud Platform services. | ||
</description> | ||
|
||
<!-- | ||
The parent pom defines common style checks and testing strategies for our samples. | ||
Removing or replacing it should not affect the execution of the samples in anyway. | ||
--> | ||
<parent> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>shared-configuration</artifactId> | ||
<version>1.2.0</version> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>libraries-bom</artifactId> | ||
<version>26.1.4</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-optimization</artifactId> | ||
<version>1.1.14</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-storage</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>1.1.3</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# proto-file: google3/google/cloud/optimization/v1/fleet_routing.proto | ||
# proto-message: OptimizeToursRequest | ||
model { | ||
shipments { | ||
pickups { | ||
arrival_location { latitude: 48.874507 longitude: 2.30361 } | ||
time_windows { | ||
start_time { seconds: 1000 } | ||
end_time { seconds: 2000 } | ||
} | ||
duration { seconds: 150 } | ||
} | ||
deliveries { | ||
arrival_location { latitude: 48.880942 longitude: 2.323866 } | ||
time_windows { | ||
start_time { seconds: 3000 } | ||
end_time { seconds: 4000 } | ||
} | ||
duration: { seconds: 250 } | ||
} | ||
load_demands { | ||
key: "weight" | ||
value: { amount: 10 } | ||
} | ||
} | ||
shipments { | ||
pickups { | ||
arrival_location { latitude: 48.880943 longitude: 2.323867 } | ||
time_windows { | ||
start_time { seconds: 1001 } | ||
end_time { seconds: 2001 } | ||
} | ||
duration { seconds: 151 } | ||
} | ||
deliveries { | ||
arrival_location { latitude: 48.880940 longitude: 2.323844 } | ||
time_windows { | ||
start_time { seconds: 3001 } | ||
end_time { seconds: 4001 } | ||
} | ||
duration { seconds: 251 } | ||
} | ||
load_demands { | ||
key: "weight" | ||
value: { amount: 20 } | ||
} | ||
} | ||
vehicles { | ||
start_location { latitude: 48.863102 longitude: 2.341204 } | ||
end_location { latitude: 48.863110 longitude: 2.341205 } | ||
load_limits { | ||
key: "weight" | ||
value: { max_load: 50 } | ||
} | ||
} | ||
vehicles { | ||
start_location { latitude: 48.863112 longitude: 2.341214 } | ||
end_location { latitude: 48.863120 longitude: 2.341215 } | ||
load_limits { | ||
key: "weight" | ||
value: { max_load: 60 } | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
optimization/snippets/src/main/java/com/example/optimizationai/AsyncApi.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.optimizationai; | ||
|
||
// [START cloudoptimization_async_api] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.optimization.v1.AsyncModelMetadata; | ||
import com.google.cloud.optimization.v1.BatchOptimizeToursRequest; | ||
import com.google.cloud.optimization.v1.BatchOptimizeToursRequest.AsyncModelConfig; | ||
import com.google.cloud.optimization.v1.BatchOptimizeToursResponse; | ||
import com.google.cloud.optimization.v1.DataFormat; | ||
import com.google.cloud.optimization.v1.FleetRoutingClient; | ||
import com.google.cloud.optimization.v1.GcsDestination; | ||
import com.google.cloud.optimization.v1.GcsSource; | ||
import com.google.cloud.optimization.v1.InputConfig; | ||
import com.google.cloud.optimization.v1.OutputConfig; | ||
|
||
/** | ||
* This is an example to send a request to Cloud Fleet Routing asynchronous API via Java API Client. | ||
* A sample async_request_java.textproto file and a sample request_model_java.json file can be found | ||
* in the resources folder. | ||
*/ | ||
public class AsyncApi { | ||
public static void callAsyncApi() throws Exception { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectParent = "projects/{YOUR_GCP_PROJECT_ID}"; | ||
String inputUri = "gs://YOUR_GCS_PATH"; | ||
String outputUri = "gs://YOUR_SOLUTION_PATH"; | ||
callAsyncApi(projectParent, inputUri, outputUri); | ||
} | ||
|
||
public static void callAsyncApi(String projectParent, String inputUri, String outputUri) | ||
throws Exception { | ||
GcsSource gcsSource = GcsSource.newBuilder().setUri(inputUri).build(); | ||
InputConfig inputConfig = | ||
InputConfig.newBuilder().setGcsSource(gcsSource).setDataFormat(DataFormat.JSON).build(); | ||
GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(outputUri).build(); | ||
OutputConfig outputConfig = | ||
OutputConfig.newBuilder() | ||
.setGcsDestination(gcsDestination) | ||
.setDataFormat(DataFormat.JSON) | ||
.build(); | ||
|
||
AsyncModelConfig asyncModelConfig = | ||
AsyncModelConfig.newBuilder() | ||
.setInputConfig(inputConfig) | ||
.setOutputConfig(outputConfig) | ||
.build(); | ||
BatchOptimizeToursRequest request = | ||
BatchOptimizeToursRequest.newBuilder() | ||
.setParent(projectParent) | ||
.addModelConfigs(asyncModelConfig) | ||
.build(); | ||
|
||
FleetRoutingClient fleetRoutingClient = FleetRoutingClient.create(); | ||
OperationFuture<BatchOptimizeToursResponse, AsyncModelMetadata> response = | ||
fleetRoutingClient.batchOptimizeToursAsync(request); | ||
System.out.format("the response name: %s\n", response.getInitialFuture().get().getName()); | ||
|
||
// Block to wait for the job to finish. | ||
response.getPollingFuture().get(); | ||
if (response.getMetadata().get().getState() == AsyncModelMetadata.State.SUCCEEDED) { | ||
// Code to do your stuff | ||
System.out.println("Job finished successfully."); | ||
} else { | ||
System.out.println( | ||
"Job failed with message:" + response.getPollingFuture().get().getErrorMessage()); | ||
} | ||
} | ||
} | ||
// [END cloudoptimization_async_api] |
57 changes: 57 additions & 0 deletions
57
optimization/snippets/src/main/java/com/example/optimizationai/GetOperation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.optimizationai; | ||
|
||
// [START cloudoptimization_get_operation] | ||
import com.google.cloud.optimization.v1.FleetRoutingClient; | ||
import com.google.longrunning.Operation; | ||
import java.io.IOException; | ||
|
||
class GetOperation { | ||
|
||
static void getOperation() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String operationFullId = "projects/[projectId]/operations/[operationId]"; | ||
getOperation(operationFullId); | ||
} | ||
|
||
// Get the status of an operation | ||
static void getOperation(String operationFullId) throws IOException { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (FleetRoutingClient client = FleetRoutingClient.create()) { | ||
// Get the latest state of a long-running operation. | ||
Operation operation = client.getOperationsClient().getOperation(operationFullId); | ||
|
||
// Display operation details. | ||
System.out.println("Operation details:"); | ||
System.out.format("\tName: %s\n", operation.getName()); | ||
System.out.format("\tMetadata Type Url: %s\n", operation.getMetadata().getTypeUrl()); | ||
System.out.format("\tDone: %s\n", operation.getDone()); | ||
if (operation.hasResponse()) { | ||
System.out.format("\tResponse Type Url: %s\n", operation.getResponse().getTypeUrl()); | ||
} | ||
if (operation.hasError()) { | ||
System.out.println("\tResponse:"); | ||
System.out.format("\t\tError code: %s\n", operation.getError().getCode()); | ||
System.out.format("\t\tError message: %s\n", operation.getError().getMessage()); | ||
} | ||
} | ||
} | ||
} | ||
// [END cloudoptimization_get_operation] |
57 changes: 57 additions & 0 deletions
57
optimization/snippets/src/main/java/com/example/optimizationai/SyncApi.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.optimizationai; | ||
|
||
// [START cloudoptimization_sync_api] | ||
|
||
import com.google.cloud.optimization.v1.FleetRoutingClient; | ||
import com.google.cloud.optimization.v1.OptimizeToursRequest; | ||
import com.google.cloud.optimization.v1.OptimizeToursResponse; | ||
import com.google.protobuf.Duration; | ||
import com.google.protobuf.TextFormat; | ||
import java.io.FileInputStream; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
|
||
/** | ||
* This is an example to send a request to Cloud Fleet Routing synchronous API via Java API Client. | ||
* A sample sync_request.textproto file can be found in the resources folder. | ||
*/ | ||
public class SyncApi { | ||
public static void callSyncApi() throws Exception { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectParent = "projects/{YOUR_GCP_PROJECT_ID}"; | ||
String modelPath = "YOUR_MODEL_PATH"; | ||
callSyncApi(projectParent, modelPath); | ||
} | ||
|
||
public static void callSyncApi(String projectParent, String modelPath) throws Exception { | ||
int timeoutSeconds = 100; | ||
InputStream modelInputstream = new FileInputStream(modelPath); | ||
Reader modelInputStreamReader = new InputStreamReader(modelInputstream); | ||
OptimizeToursRequest.Builder requestBuilder = | ||
OptimizeToursRequest.newBuilder() | ||
.setTimeout(Duration.newBuilder().setSeconds(timeoutSeconds).build()) | ||
.setParent(projectParent); | ||
TextFormat.getParser().merge(modelInputStreamReader, requestBuilder); | ||
FleetRoutingClient fleetRoutingClient = FleetRoutingClient.create(); | ||
OptimizeToursResponse response = fleetRoutingClient.optimizeTours(requestBuilder.build()); | ||
System.out.println(response.toString()); | ||
} | ||
} | ||
// [END cloudoptimization_sync_api] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.