/* * Copyright (C) 2023 pbourdau * * 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 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU 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 * . */ package scripts; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import fr.ifremer.isisfish.annotations.Doc; import fr.ifremer.isisfish.entities.*; import org.nuiton.math.matrix.*; import java.util.*; import java.io.*; import java.util.stream.Collectors; import org.nuiton.topia.TopiaContext; import fr.ifremer.isisfish.equation.*; import fr.ifremer.isisfish.datastore.RegionStorage; import fr.ifremer.isisfish.IsisFishDAOHelper; /** * Remplit la proportion de la flottille participant a la strategie lue dans un fichier * Auteur: PHAN Tuan Anh, 2022 */ /** * FillStrPropFlt.java */ public class FillStrPropFlt { /** to use log facility, just put in your code: log.info("..."); */ private static Log log = LogFactory.getLog(FillStrPropFlt.class); /** mettre ici dossier de fichier csv semantics contenant les nb de navires */ String myCsvFile = PTAtoolbox_DEFIPEL.DATA_FOLDER + "/inactivite/Total.NbBateau.effort.csv"; /** les objets pour manipuler objets isis */ TopiaContext myTX = RegionStorage.getRegion(PTAtoolbox_DEFIPEL.REGION_NAME).getStorage().beginTransaction(); StrategyDAO myStrDAO = IsisFishDAOHelper.getStrategyDAO(myTX); List listStrategies = IsisFishDAOHelper.getStrategyDAO(myTX).findAll(); /** lancer le script dans isis */ public static void main(String[] args) throws Exception { System.out.println("~~~ Start: " + new Date() + " ~~~" + System.lineSeparator()); FillStrPropFlt myScript = new FillStrPropFlt(); myScript.runIfTrueClean(false); // see definition below System.out.println("~~~ Done: " + new Date() + " ~~~"); } /** vrai lancement du script car main() accepte pas ces fx */ public void runIfTrueClean(boolean cleanup) throws Exception { if (cleanup) { // supprimer toutes stratégies myStrDAO.deleteAll(myStrDAO .findAll() ); } else { fillPropStr(); } // save the changes myTX.commitTransaction(); myTX.closeContext(); } /** cœur de ce script */ /* Lit le fichier de nb de navire, cherche si la strategy existe, sinon la créer et remplir la proportion de la flt*/ private void fillPropStr() throws Exception { File f = new File(myCsvFile); if (f.exists()) { MatrixND matNbVess = MatrixFactory.getInstance().create(f); for (Strategy str : listStrategies) { // or matrix iterator String strName = str.getName(); List Lstr = matNbVess.getSemantic(0); //System.out.println("list str : " + Lstr); if( Lstr.contains(str) ) { System.out.println(" > found : " + strName); double val = matNbVess.getValue(str); System.out.println("Val str : " + val); str.setProportionSetOfVessels(val); str.update(); } else { System.out.println(" > no value for strategy: " + strName); } } } else { System.out.println(" > file does not exist"); } } }