List of usage examples for com.google.common.collect BiMap computeIfAbsent
default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
From source file:net.automatalib.serialization.etf.writer.Mealy2ETFWriterIO.java
private <S, T> void writeETFInternal(PrintWriter pw, MealyMachine<S, I, T, O> mealy, Alphabet<I> inputs) { final StateIDs<S> stateIDs = mealy.stateIDs(); // write the initial state pw.println("begin init"); pw.printf("%d%n", stateIDs.getStateId(mealy.getInitialState())); pw.println("end init"); // write the state ids pw.println("begin sort id"); mealy.getStates().forEach(s -> pw.printf("\"%s\"%n", s)); pw.println("end sort"); // create a new bi-map that contains indices for the output alphabet final BiMap<O, Integer> outputIndices = HashBiMap.create(); // write the transitions pw.println("begin trans"); for (S s : mealy.getStates()) { for (I i : inputs) { final T t = mealy.getTransition(s, i); if (t != null) { final O o = mealy.getTransitionOutput(t); outputIndices.computeIfAbsent(o, ii -> outputIndices.size()); final S n = mealy.getSuccessor(t); pw.printf("%s/%s %d %d%n", stateIDs.getStateId(s), stateIDs.getStateId(n), inputs.getSymbolIndex(i), outputIndices.get(o)); }/*from w w w. ja va 2 s . c om*/ } } pw.println("end trans"); // write the letters in the input alphabet pw.println("begin sort input"); inputs.forEach(i -> pw.printf("\"%s\"%n", i)); pw.println("end sort"); // write the letters in the output alphabet pw.println("begin sort output"); for (int i = 0; i < outputIndices.size(); i++) { pw.printf("\"%s\"%n", outputIndices.inverse().get(i)); } pw.println("end sort"); }
From source file:net.automatalib.serialization.etf.writer.Mealy2ETFWriterAlternating.java
private <S, T> void writeETFInternal(PrintWriter pw, MealyMachine<S, I, T, O> mealy, Alphabet<I> inputs) { // create a bi-mapping from states to integers final BiMap<S, Integer> oldStates = HashBiMap.create(); mealy.getStates().forEach(s -> oldStates.put(s, oldStates.size())); // write the initial state, using the bi-map pw.println("begin init"); pw.printf("%d%n", oldStates.get(mealy.getInitialState())); pw.println("end init"); // create a bi-map for transitions containing output final BiMap<Pair<O, S>, Integer> outputTransitions = HashBiMap.create(); // create a bi-map that maps output to integers final BiMap<O, Integer> outputIndices = HashBiMap.create(); /*//ww w . j ava 2s.c o m Write the transitions (here is where the horror begins). The key to writing transitions with alternating semantics is that one have of see if appropriate intermediate states, and output transitions have already been created. If this is the case, that state, and output transitions has to be reused. */ pw.println("begin trans"); for (S s : mealy.getStates()) { for (I i : inputs) { T t = mealy.getTransition(s, i); if (t != null) { final S n = mealy.getSuccessor(t); final O o = mealy.getTransitionOutput(t); // construct a triple that serves as a key in the outputTransitions bi-map final Pair<O, S> outputTransition = Pair.of(o, n); // compute the integer value of the intermediate state (this may be a new state) final Integer intermediateState = outputTransitions.computeIfAbsent(outputTransition, ii -> { // the output may also be a new letter in the alphabet. final Integer outputIndex = outputIndices.computeIfAbsent(o, iii -> inputs.size() + outputIndices.size()); /* Write the output transition. Note that this will only be done if the output transition was not written before. */ final Integer res = oldStates.size() + outputTransitions.size(); pw.printf("%d/%d %d%n", res, oldStates.get(n), outputIndex); return res; }); // always write the input transition to the output transition pw.printf("%d/%d %d%n", oldStates.get(s), intermediateState, inputs.getSymbolIndex(i)); } } } pw.println("end trans"); // write all state ids, including the newly created intermediate states pw.println("begin sort id"); for (int i = 0; i < oldStates.size(); i++) { pw.printf("\"%s\"%n", oldStates.inverse().get(i)); } final Map<Integer, Pair<O, S>> inverseTransitions = outputTransitions.inverse(); for (int i = 0; i < outputTransitions.size(); i++) { final Pair<O, S> t = inverseTransitions.get(oldStates.size() + i); pw.printf("\"(%s,%s)\"%n", t.getFirst(), t.getSecond()); } pw.println("end sort"); // write all the letters in the new alphabet pw.println("begin sort letter"); inputs.forEach(i -> pw.printf("\"%s\"%n", i)); for (int i = 0; i < outputIndices.size(); i++) { pw.printf("\"%s\"%n", outputIndices.inverse().get(inputs.size() + i)); } pw.println("end sort"); }