Skip to content

Commit e1fc8c4

Browse files
committed
refactor some Mapper code
- removed AbstractSULMapper, since #pre and #post are now default methods in the interface - added Mappers class for composing arbitrary mappers - split up SULMapperComposition for re-use
1 parent adc2d1a commit e1fc8c4

6 files changed

Lines changed: 136 additions & 84 deletions

File tree

drivers/basic/src/main/java/de/learnlib/drivers/reflect/SimplePOJODataMapper.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@
2121
import java.util.Map;
2222

2323
import de.learnlib.api.exception.SULException;
24-
import de.learnlib.mapper.AbstractSULMapper;
2524
import de.learnlib.mapper.api.SULMapper;
2625

2726
/**
2827
* Basic stateless data mapper for objects.
2928
*
3029
* @author falkhowar
3130
*/
32-
public class SimplePOJODataMapper
33-
extends AbstractSULMapper<MethodInput, AbstractMethodOutput, ConcreteMethodInput, Object> {
31+
public class SimplePOJODataMapper implements SULMapper<MethodInput, AbstractMethodOutput, ConcreteMethodInput, Object> {
3432

3533
private final Constructor<?> initMethod;
3634
private final Object[] initParams;

drivers/mapper/src/main/java/de/learnlib/mapper/AbstractSULMapper.java

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* Copyright (C) 2013-2017 TU Dortmund
2+
* This file is part of LearnLib, http://www.learnlib.de/.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.learnlib.mapper;
17+
18+
import de.learnlib.api.Mapper;
19+
import de.learnlib.api.Mapper.AsynchronousMapper;
20+
import de.learnlib.api.Mapper.SynchronousMapper;
21+
22+
/**
23+
* Utility class for the nested the application of two {@link Mapper mappers}.
24+
* <p>
25+
* This class implements both {@link SynchronousMapper} and {@link AsynchronousMapper} since for the nesting semantics
26+
* is the same for both processing contracts.
27+
*
28+
* @param <AI>
29+
* abstract (outer) input type
30+
* @param <AO>
31+
* abstract (outer) output type
32+
* @param <ACI>
33+
* intermediate input type
34+
* @param <CAO>
35+
* intermediate output type
36+
* @param <CI>
37+
* concrete (inner) input type
38+
* @param <CO>
39+
* concrete (inner) output output
40+
* @param <OUTER>
41+
* type of the outer mapper
42+
* @param <INNER>
43+
* type of the inner mapper
44+
*
45+
* @author frohme
46+
*/
47+
class MapperComposition<AI, AO, ACI, CAO, CI, CO, OUTER extends Mapper<? super AI, ? extends AO, ACI, CAO>, INNER extends Mapper<? super ACI, ? extends CAO, ? extends CI, ? super CO>>
48+
implements SynchronousMapper<AI, AO, CI, CO>, AsynchronousMapper<AI, AO, CI, CO> {
49+
50+
protected final OUTER mapper1;
51+
protected final INNER mapper2;
52+
53+
MapperComposition(OUTER outerMapper, INNER innerMapper) {
54+
this.mapper1 = outerMapper;
55+
this.mapper2 = innerMapper;
56+
}
57+
58+
@Override
59+
public void pre() {
60+
mapper1.pre();
61+
mapper2.pre();
62+
}
63+
64+
@Override
65+
public void post() {
66+
mapper2.post();
67+
mapper1.post();
68+
}
69+
70+
@Override
71+
public CI mapInput(AI abstractInput) {
72+
ACI aci = mapper1.mapInput(abstractInput);
73+
return mapper2.mapInput(aci);
74+
}
75+
76+
@Override
77+
public AO mapOutput(CO concreteOutput) {
78+
CAO cao = mapper2.mapOutput(concreteOutput);
79+
return mapper1.mapOutput(cao);
80+
}
81+
82+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Copyright (C) 2013-2017 TU Dortmund
2+
* This file is part of LearnLib, http://www.learnlib.de/.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.learnlib.mapper;
17+
18+
import de.learnlib.api.Mapper;
19+
import de.learnlib.api.Mapper.AsynchronousMapper;
20+
import de.learnlib.api.Mapper.SynchronousMapper;
21+
22+
/**
23+
* Utility methods for manipulating mappers.
24+
*
25+
* @author frohme
26+
*/
27+
public final class Mappers {
28+
29+
private Mappers() {
30+
// prevent instantiation
31+
}
32+
33+
public static <AI, AO, ACI, CAO, CI, CO> Mapper<AI, AO, CI, CO> compose(Mapper<? super AI, ? extends AO, ACI, CAO> outerMapper,
34+
Mapper<? super ACI, ? extends CAO, ? extends CI, ? super CO> innerMapper) {
35+
return new MapperComposition<>(outerMapper, innerMapper);
36+
}
37+
38+
public static <AI, AO, ACI, CAO, CI, CO> AsynchronousMapper<AI, AO, CI, CO> compose(AsynchronousMapper<? super AI, ? extends AO, ACI, CAO> outerMapper,
39+
AsynchronousMapper<? super ACI, ? extends CAO, ? extends CI, ? super CO> innerMapper) {
40+
return new MapperComposition<>(outerMapper, innerMapper);
41+
}
42+
43+
public static <AI, AO, ACI, CAO, CI, CO> SynchronousMapper<AI, AO, CI, CO> compose(SynchronousMapper<? super AI, ? extends AO, ACI, CAO> outerMapper,
44+
SynchronousMapper<? super ACI, ? extends CAO, ? extends CI, ? super CO> innerMapper) {
45+
return new MapperComposition<>(outerMapper, innerMapper);
46+
}
47+
}

drivers/mapper/src/main/java/de/learnlib/mapper/SULMapperComposition.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,13 @@
2020
import de.learnlib.api.exception.SULException;
2121
import de.learnlib.mapper.api.SULMapper;
2222

23-
final class SULMapperComposition<AI, AO, ACI, CAO, CI, CO> implements SULMapper<AI, AO, CI, CO> {
24-
25-
private final SULMapper<? super AI, ? extends AO, ACI, CAO> mapper1;
26-
private final SULMapper<? super ACI, ? extends CAO, ? extends CI, ? super CO> mapper2;
23+
final class SULMapperComposition<AI, AO, ACI, CAO, CI, CO>
24+
extends MapperComposition<AI, AO, ACI, CAO, CI, CO, SULMapper<? super AI, ? extends AO, ACI, CAO>, SULMapper<? super ACI, ? extends CAO, ? extends CI, ? super CO>>
25+
implements SULMapper<AI, AO, CI, CO> {
2726

2827
SULMapperComposition(SULMapper<? super AI, ? extends AO, ACI, CAO> outerMapper,
2928
SULMapper<? super ACI, ? extends CAO, ? extends CI, ? super CO> innerMapper) {
30-
this.mapper1 = outerMapper;
31-
this.mapper2 = innerMapper;
32-
}
33-
34-
@Override
35-
public void pre() {
36-
mapper1.pre();
37-
mapper2.pre();
38-
}
39-
40-
@Override
41-
public void post() {
42-
mapper2.post();
43-
mapper1.post();
44-
}
45-
46-
@Override
47-
public CI mapInput(AI abstractInput) {
48-
ACI aci = mapper1.mapInput(abstractInput);
49-
return mapper2.mapInput(aci);
50-
}
51-
52-
@Override
53-
public AO mapOutput(CO concreteOutput) {
54-
CAO cao = mapper2.mapOutput(concreteOutput);
55-
return mapper1.mapOutput(cao);
29+
super(outerMapper, innerMapper);
5630
}
5731

5832
@Override

drivers/mapper/src/main/java/de/learnlib/mapper/StringMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
import java.util.HashMap;
1919
import java.util.Map;
2020

21+
import de.learnlib.mapper.api.SULMapper;
2122
import net.automatalib.words.Alphabet;
2223
import net.automatalib.words.impl.SimpleAlphabet;
2324

24-
public class StringMapper<CI> extends AbstractSULMapper<String, String, CI, Object> {
25+
public class StringMapper<CI> implements SULMapper<String, String, CI, Object> {
2526

2627
private final Map<String, CI> inputs = new HashMap<>();
2728
private final Alphabet<String> mappedInputs = new SimpleAlphabet<>();

0 commit comments

Comments
 (0)