Skip to content

Commit 49b27ce

Browse files
Brian OlsenBrian Olsen
Brian Olsen
authored and
Brian Olsen
committed
Remove extra node folder and implement the most of the rest of the functions.
1 parent 2f47d18 commit 49b27ce

File tree

1,485 files changed

+1785
-38668
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,485 files changed

+1785
-38668
lines changed

Diff for: .classpath

-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717
<attribute name="maven.pomderived" value="true"/>
1818
</attributes>
1919
</classpathentry>
20-
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
21-
<attributes>
22-
<attribute name="maven.pomderived" value="true"/>
23-
</attributes>
24-
</classpathentry>
2520
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
2621
<attributes>
2722
<attribute name="maven.pomderived" value="true"/>

Diff for: pom.xml

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<j2v8.version>4.5.0</j2v8.version>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<maven.compiler.target>1.8</maven.compiler.target>
1618
</properties>
1719

1820
<repositories>

Diff for: src/main/java/us/brianolsen/instructions/OSRMTextInstructions.java

+39-29
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@
88
import com.eclipsesource.v8.V8Array;
99
import com.eclipsesource.v8.V8Function;
1010
import com.eclipsesource.v8.V8Object;
11+
import com.google.gson.Gson;
1112
import com.mapbox.services.api.directions.v5.models.LegStep;
1213

14+
import us.brianolsen.instructions.util.V8Util;
15+
1316
public class OSRMTextInstructions implements Closeable {
14-
private static final String MODULE_NAME = "osrm-text-instructions";
15-
private static final String RESOURCES_DIRECTORY = OSRMTextInstructions.class.getClassLoader().getResource("")
16-
.getPath() + "/";
17-
private static final String NODE_MODULES_DIRECTORY = RESOURCES_DIRECTORY + "node_modules/";
18-
private static final String OSRM_TEXT_INSTRUCTIONS_MODULE_DIRECTORY = NODE_MODULES_DIRECTORY + MODULE_NAME + "/";
17+
public static final String DEFAULT_VERSION = "v5";
18+
public static final String DEFAULT_LANGUAGE = "en";
19+
protected static final String MODULE_NAME = "osrm-text-instructions";
20+
protected static final String NODE_MODULES_DIRECTORY = V8Util.RESOURCES_DIRECTORY + "node_modules/";
21+
protected static final String OSRM_TEXT_INSTRUCTIONS_MODULE_DIRECTORY = NODE_MODULES_DIRECTORY + MODULE_NAME + "/";
22+
protected static final Gson gson = new Gson();
1923

2024
private NodeJS nodeJS;
2125
private V8Object osrmTextInstructions;
22-
private String version = "v5";
26+
private String version = DEFAULT_VERSION;
2327

2428
public V8 getRuntime() {
2529
return nodeJS.getRuntime();
@@ -44,6 +48,7 @@ public OSRMTextInstructions(String version) {
4448

4549
private void init() {
4650
nodeJS = NodeJS.createNodeJS();
51+
4752
V8Function osrmModule = (V8Function) getNodeJS().require(new File(OSRM_TEXT_INSTRUCTIONS_MODULE_DIRECTORY));
4853

4954
V8Array parameters = new V8Array(osrmModule.getRuntime());
@@ -79,46 +84,51 @@ public String directionFromDegree(String language, Double degree) {
7984
}
8085

8186
public String laneConfig(LegStep step) {
82-
throw new UnsupportedOperationException("still needs to be implemented");
87+
V8Object v8Step = V8Util.jsonStringToV8Object(getRuntime(), gson.toJson(step));
88+
String laneConfig = (String) osrmTextInstructions.executeJSFunction("laneConfig", v8Step);
89+
90+
v8Step.release();
91+
return laneConfig;
8392
}
8493

8594
public String getWayName(String language, LegStep step, String options) {
86-
throw new UnsupportedOperationException("still needs to be implemented");
95+
V8Object v8Step = V8Util.jsonStringToV8Object(getRuntime(), gson.toJson(step));
96+
V8Object v8Options = V8Util.jsonStringToV8Object(getRuntime(), options);
97+
String wayName = (String) osrmTextInstructions.executeJSFunction("getWayName", language, v8Step, v8Options);
8798

99+
v8Options.release();
100+
v8Step.release();
101+
return wayName;
88102
}
89103

90-
public String compile(String language, V8Object step, V8Object options) {
91-
// FIXME take LegStep step from mapbox
92-
return (String) osrmTextInstructions.executeJSFunction("compile", language, step, options);
93-
}
104+
public String compile(String language, LegStep step, String options) {
105+
V8Object v8Step = V8Util.jsonStringToV8Object(getRuntime(), gson.toJson(step));
106+
V8Object v8Options = V8Util.jsonStringToV8Object(getRuntime(), options);
107+
String instruction = (String) osrmTextInstructions.executeJSFunction("compile", language, v8Step, v8Options);
108+
109+
v8Options.release();
110+
v8Step.release();
111+
return instruction;
94112

95-
public String compile(String language, LegStep step, V8Object options) {
96-
throw new UnsupportedOperationException("still needs to be implemented");
97113
}
98114

99115
public String grammarize(String language, String name, String grammar) {
100-
throw new UnsupportedOperationException("still needs to be implemented");
101-
116+
return (String) osrmTextInstructions.executeJSFunction("grammarize", language, name, grammar);
102117
}
103118

104119
public String tokenize(String language, String instruction, String tokens, String options) {
105-
throw new UnsupportedOperationException("still needs to be implemented");
120+
V8Object v8Tokens = V8Util.jsonStringToV8Object(getRuntime(), tokens);
121+
V8Object v8Options = V8Util.jsonStringToV8Object(getRuntime(), options);
122+
String tokenizedString = (String) osrmTextInstructions.executeJSFunction("tokenize", language, instruction,
123+
v8Tokens, v8Options);
124+
125+
v8Options.release();
126+
v8Tokens.release();
127+
return tokenizedString;
106128
}
107129

108130
public String getBestMatchingLanguage(String language) {
109131
return (String) osrmTextInstructions.executeJSFunction("getBestMatchingLanguage", language);
110132
}
111133

112-
// private static File createTemporaryScriptFile(final String script, final
113-
// String name) throws IOException {
114-
// File tempFile = File.createTempFile(name, ".js.tmp", new
115-
// File(RESOURCES_DIRECTORY));
116-
// PrintWriter writer = new PrintWriter(tempFile, "UTF-8");
117-
// try {
118-
// writer.print(script);
119-
// } finally {
120-
// writer.close();
121-
// }
122-
// return tempFile;
123-
// }
124134
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package us.brianolsen.instructions.model;
2+
3+
import java.io.Closeable;
4+
import java.io.IOException;
5+
import java.util.List;
6+
7+
import com.eclipsesource.v8.V8;
8+
import com.eclipsesource.v8.V8Array;
9+
import com.eclipsesource.v8.V8Object;
10+
import com.mapbox.services.api.directions.v5.models.IntersectionLanes;
11+
import com.mapbox.services.api.directions.v5.models.LegStep;
12+
import com.mapbox.services.api.directions.v5.models.StepIntersection;
13+
import com.mapbox.services.api.directions.v5.models.StepManeuver;
14+
15+
import us.brianolsen.instructions.util.V8Util;
16+
17+
public class V8LegStep implements Closeable {
18+
private final V8 runtime;
19+
private final V8Object v8Step;
20+
21+
public V8LegStep(LegStep legStep, V8 runtime) {
22+
this.runtime = runtime;
23+
this.v8Step = convertFromLegStep(legStep);
24+
}
25+
26+
public V8 getRuntime() {
27+
return runtime;
28+
}
29+
30+
public V8Object getV8Step() {
31+
return v8Step;
32+
}
33+
34+
@Override
35+
public void close() throws IOException {
36+
releaseV8LegStep();
37+
}
38+
39+
private V8Object convertFromLegStep(LegStep legStep) {
40+
V8Object step = new V8Object(getRuntime());
41+
V8Object tempManeuver = convertFromStepManeuver(legStep.getManeuver());
42+
V8Array tempIntersections = convertFromStepIntersectionList(legStep.getIntersections());
43+
44+
V8Util.add(step, "destinations", legStep.getDestinations());
45+
V8Util.add(step, "distance", legStep.getDistance());
46+
V8Util.add(step, "duration", legStep.getDuration());
47+
V8Util.add(step, "geometry", legStep.getGeometry());
48+
V8Util.add(step, "mode", legStep.getMode());
49+
V8Util.add(step, "pronunciation", legStep.getPronunciation());
50+
V8Util.add(step, "ref", legStep.getRef());
51+
V8Util.add(step, "rotary_name", legStep.getRotaryName());
52+
V8Util.add(step, "rotary_pronunciation", legStep.getRotaryPronunciation());
53+
V8Util.add(step, "intersections", tempIntersections);
54+
V8Util.add(step, "weight", legStep.getWeight());
55+
V8Util.add(step, "maneuver", tempManeuver);
56+
V8Util.add(step, "name", legStep.getName());
57+
58+
V8Util.release(tempManeuver);
59+
V8Util.release(tempIntersections);
60+
return step;
61+
}
62+
63+
private V8Object convertFromStepManeuver(StepManeuver stepManeuver) {
64+
if (stepManeuver == null) {
65+
return null;
66+
}
67+
V8Object maneuver = new V8Object(getRuntime());
68+
V8Util.add(maneuver, "bearing_after", stepManeuver.getBearingAfter());
69+
V8Util.add(maneuver, "bearing_before", stepManeuver.getBearingBefore());
70+
V8Util.add(maneuver, "instruction", stepManeuver.getInstruction());
71+
V8Util.add(maneuver, "modifier", stepManeuver.getModifier());
72+
V8Util.add(maneuver, "type", stepManeuver.getType());
73+
V8Util.add(maneuver, "exit", stepManeuver.getExit());
74+
return maneuver;
75+
}
76+
77+
private V8Array convertFromStepIntersectionList(List<StepIntersection> stepIntersections) {
78+
if (stepIntersections == null || stepIntersections.isEmpty()) {
79+
return null;
80+
}
81+
82+
V8Array v8Array = new V8Array(getRuntime());
83+
if (stepIntersections != null) {
84+
for (StepIntersection stepIntersection : stepIntersections) {
85+
V8Object tempStepIntersection = convertFromStepIntersection(stepIntersection);
86+
v8Array.push(tempStepIntersection);
87+
V8Util.release(tempStepIntersection);
88+
}
89+
}
90+
return v8Array;
91+
}
92+
93+
private V8Object convertFromStepIntersection(StepIntersection stepIntersection) {
94+
if (stepIntersection == null) {
95+
return null;
96+
}
97+
V8Object intersection = new V8Object(getRuntime());
98+
V8Util.add(intersection, "in", stepIntersection.getIn());
99+
V8Util.add(intersection, "out", stepIntersection.getOut());
100+
101+
V8Array tempBearings = new V8Array(getRuntime());
102+
V8Array tempEntry = new V8Array(getRuntime());
103+
V8Array tempLanes = new V8Array(getRuntime());
104+
V8Array tempLocation = new V8Array(getRuntime());
105+
106+
if (stepIntersection.getBearings() != null) {
107+
for (Integer bearing : stepIntersection.getBearings()) {
108+
tempBearings.push(bearing);
109+
}
110+
}
111+
V8Util.add(intersection, "bearings", tempBearings);
112+
113+
if (stepIntersection.getEntry() != null) {
114+
for (Boolean lane : stepIntersection.getEntry()) {
115+
tempEntry.push(lane);
116+
}
117+
}
118+
V8Util.add(intersection, "entry", tempEntry);
119+
120+
if (stepIntersection.getLanes() != null) {
121+
for (IntersectionLanes lane : stepIntersection.getLanes()) {
122+
V8Object tempLane = convertFromIntersectionLanes(lane);
123+
tempLanes.push(tempLane);
124+
V8Util.release(tempLane);
125+
}
126+
}
127+
V8Util.add(intersection, "lanes", tempLanes);
128+
129+
if (stepIntersection.getLocation() != null) {
130+
for (Double item : stepIntersection.getLocation()) {
131+
tempLocation.push(item);
132+
}
133+
}
134+
V8Util.add(intersection, "location", tempLocation);
135+
136+
V8Util.release(tempBearings);
137+
V8Util.release(tempEntry);
138+
V8Util.release(tempLanes);
139+
V8Util.release(tempLocation);
140+
141+
return intersection;
142+
}
143+
144+
private V8Object convertFromIntersectionLanes(IntersectionLanes intersectionLanes) {
145+
if (intersectionLanes == null) {
146+
return null;
147+
}
148+
V8Object lanes = new V8Object(getRuntime());
149+
V8Util.add(lanes, "valid", intersectionLanes.getValid());
150+
151+
V8Array indications = new V8Array(getRuntime());
152+
if (intersectionLanes.getIndications() != null) {
153+
for (String indication : intersectionLanes.getIndications()) {
154+
indications.push(indication);
155+
}
156+
}
157+
V8Util.add(lanes, "indications", indications);
158+
159+
return lanes;
160+
}
161+
162+
private void releaseV8LegStep() {
163+
V8Object maneuver = (V8Object) V8Util.get(v8Step, "maneuver");
164+
V8Array intersections = (V8Array) V8Util.getV8Array(v8Step, "intersections");
165+
166+
if (intersections != null) {
167+
168+
for (int i = 0; i < intersections.length(); i++) {
169+
V8Object stepIntersection = (V8Object) intersections.get(i);
170+
V8Array bearings = (V8Array) V8Util.getV8Array(stepIntersection, "bearings");
171+
V8Array entry = (V8Array) V8Util.getV8Array(stepIntersection, "entry");
172+
V8Array lanes = (V8Array) V8Util.getV8Array(stepIntersection, "lanes");
173+
V8Array location = (V8Array) V8Util.getV8Array(stepIntersection, "location");
174+
175+
if (lanes != null) {
176+
for (int j = 0; j < lanes.length(); j++) {
177+
V8Object intersectionLanes = (V8Object) lanes.get(j);
178+
V8Array indications = (V8Array) V8Util.getV8Array(stepIntersection, "indications");
179+
180+
V8Util.release(indications);
181+
V8Util.release(intersectionLanes);
182+
}
183+
}
184+
185+
V8Util.release(bearings);
186+
V8Util.release(entry);
187+
V8Util.release(lanes);
188+
V8Util.release(location);
189+
V8Util.release(stepIntersection);
190+
}
191+
}
192+
193+
V8Util.release(intersections);
194+
V8Util.release(maneuver);
195+
V8Util.release(v8Step);
196+
}
197+
}

0 commit comments

Comments
 (0)