Skip to content

Commit

Permalink
Composite pickers
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainhalle committed Mar 24, 2020
1 parent cffb1ef commit 05c2620
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 58 deletions.
40 changes: 11 additions & 29 deletions Source/Core/src/ca/uqac/lif/synthia/util/ArrayPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,26 @@
/**
* Picker that merges the result of other pickers into an array.
*/
public class ArrayPicker implements Picker<Object[]>
public class ArrayPicker extends CompositePicker<Object[]>
{
protected Picker<?>[] m_pickers;

/**
* Creates a new array picker
* @param pickers The pickers used to generate the values
*/
public ArrayPicker(Picker<?> ... pickers)
{
super();
m_pickers = pickers;
}

@Override
public Picker<Object[]> duplicate(boolean with_state)
{
Picker<?>[] duplicates = new Picker<?>[m_pickers.length];
for (int i = 0; i < m_pickers.length; i++)
{
duplicates[i] = m_pickers[i].duplicate(with_state);
}
return new ArrayPicker(duplicates);
super(pickers);
}

@Override
public Object[] pick()
public ArrayPicker newPicker(Picker<?> ... pickers)
{
Object[] out = new Object[m_pickers.length];
for (int i = 0; i < m_pickers.length; i++)
{
out[i] = m_pickers[i].pick();
}
return out;
return new ArrayPicker(pickers);
}

@Override
public void reset()
public Object[] getOutput(Object ... values)
{
for (int i = 0; i < m_pickers.length; i++)
{
m_pickers[i].reset();
}
return values;
}
}
90 changes: 90 additions & 0 deletions Source/Core/src/ca/uqac/lif/synthia/util/CompositePicker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Synthia, a data structure generator
Copyright (C) 2019-2020 Laboratoire d'informatique formelle
Université du Québec à Chicoutimi, Canada
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ca.uqac.lif.synthia.util;

import ca.uqac.lif.synthia.Picker;

/**
* Picker that merges the result of other pickers into a
* composite data structure. Descendants of this class use a
* different data structure.
*/
public abstract class CompositePicker<T> implements Picker<T>
{
/**
* The pickers used to generate the values
*/
/*@ non_null @*/ protected Picker<?>[] m_pickers;

/**
* Creates a new composite picker
* @param pickers The pickers used to generate the values
*/
public CompositePicker(/*@ non_null @*/ Picker<?> ... pickers)
{
super();
m_pickers = pickers;
}

@Override
public Picker<T> duplicate(boolean with_state)
{
Picker<?>[] duplicates = new Picker<?>[m_pickers.length];
for (int i = 0; i < m_pickers.length; i++)
{
duplicates[i] = m_pickers[i].duplicate(with_state);
}
return newPicker(duplicates);
}

@Override
public T pick()
{
Object[] out = new Object[m_pickers.length];
for (int i = 0; i < m_pickers.length; i++)
{
out[i] = m_pickers[i].pick();
}
return getOutput(out);
}

@Override
public void reset()
{
for (int i = 0; i < m_pickers.length; i++)
{
m_pickers[i].reset();
}
}

/**
* Creates a duplicate of the current picker
* @param pickers The internal pickers, already duplicated
* @return A new instance of the picker
*/
protected abstract CompositePicker<T> newPicker(Picker<?> ... pickers);

/**
* Creates the output composite object from the internal
* values that have been picked
* @param objects The internal values
* @return The composite object
*/
protected abstract T getOutput(Object ... objects);
}
56 changes: 56 additions & 0 deletions Source/Core/src/ca/uqac/lif/synthia/util/ListPicker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Synthia, a data structure generator
Copyright (C) 2019-2020 Laboratoire d'informatique formelle
Université du Québec à Chicoutimi, Canada
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ca.uqac.lif.synthia.util;

import java.util.HashSet;
import java.util.Set;

import ca.uqac.lif.synthia.Picker;

/**
* Picker that merges the result of other pickers into a set.
*/
public class ListPicker extends CompositePicker<Set<Object>>
{
/**
* Creates a new set picker
* @param pickers The pickers used to generate the values
*/
public ListPicker(Picker<?> ... pickers)
{
super(pickers);
}

@Override
public ListPicker newPicker(Picker<?> ... pickers)
{
return new ListPicker(pickers);
}

@Override
public Set<Object> getOutput(Object ... values)
{
Set<Object> set = new HashSet<Object>(values.length);
for (Object v : values)
{
set.add(v);
}
return set;
}
}
56 changes: 56 additions & 0 deletions Source/Core/src/ca/uqac/lif/synthia/util/SetPicker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Synthia, a data structure generator
Copyright (C) 2019-2020 Laboratoire d'informatique formelle
Université du Québec à Chicoutimi, Canada
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ca.uqac.lif.synthia.util;

import java.util.ArrayList;
import java.util.List;

import ca.uqac.lif.synthia.Picker;

/**
* Picker that merges the result of other pickers into an array.
*/
public class SetPicker extends CompositePicker<List<Object>>
{
/**
* Creates a new list picker
* @param pickers The pickers used to generate the values
*/
public SetPicker(Picker<?> ... pickers)
{
super(pickers);
}

@Override
public SetPicker newPicker(Picker<?> ... pickers)
{
return new SetPicker(pickers);
}

@Override
public List<Object> getOutput(Object ... values)
{
List<Object> list = new ArrayList<Object>(values.length);
for (Object v : values)
{
list.add(v);
}
return list;
}
}
36 changes: 7 additions & 29 deletions Source/Core/src/ca/uqac/lif/synthia/util/StringPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@
* String s2 = pat.pick(); // "aGhRe is equal to true"
* ...</pre>
*/
public class StringPattern implements Picker<String>
{
/*@ non_null @*/ protected Picker<?>[] m_pickers;

public class StringPattern extends CompositePicker<String>
{
/**
* The string pattern
*/
Expand All @@ -49,9 +47,8 @@ public class StringPattern implements Picker<String>
*/
public StringPattern(/*@ non_null @*/ String pattern, /*@ non_null @*/ Picker<?> ... parts)
{
super();
super(parts);
m_pattern = pattern;
m_pickers = parts;
}

@Override
Expand All @@ -61,38 +58,19 @@ public String toString()
}

@Override
public void reset()
{
for (Picker<?> p : m_pickers)
{
p.reset();
}
}

@Override
public String pick()
public String getOutput(Object ... parts)
{
String[] parts = new String[m_pickers.length];
for (int i = 0; i < parts.length; i++)
{
parts[i] = m_pickers[i].pick().toString();
}
String out = m_pattern;
for (int i = 0; i < parts.length; i++)
{
out = out.replaceAll("\\{\\$" + i + "\\}", parts[i]);
out = out.replaceAll("\\{\\$" + i + "\\}", parts[i].toString());
}
return out;
}

@Override
public StringPattern duplicate(boolean with_state)
public StringPattern newPicker(Picker<?> ... pickers)
{
Picker<?>[] new_provs = new Picker<?>[m_pickers.length];
for (int i = 0; i < m_pickers.length; i++)
{
new_provs[i] = m_pickers[i].duplicate(with_state);
}
return new StringPattern(m_pattern, new_provs);
return new StringPattern(m_pattern, pickers);
}
}

0 comments on commit 05c2620

Please sign in to comment.