mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-18 06:22:29 +00:00
Added another variable 'index' that lets people create sparse matrix
git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@14794 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
1 parent
12e6a35672
commit
92d1759bcb
6 files changed
+84
-13
No files matched your search
@@ -75,6 +75,13 @@ public final class Axis implements Comparable<Axis>, Iterable<String> {
|
||||
return values.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* The inverse of {@link #value(int)}.
|
||||
*/
|
||||
public int indexOf(String value) {
|
||||
return values.indexOf(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Axis is fully ordered so that we can convert between a list of axis
|
||||
* and a string unambiguously.
|
||||
|
||||
@@ -7,6 +7,7 @@ import hudson.util.RobustCollectionConverter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* List of {@link Axis}.
|
||||
@@ -21,6 +22,10 @@ public class AxisList extends ArrayList<Axis> {
|
||||
super(c);
|
||||
}
|
||||
|
||||
public AxisList(Axis... c) {
|
||||
this(Arrays.asList(c));
|
||||
}
|
||||
|
||||
public Axis find(String name) {
|
||||
for (Axis a : this) {
|
||||
if(a.name.equals(name))
|
||||
|
||||
@@ -42,6 +42,39 @@ public final class Combination extends TreeMap<String,String> implements Compara
|
||||
super.put(e.getKey(),e.getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the continuous unique index number of this {@link Combination}
|
||||
* in the given {@link AxisList}.
|
||||
*/
|
||||
public int toIndex(AxisList axis) {
|
||||
int r = 0;
|
||||
for (Axis a : axis) {
|
||||
r *= a.size();
|
||||
r += a.indexOf(get(a.name));
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains a number N such that "N%M==0" would create
|
||||
* a reasonable sparse matrix for integer M.
|
||||
*
|
||||
* <p>
|
||||
* This is bit like {@link #toIndex(AxisList)}, but instead
|
||||
* of creating a continuous number (which often maps different
|
||||
* values of the same axis to the same index in modulo N residue ring,
|
||||
* we use a prime number P as the base. I think this guarantees the uniform
|
||||
* distribution in any N smaller than 2P (but proof, anyone?)
|
||||
*/
|
||||
private long toModuloIndex(AxisList axis) {
|
||||
long r = 0;
|
||||
for (Axis a : axis) {
|
||||
r += a.indexOf(get(a.name));
|
||||
r *= 31;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates the given Groovy expression with values bound from this combination.
|
||||
*
|
||||
@@ -49,7 +82,7 @@ public final class Combination extends TreeMap<String,String> implements Compara
|
||||
* For example, if this combination is a=X,b=Y, then expressions like <tt>a=="X"</tt> would evaluate to
|
||||
* true.
|
||||
*/
|
||||
public boolean evalGroovyExpression(String expression) {
|
||||
public boolean evalGroovyExpression(AxisList axes, String expression) {
|
||||
if(Util.fixEmptyAndTrim(expression)==null)
|
||||
return true;
|
||||
|
||||
@@ -57,6 +90,9 @@ public final class Combination extends TreeMap<String,String> implements Compara
|
||||
for (Map.Entry<String, String> e : entrySet())
|
||||
binding.setVariable(e.getKey(),e.getValue());
|
||||
|
||||
binding.setVariable("index",toModuloIndex(axes));
|
||||
binding.setVariable("uniqueId",toIndex(axes));
|
||||
|
||||
GroovyShell shell = new GroovyShell(binding);
|
||||
|
||||
Object result = shell.evaluate("use("+BooleanCategory.class.getName().replace('$','.')+") {"+expression+"}");
|
||||
@@ -199,7 +235,7 @@ public final class Combination extends TreeMap<String,String> implements Compara
|
||||
/**
|
||||
* Duck-typing for boolean expressions.
|
||||
*
|
||||
* @see Combination#evalGroovyExpression(String)
|
||||
* @see Combination#evalGroovyExpression(AxisList,String)
|
||||
*/
|
||||
public static final class BooleanCategory {
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package hudson.matrix;
|
||||
|
||||
import groovy.lang.Binding;
|
||||
import groovy.lang.GroovyShell;
|
||||
import hudson.CopyOnWrite;
|
||||
import hudson.FilePath;
|
||||
import hudson.XmlFile;
|
||||
@@ -54,7 +52,6 @@ import javax.servlet.ServletException;
|
||||
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
import org.kohsuke.stapler.StaplerRequest;
|
||||
import org.kohsuke.stapler.StaplerResponse;
|
||||
|
||||
@@ -316,7 +313,7 @@ public class MatrixProject extends AbstractProject<MatrixProject,MatrixBuild> im
|
||||
// find all active configurations
|
||||
Set<MatrixConfiguration> active = new LinkedHashSet<MatrixConfiguration>();
|
||||
for (Combination c : axes.list()) {
|
||||
if(c.evalGroovyExpression(combinationFilter)) {
|
||||
if(c.evalGroovyExpression(axes,combinationFilter)) {
|
||||
LOGGER.fine("Adding configuration: " + c);
|
||||
MatrixConfiguration config = configurations.get(c);
|
||||
if(config==null) {
|
||||
|
||||
Reference in new issue
Block a user