Synthia
Generic and flexible data structure generator
StringPattern.java
Go to the documentation of this file.
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.string;
20 
21 import ca.uqac.lif.synthia.Picker;
26 
27 /**
28  * Generates a string according to a predefined pattern. The picker is
29  * instantiated by giving it a string pattern; this pattern can contain
30  * placeholders of the form <tt>{$n}</tt>; when called, the picker will
31  * replace <tt>{$n}</tt> by the value returned by the <tt>n</tt>-th picker
32  * passed to the constructor. For example:
33  * <pre>
34  * StringPattern pat = new StringPattern("{$0} is equal to {$1}",
35  * new RandomString(5), new RandomBoolean());
36  * String s1 = pat.pick(); // "FhTuN is equal to false"
37  * String s2 = pat.pick(); // "aGhRe is equal to true"
38  * ...</pre>
39  * @ingroup API
40  */
41 public class StringPattern extends CompositePicker<String> implements Shrinkable<String>
42 {
43  /**
44  * The string pattern
45  */
46  /*@ non_null @*/ protected String m_pattern;
47 
48  /**
49  * Creates a new StringPattern picker
50  * @param pattern The string pattern
51  * @param parts The pickers used to give values to each placeholder
52  */
53  public StringPattern(/*@ non_null @*/ String pattern, /*@ non_null @*/ Picker<?> ... parts)
54  {
55  super(parts);
56  m_pattern = pattern;
57  }
58 
59 
60  /**
61  * Returns the pattern into a string
62  * @return The string containing the pattern.
63  */
64  @Override
65  public String toString()
66  {
67  return m_pattern;
68  }
69 
70  @Override
71  public String getOutput(Object ... parts)
72  {
73  String out = m_pattern;
74  for (int i = 0; i < parts.length; i++)
75  {
76  out = out.replaceAll("\\{\\$" + i + "\\}", parts[i].toString());
77  }
78  return out;
79  }
80 
81  /**
82  * Returns a new string pattern picker.
83  * @param pickers The internal pickers, already duplicated
84  * @return The new string pattern picker.
85  */
86  @Override
87  public StringPattern newPicker(Picker<?> ... pickers)
88  {
89  return new StringPattern(m_pattern, pickers);
90  }
91 
92  @Override
93  public Shrinkable<String> shrink(String o, Picker<Float> decision, float magnitude)
94  {
95  return new PickSmallerComparable<String>(this, o);
96  }
97 
98  @Override
99  public Shrinkable<String> shrink(String o)
100  {
101  return shrink(o, RandomFloat.instance, 1);
102  }
103 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.Shrinkable
Interface signaling that a picker can be shrunk.
Definition: Shrinkable.java:36
ca.uqac.lif.synthia.string.StringPattern
Generates a string according to a predefined pattern.
Definition: StringPattern.java:41
ca.uqac.lif.synthia.string.StringPattern.toString
String toString()
Returns the pattern into a string.
Definition: StringPattern.java:65
ca.uqac.lif.synthia.collection
Pickers generating and manipulating collections, such as lists and sets.
Definition: ComparableList.java:19
ca.uqac.lif.synthia.relative.PickSmallerComparable
A variant of PickIf that selects an element if it is smaller than a reference object.
Definition: PickSmallerComparable.java:37
ca.uqac.lif.synthia.random.RandomFloat.instance
static final transient RandomFloat instance
A public static instance of RandomFloat.
Definition: RandomFloat.java:45
ca.uqac
ca.uqac.lif.synthia.string.StringPattern.shrink
Shrinkable< String > shrink(String o, Picker< Float > decision, float magnitude)
Definition: StringPattern.java:93
ca.uqac.lif.synthia.random
Pickers that produce pseudo-random objects such as numbers.
Definition: AffineTransform.java:19
ca.uqac.lif.synthia.collection.CompositePicker
Picker that merges the result of other pickers into a composite data structure.
Definition: CompositePicker.java:29
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif
ca.uqac.lif.synthia.string.StringPattern.m_pattern
String m_pattern
The string pattern.
Definition: StringPattern.java:46
ca.uqac.lif.synthia.string.StringPattern.newPicker
StringPattern newPicker(Picker<?> ... pickers)
Returns a new string pattern picker.
Definition: StringPattern.java:87
ca.uqac.lif.synthia.string.StringPattern.getOutput
String getOutput(Object ... parts)
Creates the output composite object from the internal values that have been picked.
Definition: StringPattern.java:71
ca
ca.uqac.lif.synthia.relative
Pickers that produce a value in relation to another value.
Definition: package-info.java:24
ca.uqac.lif.synthia.random.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30
ca.uqac.lif.synthia.string.StringPattern.shrink
Shrinkable< String > shrink(String o)
Definition: StringPattern.java:99
ca.uqac.lif.synthia.string.StringPattern.StringPattern
StringPattern(String pattern, Picker<?> ... parts)
Creates a new StringPattern picker.
Definition: StringPattern.java:53