me.smoe.adar.utils.cam.o.CAMApplication.java Source code

Java tutorial

Introduction

Here is the source code for me.smoe.adar.utils.cam.o.CAMApplication.java

Source

/**
 * Copyright (c) 2016, adar.w (adar.w@outlook.com) 
 * 
 * http://www.smoe.me
 * 
 * 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 me.smoe.adar.utils.cam.o;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Component;

import me.smoe.adar.spring.Application;
import me.smoe.adar.spring.Spring;
import me.smoe.adar.spring.entity.CAM;
import me.smoe.adar.spring.entity.CAMProduct;
import me.smoe.adar.spring.repository.CAMProductRepository;
import me.smoe.adar.spring.repository.CAMRepository;
import me.smoe.adar.utils.cam.o.mapping.CAMatcher;
import me.smoe.adar.utils.cam.o.statistics.Statistics;

@Component
public class CAMApplication implements Application {

    @Autowired
    private Statistics statistics;

    @Autowired
    private CAMatcher caMatcher;

    @Autowired
    private CAMRepository camRepository;

    @Autowired
    private CAMProductRepository camProductRepository;

    private static final ThreadPoolExecutor THREADPOOLEXECUTOR = new ThreadPoolExecutor(10, 10, 0, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>());

    @Override
    public void run() throws Exception {
        go(1000, false);
    }

    private void go(int cases, boolean buildScore) throws Exception {
        if (buildScore) {
            statistics.run();
        }

        Map<Long, String> cams = new HashMap<>();
        for (CAM cam : camRepository.findAll()) {
            cams.put(cam.getId(), cam.getName());
        }

        AtomicInteger succ = new AtomicInteger();
        AtomicInteger fail = new AtomicInteger();
        AtomicInteger wrong = new AtomicInteger();
        AtomicInteger index = new AtomicInteger();
        Page<CAMProduct> products = camProductRepository
                .findAll(new PageRequest(0, cases, new Sort(Direction.DESC, "id")));
        int total = products.getSize();
        for (CAMProduct product : products) {
            THREADPOOLEXECUTOR.execute(() -> {
                try {
                    Long ca = caMatcher.matcher(product.getName(), product.getBrand(), product.getCategory(),
                            product.getDesc(), 10000);
                    //               Long ca = me.smoe.adar.utils.cam.n.matcher.CAMatcher.matcher(product.getCategory(), 20);

                    if (product.getCao().equals(ca)) {
                        System.err.println(
                                String.format("[CAM] Index: %s Id: %s Cao: %s Can: %s", index.incrementAndGet(),
                                        product.getId(), cams.get(product.getCao()), cams.get(ca)));
                    } else if (ca != null && cams.get(product.getCao()).equals(cams.get(ca))) {
                        System.err.println(
                                String.format("[CAM] Index: %s Id: %s Cao: %s Can: %s", index.incrementAndGet(),
                                        product.getId(), cams.get(product.getCao()), cams.get(ca)));
                    } else if (ca != null) {
                        System.out.println(
                                String.format("[CAM] Index: %s Id: %s Cao: %s Can: %s", index.incrementAndGet(),
                                        product.getId(), cams.get(product.getCao()), cams.get(ca)));
                    } else {
                        //                  System.out.println(String.format("[CAM] Index: %s Id: %s Cao: %s Can: %s", index.incrementAndGet(), product.getId(), cams.get(product.getCao()), cams.get(ca)));
                    }

                    if (ca == null) {
                        fail.incrementAndGet();
                        return;
                    }
                    if (product.getCao().equals(ca)) {
                        succ.incrementAndGet();
                    } else if (ca != null && cams.get(product.getCao()).equals(cams.get(ca))) {
                        succ.incrementAndGet();
                    } else {
                        wrong.incrementAndGet();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }

        while (!THREADPOOLEXECUTOR.getQueue().isEmpty()) {
            TimeUnit.SECONDS.sleep(1);
        }
        TimeUnit.SECONDS.sleep(2);

        System.out.println();
        System.out.println("[CAM] : " + total);
        System.out.println("[CAM] ?: "
                + new BigDecimal(succ.get()).divide(new BigDecimal(total), 4, RoundingMode.HALF_UP)
                        .multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP)
                + "%");
        System.out.println("[CAM] : "
                + new BigDecimal(fail.get()).divide(new BigDecimal(total), 4, RoundingMode.HALF_UP)
                        .multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP)
                + "%");
        System.out.println("[CAM] : "
                + new BigDecimal(wrong.get()).divide(new BigDecimal(total), 4, RoundingMode.HALF_UP)
                        .multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP)
                + "%");

        System.exit(0);
    }

    public static void main(String[] args) throws Exception {
        Spring.run(CAMApplication.class);
    }
}