r3659 - in branches/4.0.1/src: main/java/fr/ifremer/isisfish/mexico main/java/fr/ifremer/isisfish/mexico/xml test/java/fr/ifremer/isisfish/mexico
Author: echatellier Date: 2012-03-15 18:43:01 +0100 (Thu, 15 Mar 2012) New Revision: 3659 Url: http://forge.codelutin.com/repositories/revision/isis-fish/3659 Log: Fix design plan V1 & V2 import and begin V3 import Removed: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/ScenarioXMLVisitor.java branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/SensitivityScenariosXMLVisitor.java Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/package-info.java branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/AbstractXMLVisitor.java branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/ContinuousDomainXMLVisitor.java branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DesignPlanXMLVisitor.java branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DiscreteDomainXMLVisitor.java branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DomXMLParser.java branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DomainXMLVisitor.java branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/EquationDiscreteDomainXMLVisitor.java branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/FactorGroupXMLVisitor.java branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/FactorXMLVisitor.java branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/RuleDiscreteDomainXMLVisitor.java branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/package-info.java branches/4.0.1/src/test/java/fr/ifremer/isisfish/mexico/MexicoHelperTest.java Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/package-info.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/package-info.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/package-info.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 1999 - 2011 Ifremer, CodeLutin, Chatellier Eric + * Copyright (C) 1999 - 2012 Ifremer, CodeLutin, 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 Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/AbstractXMLVisitor.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/AbstractXMLVisitor.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/AbstractXMLVisitor.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -3,7 +3,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2011 Ifremer, Codelutin, Chatellier Eric + * Copyright (C) 2011 - 2012 Ifremer, Codelutin, 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 Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/ContinuousDomainXMLVisitor.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/ContinuousDomainXMLVisitor.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/ContinuousDomainXMLVisitor.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -49,28 +49,15 @@ */ @Override public void start(Domain domain) { - super.start(domain); - ContinuousDomain cDomain = (ContinuousDomain)domain; - appendDomain(cDomain); - } - - /** - * Default ContinuousDomain implementation for double. - * Maybe overridden by specific continuous domains. - * - * @param domain domain to print - */ - protected void appendDomain(ContinuousDomain domain) { - xmlBuffer.append("<distribution name=\"" + domain.getDistribution().toString().toLowerCase() + "\">"); - for (Entry<String, Object> distributionParameterEntry : domain.getDistributionParameters().entrySet()) { + xmlBuffer.append("<domain distribution=\"" + cDomain.getDistribution().toString().toLowerCase() + "\">"); + for (Entry<String, Object> distributionParameterEntry : cDomain.getDistributionParameters().entrySet()) { String name = distributionParameterEntry.getKey(); Object value = distributionParameterEntry.getValue(); xmlBuffer.append("<distributionParameter name=\"" + name + "\""); appendParamValue(xmlBuffer, value); xmlBuffer.append("</distributionParameter>"); } - xmlBuffer.append("</distribution>"); } /** @@ -80,7 +67,6 @@ * @param value value to append */ protected void appendParamValue(StringBuffer xmlBuffer, Object value) { - if (value instanceof MatrixND) { xmlBuffer.append(" type=\"matrix\">"); xmlBuffer.append(MexicoHelper.getMatrixAsXML((MatrixND)value)); @@ -88,7 +74,6 @@ xmlBuffer.append(" type=\"double\">"); xmlBuffer.append(value.toString()); } - } /** @@ -98,4 +83,12 @@ public void visit(Domain domain, Object label, Object value) { } + + /* + * @see fr.ifremer.isisfish.simulator.sensitivity.visitor.DomainVisitor#end(fr.ifremer.isisfish.simulator.sensitivity.Domain) + */ + @Override + public void end(Domain domain) { + xmlBuffer.append("</domain>"); + } } Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DesignPlanXMLVisitor.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DesignPlanXMLVisitor.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DesignPlanXMLVisitor.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -68,7 +68,10 @@ int dateIndex = ArrayUtils.indexOf(args, "date"); int idIndex = ArrayUtils.indexOf(args, "id"); - xmlBuffer.append("<experimentalDesign version=\"3\""); + xmlBuffer.append("<experimentalDesign"); + // for validation (tests) + xmlBuffer.append(" xmlns='http://www.reseau-mexico.fr/sites/reseau-mexico.fr/files/expDesign.xsd'"); + xmlBuffer.append(" version=\"3\""); xmlBuffer.append(" author=\"" + (authorIndex != -1 ? args[authorIndex + 1] : "") + "\""); xmlBuffer.append(" license=\"" + (licenseIndex != -1 ? args[licenseIndex + 1] : "") + "\""); xmlBuffer.append(" date=\"" + (dateIndex != -1 ? args[dateIndex + 1] : new Date()) + "\""); @@ -79,11 +82,12 @@ * {@inheritDoc} */ @Override - public void visit(DesignPlan designPlan, - FactorGroup factorGroup) { + public void visit(DesignPlan designPlan, FactorGroup factorGroup) { + xmlBuffer.append("<factors>"); FactorGroupXMLVisitor visitor = new FactorGroupXMLVisitor(); factorGroup.accept(visitor); xmlBuffer.append(visitor.getXML()); + xmlBuffer.append("</factors>"); } /** Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DiscreteDomainXMLVisitor.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DiscreteDomainXMLVisitor.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DiscreteDomainXMLVisitor.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -39,6 +39,16 @@ */ public class DiscreteDomainXMLVisitor extends DomainXMLVisitor { + /* + * @see fr.ifremer.isisfish.simulator.sensitivity.visitor.DomainVisitor#start(fr.ifremer.isisfish.simulator.sensitivity.Domain) + */ + @Override + public void start(Domain domain) { + // lorsque le facteur est catégoriel, la liste de ses niveaux, par une + // liste ouverte d'éléments 'level', d'attributs 'value' et 'weight') + xmlBuffer.append("<domain distributionName=\"categorical\" type=\"todo\""); + } + /** * {@inheritDoc} */ @@ -46,6 +56,14 @@ public void visit(Domain domain, Object label, Object value) { xmlBuffer.append("<level name=\"" + label + "\">"); xmlBuffer.append(MexicoHelper.getStringFromObject(value)); - xmlBuffer.append("<level>"); + xmlBuffer.append("</level>"); } + + /* + * @see fr.ifremer.isisfish.simulator.sensitivity.visitor.DomainVisitor#end(fr.ifremer.isisfish.simulator.sensitivity.Domain) + */ + @Override + public void end(Domain domain) { + xmlBuffer.append("</domain>"); + } } Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DomXMLParser.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DomXMLParser.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DomXMLParser.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -25,10 +25,14 @@ package fr.ifremer.isisfish.mexico.xml; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.StringEscapeUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.dom4j.Element; import org.nuiton.math.matrix.MatrixND; import org.nuiton.topia.TopiaContext; @@ -36,6 +40,8 @@ import fr.ifremer.isisfish.mexico.MexicoHelper; import fr.ifremer.isisfish.rule.Rule; import fr.ifremer.isisfish.simulator.sensitivity.DesignPlan; +import fr.ifremer.isisfish.simulator.sensitivity.Distribution; +import fr.ifremer.isisfish.simulator.sensitivity.Domain; import fr.ifremer.isisfish.simulator.sensitivity.Factor; import fr.ifremer.isisfish.simulator.sensitivity.FactorGroup; import fr.ifremer.isisfish.simulator.sensitivity.domain.ContinuousDomain; @@ -56,32 +62,7 @@ */ public class DomXMLParser { - public static final String DISCRETE = "discrete"; - public static final String ENUMERATION = "enumeration"; - public static final String DOMAIN = "domain"; - public static final String FIXED = "fixed"; - public static final String VALUE = "value"; - public static final String RULE = "rule"; - public static final String RULES = "rules"; - public static final String VERSION = "version"; - public static final String NAME = "name"; - public static final String TYPE = "type"; - public static final String PROPERTY = "property"; - public static final String TARGET = "target"; - public static final String CONTINUOUS = "continuous"; - public static final String MATRIXCONTINUOUS = "matrixcontinuous"; - public static final String COEFFICIENT = "coefficient"; - public static final String EQUATIONCONTINUOUS = "equationcontinuous"; - public static final String EQUATION = "equation"; - public static final String VARIABLE = "variable"; - public static final String PERCENTAGE = "percentage"; - public static final String REFERENCE = "reference"; - public static final String RANGE = "range"; - public static final String MIN = "min"; - public static final String MAX = "max"; - public static final String CARDINALITY = "cardinality"; - public static final String INTEGER = "integer"; - public static final String MX = "mx"; + private static Log log = LogFactory.getLog(DomXMLParser.class); /** * Parse element root node as Design plan. @@ -95,7 +76,7 @@ DesignPlan plan = new DesignPlan(); // could return "2" or null - String version = rootElement.attributeValue(VERSION); + String version = rootElement.attributeValue("version"); List<Element> factorGroupElements = rootElement.selectNodes("child::factors"); FactorGroup factorGroup = null; @@ -128,12 +109,12 @@ @Deprecated protected static FactorGroup parseFactorGroup(Element fgElement, TopiaContext topiaContext) { - String factorGroupName = fgElement.attributeValue(NAME); + String factorGroupName = fgElement.attributeValue("name"); String factorGroupType = fgElement.attributeValue("type"); boolean continuous = "continuous".equalsIgnoreCase(factorGroupType); FactorGroup factorGroup = new FactorGroup(factorGroupName, continuous); - /*// sub factor group + // sub factor group List<Element> factorGroupElements = fgElement.selectNodes("child::factors"); for (Element factorGroupElement : factorGroupElements) { FactorGroup subFactorGroup = parseFactorGroup(factorGroupElement, topiaContext); @@ -144,141 +125,136 @@ List<Element> factorElements = fgElement.selectNodes("child::factor"); for (Element factorElement : factorElements) { - String type = factorElement.attributeValue(TYPE); - String name = factorElement.attributeValue(NAME); - String property = factorElement.attributeValue(PROPERTY); - String path = factorElement.element(TARGET).getText().trim(); + String type = factorElement.attributeValue("type"); + String name = factorElement.attributeValue("name"); + String property = factorElement.attributeValue("property"); + String path = factorElement.element("target").getTextTrim(); // double if ("real".equals(type)) { Factor factor = new Factor(name); factor.setPath(path); - Element fixedElement = factorElement.element(DOMAIN).element(FIXED); + Element fixedElement = factorElement.element("domain").element("fixed"); // tous les domaines continues - if (property.endsWith(CONTINUOUS)) { + if (property.endsWith("continuous")) { ContinuousDomain domain; - if (property.equals(MATRIXCONTINUOUS)) { + if (property.equals("matrixcontinuous")) { // matrix specific // <coefficient operator="-" value="0.799"/> - MatrixContinuousDomain mdomain = new MatrixContinuousDomain(true); - Element coefficientElement = fixedElement.element(COEFFICIENT); - mdomain.setCoefficient(Double.valueOf(coefficientElement.attributeValue(VALUE))); + domain = new ContinuousDomain(Distribution.QUNIFPC); + Element coefficientElement = fixedElement.element("coefficient"); + domain.addDistributionParam("coefficient", Double.valueOf(coefficientElement.attributeValue("value"))); // <mx name="test1" step="0">... - Element matrixElement = fixedElement.element(MX); + Element matrixElement = fixedElement.element("mx"); MatrixND matrix = MexicoHelper.getMatrixFromXml(matrixElement, topiaContext); - mdomain.setReferenceValue(matrix); - - domain = mdomain; + domain.addDistributionParam("reference", matrix); } - else if (property.equals(EQUATIONCONTINUOUS)) { + else if (property.equals("equationcontinuous")) { // equation specific - EquationContinuousDomain edomain = new EquationContinuousDomain(true); - Element coefficientElement = fixedElement.element(COEFFICIENT); - edomain.setCoefficient(Double.valueOf(coefficientElement.attributeValue(VALUE))); + domain = new ContinuousDomain(Distribution.QUNIFPC); + Element coefficientElement = fixedElement.element("coefficient"); + domain.addDistributionParam("coefficient", Double.valueOf(coefficientElement.attributeValue("value"))); - Element equationElement = fixedElement.element(EQUATION); - edomain.setReferenceValue(Double.valueOf(equationElement.attributeValue(REFERENCE))); - edomain.setVariableName(equationElement.attributeValue(VARIABLE)); + Element equationElement = fixedElement.element("equation"); + domain.addDistributionParam("reference", Double.valueOf(equationElement.attributeValue("reference"))); + String variable = equationElement.attributeValue("variable"); + factor.setEquationVariableName(variable); + // hack since 4.0.1, factor name doesn't contains variable + name = StringUtils.removeEnd(name, "." + variable); + factor.setName(name); - domain = edomain; } else { // continous domain - domain = new ContinuousDomain(); - Element percentageElement = fixedElement.element(PERCENTAGE); + Element percentageElement = fixedElement.element("percentage"); if (percentageElement != null) { - domain.setCoefficient(Double.valueOf(percentageElement.attributeValue(COEFFICIENT))); - domain.setReferenceValue(Double.valueOf(percentageElement.attributeValue(REFERENCE))); - domain.setPercentageType(true); + domain = new ContinuousDomain(Distribution.QUNIFPC); + domain.addDistributionParam("coefficient", Double.valueOf(percentageElement.attributeValue("coefficient"))); + domain.addDistributionParam("reference", Double.valueOf(percentageElement.attributeValue("reference"))); } else { // <range max="1.0" min="0.0"/> - Element rangeElement = fixedElement.element(RANGE); - domain.setMinBound(Double.valueOf(rangeElement.attributeValue(MIN))); - domain.setMaxBound(Double.valueOf(rangeElement.attributeValue(MAX))); + domain = new ContinuousDomain(Distribution.QUNIFMM); + Element rangeElement = fixedElement.element("range"); + domain.addDistributionParam("min", Double.valueOf(rangeElement.attributeValue("min"))); + domain.addDistributionParam("max", Double.valueOf(rangeElement.attributeValue("max"))); } } - factor.setCardinality(Integer.valueOf(fixedElement.attributeValue(CARDINALITY))); + factor.setCardinality(Integer.valueOf(fixedElement.attributeValue("cardinality"))); factor.setDomain(domain); - } else if (DISCRETE.equals(property)) { + } else if ("discrete".equals(property)) { DiscreteDomain domain = new DiscreteDomain(); - List<Element> valueElements = fixedElement.element( - ENUMERATION).elements(VALUE); + List<Element> valueElements = fixedElement.element("enumeration").elements("value"); int label = 0; for (Element valueElement : valueElements) { - domain.getValues().put(label, - Double.valueOf(valueElement.getTextTrim())); + domain.getValues().put(label, Double.valueOf(valueElement.getTextTrim())); ++label; } factor.setDomain(domain); } factorGroup.addFactor(factor); - } else if (INTEGER.equals(type)) { + } else if ("integer".equals(type)) { Factor factor = new Factor(name); factor.setPath(path); - Element fixedElement = factorElement.element(DOMAIN).element(FIXED); - if (CONTINUOUS.equals(property)) { + Element fixedElement = factorElement.element("domain").element("fixed"); + if ("continuous".equals(property)) { ContinuousDomain domain; - if(property.equals(MATRIXCONTINUOUS)) { + if(property.equals("matrixcontinuous")) { // matrix specific - MatrixContinuousDomain mdomain = new MatrixContinuousDomain(true); + domain = new ContinuousDomain(Distribution.QUNIFPC); - Element coefficientElement = fixedElement.element(COEFFICIENT); - mdomain.setCoefficient(Double.valueOf(coefficientElement.attributeValue(VALUE))); + Element coefficientElement = fixedElement.element("coefficient"); + domain.addDistributionParam("coefficient", Double.valueOf(coefficientElement.attributeValue("value"))); // matrix specific - Element matrixElement = fixedElement.element(MX); + Element matrixElement = fixedElement.element("mx"); MatrixND matrix = MexicoHelper.getMatrixFromXml(matrixElement, topiaContext); - mdomain.setReferenceValue(matrix); - - domain = mdomain; + domain.addDistributionParam("reference", matrix); } - else if (property.equals(EQUATIONCONTINUOUS)) { + else if (property.equals("equationcontinuous")) { // equation specific - - EquationContinuousDomain edomain = new EquationContinuousDomain(true); - - Element coefficientElement = fixedElement.element(COEFFICIENT); - edomain.setCoefficient(Double.valueOf(coefficientElement.attributeValue(VALUE))); - - Element equationElement = fixedElement.element(EQUATION); - edomain.setReferenceValue(Double.valueOf(equationElement.attributeValue(REFERENCE))); - edomain.setVariableName(equationElement.attributeValue(VARIABLE)); + domain = new ContinuousDomain(Distribution.QUNIFPC); + Element coefficientElement = fixedElement.element("coefficient"); + domain.addDistributionParam("coefficient", Double.valueOf(coefficientElement.attributeValue("value"))); - domain = edomain; + Element equationElement = fixedElement.element("equation"); + domain.addDistributionParam("reference", Double.valueOf(equationElement.attributeValue("reference"))); + String variable = equationElement.attributeValue("variable"); + factor.setEquationVariableName(variable); + // hack since 4.0.1, factor name doesn't contains variable + name = StringUtils.removeEnd(name, "." + variable); + factor.setName(name); } else { // continous domain - domain = new ContinuousDomain(); - - Element percentageElement = fixedElement.element(PERCENTAGE); + Element percentageElement = fixedElement.element("percentage"); if (percentageElement != null) { - domain.setCoefficient(Double.valueOf(percentageElement.attributeValue(COEFFICIENT))); - domain.setReferenceValue(Double.valueOf(percentageElement.attributeValue(REFERENCE))); - domain.setPercentageType(true); + domain = new ContinuousDomain(Distribution.QUNIFPC); + domain.addDistributionParam("coefficient", Double.valueOf(percentageElement.attributeValue("coefficient"))); + domain.addDistributionParam("reference", Double.valueOf(percentageElement.attributeValue("reference"))); } else { // <range max="1" min="3"/> - Element rangeElement = fixedElement.element(RANGE); - domain.setMinBound(Integer.valueOf(rangeElement.attributeValue(MIN))); - domain.setMaxBound(Integer.valueOf(rangeElement.attributeValue(MAX))); + domain = new ContinuousDomain(Distribution.QUNIFMM); + Element rangeElement = fixedElement.element("range"); + domain.addDistributionParam("min", Integer.valueOf(rangeElement.attributeValue("min"))); + domain.addDistributionParam("max", Integer.valueOf(rangeElement.attributeValue("max"))); } - } - factor.setCardinality(Integer.valueOf(fixedElement.attributeValue(CARDINALITY))); + factor.setCardinality(Integer.valueOf(fixedElement.attributeValue("cardinality"))); factor.setDomain(domain); - } else if (DISCRETE.equals(property)) { + } else if ("discrete".equals(property)) { DiscreteDomain domain = new DiscreteDomain(); List<Element> valueElements = fixedElement.element( - ENUMERATION).elements(VALUE); + "enumeration").elements("value"); int label = 0; for (Element valueElement : valueElements) { domain.getValues().put(label, @@ -288,16 +264,16 @@ factor.setDomain(domain); } factorGroup.addFactor(factor); - } else if (RULE.equals(type)) { + } else if ("rule".equals(type)) { Factor factor = new Factor(name); factor.setPath(path); - Element fixedElement = factorElement.element(DOMAIN).element(FIXED); - if (DISCRETE.equals(property)) { + Element fixedElement = factorElement.element("domain").element("fixed"); + if ("discrete".equals(property)) { RuleDiscreteDomain domain = new RuleDiscreteDomain(); - List<Element> valueElements = fixedElement.element(ENUMERATION).elements(VALUE); + List<Element> valueElements = fixedElement.element("enumeration").elements("value"); int label = 0; for (Element valueElement : valueElements) { - Element rulesElement = valueElement.element(RULES); + Element rulesElement = valueElement.element("rules"); List<Rule> rulesValue = MexicoHelper.getRulesFromXml(rulesElement, topiaContext); domain.getValues().put(label, rulesValue); ++label; @@ -306,7 +282,7 @@ } factorGroup.addFactor(factor); } - }*/ + } return factorGroup; } @@ -327,12 +303,12 @@ */ protected static FactorGroup parseFactorGroupV2(Element fgElement, TopiaContext topiaContext) { - String factorGroupName = fgElement.attributeValue(NAME); + String factorGroupName = fgElement.attributeValue("name"); String factorGroupType = fgElement.attributeValue("type"); boolean continuous = "continuous".equalsIgnoreCase(factorGroupType); FactorGroup factorGroup = new FactorGroup(factorGroupName, continuous); - /*// sub factor group + // sub factor group List<Element> factorGroupElements = fgElement.selectNodes("child::factors"); for (Element factorGroupElement : factorGroupElements) { FactorGroup subFactorGroup = parseFactorGroupV2(factorGroupElement, topiaContext); @@ -343,11 +319,11 @@ List<Element> factorElements = fgElement.selectNodes("child::factor"); for (Element factorElement : factorElements) { - String type = factorElement.attributeValue(TYPE); - String name = factorElement.attributeValue(NAME); - String property = factorElement.attributeValue(PROPERTY); - String path = factorElement.element(TARGET).getText().trim(); - String cardinalityString = factorElement.attributeValue(CARDINALITY); + String type = factorElement.attributeValue("type"); + String name = factorElement.attributeValue("name"); + String property = factorElement.attributeValue("property"); + String path = factorElement.element("target").getTextTrim(); + String cardinalityString = factorElement.attributeValue("cardinality"); Integer cardinality = 0; if (StringUtils.isNotEmpty(cardinalityString)) { cardinality = Integer.valueOf(cardinalityString); @@ -357,82 +333,83 @@ if ("real".equals(type)) { Factor factor = new Factor(name); factor.setPath(path); - Element fixedElement = factorElement.element(DOMAIN).element(FIXED); + Element fixedElement = factorElement.element("domain").element("fixed"); // tous les domaines continues - if (property.endsWith(CONTINUOUS)) { + if (property.endsWith("continuous")) { ContinuousDomain domain; - if(property.equals(MATRIXCONTINUOUS)) { - - MatrixContinuousDomain mdomain = new MatrixContinuousDomain(); - Element referenceElement = fixedElement.element(REFERENCE); + if(property.equals("matrixcontinuous")) { + + Element referenceElement = fixedElement.element("reference"); if (referenceElement != null) { + domain = new ContinuousDomain(Distribution.QUNIFPC); // matrix specific // <coefficient operator="-" value="0.799"/> - mdomain.setCoefficient(Double.valueOf(referenceElement.attributeValue(COEFFICIENT))); + domain.addDistributionParam("coefficient", Double.valueOf(referenceElement.attributeValue("coefficient"))); // <mx name="test1" step="0">... - Element matrixElement = referenceElement.element(MX); + Element matrixElement = referenceElement.element("mx"); MatrixND matrix = MexicoHelper.getMatrixFromXml(matrixElement, topiaContext); - mdomain.setReferenceValue(matrix); - mdomain.setPercentageType(true); + domain.addDistributionParam("reference", matrix); } else { - Element rangeElement = fixedElement.element(RANGE); - Element minElement = rangeElement.element(MIN); - Element maxElement = rangeElement.element(MAX); - MatrixND minMatrix = MexicoHelper.getMatrixFromXml(minElement.element(MX), topiaContext); - MatrixND maxMatrix = MexicoHelper.getMatrixFromXml(maxElement.element(MX), topiaContext); - mdomain.setMinBound(minMatrix); - mdomain.setMaxBound(maxMatrix); + domain = new ContinuousDomain(Distribution.QUNIFMM); + Element rangeElement = fixedElement.element("range"); + Element minElement = rangeElement.element("min"); + Element maxElement = rangeElement.element("max"); + MatrixND minMatrix = MexicoHelper.getMatrixFromXml(minElement.element("mx"), topiaContext); + MatrixND maxMatrix = MexicoHelper.getMatrixFromXml(maxElement.element("mx"), topiaContext); + domain.addDistributionParam("min", minMatrix); + domain.addDistributionParam("max", maxMatrix); } - - domain = mdomain; } - else if (property.equals(EQUATIONCONTINUOUS)) { + else if (property.equals("equationcontinuos")) { // equation specific - EquationContinuousDomain edomain = new EquationContinuousDomain(); - edomain.setVariableName(fixedElement.attributeValue(VARIABLE)); - Element referenceElement = fixedElement.element(REFERENCE); + + String variable = fixedElement.attributeValue("variable"); + factor.setEquationVariableName(variable); + // hack since 4.0.1, factor name doesn't contains variable + name = StringUtils.removeEnd(name, "." + variable); + factor.setName(name); + + Element referenceElement = fixedElement.element("reference"); if (referenceElement != null) { - edomain.setCoefficient(Double.valueOf(referenceElement.attributeValue(COEFFICIENT))); - edomain.setReferenceValue(Double.valueOf(referenceElement.getTextTrim())); - edomain.setPercentageType(true); + domain = new ContinuousDomain(Distribution.QUNIFPC); + domain.addDistributionParam("coefficient", Double.valueOf(referenceElement.attributeValue("coefficient"))); + domain.addDistributionParam("reference", Double.valueOf(referenceElement.getTextTrim())); } else { - Element rangeElement = fixedElement.element(RANGE); - Element minElement = rangeElement.element(MIN); - Element maxElement = rangeElement.element(MAX); - edomain.setMinBound(Double.valueOf(minElement.getTextTrim())); - edomain.setMaxBound(Double.valueOf(maxElement.getTextTrim())); + domain = new ContinuousDomain(Distribution.QUNIFMM); + Element rangeElement = fixedElement.element("range"); + Element minElement = rangeElement.element("min"); + Element maxElement = rangeElement.element("max"); + domain.addDistributionParam("min", minElement.getTextTrim()); + domain.addDistributionParam("max", maxElement.getTextTrim()); } - - domain = edomain; } else { // continous domain - domain = new ContinuousDomain(); - - Element referenceElement = fixedElement.element(REFERENCE); + Element referenceElement = fixedElement.element("reference"); if (referenceElement != null) { - domain.setCoefficient(Double.valueOf(referenceElement.attributeValue(COEFFICIENT))); - domain.setReferenceValue(Double.valueOf(referenceElement.getTextTrim())); - domain.setPercentageType(true); + domain = new ContinuousDomain(Distribution.QUNIFPC); + domain.addDistributionParam("coefficient", Double.valueOf(referenceElement.attributeValue("coefficient"))); + domain.addDistributionParam("reference", Double.valueOf(referenceElement.getTextTrim())); } else { // <range max="1.0" min="0.0"/> - Element rangeElement = fixedElement.element(RANGE); - Element minElement = rangeElement.element(MIN); - Element maxElement = rangeElement.element(MAX); - domain.setMinBound(Double.valueOf(minElement.getTextTrim())); - domain.setMaxBound(Double.valueOf(maxElement.getTextTrim())); + domain = new ContinuousDomain(Distribution.QUNIFMM); + Element rangeElement = fixedElement.element("range"); + Element minElement = rangeElement.element("min"); + Element maxElement = rangeElement.element("max"); + domain.addDistributionParam("min", Double.valueOf(minElement.getTextTrim())); + domain.addDistributionParam("max", Double.valueOf(maxElement.getTextTrim())); } } factor.setDomain(domain); - } else if (DISCRETE.equals(property)) { + } else if ("discrete".equals(property)) { DiscreteDomain domain = new DiscreteDomain(); List<Element> valueElements = fixedElement.element( - ENUMERATION).elements(VALUE); + "enumeration").elements("value"); int label = 0; for (Element valueElement : valueElements) { domain.getValues().put(label, @@ -443,84 +420,85 @@ } factor.setCardinality(cardinality); factorGroup.addFactor(factor); - } else if (INTEGER.equals(type)) { + } else if ("integer".equals(type)) { Factor factor = new Factor(name); factor.setPath(path); - Element fixedElement = factorElement.element(DOMAIN).element(FIXED); - if (CONTINUOUS.equals(property)) { + Element fixedElement = factorElement.element("domain").element("fixed"); + if ("continuous".equals(property)) { ContinuousDomain domain; - if(property.equals(MATRIXCONTINUOUS)) { + if(property.equals("matrixcontinuous")) { - MatrixContinuousDomain mdomain = new MatrixContinuousDomain(); - Element referenceElement = fixedElement.element(REFERENCE); + + Element referenceElement = fixedElement.element("reference"); if (referenceElement != null) { + domain = new ContinuousDomain(Distribution.QUNIFPC); // matrix specific // <coefficient operator="-" value="0.799"/> - mdomain.setCoefficient(Double.valueOf(referenceElement.attributeValue(COEFFICIENT))); + domain.addDistributionParam("coefficient", Double.valueOf(referenceElement.attributeValue("coefficient"))); // <mx name="test1" step="0">... - Element matrixElement = referenceElement.element(MX); + Element matrixElement = referenceElement.element("mx"); MatrixND matrix = MexicoHelper.getMatrixFromXml(matrixElement, topiaContext); - mdomain.setReferenceValue(matrix); - mdomain.setPercentageType(true); + domain.addDistributionParam("reference", matrix); } else { - Element rangeElement = fixedElement.element(RANGE); - Element minElement = rangeElement.element(MIN); - Element maxElement = rangeElement.element(MAX); - MatrixND minMatrix = MexicoHelper.getMatrixFromXml(minElement.element(MX), topiaContext); - MatrixND maxMatrix = MexicoHelper.getMatrixFromXml(maxElement.element(MX), topiaContext); - mdomain.setMinBound(minMatrix); - mdomain.setMaxBound(maxMatrix); + domain = new ContinuousDomain(Distribution.QUNIFMM); + Element rangeElement = fixedElement.element("range"); + Element minElement = rangeElement.element("min"); + Element maxElement = rangeElement.element("max"); + MatrixND minMatrix = MexicoHelper.getMatrixFromXml(minElement.element("mx"), topiaContext); + MatrixND maxMatrix = MexicoHelper.getMatrixFromXml(maxElement.element("mx"), topiaContext); + domain.addDistributionParam("min", minMatrix); + domain.addDistributionParam("max", maxMatrix); } - - domain = mdomain; } - else if (property.equals(EQUATIONCONTINUOUS)) { + else if (property.equals("equationcontinuous")) { // equation specific - EquationContinuousDomain edomain = new EquationContinuousDomain(); - edomain.setVariableName(fixedElement.attributeValue(VARIABLE)); - Element referenceElement = fixedElement.element(REFERENCE); + String variable = fixedElement.attributeValue("variable"); + factor.setEquationVariableName(variable); + // hack since 4.0.1, factor name doesn't contains variable + name = StringUtils.removeEnd(name, "." + variable); + factor.setName(name); + + Element referenceElement = fixedElement.element("reference"); if (referenceElement != null) { - edomain.setCoefficient(Double.valueOf(referenceElement.attributeValue(COEFFICIENT))); - edomain.setReferenceValue(Double.valueOf(referenceElement.getTextTrim())); - edomain.setPercentageType(true); + domain = new ContinuousDomain(Distribution.QUNIFPC); + domain.addDistributionParam("coefficient", Double.valueOf(referenceElement.attributeValue("coefficient"))); + domain.addDistributionParam("reference", Double.valueOf(referenceElement.getTextTrim())); } else { - Element rangeElement = fixedElement.element(RANGE); - Element minElement = rangeElement.element(MIN); - Element maxElement = rangeElement.element(MAX); - edomain.setMinBound(Double.valueOf(minElement.getTextTrim())); - edomain.setMaxBound(Double.valueOf(maxElement.getTextTrim())); + domain = new ContinuousDomain(Distribution.QUNIFMM); + Element rangeElement = fixedElement.element("range"); + Element minElement = rangeElement.element("min"); + Element maxElement = rangeElement.element("max"); + domain.addDistributionParam("min", Double.valueOf(minElement.getTextTrim())); + domain.addDistributionParam("max", Double.valueOf(maxElement.getTextTrim())); } - - domain = edomain; } else { // continous domain - domain = new ContinuousDomain(); - - Element referenceElement = fixedElement.element(REFERENCE); + Element referenceElement = fixedElement.element("reference"); if (referenceElement != null) { - domain.setCoefficient(Double.valueOf(referenceElement.attributeValue(COEFFICIENT))); - domain.setReferenceValue(Double.valueOf(referenceElement.getTextTrim())); - domain.setPercentageType(true); + domain = new ContinuousDomain(Distribution.QUNIFPC); + domain.addDistributionParam("coefficient", Double.valueOf(referenceElement.attributeValue("coefficient"))); + domain.addDistributionParam("reference", Double.valueOf(referenceElement.getTextTrim())); } else { // <range max="1.0" min="0.0"/> - Element rangeElement = fixedElement.element(RANGE); - Element minElement = rangeElement.element(MIN); - Element maxElement = rangeElement.element(MAX); - domain.setMinBound(Integer.valueOf(minElement.getTextTrim())); - domain.setMaxBound(Integer.valueOf(maxElement.getTextTrim())); + domain = new ContinuousDomain(Distribution.QUNIFMM); + Element rangeElement = fixedElement.element("range"); + Element minElement = rangeElement.element("min"); + Element maxElement = rangeElement.element("max"); + domain.addDistributionParam("min", Integer.valueOf(minElement.getTextTrim())); + domain.addDistributionParam("max", Integer.valueOf(maxElement.getTextTrim())); } } factor.setDomain(domain); - } else if (DISCRETE.equals(property)) { + } else if ("discrete".equals(property)) { DiscreteDomain domain = new DiscreteDomain(); List<Element> valueElements = fixedElement.element( - ENUMERATION).elements(VALUE); + "enumeration").elements("value"); int label = 0; for (Element valueElement : valueElements) { domain.getValues().put(label, @@ -531,16 +509,16 @@ } factor.setCardinality(cardinality); factorGroup.addFactor(factor); - } else if (RULE.equals(type)) { + } else if ("rule".equals(type)) { Factor factor = new Factor(name); factor.setPath(path); - Element fixedElement = factorElement.element(DOMAIN).element(FIXED); - if (DISCRETE.equals(property)) { + Element fixedElement = factorElement.element("domain").element("fixed"); + if ("discrete".equals(property)) { RuleDiscreteDomain domain = new RuleDiscreteDomain(); - List<Element> valueElements = fixedElement.element(ENUMERATION).elements(VALUE); + List<Element> valueElements = fixedElement.element("enumeration").elements("value"); int label = 0; for (Element valueElement : valueElements) { - Element rulesElement = valueElement.element(RULES); + Element rulesElement = valueElement.element("rules"); List<Rule> rulesValue = MexicoHelper.getRulesFromXml(rulesElement, topiaContext); domain.getValues().put(label, rulesValue); ++label; @@ -549,13 +527,13 @@ } factor.setCardinality(cardinality); factorGroup.addFactor(factor); - } else if (EQUATION.equals(type)) { + } else if ("equation".equals(type)) { Factor factor = new Factor(name); factor.setPath(path); - Element fixedElement = factorElement.element(DOMAIN).element(FIXED); - if (DISCRETE.equals(property)) { + Element fixedElement = factorElement.element("domain").element("fixed"); + if ("discrete".equals(property)) { EquationDiscreteDomain domain = new EquationDiscreteDomain(); - List<Element> valueElements = fixedElement.element(ENUMERATION).elements(VALUE); + List<Element> valueElements = fixedElement.element("enumeration").elements("value"); int label = 0; for (Element valueElement : valueElements) { String content = StringEscapeUtils.unescapeXml(valueElement.getText()); @@ -569,10 +547,10 @@ } else if ("string".equals(type)) { Factor factor = new Factor(name); factor.setPath(path); - Element fixedElement = factorElement.element(DOMAIN).element(FIXED); - if (DISCRETE.equals(property)) { + Element fixedElement = factorElement.element("domain").element("fixed"); + if ("discrete".equals(property)) { DiscreteDomain domain = new DiscreteDomain(); - List<Element> valueElements = fixedElement.element(ENUMERATION).elements(VALUE); + List<Element> valueElements = fixedElement.element("enumeration").elements("value"); int label = 0; for (Element valueElement : valueElements) { String content = valueElement.getText(); @@ -585,17 +563,20 @@ factor.setCardinality(cardinality); factorGroup.addFactor(factor); } - }*/ + } return factorGroup; } /** - * Recursive parse of factor group elements (>factors<). + * Non recursive parse of factor group elements (>factors<). * * This version handle xml file distribution definition in each continuous * factors. * + * The parsing is more complicated here because mexico file format is a bit + * hard too read (factor groups, features...) + * * @param fgElement factor group element (>factors<) * @param topiaContext context * @return factor group @@ -603,266 +584,65 @@ */ protected static FactorGroup parseFactorGroupV3(Element fgElement, TopiaContext topiaContext) { - String factorGroupName = fgElement.attributeValue(NAME); - String factorGroupType = fgElement.attributeValue("type"); - boolean continuous = "continuous".equalsIgnoreCase(factorGroupType); - FactorGroup factorGroup = new FactorGroup(factorGroupName, continuous); + FactorGroup rootGroup = new FactorGroup(null); // root group + Map<String, FactorGroup> groups = new HashMap<String, FactorGroup>(); - /*// sub factor group - List<Element> factorGroupElements = fgElement.selectNodes("child::factors"); - for (Element factorGroupElement : factorGroupElements) { - FactorGroup subFactorGroup = parseFactorGroupV3(factorGroupElement, topiaContext); - factorGroup.addFactor(subFactorGroup); - } - // normal factors List<Element> factorElements = fgElement.selectNodes("child::factor"); - for (Element factorElement : factorElements) { - String type = factorElement.attributeValue(TYPE); - String name = factorElement.attributeValue(NAME); - String property = factorElement.attributeValue(PROPERTY); - String path = factorElement.element(TARGET).getText().trim(); - String cardinalityString = factorElement.attributeValue(CARDINALITY); - Integer cardinality = 0; + String name = factorElement.attributeValue("name"); + String cardinalityString = fgElement.element("child::feature[@name='cardinality']").getTextTrim(); + String path = fgElement.element("child::feature[@name='target']").getTextTrim(); + String groupName = fgElement.element("child::feature[@name='group']").getTextTrim(); + String groupType = fgElement.element("child::feature[@name='grouptype']").getTextTrim(); + + Factor factor = new Factor(name); + factor.setPath(path); if (StringUtils.isNotEmpty(cardinalityString)) { - cardinality = Integer.valueOf(cardinalityString); + factor.setCardinality(Integer.valueOf(cardinalityString)); } - // double - if ("real".equals(type)) { - Factor factor = new Factor(name); - factor.setPath(path); - Element fixedElement = factorElement.element(DOMAIN).element(FIXED); - // tous les domaines continues - if (property.endsWith(CONTINUOUS)) { - - ContinuousDomain domain; - if(property.equals(MATRIXCONTINUOUS)) { - - MatrixContinuousDomain mdomain = new MatrixContinuousDomain(); - Element referenceElement = fixedElement.element(REFERENCE); - if (referenceElement != null) { - // matrix specific - // <coefficient operator="-" value="0.799"/> - mdomain.setCoefficient(Double.valueOf(referenceElement.attributeValue(COEFFICIENT))); - // <mx name="test1" step="0">... - Element matrixElement = referenceElement.element(MX); - MatrixND matrix = MexicoHelper.getMatrixFromXml(matrixElement, topiaContext); - mdomain.setReferenceValue(matrix); - mdomain.setPercentageType(true); - } - else { - Element rangeElement = fixedElement.element(RANGE); - Element minElement = rangeElement.element(MIN); - Element maxElement = rangeElement.element(MAX); - MatrixND minMatrix = MexicoHelper.getMatrixFromXml(minElement.element(MX), topiaContext); - MatrixND maxMatrix = MexicoHelper.getMatrixFromXml(maxElement.element(MX), topiaContext); - mdomain.setMinBound(minMatrix); - mdomain.setMaxBound(maxMatrix); - } - - domain = mdomain; + // parse domain + Element domainElement = factorElement.element("domain"); + String distributionName = domainElement.attributeValue("distributionName"); + String type = domainElement.attributeValue("type"); + Domain domain; + // facteur discret + if ("categorical".equals(distributionName)) { + if ("rule".equals(type)) { + + } + } else if ("sequence".equals(distributionName)) { + // can happen in mexico, but not in isis + // skip this one + if (log.isInfoEnabled()) { + log.info(String.format("Skip factor %s : unknown distribution name : %s !", name, distributionName)); + } + } else { + // facteur continuous with custom distribution + Distribution distribution = Distribution.valueOf(distributionName.toUpperCase()); + if (distribution == null) { + if (log.isWarnEnabled()) { + log.warn(String.format("Skip factor %s : can't found distribution name : %s !", name, distributionName)); } - else if (property.equals(EQUATIONCONTINUOUS)) { - // equation specific - EquationContinuousDomain edomain = new EquationContinuousDomain(); - edomain.setVariableName(fixedElement.attributeValue(VARIABLE)); - Element referenceElement = fixedElement.element(REFERENCE); - if (referenceElement != null) { - edomain.setCoefficient(Double.valueOf(referenceElement.attributeValue(COEFFICIENT))); - edomain.setReferenceValue(Double.valueOf(referenceElement.getTextTrim())); - edomain.setPercentageType(true); - } - else { - Element rangeElement = fixedElement.element(RANGE); - Element minElement = rangeElement.element(MIN); - Element maxElement = rangeElement.element(MAX); - edomain.setMinBound(Double.valueOf(minElement.getTextTrim())); - edomain.setMaxBound(Double.valueOf(maxElement.getTextTrim())); - } - - domain = edomain; - } - else { - // continous domain - domain = new ContinuousDomain(); - - Element referenceElement = fixedElement.element(REFERENCE); - if (referenceElement != null) { - domain.setCoefficient(Double.valueOf(referenceElement.attributeValue(COEFFICIENT))); - domain.setReferenceValue(Double.valueOf(referenceElement.getTextTrim())); - domain.setPercentageType(true); - } - else { - // <range max="1.0" min="0.0"/> - Element rangeElement = fixedElement.element(RANGE); - Element minElement = rangeElement.element(MIN); - Element maxElement = rangeElement.element(MAX); - domain.setMinBound(Double.valueOf(minElement.getTextTrim())); - domain.setMaxBound(Double.valueOf(maxElement.getTextTrim())); - } - } - - factor.setDomain(domain); - } else if (DISCRETE.equals(property)) { - DiscreteDomain domain = new DiscreteDomain(); - List<Element> valueElements = fixedElement.element( - ENUMERATION).elements(VALUE); - int label = 0; - for (Element valueElement : valueElements) { - domain.getValues().put(label, - Double.valueOf(valueElement.getTextTrim())); - ++label; - } - factor.setDomain(domain); + continue; } - factor.setCardinality(cardinality); - factorGroup.addFactor(factor); - } else if (INTEGER.equals(type)) { - Factor factor = new Factor(name); - factor.setPath(path); - Element fixedElement = factorElement.element(DOMAIN).element(FIXED); - if (CONTINUOUS.equals(property)) { - ContinuousDomain domain; + } - if(property.equals(MATRIXCONTINUOUS)) { - - MatrixContinuousDomain mdomain = new MatrixContinuousDomain(); - Element referenceElement = fixedElement.element(REFERENCE); - if (referenceElement != null) { - // matrix specific - // <coefficient operator="-" value="0.799"/> - mdomain.setCoefficient(Double.valueOf(referenceElement.attributeValue(COEFFICIENT))); - // <mx name="test1" step="0">... - Element matrixElement = referenceElement.element(MX); - MatrixND matrix = MexicoHelper.getMatrixFromXml(matrixElement, topiaContext); - mdomain.setReferenceValue(matrix); - mdomain.setPercentageType(true); - } - else { - Element rangeElement = fixedElement.element(RANGE); - Element minElement = rangeElement.element(MIN); - Element maxElement = rangeElement.element(MAX); - MatrixND minMatrix = MexicoHelper.getMatrixFromXml(minElement.element(MX), topiaContext); - MatrixND maxMatrix = MexicoHelper.getMatrixFromXml(maxElement.element(MX), topiaContext); - mdomain.setMinBound(minMatrix); - mdomain.setMaxBound(maxMatrix); - } - domain = mdomain; - } - else if (property.equals(EQUATIONCONTINUOUS)) { - // equation specific - EquationContinuousDomain edomain = new EquationContinuousDomain(); - edomain.setVariableName(fixedElement.attributeValue(VARIABLE)); - Element referenceElement = fixedElement.element(REFERENCE); - if (referenceElement != null) { - edomain.setCoefficient(Double.valueOf(referenceElement.attributeValue(COEFFICIENT))); - edomain.setReferenceValue(Double.valueOf(referenceElement.getTextTrim())); - edomain.setPercentageType(true); - } - else { - Element rangeElement = fixedElement.element(RANGE); - Element minElement = rangeElement.element(MIN); - Element maxElement = rangeElement.element(MAX); - edomain.setMinBound(Double.valueOf(minElement.getTextTrim())); - edomain.setMaxBound(Double.valueOf(maxElement.getTextTrim())); - } - - domain = edomain; - } - else { - // continous domain - domain = new ContinuousDomain(); - - Element referenceElement = fixedElement.element(REFERENCE); - if (referenceElement != null) { - domain.setCoefficient(Double.valueOf(referenceElement.attributeValue(COEFFICIENT))); - domain.setReferenceValue(Double.valueOf(referenceElement.getTextTrim())); - domain.setPercentageType(true); - } - else { - // <range max="1.0" min="0.0"/> - Element rangeElement = fixedElement.element(RANGE); - Element minElement = rangeElement.element(MIN); - Element maxElement = rangeElement.element(MAX); - domain.setMinBound(Integer.valueOf(minElement.getTextTrim())); - domain.setMaxBound(Integer.valueOf(maxElement.getTextTrim())); - } - } - - factor.setDomain(domain); - } else if (DISCRETE.equals(property)) { - DiscreteDomain domain = new DiscreteDomain(); - List<Element> valueElements = fixedElement.element( - ENUMERATION).elements(VALUE); - int label = 0; - for (Element valueElement : valueElements) { - domain.getValues().put(label, - Integer.valueOf(valueElement.getTextTrim())); - ++label; - } - factor.setDomain(domain); + // add factor to existing or new group + FactorGroup group = rootGroup; + if (StringUtils.isNotBlank(groupName)) { + group = groups.get(group); + if (group == null) { + group = new FactorGroup(groupName, "continuous".equals(groupType)); + groups.put(groupName, group); + rootGroup.addFactor(group); } - factor.setCardinality(cardinality); - factorGroup.addFactor(factor); - } else if (RULE.equals(type)) { - Factor factor = new Factor(name); - factor.setPath(path); - Element fixedElement = factorElement.element(DOMAIN).element(FIXED); - if (DISCRETE.equals(property)) { - RuleDiscreteDomain domain = new RuleDiscreteDomain(); - List<Element> valueElements = fixedElement.element(ENUMERATION).elements(VALUE); - int label = 0; - for (Element valueElement : valueElements) { - Element rulesElement = valueElement.element(RULES); - List<Rule> rulesValue = MexicoHelper.getRulesFromXml(rulesElement, topiaContext); - domain.getValues().put(label, rulesValue); - ++label; - } - factor.setDomain(domain); - } - factor.setCardinality(cardinality); - factorGroup.addFactor(factor); - } else if (EQUATION.equals(type)) { - Factor factor = new Factor(name); - factor.setPath(path); - Element fixedElement = factorElement.element(DOMAIN).element(FIXED); - if (DISCRETE.equals(property)) { - EquationDiscreteDomain domain = new EquationDiscreteDomain(); - List<Element> valueElements = fixedElement.element(ENUMERATION).elements(VALUE); - int label = 0; - for (Element valueElement : valueElements) { - String content = StringEscapeUtils.unescapeXml(valueElement.getText()); - domain.getValues().put(label, content); - ++label; - } - factor.setDomain(domain); - } - factor.setCardinality(cardinality); - factorGroup.addFactor(factor); - } else if ("string".equals(type)) { - Factor factor = new Factor(name); - factor.setPath(path); - Element fixedElement = factorElement.element(DOMAIN).element(FIXED); - if (DISCRETE.equals(property)) { - DiscreteDomain domain = new DiscreteDomain(); - List<Element> valueElements = fixedElement.element(ENUMERATION).elements(VALUE); - int label = 0; - for (Element valueElement : valueElements) { - String content = valueElement.getText(); - Object object = MexicoHelper.getObjectFromString(content, topiaContext); - domain.getValues().put(label, object); - ++label; - } - factor.setDomain(domain); - } - factor.setCardinality(cardinality); - factorGroup.addFactor(factor); } - }*/ + group.addFactor(factor); + } - return factorGroup; + return rootGroup; } } Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DomainXMLVisitor.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DomainXMLVisitor.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/DomainXMLVisitor.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -25,7 +25,6 @@ package fr.ifremer.isisfish.mexico.xml; -import fr.ifremer.isisfish.simulator.sensitivity.Domain; import fr.ifremer.isisfish.simulator.sensitivity.visitor.DomainVisitor; /** @@ -39,19 +38,4 @@ */ public abstract class DomainXMLVisitor extends AbstractXMLVisitor implements DomainVisitor { - /** - * {@inheritDoc} - */ - @Override - public void start(Domain domain) { - xmlBuffer.append("<domain>"); - } - - /** - * {@inheritDoc} - */ - @Override - public void end(Domain domain) { - xmlBuffer.append("</domain>"); - } } Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/EquationDiscreteDomainXMLVisitor.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/EquationDiscreteDomainXMLVisitor.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/EquationDiscreteDomainXMLVisitor.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -40,6 +40,13 @@ */ public class EquationDiscreteDomainXMLVisitor extends DiscreteDomainXMLVisitor { + @Override + public void start(Domain domain) { + // lorsque le facteur est catégoriel, la liste de ses niveaux, par une + // liste ouverte d'éléments 'level', d'attributs 'value' et 'weight') + xmlBuffer.append("<domain distributionName=\"categorical\" type=\"equation\""); + } + /** * {@inheritDoc} */ Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/FactorGroupXMLVisitor.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/FactorGroupXMLVisitor.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/FactorGroupXMLVisitor.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2011 Ifremer, CodeLutin, Chatellier Eric + * Copyright (C) 2011 - 2012 Ifremer, CodeLutin, 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 @@ -45,16 +45,7 @@ */ @Override public void start(FactorGroup factorGroup) { - xmlBuffer.append("<factors"); - if (factorGroup.getName() != null) { - xmlBuffer.append(" name=\"" + factorGroup.getName() + "\""); - } - if (factorGroup.isContinuous()) { - xmlBuffer.append(" type=\"continuous\""); - } else { - xmlBuffer.append(" type=\"discrete\""); - } - xmlBuffer.append(">"); + } /* @@ -64,12 +55,10 @@ public void visit(FactorGroup factorGroup, Factor factor) { if (factor instanceof FactorGroup) { FactorGroup factorGroup2 = (FactorGroup)factor; - FactorGroupXMLVisitor visitor = new FactorGroupXMLVisitor(); - factorGroup2.accept(visitor); - xmlBuffer.append(visitor.getXML()); + factorGroup2.accept(this); } else { - FactorXMLVisitor visitor = new FactorXMLVisitor(); + FactorXMLVisitor visitor = new FactorXMLVisitor(factorGroup); factor.accept(visitor); xmlBuffer.append(visitor.getXML()); } @@ -80,6 +69,6 @@ */ @Override public void end(FactorGroup factorGroup) { - xmlBuffer.append("</factors>"); + } } Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/FactorXMLVisitor.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/FactorXMLVisitor.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/FactorXMLVisitor.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2009 - 2010 Ifremer, CodeLutin, Chatellier Eric + * Copyright (C) 2009 - 2012 Ifremer, CodeLutin, 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 @@ -25,8 +25,11 @@ package fr.ifremer.isisfish.mexico.xml; +import org.apache.commons.lang3.StringUtils; + import fr.ifremer.isisfish.simulator.sensitivity.Domain; import fr.ifremer.isisfish.simulator.sensitivity.Factor; +import fr.ifremer.isisfish.simulator.sensitivity.FactorGroup; import fr.ifremer.isisfish.simulator.sensitivity.domain.ContinuousDomain; import fr.ifremer.isisfish.simulator.sensitivity.domain.DiscreteDomain; import fr.ifremer.isisfish.simulator.sensitivity.domain.EquationDiscreteDomain; @@ -44,28 +47,57 @@ */ public class FactorXMLVisitor extends AbstractXMLVisitor implements FactorVisitor { + protected FactorGroup group; + + /** + * Factor constructor with group (for mexico features). + * + * @param group group + */ + public FactorXMLVisitor(FactorGroup group) { + this.group = group; + } + /* * @see fr.ifremer.isisfish.simulator.sensitivity.visitor.FactorVisitor#start(fr.ifremer.isisfish.simulator.sensitivity.Factor) */ @Override public void start(Factor factor) { + + // required by mexico, must be unique, but not used by isis + String factorId = factor.getPath(); + if (StringUtils.isNotBlank(factor.getEquationVariableName())) { + factorId += "." + factor.getEquationVariableName(); + } + xmlBuffer.append("<factor"); + xmlBuffer.append(" id=\"").append(factorId).append("\""); xmlBuffer.append(" name=\"").append(factor.getName()).append("\""); + if (StringUtils.isNotBlank(factor.getComment())) { + xmlBuffer.append(" description=\"").append(factor.getComment()).append("\""); + } + xmlBuffer.append(">"); + // other things are features //cardinality - xmlBuffer.append(" cardinality=\""); - xmlBuffer.append(factor.getCardinality()); - xmlBuffer.append("\""); - - xmlBuffer.append(">"); - // target element - xmlBuffer.append("<target>").append(factor.getPath()).append("</target>"); - // value element - /* TODO value should be only in simulation export, - not in design plan export - if (factor.getValue() != null) { - xmlBuffer.append("<value>" + factor.getValue() + "</value>"); - }*/ + xmlBuffer.append("<feature name=\"cardinality\">" + factor.getCardinality() + "</feature>"); + // group + if (StringUtils.isNotBlank(group.getName())) { + xmlBuffer.append("<feature name=\"group\">" + group.getName() + "</feature>"); + xmlBuffer.append("<feature name=\"grouptype\">"); + if (group.isContinuous()) { + xmlBuffer.append("continous"); + } else { + xmlBuffer.append("discrete"); + } + xmlBuffer.append("</feature>"); + } + //path + xmlBuffer.append("<feature name=\"target\">" + factor.getPath() + "</feature>"); + // equation variable name + if (StringUtils.isNotBlank(factor.getEquationVariableName())) { + xmlBuffer.append("<feature name=\"equationVariableName\">" + factor.getEquationVariableName() + "</feature>"); + } } /* Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/RuleDiscreteDomainXMLVisitor.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/RuleDiscreteDomainXMLVisitor.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/RuleDiscreteDomainXMLVisitor.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2011 Ifremer, CodeLutin, Chatellier Eric + * Copyright (C) 2011 - 2012 Ifremer, CodeLutin, 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 @@ -42,6 +42,13 @@ */ public class RuleDiscreteDomainXMLVisitor extends DiscreteDomainXMLVisitor { + @Override + public void start(Domain domain) { + // lorsque le facteur est catégoriel, la liste de ses niveaux, par une + // liste ouverte d'éléments 'level', d'attributs 'value' et 'weight') + xmlBuffer.append("<domain distributionName=\"categorical\" type=\"rule\""); + } + /** * {@inheritDoc} */ Deleted: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/ScenarioXMLVisitor.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/ScenarioXMLVisitor.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/ScenarioXMLVisitor.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -1,68 +0,0 @@ -/* - * #%L - * IsisFish - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2010 Ifremer, CodeLutin, 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, see - * <http://www.gnu.org/licenses/gpl-2.0.html>. - * #L% - */ - -package fr.ifremer.isisfish.mexico.xml; - -import fr.ifremer.isisfish.simulator.sensitivity.Factor; -import fr.ifremer.isisfish.simulator.sensitivity.Scenario; -import fr.ifremer.isisfish.simulator.sensitivity.visitor.ScenarioVisitor; - -/** - * XML domain visitor. - * - * @author chatellier - * @version $Revision: 1.0 $ - * - * Last update : $Date: 5 mars 2009 $ - * By : $Author: chatellier $ - */ -public class ScenarioXMLVisitor extends AbstractXMLVisitor implements ScenarioVisitor { - - /** - * {@inheritDoc} - */ - @Override - public void start(Scenario scenario) { - xmlBuffer.append("<scenario>"); - } - - /** - * {@inheritDoc} - */ - @Override - public void visit(Scenario scenario, Factor factor) { - FactorXMLVisitor visitor = new FactorXMLVisitor(); - factor.accept(visitor); - xmlBuffer.append(visitor.getXML()); - } - - /** - * {@inheritDoc} - */ - @Override - public void end(Scenario scenario) { - xmlBuffer.append("</scenario>"); - } -} Deleted: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/SensitivityScenariosXMLVisitor.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/SensitivityScenariosXMLVisitor.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/SensitivityScenariosXMLVisitor.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -1,70 +0,0 @@ -/* - * #%L - * IsisFish - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2010 Ifremer, CodeLutin, 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, see - * <http://www.gnu.org/licenses/gpl-2.0.html>. - * #L% - */ - -package fr.ifremer.isisfish.mexico.xml; - -import fr.ifremer.isisfish.simulator.sensitivity.Scenario; -import fr.ifremer.isisfish.simulator.sensitivity.SensitivityScenarios; -import fr.ifremer.isisfish.simulator.sensitivity.visitor.SensitivityScenariosVisitor; - -/** - * XML sensitivity scenarios visitor. - * - * @author chatellier - * @version $Revision: 1.0 $ - * - * Last update : $Date: 5 mars 2009 $ - * By : $Author: chatellier $ - */ -public class SensitivityScenariosXMLVisitor extends AbstractXMLVisitor implements SensitivityScenariosVisitor { - - /** - * {@inheritDoc} - */ - @Override - public void start(SensitivityScenarios scenarios) { - xmlBuffer.append("<experimentalScenarios>"); - xmlBuffer.append("<scenarios>"); - } - - /** - * {@inheritDoc} - */ - @Override - public void visit(SensitivityScenarios scenarios, Scenario scenario) { - ScenarioXMLVisitor visitor = new ScenarioXMLVisitor(); - scenario.accept(visitor); - xmlBuffer.append(visitor.getXML()); - } - - /** - * {@inheritDoc} - */ - @Override - public void end(SensitivityScenarios scenarios) { - xmlBuffer.append("</scenarios>"); - xmlBuffer.append("</experimentalScenarios>"); - } -} Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/package-info.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/package-info.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/mexico/xml/package-info.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2009 - 2011 Ifremer, CodeLutin, Chatellier Eric + * Copyright (C) 2009 - 2012 Ifremer, CodeLutin, 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 Modified: branches/4.0.1/src/test/java/fr/ifremer/isisfish/mexico/MexicoHelperTest.java =================================================================== --- branches/4.0.1/src/test/java/fr/ifremer/isisfish/mexico/MexicoHelperTest.java 2012-03-15 15:26:06 UTC (rev 3658) +++ branches/4.0.1/src/test/java/fr/ifremer/isisfish/mexico/MexicoHelperTest.java 2012-03-15 17:43:01 UTC (rev 3659) @@ -35,6 +35,7 @@ import java.util.SortedMap; import java.util.TreeMap; +import javax.xml.XMLConstants; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; @@ -101,11 +102,12 @@ * Get a test design plan composed with all possibles value types. * * @param use40Features build design plan with sub factor groups + * @param use401Features build plan with distribution definition * @return a test design plan * @throws IsisFishException * @throws TopiaException */ - protected DesignPlan getTestDesignPlan(boolean use40Features) throws IsisFishException, TopiaException { + protected DesignPlan getTestDesignPlan(boolean use40Features, boolean use401Features) throws IsisFishException, TopiaException { DesignPlan designPlan = new DesignPlan(); // matrix 1 @@ -248,7 +250,7 @@ @Test public void testGetDesignPlanAsXML() throws IOException, IsisFishException, TopiaException { - DesignPlan testDesignPlan = getTestDesignPlan(false); + DesignPlan testDesignPlan = getTestDesignPlan(false, false); // test xml export String xml = MexicoHelper.getDesignPlanAsXML(testDesignPlan); @@ -314,7 +316,7 @@ public void testExportImport() throws IOException, IsisFishException, TopiaException { // first export - DesignPlan testDesignPlan = getTestDesignPlan(false); + DesignPlan testDesignPlan = getTestDesignPlan(false, false); String xml1 = MexicoHelper.getDesignPlanAsXML(testDesignPlan); if (log.isDebugEnabled()) { log.debug("xml 1 = " + xml1); @@ -342,7 +344,7 @@ @Test public void testFactorGroupAndRule() throws IOException, IsisFishException, TopiaException { - DesignPlan testDesignPlan = getTestDesignPlan(true); + DesignPlan testDesignPlan = getTestDesignPlan(true, false); // test xml export String xml = MexicoHelper.getDesignPlanAsXML(testDesignPlan); @@ -373,7 +375,7 @@ public void testExportImportWithGroup() throws IOException, IsisFishException, TopiaException { // first export - DesignPlan testDesignPlan = getTestDesignPlan(true); + DesignPlan testDesignPlan = getTestDesignPlan(true, false); String xml1 = MexicoHelper.getDesignPlanAsXML(testDesignPlan); if (log.isDebugEnabled()) { log.debug("xml 1 = " + xml1); @@ -404,7 +406,7 @@ @Test public void testFactorRuleParams() throws IOException, IsisFishException, TopiaException { - DesignPlan testDesignPlan = getTestDesignPlan(true); + DesignPlan testDesignPlan = getTestDesignPlan(true, false); // test xml export String xml = MexicoHelper.getDesignPlanAsXML(testDesignPlan); @@ -432,7 +434,7 @@ public void testPercentageMinMaxFactor() throws IOException, IsisFishException, TopiaException { // get test plan and add some STRANGES factors - DesignPlan testDesignPlan = getTestDesignPlan(false); + DesignPlan testDesignPlan = getTestDesignPlan(false, false); // matrix 1 MatrixND matrix1 = MatrixFactory.getInstance().create("test1", @@ -492,7 +494,7 @@ public void testDiscreteEquationFactor() throws IOException, IsisFishException, TopiaException { // get test plan and add some STRANGES factors - DesignPlan testDesignPlan = getTestDesignPlan(false); + DesignPlan testDesignPlan = getTestDesignPlan(false, false); // factor x6 Factor factorEquationDiscrete = new Factor("factor x6"); @@ -531,7 +533,7 @@ public void testExportImportV2() throws IOException, IsisFishException, TopiaException { // first export - DesignPlan testDesignPlan = getTestDesignPlan(false); + DesignPlan testDesignPlan = getTestDesignPlan(false, false); String xml1 = MexicoHelper.getDesignPlanAsXML(testDesignPlan); if (log.isDebugEnabled()) { log.debug("xml 1 = " + xml1); @@ -593,13 +595,11 @@ @Test public void validOutputXml() throws Exception { - DesignPlan testDesignPlan = getTestDesignPlan(false); + DesignPlan testDesignPlan = getTestDesignPlan(true, true); String xml1 = MexicoHelper.getDesignPlanAsXML(testDesignPlan); - URL schemaUrl = MexicoHelperTest.class.getResource("/mexico/expDesign.xsd"); - - String schemaLang = "http://www.w3.org/2001/XMLSchema"; - SchemaFactory factory = SchemaFactory.newInstance(schemaLang); + + SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(schemaUrl); Validator validator = schema.newValidator(); // at last perform validation:
participants (1)
-
echatellier@users.forge.codelutin.com