r2983 - in isis-fish/branches/3.3.1/src: main/java/fr/ifremer/isisfish/simulator/sensitivity/domain main/java/fr/ifremer/isisfish/simulator/sensitivity/group test/java/fr/ifremer/isisfish/simulator/sensitivity test/java/fr/ifremer/isisfish/simulator/sensitivity/group
Author: chatellier Date: 2010-02-04 16:05:30 +0000 (Thu, 04 Feb 2010) New Revision: 2983 Added: isis-fish/branches/3.3.1/src/main/java/fr/ifremer/isisfish/simulator/sensitivity/group/FactorGroup.java isis-fish/branches/3.3.1/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/group/ isis-fish/branches/3.3.1/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/group/FactorGroupTest.java Modified: isis-fish/branches/3.3.1/src/main/java/fr/ifremer/isisfish/simulator/sensitivity/domain/RuleDomain.java isis-fish/branches/3.3.1/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/FactorTest.java Log: Add new ruledomain (for rules) and FactorGroup (for group screening) Modified: isis-fish/branches/3.3.1/src/main/java/fr/ifremer/isisfish/simulator/sensitivity/domain/RuleDomain.java =================================================================== --- isis-fish/branches/3.3.1/src/main/java/fr/ifremer/isisfish/simulator/sensitivity/domain/RuleDomain.java 2010-02-04 16:04:31 UTC (rev 2982) +++ isis-fish/branches/3.3.1/src/main/java/fr/ifremer/isisfish/simulator/sensitivity/domain/RuleDomain.java 2010-02-04 16:05:30 UTC (rev 2983) @@ -18,24 +18,20 @@ package fr.ifremer.isisfish.simulator.sensitivity.domain; -import java.io.Serializable; -import java.util.Collection; - import fr.ifremer.isisfish.rule.Rule; /** * Factor domain for {@link Rule}s. * - * @param <F> type des labels - * * @author chatellier * @version $Revision$ - * @since 3.3.0.0 * + * @since 3.3.1.0 + * * Last update : $Date$ * By : $Author$ */ -public class RuleDomain<F extends Serializable> extends DiscreteDomain<Collection<Rule>, F> { +public class RuleDomain extends DiscreteDomain { /** serialVersionUID. */ private static final long serialVersionUID = -5611785362638191719L; Added: isis-fish/branches/3.3.1/src/main/java/fr/ifremer/isisfish/simulator/sensitivity/group/FactorGroup.java =================================================================== --- isis-fish/branches/3.3.1/src/main/java/fr/ifremer/isisfish/simulator/sensitivity/group/FactorGroup.java (rev 0) +++ isis-fish/branches/3.3.1/src/main/java/fr/ifremer/isisfish/simulator/sensitivity/group/FactorGroup.java 2010-02-04 16:05:30 UTC (rev 2983) @@ -0,0 +1,166 @@ +/* *##% + * Copyright (C) 2010 Ifremer, Code Lutin, Chatellier Eric + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package fr.ifremer.isisfish.simulator.sensitivity.group; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import fr.ifremer.isisfish.simulator.sensitivity.Factor; +import fr.ifremer.isisfish.simulator.sensitivity.domain.ContinuousDomain; +import fr.ifremer.isisfish.simulator.sensitivity.domain.DiscreteDomain; + +/** + * Factor group. Used for group screening. + * + * @author chatellier + * @version $Revision$ + * + * @since 3.3.1.0 + * + * Last update : $Date$ + * By : $Author$ + */ +public class FactorGroup extends Factor { + + /** serialVersionUID. */ + private static final long serialVersionUID = 1893573810633639712L; + + /** Factors collection. */ + protected List<Factor> factors; + + /** + * Mixed factor composition. + * + * Default to false; + * + * To enable both {@link DiscreteDomain} and {@link ContinuousDomain} ins current group. + * Currently used in UI to use it as tree root and drag'n'drop. + */ + protected boolean mixed; + + /** + * Init factor group. + * + * @param name group name + * @param mixed property + */ + public FactorGroup(String name, boolean mixed) { + super(name); + this.mixed = mixed; + factors = new ArrayList<Factor>(); + } + + /** + * Init factor group. + * + * @param name group name + */ + public FactorGroup(String name) { + this(name, false); + } + + /** + * Check factor type and add it into factor collection. + * + * @param factor + */ + public void addFactor(Factor factor) { + // if mixed enabled, don't check + if (!mixed) { + checkFactor(factor); + } + + addCheckedFactor(factor); + } + + /** + * Check factor type with other factor collection types. + * + * @throws IllegalArgumentException if factor doesn't match other factor type + */ + protected void checkFactor(Factor factor) { + // basiquement, il doit être du même type que le + // premier element + if (factor.getDomain() == null) { + throw new IllegalArgumentException("Factor domain is null"); + } + + // on va dire, que si c'est le premier, il est du meme + // type que lui meme + if (!factors.isEmpty()) { + Factor first = factors.get(0); + + // les deux sont discret ou continue + if (!first.getDomain().getClass().isAssignableFrom( + factor.getDomain().getClass())) { + throw new IllegalArgumentException( + "Factor type is not in same type as other factor in group"); + } + } + } + + /** + * Add factor into factor list without check. + * + * @param factor + */ + protected void addCheckedFactor(Factor factor) { + factors.add(factor); + } + + /** + * Get groups factors. + * + * @return unmodifiable factors list + */ + public List<Factor> getFactors() { + return Collections.unmodifiableList(factors); + } + + /** + * Convenient method to access specific factor. + * + * @param index index + * @return factor at index + */ + public Factor get(int index) { + return factors.get(index); + } + + /** + * Get factor list size. + * + * @return factor list size + */ + public int size() { + return factors.size(); + } + + /** + * Returns the index of the first occurrence of the specified element in this + * group. + * @param o element to search for + * @return the index of the first occurrence of the specified element in this + * group, or -1 if this list does not contain the element + */ + public int indexOf(Object o) { + return factors.indexOf(o); + } +} Property changes on: isis-fish/branches/3.3.1/src/main/java/fr/ifremer/isisfish/simulator/sensitivity/group/FactorGroup.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL" Modified: isis-fish/branches/3.3.1/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/FactorTest.java =================================================================== --- isis-fish/branches/3.3.1/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/FactorTest.java 2010-02-04 16:04:31 UTC (rev 2982) +++ isis-fish/branches/3.3.1/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/FactorTest.java 2010-02-04 16:05:30 UTC (rev 2983) @@ -1,5 +1,5 @@ /* *##% - * Copyright (C) 2009-2010 Ifremer, Code Lutin + * Copyright (C) 2009 - 2010 Ifremer, Code Lutin * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -56,8 +56,8 @@ @Test public void testIntFactor() { - Factor<Integer,Integer> factor = new Factor<Integer,Integer>("testint"); - ContinuousDomain<Integer,Integer> domain = new ContinuousDomain<Integer,Integer>(); + Factor factor = new Factor("testint"); + ContinuousDomain domain = new ContinuousDomain(); domain.setMinBound(0); domain.setMaxBound(50); factor.setDomain(domain); @@ -100,8 +100,8 @@ matrix2.setValue(new int[] { 1, 2 }, -13000); // factor - Factor<MatrixND,String> factor = new Factor<MatrixND,String>("testmatrix"); - DiscreteDomain<MatrixND,String> domain = new DiscreteDomain<MatrixND,String>(); + Factor factor = new Factor("testmatrix"); + DiscreteDomain domain = new DiscreteDomain(); domain.getValues().put("m1", matrix1); domain.getValues().put("m2", matrix2); factor.setDomain(domain); @@ -143,7 +143,7 @@ matrix1.setValue(new int[] { 2, 1 }, -1); // factor - Factor<MatrixND,Double> factor = new Factor<MatrixND,Double>("testmatrix"); + Factor factor = new Factor("testmatrix"); MatrixContinuousDomain domain = new MatrixContinuousDomain (); domain.setMatrix(matrix1); domain.setCoefficient(0.1); @@ -152,8 +152,8 @@ factor.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim"); factor.setValueForIdentifier(0.1); - Assert.assertEquals(0.92, factor.getValue().getValue(new int[] { 0, 0 }),0.0000001); - Assert.assertEquals(2.76, factor.getValue().getValue(new int[] { 2, 0 }),0.0000001); + Assert.assertEquals(0.92, ((MatrixND)factor.getValue()).getValue(new int[] { 0, 0 }),0.0000001); + Assert.assertEquals(2.76, ((MatrixND)factor.getValue()).getValue(new int[] { 2, 0 }),0.0000001); Assert.assertEquals(0,((ContinuousDomain)factor.getDomain()).getMinBound(),0); Assert.assertEquals(1,((ContinuousDomain)factor.getDomain()).getMaxBound(),0); Assert.assertEquals(0.1, ((MatrixContinuousDomain)factor.getDomain()).getValue(),0); @@ -169,7 +169,7 @@ matrix2.setValue(new int[] { 2, 1 }, -1); // factor 2 - Factor<MatrixND,Double> factor2 = new Factor<MatrixND,Double>("testmatrix"); + Factor factor2 = new Factor("testmatrix"); MatrixContinuousDomain domain2 = new MatrixContinuousDomain(); domain2.setMatrix(matrix2); domain2.setCoefficient(0.1); @@ -178,8 +178,8 @@ factor2.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim"); factor2.setValueForIdentifier(0.1); - Assert.assertEquals(-7.0, factor2.getValue().getValue(new int[] { 0, 0 }),0.0000001); - Assert.assertEquals(-21.0, factor2.getValue().getValue(new int[] { 2, 0 }),0.0000001); + Assert.assertEquals(-7.0, ((MatrixND)factor2.getValue()).getValue(new int[] { 0, 0 }),0.0000001); + Assert.assertEquals(-21.0, ((MatrixND)factor2.getValue()).getValue(new int[] { 2, 0 }),0.0000001); Assert.assertEquals(0,((ContinuousDomain)factor.getDomain()).getMinBound(),0); Assert.assertEquals(1,((ContinuousDomain)factor.getDomain()).getMaxBound(),0); @@ -194,7 +194,7 @@ matrix3.setValue(new int[] { 2, 1 }, -1); // factor 3 - Factor<MatrixND,Double> factor3 = new Factor<MatrixND,Double>("testmatrix"); + Factor factor3 = new Factor("testmatrix"); MatrixContinuousDomain domain3 = new MatrixContinuousDomain(); domain3.setMatrix(matrix3); domain3.setCoefficient(0.1); @@ -203,8 +203,8 @@ factor3.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim"); factor3.setValueForIdentifier(0.1); - Assert.assertEquals(0.28, factor3.getValue().getValue(new int[] { 0, 0 }),0.0000001); - Assert.assertEquals(0.68, factor3.getValue().getValue(new int[] { 2, 0 }),0.0000001); + Assert.assertEquals(0.28, ((MatrixND)factor3.getValue()).getValue(new int[] { 0, 0 }),0.0000001); + Assert.assertEquals(0.68, ((MatrixND)factor3.getValue()).getValue(new int[] { 2, 0 }),0.0000001); Assert.assertEquals(0,((ContinuousDomain)factor.getDomain()).getMinBound(),0); Assert.assertEquals(1,((ContinuousDomain)factor.getDomain()).getMaxBound(),0); @@ -219,7 +219,7 @@ matrix4.setValue(new int[] { 2, 1 }, -1); // factor 3 - Factor<MatrixND,Double> factor4 = new Factor<MatrixND,Double>("testmatrix"); + Factor factor4 = new Factor("testmatrix"); MatrixContinuousDomain domain4 = new MatrixContinuousDomain(); domain4.setMatrix(matrix4); domain4.setCoefficient(0.1); @@ -228,8 +228,8 @@ factor4.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim"); factor4.setValueForIdentifier(0.1); - Assert.assertEquals(0.12, factor4.getValue().getValue(new int[] { 0, 0 }),0.0000001); - Assert.assertEquals(0.52, factor4.getValue().getValue(new int[] { 2, 0 }),0.0000001); + Assert.assertEquals(0.12, ((MatrixND)factor4.getValue()).getValue(new int[] { 0, 0 }),0.0000001); + Assert.assertEquals(0.52, ((MatrixND)factor4.getValue()).getValue(new int[] { 2, 0 }),0.0000001); Assert.assertEquals(0,((ContinuousDomain)factor.getDomain()).getMinBound(),0); Assert.assertEquals(1,((ContinuousDomain)factor.getDomain()).getMaxBound(),0); @@ -247,7 +247,7 @@ public void testEquationContinuousFactor() { // factor - Factor<Double,Double> factor = new Factor<Double,Double>("testequation"); + Factor factor = new Factor("testequation"); EquationContinuousDomain domain = new EquationContinuousDomain(); domain.setCoefficient(0.1); domain.setOperator("*"); @@ -257,13 +257,13 @@ factor.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim"); factor.setValueForIdentifier(0.1); - Assert.assertEquals(2.76, factor.getValue(),0.0000001); + Assert.assertEquals(2.76, (Double)factor.getValue(),0.0000001); Assert.assertEquals(0,((ContinuousDomain)factor.getDomain()).getMinBound(),0); Assert.assertEquals(1,((ContinuousDomain)factor.getDomain()).getMaxBound(),0); Assert.assertEquals(0.1, ((EquationContinuousDomain)factor.getDomain()).getValue(),0); // factor 2 - Factor<Double,Double> factor2 = new Factor<Double,Double>("testequation"); + Factor factor2 = new Factor("testequation"); EquationContinuousDomain domain2 = new EquationContinuousDomain(); domain2.setCoefficient(0.1); domain2.setOperator("/"); @@ -273,12 +273,12 @@ factor2.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim"); factor2.setValueForIdentifier(0.1); - Assert.assertEquals(-21, Double.valueOf(factor2.getValue()),0.0000001); + Assert.assertEquals(-21.0, (Double)factor2.getValue(),0.0000001); Assert.assertEquals(0,((ContinuousDomain)factor.getDomain()).getMinBound(),0); Assert.assertEquals(1,((ContinuousDomain)factor.getDomain()).getMaxBound(),0); // factor 3 - Factor<Double,Double> factor3 = new Factor<Double,Double>("testequation"); + Factor factor3 = new Factor("testequation"); EquationContinuousDomain domain3 = new EquationContinuousDomain(); domain3.setCoefficient(0.1); domain3.setOperator("+"); @@ -288,12 +288,12 @@ factor3.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim"); factor3.setValueForIdentifier(0.1); - Assert.assertEquals(0.52, Double.valueOf(factor3.getValue()),0.0000001); + Assert.assertEquals(0.52, (Double)factor3.getValue(),0.0000001); Assert.assertEquals(0,((ContinuousDomain)factor.getDomain()).getMinBound(),0); Assert.assertEquals(1,((ContinuousDomain)factor.getDomain()).getMaxBound(),0); // factor 4 - Factor<Double,Double> factor4 = new Factor<Double,Double>("testequation"); + Factor factor4 = new Factor("testequation"); EquationContinuousDomain domain4 = new EquationContinuousDomain(); domain4.setCoefficient(0.1); domain4.setOperator("-"); @@ -303,7 +303,7 @@ factor4.setPath("org.nuiton.math.matrix.MatrixND#563456293453#2.456347646#dim"); factor4.setValueForIdentifier(0.1); - Assert.assertEquals(0.68, Double.valueOf(factor4.getValue()),0.0000001); + Assert.assertEquals(0.68, (Double)factor4.getValue(),0.0000001); Assert.assertEquals(0,((ContinuousDomain)factor.getDomain()).getMinBound(),0); Assert.assertEquals(1,((ContinuousDomain)factor.getDomain()).getMaxBound(),0); @@ -335,8 +335,8 @@ rules3.add(ruleC); // factor and domain definition - Factor<Collection<Rule>, String> factor = new Factor<Collection<Rule>, String>("testrule"); - RuleDomain<String> ruleDomain = new RuleDomain<String>(); + Factor factor = new Factor("testrule"); + RuleDomain ruleDomain = new RuleDomain(); ruleDomain.getValues().put("rules1", rules1); ruleDomain.getValues().put("rules2", rules2); ruleDomain.getValues().put("rules3", rules3); @@ -344,4 +344,32 @@ // TODO post r operation, and some asserts } + + /** + * Assert that clone do work (with inheritance). + */ + @Test + public void testClone() { + + Factor factor = new Factor("testclone"); + factor.setPath("fr.ifremer.isisfish.entities.Cell#lenght"); + factor.setComment("answer to life"); + factor.setValue(42.0); + + ContinuousDomain domain = new ContinuousDomain(); + domain.setMinBound(0); + domain.setMaxBound(50); + factor.setDomain(domain); + + // clone + Factor clone = (Factor)factor.clone(); + + // test on clone + Assert.assertEquals("testclone", clone.getName()); + Assert.assertEquals("fr.ifremer.isisfish.entities.Cell#lenght", clone.getPath()); + Assert.assertEquals("answer to life", clone.getComment()); + Assert.assertEquals(42.0, clone.getValue()); + Assert.assertNotSame(domain, clone.getDomain()); + Assert.assertEquals("id1", clone.getDomain().getValueForIdentifier("id1")); + } } Added: isis-fish/branches/3.3.1/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/group/FactorGroupTest.java =================================================================== --- isis-fish/branches/3.3.1/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/group/FactorGroupTest.java (rev 0) +++ isis-fish/branches/3.3.1/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/group/FactorGroupTest.java 2010-02-04 16:05:30 UTC (rev 2983) @@ -0,0 +1,80 @@ +/* *##% + * Copyright (C) 2010 Code Lutin, Chatellier Eric + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package fr.ifremer.isisfish.simulator.sensitivity.group; + +import org.junit.Test; + +import fr.ifremer.isisfish.simulator.sensitivity.Domain; +import fr.ifremer.isisfish.simulator.sensitivity.Factor; +import fr.ifremer.isisfish.simulator.sensitivity.domain.ContinuousDomain; +import fr.ifremer.isisfish.simulator.sensitivity.domain.DiscreteDomain; + +/** + * Test for FactorGroup. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class FactorGroupTest { + + /** + * Test to add a factor into group (null domain). + */ + @Test(expected=IllegalArgumentException.class) + public void testFactorGroupAddNullDomain() { + Factor f = new Factor("test"); + FactorGroup g = new FactorGroup("test group"); + g.addFactor(f); + } + + /** + * Test to add a factor into group (authorized). + */ + @Test + public void testFactorGroupAdd() { + Factor f1 = new Factor("test"); + Domain d1 = new DiscreteDomain(); + f1.setDomain(d1); + Factor f2 = new Factor("test 2"); + Domain d2 = new DiscreteDomain(); + f2.setDomain(d2); + FactorGroup g = new FactorGroup("test group"); + g.addFactor(f1); + g.addFactor(f2); + } + + /** + * Test to add a factor into group (forbidden). + */ + @Test(expected=IllegalArgumentException.class) + public void testFactorGroupAddException() { + Factor f1 = new Factor("test"); + Domain d1 = new DiscreteDomain(); + f1.setDomain(d1); + Factor f2 = new Factor("test 2"); + Domain d2 = new ContinuousDomain(); + f2.setDomain(d2); + FactorGroup g = new FactorGroup("test group"); + g.addFactor(f1); + g.addFactor(f2); + } +} Property changes on: isis-fish/branches/3.3.1/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/group/FactorGroupTest.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL"
participants (1)
-
chatellier@users.labs.libre-entreprise.org