Skip to content
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

migrate code from googleapis/java-optimization #7409

Merged
merged 38 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
df956c3
feat: initial generation
Neenu1995 Mar 16, 2022
5707cb1
test(deps): update dependency com.google.truth:truth to v1.1.3 (#11)
renovate-bot Mar 24, 2022
83077b2
test(deps): update dependency junit:junit to v4.13.2 (#5)
renovate-bot Mar 24, 2022
106779c
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Mar 24, 2022
85ad3d9
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Apr 4, 2022
4ada54b
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Apr 6, 2022
715bbb3
docs: add async api code snippet (#25)
changsongd Apr 8, 2022
c75d5f0
docs: add a code snippet for the sync api (#24)
changsongd Apr 8, 2022
82829d4
samples: add long timeout sample (#27)
changsongd Apr 11, 2022
3281436
docs: add get operation code snippet (#28)
changsongd Apr 12, 2022
32742a5
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Apr 21, 2022
305ccf3
chore(deps): update dependency com.google.cloud:libraries-bom to v25.…
renovate-bot Apr 27, 2022
de6c638
chore(deps): update dependency com.google.cloud:libraries-bom to v25.…
renovate-bot May 16, 2022
2bdd021
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot May 25, 2022
3796253
chore(deps): update dependency com.google.cloud:libraries-bom to v25.…
renovate-bot Jun 6, 2022
b826951
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Jul 12, 2022
4a6ae0b
chore(deps): update dependency com.google.cloud:libraries-bom to v26 …
renovate-bot Jul 12, 2022
9f476df
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Aug 2, 2022
ad1a584
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Aug 3, 2022
7ea146c
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Aug 4, 2022
1b5f21e
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Aug 4, 2022
ecf5061
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Aug 6, 2022
1374b66
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Aug 16, 2022
0eab931
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot Aug 16, 2022
aab9327
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot Aug 31, 2022
5cee42c
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Sep 6, 2022
9cfb234
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Sep 9, 2022
3522fde
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Sep 20, 2022
07aec77
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot Sep 20, 2022
a565f99
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Sep 20, 2022
99e1ec3
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Oct 6, 2022
c38cdc1
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot Oct 7, 2022
125eda1
chore(deps): update dependency com.google.cloud:google-cloud-optimiza…
renovate-bot Oct 10, 2022
7a7934f
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot Nov 8, 2022
936bb0b
Merge remote-tracking branch 'migration/main' into java-optimization-…
charlieyu1996 Nov 11, 2022
e5dfb3e
Update pom.xml
charlieyu1996 Nov 11, 2022
f9c9c4b
Update pom.xml
charlieyu1996 Nov 11, 2022
ca722ba
Update pom.xml
charlieyu1996 Nov 11, 2022
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
65 changes: 65 additions & 0 deletions optimization/snippets/pom.xml
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.example.optimization</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>
64 changes: 64 additions & 0 deletions optimization/snippets/resources/sync_request.textproto
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 }
}
}
}
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]
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]
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]
Loading