From 92d1759bcbc052ea01dd00298cbf1ef0a120f26e Mon Sep 17 00:00:00 2001 From: kohsuke Date: Sun, 25 Jan 2009 17:01:03 +0000 Subject: [PATCH] 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 --- core/src/main/java/hudson/matrix/Axis.java | 7 ++++ .../src/main/java/hudson/matrix/AxisList.java | 5 +++ .../main/java/hudson/matrix/Combination.java | 40 ++++++++++++++++++- .../java/hudson/matrix/MatrixProject.java | 5 +-- .../java/hudson/matrix/CombinationTest.java | 25 +++++++++--- .../help/matrix/combinationfilter.html | 15 ++++++- 6 files changed, 84 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/hudson/matrix/Axis.java b/core/src/main/java/hudson/matrix/Axis.java index 619c493b72..632232c2a9 100644 --- a/core/src/main/java/hudson/matrix/Axis.java +++ b/core/src/main/java/hudson/matrix/Axis.java @@ -75,6 +75,13 @@ public final class Axis implements Comparable, Iterable { 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. diff --git a/core/src/main/java/hudson/matrix/AxisList.java b/core/src/main/java/hudson/matrix/AxisList.java index d31ce4e32e..f20d10243c 100644 --- a/core/src/main/java/hudson/matrix/AxisList.java +++ b/core/src/main/java/hudson/matrix/AxisList.java @@ -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 { super(c); } + public AxisList(Axis... c) { + this(Arrays.asList(c)); + } + public Axis find(String name) { for (Axis a : this) { if(a.name.equals(name)) diff --git a/core/src/main/java/hudson/matrix/Combination.java b/core/src/main/java/hudson/matrix/Combination.java index baa1df17e4..d3e9a7d600 100644 --- a/core/src/main/java/hudson/matrix/Combination.java +++ b/core/src/main/java/hudson/matrix/Combination.java @@ -42,6 +42,39 @@ public final class Combination extends TreeMap 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. + * + *

+ * 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 implements Compara * For example, if this combination is a=X,b=Y, then expressions like a=="X" 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 implements Compara for (Map.Entry 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 implements Compara /** * Duck-typing for boolean expressions. * - * @see Combination#evalGroovyExpression(String) + * @see Combination#evalGroovyExpression(AxisList,String) */ public static final class BooleanCategory { /** diff --git a/core/src/main/java/hudson/matrix/MatrixProject.java b/core/src/main/java/hudson/matrix/MatrixProject.java index 45d6c50958..617f2e8237 100644 --- a/core/src/main/java/hudson/matrix/MatrixProject.java +++ b/core/src/main/java/hudson/matrix/MatrixProject.java @@ -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 im // find all active configurations Set active = new LinkedHashSet(); 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) { diff --git a/core/src/test/java/hudson/matrix/CombinationTest.java b/core/src/test/java/hudson/matrix/CombinationTest.java index e4ba4218bf..a175f72795 100644 --- a/core/src/test/java/hudson/matrix/CombinationTest.java +++ b/core/src/test/java/hudson/matrix/CombinationTest.java @@ -9,18 +9,31 @@ import java.util.HashMap; * @author Kohsuke Kawaguchi */ public class CombinationTest extends TestCase { + AxisList axes = new AxisList( + new Axis("a","X","x"), + new Axis("b","Y","y")); + @SuppressWarnings({"RedundantStringConstructorCall"}) public void testEval() { Map r = new HashMap(); r.put("a","X"); r.put("b",new String("Y")); // make sure this 'Y' is not the same object as literal "Y". Combination c = new Combination(r); - assertTrue(c.evalGroovyExpression(null)); - assertTrue(c.evalGroovyExpression(" ")); - assertTrue(c.evalGroovyExpression("true")); - assertTrue(c.evalGroovyExpression("a=='X'")); - assertTrue(c.evalGroovyExpression("b=='Y'")); - assertTrue(c.evalGroovyExpression("(a=='something').implies(b=='other')")); + r.put("a","x"); + Combination d = new Combination(r); + + assertTrue(eval(c, null)); + assertTrue(eval(c," ")); + assertTrue(eval(c,"true")); + assertTrue(eval(c,"a=='X'")); + assertTrue(eval(c,"b=='Y'")); + assertTrue(eval(c,"(a=='something').implies(b=='other')")); + assertTrue(eval(c,"index%2==0")^eval(d,"index%2==0")); + assertTrue(eval(c,"index%2==1")^eval(d,"index%2==1")); + } + + private boolean eval(Combination c, String exp) { + return c.evalGroovyExpression(axes, exp); } } diff --git a/war/resources/help/matrix/combinationfilter.html b/war/resources/help/matrix/combinationfilter.html index 199447635c..40379d0ae2 100644 --- a/war/resources/help/matrix/combinationfilter.html +++ b/war/resources/help/matrix/combinationfilter.html @@ -1,5 +1,6 @@

- By default, Hudson builds all the possible combinations of axes exhaustively. But sometimes this is too many. + By default, Hudson builds all the possible combinations of axes exhaustively. But sometimes this is too many, + or may contain combinations that don't make sense. In such a situation, you can make the matrix sparse by filtering out combinations that you don't want through a Goorvy expression that returns true or false. @@ -8,6 +9,7 @@ will be built. In evaluating the expression, axes are exposed as variables (with their values set to the current combination evaluated.) +

Filtering based on values

For example, let's say you are building on different operating systems for different compilers. Assume that your slave labels are label=[linux,solaris] and you have @@ -38,4 +40,15 @@ + +

Sparsening of the matrix

+

+ In addition to the specific filtering rules based on values, one can also use a special + variable "index", which can be used to sparsen the matrix. + +

+ For example, index%2==0 would cut the matrix size in half by removing one + combination per every 2 combinations, in such a way that the coverage is still reasonable. + Similarly, index%3!=0 would cut the matrix size into 66% by throwing + away every 1 out of 3 combinations.

\ No newline at end of file