forked from HSLdevcom/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Properly implement the
relaxTransitGroupPriority
parameter in …
…Transmodel API
- Loading branch information
Showing
10 changed files
with
210 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
83 changes: 83 additions & 0 deletions
83
src/main/java/org/opentripplanner/framework/graphql/scalar/CostScalarFactory.java
This file contains 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,83 @@ | ||
package org.opentripplanner.framework.graphql.scalar; | ||
|
||
import graphql.GraphQLContext; | ||
import graphql.execution.CoercedVariables; | ||
import graphql.language.StringValue; | ||
import graphql.language.Value; | ||
import graphql.schema.Coercing; | ||
import graphql.schema.CoercingParseLiteralException; | ||
import graphql.schema.CoercingParseValueException; | ||
import graphql.schema.GraphQLScalarType; | ||
import java.util.Locale; | ||
import java.util.NoSuchElementException; | ||
import javax.annotation.Nonnull; | ||
import org.opentripplanner.framework.model.Cost; | ||
import org.opentripplanner.framework.time.DurationUtils; | ||
|
||
public class CostScalarFactory { | ||
|
||
private static final String TYPENAME = "Cost"; | ||
|
||
private static final String DOCUMENTATION = | ||
"A cost value, normally a value of 1 is equivalent to riding transit for 1 second, " + | ||
"but it might not depending on the use-case. Format: 3665 = DT1h1m5s = 1h1m5s"; | ||
|
||
private static final GraphQLScalarType SCALAR_INSTANCE = createCostScalar(); | ||
|
||
private CostScalarFactory() {} | ||
|
||
public static GraphQLScalarType costScalar() { | ||
return SCALAR_INSTANCE; | ||
} | ||
|
||
private static GraphQLScalarType createCostScalar() { | ||
return GraphQLScalarType | ||
.newScalar() | ||
.name(TYPENAME) | ||
.description(DOCUMENTATION) | ||
.coercing(createCoercing()) | ||
.build(); | ||
} | ||
|
||
private static String serializeCost(Cost cost) { | ||
return cost.asDuration().toString(); | ||
} | ||
|
||
private static Cost parseCost(String input) throws CoercingParseValueException { | ||
try { | ||
return Cost.fromDuration(DurationUtils.parseSecondsOrDuration(input).orElseThrow()); | ||
} catch (IllegalArgumentException | NoSuchElementException e) { | ||
throw new CoercingParseValueException(e.getMessage(), e); | ||
} | ||
} | ||
|
||
private static Coercing<Cost, String> createCoercing() { | ||
return new Coercing<>() { | ||
@Override | ||
public String serialize(@Nonnull Object result, GraphQLContext c, Locale l) { | ||
return serializeCost((Cost) result); | ||
} | ||
|
||
@Override | ||
public Cost parseValue(Object input, GraphQLContext c, Locale l) | ||
throws CoercingParseValueException { | ||
return parseCost((String) input); | ||
} | ||
|
||
@Override | ||
public Cost parseLiteral(Value<?> input, CoercedVariables v, GraphQLContext c, Locale l) | ||
throws CoercingParseLiteralException { | ||
if (input instanceof StringValue stringValue) { | ||
return parseCost(stringValue.getValue()); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public Value<?> valueToLiteral(Object input, GraphQLContext c, Locale l) { | ||
return StringValue.of((String) input); | ||
} | ||
}; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
71 changes: 71 additions & 0 deletions
71
src/test/java/org/opentripplanner/apis/transmodel/model/plan/RelaxCostTypeTest.java
This file contains 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,71 @@ | ||
package org.opentripplanner.apis.transmodel.model.plan; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.opentripplanner.apis.transmodel.model.plan.RelaxCostType.CONSTANT; | ||
import static org.opentripplanner.apis.transmodel.model.plan.RelaxCostType.RATIO; | ||
|
||
import graphql.language.FloatValue; | ||
import graphql.language.ObjectField; | ||
import graphql.language.ObjectValue; | ||
import graphql.language.StringValue; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
import org.opentripplanner.framework.model.Cost; | ||
import org.opentripplanner.routing.api.request.framework.CostLinearFunction; | ||
|
||
class RelaxCostTypeTest { | ||
|
||
@Test | ||
void valueOf() { | ||
assertEquals( | ||
ObjectValue | ||
.newObjectValue() | ||
.objectField(ObjectField.newObjectField().name(RATIO).value(FloatValue.of(1.0)).build()) | ||
.objectField( | ||
ObjectField.newObjectField().name(CONSTANT).value(StringValue.of("0s")).build() | ||
) | ||
.build() | ||
.toString(), | ||
RelaxCostType.valueOf(CostLinearFunction.NORMAL).toString() | ||
); | ||
assertEquals( | ||
ObjectValue | ||
.newObjectValue() | ||
.objectField(ObjectField.newObjectField().name(RATIO).value(FloatValue.of(1.3)).build()) | ||
.objectField( | ||
ObjectField.newObjectField().name(CONSTANT).value(StringValue.of("1m7s")).build() | ||
) | ||
.build() | ||
.toString(), | ||
RelaxCostType.valueOf(CostLinearFunction.of(Cost.costOfSeconds(67), 1.3)).toString() | ||
); | ||
} | ||
|
||
@Test | ||
void mapToDomain() { | ||
Map<String, Object> input; | ||
|
||
input = Map.of(RATIO, 1.0, CONSTANT, Cost.ZERO); | ||
assertEquals( | ||
CostLinearFunction.NORMAL, | ||
RelaxCostType.mapToDomain(input, CostLinearFunction.ZERO) | ||
); | ||
|
||
input = Map.of(RATIO, 0.0, CONSTANT, Cost.ZERO); | ||
assertEquals( | ||
CostLinearFunction.ZERO, | ||
RelaxCostType.mapToDomain(input, CostLinearFunction.ZERO) | ||
); | ||
|
||
input = Map.of(RATIO, 1.7, CONSTANT, Cost.costOfSeconds(3600 + 3 * 60 + 7)); | ||
assertEquals( | ||
CostLinearFunction.of("1h3m7s + 1.7t"), | ||
RelaxCostType.mapToDomain(input, CostLinearFunction.ZERO) | ||
); | ||
assertEquals( | ||
CostLinearFunction.NORMAL, | ||
RelaxCostType.mapToDomain(null, CostLinearFunction.NORMAL) | ||
); | ||
assertEquals(CostLinearFunction.ZERO, RelaxCostType.mapToDomain(null, CostLinearFunction.ZERO)); | ||
} | ||
} |
This file contains 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