From 7d4c7243389a3beafecb14e75e014b2a36d815e6 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Sat, 10 Jul 2021 12:10:54 +0200 Subject: [PATCH] Change type assumptions permalink format --- .../edu/kit/typicalc/view/main/InputBar.java | 20 +++----------- .../kit/typicalc/view/main/MainViewImpl.java | 27 ++++++++++--------- .../edu/kit/typicalc/view/main/UpperBar.java | 7 +++-- 3 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/main/java/edu/kit/typicalc/view/main/InputBar.java b/src/main/java/edu/kit/typicalc/view/main/InputBar.java index 8bdfaa2..c5cbc7a 100644 --- a/src/main/java/edu/kit/typicalc/view/main/InputBar.java +++ b/src/main/java/edu/kit/typicalc/view/main/InputBar.java @@ -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>> callback; + private final transient Consumer> 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>> callback) { + protected InputBar(Consumer> callback) { this.callback = callback; setId(INPUT_BAR_ID); @@ -126,7 +124,7 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver { /** * Set to provided input as the value of the termInputField and assumptionInputField. - * + * * @param input pair of a term (left) and type assumptions (right) */ protected void setInputAndClickTypeInfer(Pair input) { @@ -138,17 +136,7 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver { private void onTypeInferButtonClick() { termInputField.blur(); String assumptions = assumptionInputField.getValue(); - Map 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() { diff --git a/src/main/java/edu/kit/typicalc/view/main/MainViewImpl.java b/src/main/java/edu/kit/typicalc/view/main/MainViewImpl.java index 00ca04c..efc2cd5 100644 --- a/src/main/java/edu/kit/typicalc/view/main/MainViewImpl.java +++ b/src/main/java/edu/kit/typicalc/view/main/MainViewImpl.java @@ -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 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 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> lambdaTermAndAssumptions) { + private void processInput(Pair 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) { diff --git a/src/main/java/edu/kit/typicalc/view/main/UpperBar.java b/src/main/java/edu/kit/typicalc/view/main/UpperBar.java index e4adf67..299a18c 100644 --- a/src/main/java/edu/kit/typicalc/view/main/UpperBar.java +++ b/src/main/java/edu/kit/typicalc/view/main/UpperBar.java @@ -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 renderer; private final transient MainViewListener presenter; - private final transient Consumer>> inputConsumer; + private final transient Consumer> 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>> inputConsumer) { + protected UpperBar(MainViewListener presenter, Consumer> 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> termAndAssumptions) { + protected void typeInfer(Pair termAndAssumptions) { inputConsumer.accept(termAndAssumptions); }