Skip to content

Commit c75d5f0

Browse files
authored
docs: add a code snippet for the sync api (#24)
* Added a sync api code snippet
1 parent 715bbb3 commit c75d5f0

File tree

4 files changed

+179
-2
lines changed

4 files changed

+179
-2
lines changed

optimization/snippets/pom.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2727
</properties>
2828

29-
<!-- [START optimizationai_install_with_bom] -->
29+
3030
<dependencyManagement>
3131
<dependencies>
3232
<dependency>
@@ -45,7 +45,6 @@
4545
<artifactId>google-cloud-optimization</artifactId>
4646
<version>0.1.1</version>
4747
</dependency>
48-
<!-- [END optimizationai_install_with_bom] -->
4948
<dependency>
5049
<groupId>com.google.cloud</groupId>
5150
<artifactId>google-cloud-storage</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# proto-file: google3/google/cloud/optimization/v1/fleet_routing.proto
2+
# proto-message: OptimizeToursRequest
3+
model {
4+
shipments {
5+
pickups {
6+
arrival_location { latitude: 48.874507 longitude: 2.30361 }
7+
time_windows {
8+
start_time { seconds: 1000 }
9+
end_time { seconds: 2000 }
10+
}
11+
duration { seconds: 150 }
12+
}
13+
deliveries {
14+
arrival_location { latitude: 48.880942 longitude: 2.323866 }
15+
time_windows {
16+
start_time { seconds: 3000 }
17+
end_time { seconds: 4000 }
18+
}
19+
duration: { seconds: 250 }
20+
}
21+
load_demands {
22+
key: "weight"
23+
value: { amount: 10 }
24+
}
25+
}
26+
shipments {
27+
pickups {
28+
arrival_location { latitude: 48.880943 longitude: 2.323867 }
29+
time_windows {
30+
start_time { seconds: 1001 }
31+
end_time { seconds: 2001 }
32+
}
33+
duration { seconds: 151 }
34+
}
35+
deliveries {
36+
arrival_location { latitude: 48.880940 longitude: 2.323844 }
37+
time_windows {
38+
start_time { seconds: 3001 }
39+
end_time { seconds: 4001 }
40+
}
41+
duration { seconds: 251 }
42+
}
43+
load_demands {
44+
key: "weight"
45+
value: { amount: 20 }
46+
}
47+
}
48+
vehicles {
49+
start_location { latitude: 48.863102 longitude: 2.341204 }
50+
end_location { latitude: 48.863110 longitude: 2.341205 }
51+
load_limits {
52+
key: "weight"
53+
value: { max_load: 50 }
54+
}
55+
}
56+
vehicles {
57+
start_location { latitude: 48.863112 longitude: 2.341214 }
58+
end_location { latitude: 48.863120 longitude: 2.341215 }
59+
load_limits {
60+
key: "weight"
61+
value: { max_load: 60 }
62+
}
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.optimizationai;
18+
19+
// [START cloudoptimization_sync_api]
20+
21+
import com.google.cloud.optimization.v1.FleetRoutingClient;
22+
import com.google.cloud.optimization.v1.OptimizeToursRequest;
23+
import com.google.cloud.optimization.v1.OptimizeToursResponse;
24+
import com.google.protobuf.Duration;
25+
import com.google.protobuf.TextFormat;
26+
import java.io.FileInputStream;
27+
import java.io.InputStream;
28+
import java.io.InputStreamReader;
29+
import java.io.Reader;
30+
31+
/**
32+
* This is an example to send a request to Cloud Fleet Routing synchronous API via Java API Client.
33+
* A sample sync_request.textproto file can be found in the resources folder.
34+
*/
35+
public class SyncApi {
36+
public static void callSyncApi() throws Exception {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String projectParent = "projects/{YOUR_GCP_PROJECT_ID}";
39+
String modelPath = "YOUR_MODEL_PATH";
40+
callSyncApi(projectParent, modelPath);
41+
}
42+
43+
public static void callSyncApi(String projectParent, String modelPath) throws Exception {
44+
int timeoutSeconds = 100;
45+
InputStream modelInputstream = new FileInputStream(modelPath);
46+
Reader modelInputStreamReader = new InputStreamReader(modelInputstream);
47+
OptimizeToursRequest.Builder requestBuilder =
48+
OptimizeToursRequest.newBuilder()
49+
.setTimeout(Duration.newBuilder().setSeconds(timeoutSeconds).build())
50+
.setParent(projectParent);
51+
TextFormat.getParser().merge(modelInputStreamReader, requestBuilder);
52+
FleetRoutingClient fleetRoutingClient = FleetRoutingClient.create();
53+
OptimizeToursResponse response = fleetRoutingClient.optimizeTours(requestBuilder.build());
54+
System.out.println(response.toString());
55+
}
56+
}
57+
// [END cloudoptimization_sync_api]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.optimizationai;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
/** Tests for SyncApi sample. */
28+
public class SyncApiTest {
29+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
30+
private static final String PROJECT_PARENT = String.format("projects/%s", PROJECT_ID);
31+
private static final String MODEL_PATH = "resources/sync_request.textproto";
32+
33+
private ByteArrayOutputStream bout;
34+
private PrintStream out;
35+
private PrintStream originalPrintStream;
36+
37+
@Before
38+
public void setUp() {
39+
bout = new ByteArrayOutputStream();
40+
out = new PrintStream(bout);
41+
originalPrintStream = System.out;
42+
System.setOut(out);
43+
}
44+
45+
@After
46+
public void tearDown() {
47+
System.out.flush();
48+
System.setOut(originalPrintStream);
49+
}
50+
51+
@Test
52+
public void testSyncApi() throws Exception {
53+
SyncApi.callSyncApi(PROJECT_PARENT, MODEL_PATH);
54+
String got = bout.toString();
55+
assertThat(got).contains("routes");
56+
}
57+
}

0 commit comments

Comments
 (0)