Skip to content

Commit

Permalink
docs: add a code snippet for the sync api (#24)
Browse files Browse the repository at this point in the history
* Added a sync api code snippet
  • Loading branch information
changsongd authored Apr 8, 2022
1 parent 715bbb3 commit c75d5f0
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 2 deletions.
3 changes: 1 addition & 2 deletions optimization/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!-- [START optimizationai_install_with_bom] -->

<dependencyManagement>
<dependencies>
<dependency>
Expand All @@ -45,7 +45,6 @@
<artifactId>google-cloud-optimization</artifactId>
<version>0.1.1</version>
</dependency>
<!-- [END optimizationai_install_with_bom] -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
Expand Down
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,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]
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;

import static com.google.common.truth.Truth.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/** Tests for SyncApi sample. */
public class SyncApiTest {
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String PROJECT_PARENT = String.format("projects/%s", PROJECT_ID);
private static final String MODEL_PATH = "resources/sync_request.textproto";

private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}

@After
public void tearDown() {
System.out.flush();
System.setOut(originalPrintStream);
}

@Test
public void testSyncApi() throws Exception {
SyncApi.callSyncApi(PROJECT_PARENT, MODEL_PATH);
String got = bout.toString();
assertThat(got).contains("routes");
}
}

0 comments on commit c75d5f0

Please sign in to comment.