All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.graphaware.reco.generic.log.Slf4jRecommendationLogger Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2013-2016 GraphAware
 *
 * This file is part of the GraphAware Framework.
 *
 * GraphAware Framework 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 com.graphaware.reco.generic.log;

import com.graphaware.reco.generic.context.Context;
import com.graphaware.reco.generic.result.Recommendation;
import com.graphaware.reco.generic.result.Score;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * A logger logging recommended items and their scores using {@link Logger}.
 *
 * @param  type of the recommendations produced.
 * @param   type of the item recommendations are for / based on.
 */
public class Slf4jRecommendationLogger implements Logger {

    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(Slf4jRecommendationLogger.class);

    /**
     * {@inheritDoc}
     */
    @Override
    public void log(IN input, List> recommendations, Context context) {
        LOG.info(toString(input, recommendations, context));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString(IN input, List> recommendations, Context context) {
        StringBuilder builder = new StringBuilder("Computed recommendations for ").append(inputToString(input)).append(":");
        for (Recommendation recommendation : recommendations) {
            builder.append(" (");
            if (logUuid()) {
                builder.append(recommendation.getUuid()).append(":");
            }
            builder.append(itemToString(recommendation.getItem())).append(" ");
            builder.append(scoreToString(recommendation.getScore()));
            builder.append("),");
        }
        builder.deleteCharAt(builder.length() - 1);

        return builder.toString();
    }

    /**
     * Convert an input to the recommendation-computing process to String.
     *
     * @param input to convert.
     * @return converted input. Uses the {@link Object#toString()} method by default.
     */
    protected String inputToString(IN input) {
        return input.toString();
    }

    /**
     * Convert a recommendation String.
     *
     * @param item recommended item.
     * @return converted input. Uses the {@link Object#toString()} method by default.
     */
    protected String itemToString(OUT item) {
        return item.toString();
    }

    /**
     * Convert a score to String.
     *
     * @param score to convert.
     * @return converted score.
     */
    protected String scoreToString(Score score) {
        return score.toString();
    }

    /**
     * Should UUID be logged for recommendations?
     *
     * @return true iff UUID should be logged. true by default.
     */
    protected boolean logUuid() {
        return true;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy