/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.ml;

import com.google.common.base.Throwables;
import com.google.common.io.Files;
import com.google.common.util.concurrent.SimpleTimeLimiter;
import com.google.common.util.concurrent.TimeLimiter;
import libsvm.svm;
import libsvm.svm_model;
import libsvm.svm_node;
import libsvm.svm_parameter;
import libsvm.svm_problem;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.SortedMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.airlift.concurrent.Threads.threadsNamed;
import static java.util.concurrent.Executors.newCachedThreadPool;

public abstract class AbstractSvmModel
        implements Model
{
    protected svm_model model;
    protected svm_parameter params;

    protected AbstractSvmModel(svm_parameter params)
    {
        this.params = checkNotNull(params, "params is null");
    }

    protected AbstractSvmModel(svm_model model)
    {
        this.model = checkNotNull(model, "model is null");
    }

    @Override
    public byte[] getSerializedData()
    {
        File file = null;
        try {
            // libsvm doesn't have a method to serialize the model into a buffer, so write it out to a file and then read it back in
            file = File.createTempFile("svm", null);
            svm.svm_save_model(file.getAbsolutePath(), model);
            return Files.toByteArray(file);
        }
        catch (IOException e) {
            throw Throwables.propagate(e);
        }
        finally {
            if (file != null) {
                file.delete();
            }
        }
    }

    @Override
    public void train(Dataset dataset)
    {
        params.svm_type = getLibsvmType();

        svm_problem problem = toSvmProblem(dataset);

        ExecutorService service = newCachedThreadPool(threadsNamed("libsvm-trainer-" + System.identityHashCode(this) + "-%d"));
        try {
            TimeLimiter limiter = new SimpleTimeLimiter(service);
            //TODO: this time limit should be configurable
            model = limiter.callWithTimeout(getTrainingFunction(problem, params), 1, TimeUnit.HOURS, true);
        }
        catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw Throwables.propagate(e);
        }
        catch (Exception e) {
            throw Throwables.propagate(e);
        }
        finally {
            service.shutdownNow();
        }
    }

    private static Callable<svm_model> getTrainingFunction(final svm_problem problem, final svm_parameter param)
    {
        return new Callable<svm_model>() {
            @Override
            public svm_model call()
                    throws Exception
            {
                return svm.svm_train(problem, param);
            }
        };
    }

    protected abstract int getLibsvmType();

    private static svm_problem toSvmProblem(Dataset dataset)
    {
        svm_problem problem = new svm_problem();
        List<Double> labels = dataset.getLabels();
        problem.l = labels.size();
        problem.y = new double[labels.size()];
        for (int i = 0; i < labels.size(); i++) {
            problem.y[i] = labels.get(i);
        }
        problem.x = new svm_node[labels.size()][];
        for (int i = 0; i < dataset.getDatapoints().size(); i++) {
            problem.x[i] = toSvmNodes(dataset.getDatapoints().get(i));
        }
        return problem;
    }

    protected static svm_node[] toSvmNodes(FeatureVector features)
    {
        svm_node[] nodes = new svm_node[features.size()];
        int i = 0;
        // Features map is sorted, so we can just flatten it to a list for libsvm
        for (SortedMap.Entry<Integer, Double> feature : features.getFeatures().entrySet()) {
            nodes[i] = new svm_node();
            nodes[i].index = feature.getKey();
            nodes[i].value = feature.getValue();
            i++;
        }

        return nodes;
    }
}

@@ -2230,24 +2230,81 @@
  != null) %7B%0A
+                //noinspection ResultOfMethodCallIgnored%0A
             
@@ -3249,22 +3249,16 @@
 unction(
-final 
 svm_prob
@@ -3269,22 +3269,16 @@
 problem,
- final
  svm_par
@@ -3316,167 +3316,13 @@
 urn 
-new Callable%3Csvm_model%3E() %7B%0A            @Override%0A            public svm_model call()%0A                    throws Exception%0A            %7B%0A                return
+() -%3E
  svm
@@ -3353,33 +3353,8 @@
 m);%0A
-            %7D%0A        %7D;%0A
     

Fix IntelliJ warnings in AbstractSvmModel