Change type assumptions permalink format

This commit is contained in:
Arne Keller 2021-07-10 12:10:54 +02:00
parent c9cc12e922
commit 7d4c724338
3 changed files with 22 additions and 32 deletions

View File

@ -15,9 +15,7 @@ import com.vaadin.flow.i18n.LocaleChangeEvent;
import com.vaadin.flow.i18n.LocaleChangeObserver;
import org.apache.commons.lang3.tuple.Pair;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
/**
* Contains components which allow the user to enter a lambda term and start the type inference algorithm.
@ -41,7 +39,7 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver {
private static final short MAX_INPUT_LENGTH = 1000;
private final transient Consumer<Pair<String, Map<String, String>>> callback;
private final transient Consumer<Pair<String, String>> callback;
private final Button infoIcon;
private final TextField termInputField;
private final AssumptionInputField assumptionInputField;
@ -54,7 +52,7 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver {
*
* @param callback Consumer to call the inferType()-method in UpperBar
*/
protected InputBar(Consumer<Pair<String, Map<String, String>>> callback) {
protected InputBar(Consumer<Pair<String, String>> callback) {
this.callback = callback;
setId(INPUT_BAR_ID);
@ -138,17 +136,7 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver {
private void onTypeInferButtonClick() {
termInputField.blur();
String assumptions = assumptionInputField.getValue();
Map<String, String> assumptionsMap = Arrays.stream(assumptions.split(";"))
.filter(entry -> entry.length() > 0).map(entry -> {
if (entry.contains(":")) {
String[] parts = entry.split(":", 2);
return Pair.of(parts[0].trim(), parts[1].trim());
} else {
return Pair.of(entry, "");
}
}).collect(Collectors.toMap(Pair::getLeft, Pair::getRight,
(existing, replacement) -> existing, LinkedHashMap::new));
callback.accept(Pair.of(termInputField.getValue(), assumptionsMap));
callback.accept(Pair.of(termInputField.getValue(), assumptions));
}
private void onExampleButtonClick() {

View File

@ -20,7 +20,6 @@ import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
/**
* Contains all the displayed components and builds the applications user interface (UI).
@ -43,6 +42,7 @@ public class MainViewImpl extends AppLayout
* The title of the application
*/
public static final String PAGE_TITLE = "Typicalc";
private static final String ASSUMPTION_PARAMETER = "assumptions";
private final UpperBar upperBar;
@ -74,10 +74,9 @@ public class MainViewImpl extends AppLayout
if (url.getPath().matches(TypeInferenceView.ROUTE + "/.*")) {
List<String> segments = url.getSegments();
String term = segments.get(segments.size() - 1);
String types = url.getQueryParameters().getParameters()
.entrySet().stream().map(kv -> kv.getKey() + ": " + kv.getValue().get(0))
.collect(Collectors.joining(", "));
upperBar.inferTerm(decodeURL(term), types);
List<String> types = url.getQueryParameters().getParameters().get(ASSUMPTION_PARAMETER);
String assumptions = types != null && types.size() == 1 ? types.get(0) : "";
upperBar.inferTerm(decodeURL(term), assumptions);
} else if (url.getPath().equals(TypeInferenceView.ROUTE)) {
setContent(new StartPageView());
upperBar.inferTerm(StringUtils.EMPTY, "");
@ -86,18 +85,22 @@ public class MainViewImpl extends AppLayout
}
}
private void processInput(Pair<String, Map<String, String>> lambdaTermAndAssumptions) {
private void processInput(Pair<String, String> lambdaTermAndAssumptions) {
String lambdaTerm = lambdaTermAndAssumptions.getLeft();
if (lambdaTerm.isBlank()) {
UI.getCurrent().navigate("./");
return;
}
QueryParameters qp = new QueryParameters(lambdaTermAndAssumptions.getRight().entrySet().stream().map(entry ->
Pair.of(entry.getKey(), List.of(entry.getValue()))
).collect(Collectors.toMap(Pair::getLeft, Pair::getRight,
(existing, replacement) -> existing, LinkedHashMap::new)));
UI.getCurrent().navigate(TypeInferenceView.ROUTE + "/"
+ URLEncoder.encode(lambdaTerm, StandardCharsets.UTF_8), qp);
String assumptions = lambdaTermAndAssumptions.getRight();
if (assumptions.isEmpty()) {
UI.getCurrent().navigate(TypeInferenceView.ROUTE + "/"
+ URLEncoder.encode(lambdaTerm, StandardCharsets.UTF_8));
} else {
QueryParameters qp = new QueryParameters(
Map.of(ASSUMPTION_PARAMETER, List.of(lambdaTermAndAssumptions.getRight())));
UI.getCurrent().navigate(TypeInferenceView.ROUTE + "/"
+ URLEncoder.encode(lambdaTerm, StandardCharsets.UTF_8), qp);
}
}
private String decodeURL(String encodedUrl) {

View File

@ -19,7 +19,6 @@ import edu.kit.typicalc.view.main.MainView.MainViewListener;
import org.apache.commons.lang3.tuple.Pair;
import java.util.Locale;
import java.util.Map;
import java.util.function.Consumer;
@ -48,7 +47,7 @@ public class UpperBar extends VerticalLayout implements LocaleChangeObserver {
private final ItemLabelGenerator<Locale> renderer;
private final transient MainViewListener presenter;
private final transient Consumer<Pair<String, Map<String, String>>> inputConsumer;
private final transient Consumer<Pair<String, String>> inputConsumer;
/**
* Initializes a new UpperBar with the provided mainViewListener.
@ -56,7 +55,7 @@ public class UpperBar extends VerticalLayout implements LocaleChangeObserver {
* @param presenter the listener used to communicate with the model
* @param inputConsumer function to handle user input
*/
protected UpperBar(MainViewListener presenter, Consumer<Pair<String, Map<String, String>>> inputConsumer) {
protected UpperBar(MainViewListener presenter, Consumer<Pair<String, String>> inputConsumer) {
this.presenter = presenter;
this.inputConsumer = inputConsumer;
@ -90,7 +89,7 @@ public class UpperBar extends VerticalLayout implements LocaleChangeObserver {
*
* @param termAndAssumptions the lambda term to be type-inferred and the type assumptions to use
*/
protected void typeInfer(Pair<String, Map<String, String>> termAndAssumptions) {
protected void typeInfer(Pair<String, String> termAndAssumptions) {
inputConsumer.accept(termAndAssumptions);
}