The Higher Education and Research forge

Home My Page Projects Code Snippets Project Openings Complex Surface Machining Optimization
Summary Activity SCM

SCM Repository

authorJean-Max Redonnet <jean-max.redonnet@unniv-tlse3.fr>
Fri, 15 May 2020 16:17:51 +0000 (18:17 +0200)
committerJean-Max Redonnet <jean-max.redonnet@unniv-tlse3.fr>
Fri, 15 May 2020 16:17:51 +0000 (18:17 +0200)
src/org/ica/cosmo/tests/MachiningUnitTest.java

index c500909..ec2715b 100644 (file)
 package org.ica.cosmo.tests;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.util.ArrayList;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
 
-import org.ica.cosmo.clustering.ALGO_TYPE;
-import org.ica.cosmo.clustering.Clustering;
-import org.ica.cosmo.clustering.METRIC;
 import org.ica.cosmo.machining.AveragePDE;
 import org.ica.cosmo.machining.MillingContext;
 import org.ica.cosmo.machining.ZoneMilling_PPV3A_Approx;
+import org.ica.cosmo.tools.FileManager;
 import org.ica.cosmo.zoning.SurfaceMap;
 import org.ica.cosmo.zoning.Zone;
-import org.junit.jupiter.api.Test;
-import org.lgmt.dgl.surfaces.Surface;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
 import org.lgmt.jcam.cutter.Cutter;
 
-class MachiningUnitTest {
-       private ALGO_TYPE algo_type;
-       private METRIC metric;
-       private Surface surface;
-       private int tessU;
-       private int tessV;
-       private int k;
+import com.thoughtworks.xstream.XStream;
 
-       @Test
-       void test1() {
-               MillingContext.getInstance().setCutter(new Cutter(5.0, 2.0, 100.0));
-               MillingContext.getInstance().setScallopHeight(0.01);
-               algo_type = ALGO_TYPE.KMEANS;
-               metric = METRIC.SLOPE;
-               surface = SurfaceBuilder.getTile();
-               tessU = 40;
-               tessV = 40;
-               k = 3;
-               SurfaceMap map = new SurfaceMap(surface, tessU, tessV);
-               map.initNZone(k);
-               Clustering c = new Clustering(k, map, algo_type, metric, false);
-               c.run();
+@DisplayName("Running Machining tests")
+public class MachiningUnitTest {
+       private final static String resourcesPath = FileManager.getInstance().getResourcesPath();
+       private final static String tmpPath = FileManager.getInstance().getTmpPath();
+
+       private static SurfaceMap loadFile(File file) {
+               SurfaceMap map = null;
+
+               String filename = file.getName();
+               String zipFile = new String(resourcesPath + filename);
+
+               File destDir = new File(tmpPath);
+               byte[] buffer = new byte[1024];
+               ZipInputStream zis;
+               File tmpFile = null;
                try {
-                       map = c.call();
-               } catch (Exception e) {
+                       zis = new ZipInputStream(new FileInputStream(zipFile));
+                       ZipEntry zipEntry;
+                       zipEntry = zis.getNextEntry();
+                       tmpFile = new File(destDir, zipEntry.getName());
+
+                       FileOutputStream fos = new FileOutputStream(tmpFile);
+                       int len;
+                       while ((len = zis.read(buffer)) > 0) {
+                               fos.write(buffer, 0, len);
+                       }
+                       fos.close();
+                       zipEntry = zis.getNextEntry();
+                       zis.closeEntry();
+                       zis.close();
+               } catch (FileNotFoundException e1) {
+                       e1.printStackTrace();
+               } catch (IOException e) {
                        e.printStackTrace();
                }
 
-               ArrayList<ZoneMilling_PPV3A_Approx> zma = new ArrayList<ZoneMilling_PPV3A_Approx>(k);
-               double totalLength = 0.0;
-               double totalTime = 0.0;
-               for (int zj = 0; zj < k; zj++) {
-                       Zone z = map.getZone(zj);
-                       z.setEvaluator(new AveragePDE(z));
-                       double angle = z.evalPrefDir();
-                       ZoneMilling_PPV3A_Approx.APPROX_LEVEL apxl = ZoneMilling_PPV3A_Approx.APPROX_LEVEL.FINE;
-                       ZoneMilling_PPV3A_Approx zMilling = new ZoneMilling_PPV3A_Approx(z, angle, true, apxl);
-                       zma.add(zMilling);
-                       zMilling.run();
+               XStream xstream = new XStream();
+               XStream.setupDefaultSecurity(xstream);
+               xstream.allowTypesByWildcard(FileManager.getInstance().getAllowedTypes());
+               try {
+                       FileInputStream fis = new FileInputStream(tmpFile);
+                       map = (SurfaceMap) xstream.fromXML(fis);
+                       fis.close();
+               } catch (FileNotFoundException e) {
+                       e.printStackTrace();
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+               return map;
+       }
 
-                       totalLength = totalLength + zMilling.getToolPathApproxLength();// + zMilling.getPenalty();
-                       totalTime = totalTime + zMilling.getMillingTimeApprox();
+       public static List<Arguments> getMaps() {
+               ArrayList<Arguments> list = new ArrayList<Arguments>();
+               File dir = new File(resourcesPath);
+               for (File f : dir.listFiles()) {
+                       if (f.getName().endsWith(".map")) {
+                               list.add(Arguments.of(f.getName(), loadFile(f)));
+                       }
                }
-               assertEquals(totalTime, 102.0, 10.0);
-               assertEquals(totalLength, 5173.0, 500.0);
+
+               return list;
        }
 
-       @Test
-       void test2() {
+       @ParameterizedTest
+       @MethodSource("getMaps")
+       @DisplayName("Testing ZoneMilling_PPV3A_Approx, level FINE")
+       void test1(String name, SurfaceMap map) {
+               System.out.println("Map = " + name);
+               int k = map.getZonesCount();
+
                MillingContext.getInstance().setCutter(new Cutter(5.0, 2.0, 100.0));
                MillingContext.getInstance().setScallopHeight(0.01);
-               algo_type = ALGO_TYPE.KMEANS;
-               metric = METRIC.SLOPE;
-               surface = SurfaceBuilder.getTile();
-               tessU = 40;
-               tessV = 40;
-               k = 3;
-               SurfaceMap map = new SurfaceMap(surface, tessU, tessV);
-               map.initNZone(k);
-               Clustering c = new Clustering(k, map, algo_type, metric, false);
-               c.run();
-               try {
-                       map = c.call();
-               } catch (Exception e) {
-                       e.printStackTrace();
-               }
 
                ArrayList<ZoneMilling_PPV3A_Approx> zma = new ArrayList<ZoneMilling_PPV3A_Approx>(k);
                double totalLength = 0.0;
                double totalTime = 0.0;
+               long start_time = System.currentTimeMillis();
+
                for (int zj = 0; zj < k; zj++) {
                        Zone z = map.getZone(zj);
                        z.setEvaluator(new AveragePDE(z));
@@ -98,7 +114,12 @@ class MachiningUnitTest {
                        totalLength = totalLength + zMilling.getToolPathApproxLength();// + zMilling.getPenalty();
                        totalTime = totalTime + zMilling.getMillingTimeApprox();
                }
-               assertEquals(totalTime, 110.0, 10.0);
-               assertEquals(totalLength, 4972.0, 500.0);
+
+               long end_time = System.currentTimeMillis();
+               long sim_duration = end_time - start_time;
+
+               System.out.printf("\t durée de simulation de l'usinage = %d ms \n", sim_duration);
+               System.out.printf("\t longueur de trajectoire d'usinage = %.3f \n", totalLength);
+               System.out.printf("\t durée de l'usinage = %.3f s \n", totalTime);
        }
 }