Skip to content

Commit 05c2620

Browse files
committed
Composite pickers
1 parent cffb1ef commit 05c2620

File tree

5 files changed

+220
-58
lines changed

5 files changed

+220
-58
lines changed

Source/Core/src/ca/uqac/lif/synthia/util/ArrayPicker.java

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,26 @@
2323
/**
2424
* Picker that merges the result of other pickers into an array.
2525
*/
26-
public class ArrayPicker implements Picker<Object[]>
26+
public class ArrayPicker extends CompositePicker<Object[]>
2727
{
28-
protected Picker<?>[] m_pickers;
29-
28+
/**
29+
* Creates a new array picker
30+
* @param pickers The pickers used to generate the values
31+
*/
3032
public ArrayPicker(Picker<?> ... pickers)
3133
{
32-
super();
33-
m_pickers = pickers;
34-
}
35-
36-
@Override
37-
public Picker<Object[]> duplicate(boolean with_state)
38-
{
39-
Picker<?>[] duplicates = new Picker<?>[m_pickers.length];
40-
for (int i = 0; i < m_pickers.length; i++)
41-
{
42-
duplicates[i] = m_pickers[i].duplicate(with_state);
43-
}
44-
return new ArrayPicker(duplicates);
34+
super(pickers);
4535
}
4636

4737
@Override
48-
public Object[] pick()
38+
public ArrayPicker newPicker(Picker<?> ... pickers)
4939
{
50-
Object[] out = new Object[m_pickers.length];
51-
for (int i = 0; i < m_pickers.length; i++)
52-
{
53-
out[i] = m_pickers[i].pick();
54-
}
55-
return out;
40+
return new ArrayPicker(pickers);
5641
}
57-
42+
5843
@Override
59-
public void reset()
44+
public Object[] getOutput(Object ... values)
6045
{
61-
for (int i = 0; i < m_pickers.length; i++)
62-
{
63-
m_pickers[i].reset();
64-
}
46+
return values;
6547
}
6648
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Synthia, a data structure generator
3+
Copyright (C) 2019-2020 Laboratoire d'informatique formelle
4+
Université du Québec à Chicoutimi, Canada
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Lesser General Public License as published
8+
by the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package ca.uqac.lif.synthia.util;
20+
21+
import ca.uqac.lif.synthia.Picker;
22+
23+
/**
24+
* Picker that merges the result of other pickers into a
25+
* composite data structure. Descendants of this class use a
26+
* different data structure.
27+
*/
28+
public abstract class CompositePicker<T> implements Picker<T>
29+
{
30+
/**
31+
* The pickers used to generate the values
32+
*/
33+
/*@ non_null @*/ protected Picker<?>[] m_pickers;
34+
35+
/**
36+
* Creates a new composite picker
37+
* @param pickers The pickers used to generate the values
38+
*/
39+
public CompositePicker(/*@ non_null @*/ Picker<?> ... pickers)
40+
{
41+
super();
42+
m_pickers = pickers;
43+
}
44+
45+
@Override
46+
public Picker<T> duplicate(boolean with_state)
47+
{
48+
Picker<?>[] duplicates = new Picker<?>[m_pickers.length];
49+
for (int i = 0; i < m_pickers.length; i++)
50+
{
51+
duplicates[i] = m_pickers[i].duplicate(with_state);
52+
}
53+
return newPicker(duplicates);
54+
}
55+
56+
@Override
57+
public T pick()
58+
{
59+
Object[] out = new Object[m_pickers.length];
60+
for (int i = 0; i < m_pickers.length; i++)
61+
{
62+
out[i] = m_pickers[i].pick();
63+
}
64+
return getOutput(out);
65+
}
66+
67+
@Override
68+
public void reset()
69+
{
70+
for (int i = 0; i < m_pickers.length; i++)
71+
{
72+
m_pickers[i].reset();
73+
}
74+
}
75+
76+
/**
77+
* Creates a duplicate of the current picker
78+
* @param pickers The internal pickers, already duplicated
79+
* @return A new instance of the picker
80+
*/
81+
protected abstract CompositePicker<T> newPicker(Picker<?> ... pickers);
82+
83+
/**
84+
* Creates the output composite object from the internal
85+
* values that have been picked
86+
* @param objects The internal values
87+
* @return The composite object
88+
*/
89+
protected abstract T getOutput(Object ... objects);
90+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Synthia, a data structure generator
3+
Copyright (C) 2019-2020 Laboratoire d'informatique formelle
4+
Université du Québec à Chicoutimi, Canada
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Lesser General Public License as published
8+
by the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package ca.uqac.lif.synthia.util;
20+
21+
import java.util.HashSet;
22+
import java.util.Set;
23+
24+
import ca.uqac.lif.synthia.Picker;
25+
26+
/**
27+
* Picker that merges the result of other pickers into a set.
28+
*/
29+
public class ListPicker extends CompositePicker<Set<Object>>
30+
{
31+
/**
32+
* Creates a new set picker
33+
* @param pickers The pickers used to generate the values
34+
*/
35+
public ListPicker(Picker<?> ... pickers)
36+
{
37+
super(pickers);
38+
}
39+
40+
@Override
41+
public ListPicker newPicker(Picker<?> ... pickers)
42+
{
43+
return new ListPicker(pickers);
44+
}
45+
46+
@Override
47+
public Set<Object> getOutput(Object ... values)
48+
{
49+
Set<Object> set = new HashSet<Object>(values.length);
50+
for (Object v : values)
51+
{
52+
set.add(v);
53+
}
54+
return set;
55+
}
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Synthia, a data structure generator
3+
Copyright (C) 2019-2020 Laboratoire d'informatique formelle
4+
Université du Québec à Chicoutimi, Canada
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Lesser General Public License as published
8+
by the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package ca.uqac.lif.synthia.util;
20+
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
24+
import ca.uqac.lif.synthia.Picker;
25+
26+
/**
27+
* Picker that merges the result of other pickers into an array.
28+
*/
29+
public class SetPicker extends CompositePicker<List<Object>>
30+
{
31+
/**
32+
* Creates a new list picker
33+
* @param pickers The pickers used to generate the values
34+
*/
35+
public SetPicker(Picker<?> ... pickers)
36+
{
37+
super(pickers);
38+
}
39+
40+
@Override
41+
public SetPicker newPicker(Picker<?> ... pickers)
42+
{
43+
return new SetPicker(pickers);
44+
}
45+
46+
@Override
47+
public List<Object> getOutput(Object ... values)
48+
{
49+
List<Object> list = new ArrayList<Object>(values.length);
50+
for (Object v : values)
51+
{
52+
list.add(v);
53+
}
54+
return list;
55+
}
56+
}

Source/Core/src/ca/uqac/lif/synthia/util/StringPattern.java

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@
3333
* String s2 = pat.pick(); // "aGhRe is equal to true"
3434
* ...</pre>
3535
*/
36-
public class StringPattern implements Picker<String>
37-
{
38-
/*@ non_null @*/ protected Picker<?>[] m_pickers;
39-
36+
public class StringPattern extends CompositePicker<String>
37+
{
4038
/**
4139
* The string pattern
4240
*/
@@ -49,9 +47,8 @@ public class StringPattern implements Picker<String>
4947
*/
5048
public StringPattern(/*@ non_null @*/ String pattern, /*@ non_null @*/ Picker<?> ... parts)
5149
{
52-
super();
50+
super(parts);
5351
m_pattern = pattern;
54-
m_pickers = parts;
5552
}
5653

5754
@Override
@@ -61,38 +58,19 @@ public String toString()
6158
}
6259

6360
@Override
64-
public void reset()
65-
{
66-
for (Picker<?> p : m_pickers)
67-
{
68-
p.reset();
69-
}
70-
}
71-
72-
@Override
73-
public String pick()
61+
public String getOutput(Object ... parts)
7462
{
75-
String[] parts = new String[m_pickers.length];
76-
for (int i = 0; i < parts.length; i++)
77-
{
78-
parts[i] = m_pickers[i].pick().toString();
79-
}
8063
String out = m_pattern;
8164
for (int i = 0; i < parts.length; i++)
8265
{
83-
out = out.replaceAll("\\{\\$" + i + "\\}", parts[i]);
66+
out = out.replaceAll("\\{\\$" + i + "\\}", parts[i].toString());
8467
}
8568
return out;
8669
}
8770

8871
@Override
89-
public StringPattern duplicate(boolean with_state)
72+
public StringPattern newPicker(Picker<?> ... pickers)
9073
{
91-
Picker<?>[] new_provs = new Picker<?>[m_pickers.length];
92-
for (int i = 0; i < m_pickers.length; i++)
93-
{
94-
new_provs[i] = m_pickers[i].duplicate(with_state);
95-
}
96-
return new StringPattern(m_pattern, new_provs);
74+
return new StringPattern(m_pattern, pickers);
9775
}
9876
}

0 commit comments

Comments
 (0)