com.feedzai.fos.impl.r.RManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fos-impl-r Show documentation
Show all versions of fos-impl-r Show documentation
Feedzai Open Scoring Server - R Implementation
/*
* $#
* FOS R implementation
*
* Copyright (C) 2013 Feedzai SA
*
* This software is licensed under the Apache License, Version 2.0 (the "Apache License") or the GNU
* Lesser General Public License version 3 (the "GPL License"). You may choose either license to govern
* your use of this software only upon the condition that you accept all of the terms of either the Apache
* License or the LGPL License.
*
* You may obtain a copy of the Apache License and the LGPL License at:
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Unless required by applicable law or agreed to in writing, software distributed under the Apache License
* or the LGPL License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the Apache License and the LGPL License for the specific language governing
* permissions and limitations under the Apache License and the LGPL License.
* #$
*/
package com.feedzai.fos.impl.r;
import com.feedzai.fos.api.*;
import com.feedzai.fos.common.validation.NotBlank;
import com.feedzai.fos.common.validation.NotNull;
import com.feedzai.fos.impl.r.config.RManagerConfig;
import com.feedzai.fos.impl.r.config.RModelConfig;
import com.feedzai.fos.impl.r.rserve.FosRserve;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Files;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.zip.GZIPOutputStream;
import static com.feedzai.fos.impl.r.RScorer.rVariableName;
import static com.feedzai.fos.api.util.ManagerUtils.createModelFile;
import static com.feedzai.fos.api.util.ManagerUtils.getUuid;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* This class provides a R implementation of a FOS Manager
*
* @since 1.0.2
* @author miguel.duarte
*/
public class RManager implements Manager {
/** R Manager logger */
private final static Logger logger = LoggerFactory.getLogger(RManager.class);
/** Handle for the RServer daemon */
private final FosRserve rserve;
/** Map that stores RModel configurations for each configured model */
private Map modelConfigs = new HashMap<>();
/** Manager configuration */
private RManagerConfig rManagerConfig;
/** Reference for an R scorer */
private RScorer rScorer;
/**
* Default libraries for the R server.
*/
private final Set defaultLibraries = ImmutableSet.of("pmml");
/**
* Create a new manager from the given configuration.
* Will lookup any headers files and to to instantiate the model.
* If a model fails, a log is produced but loading other models will continue (no exception is thrown).
*
* @param rManagerConfig the manager configuration
*/
public RManager(RManagerConfig rManagerConfig) throws FOSException {
checkNotNull(rManagerConfig, "Manager config cannot be null");
this.rManagerConfig = rManagerConfig;
this.rserve = new FosRserve();
this.rScorer = new RScorer(rserve, defaultLibraries.toArray(new String[]{}));
}
@Override
public synchronized UUID addModel(ModelConfig config, Model model) throws FOSException {
if (!(model instanceof ModelBinary)) {
throw new FOSException("Currently FOS-R only supports binary models.");
}
try {
UUID uuid = getUuid(config);
File file = createModelFile(modelConfigs.get(uuid).getModel(), uuid, model);
RModelConfig rModelConfig = new RModelConfig(config, rManagerConfig);
rModelConfig.setId(uuid);
rModelConfig.setModel(file);
modelConfigs.put(uuid, rModelConfig);
rScorer.addOrUpdate(rModelConfig);
return uuid;
} catch (IOException e) {
throw new FOSException(e);
}
}
@Override
public synchronized UUID addModel(ModelConfig config, @NotBlank ModelDescriptor descriptor) throws FOSException {
if (descriptor.getFormat() != ModelDescriptor.Format.BINARY) {
throw new FOSException("Currently FOS-R only supports binary models.");
}
UUID uuid = getUuid(config);
RModelConfig rModelConfig = new RModelConfig(config, rManagerConfig);
rModelConfig.setId(uuid);
rModelConfig.setModel(new File(descriptor.getModelFilePath()));
modelConfigs.put(uuid, rModelConfig);
rScorer.addOrUpdate(rModelConfig);
return uuid;
}
@Override
public synchronized void removeModel(UUID modelId) throws FOSException {
RModelConfig rModelConfig = modelConfigs.remove(modelId);
rScorer.removeModel(modelId);
// delete the header & model file (or else it will be picked up on the next restart)
rModelConfig.getHeader().delete();
rModelConfig.getModel().delete();
rModelConfig.getPMMLModel().delete();
}
@Override
public synchronized void reconfigureModel(UUID modelId, ModelConfig modelConfig) throws FOSException {
RModelConfig rModelConfig = this.modelConfigs.get(modelId);
rModelConfig.update(modelConfig);
rScorer.addOrUpdate(rModelConfig);
}
@Override
public synchronized void reconfigureModel(UUID modelId, ModelConfig modelConfig, Model model) throws FOSException {
throw new FOSException("Model reconfiguration not yet supported for R");
}
@Override
public synchronized void reconfigureModel(UUID modelId,ModelConfig modelConfig, @NotBlank ModelDescriptor descriptor) throws FOSException {
if (descriptor.getFormat() != ModelDescriptor.Format.BINARY) {
throw new FOSException("Currently FOS-R only supports binary models.");
}
File file = new File(descriptor.getModelFilePath());
RModelConfig rModelConfig = this.modelConfigs.get(modelId);
rModelConfig.update(modelConfig);
rModelConfig.setModel(file);
rScorer.addOrUpdate(rModelConfig);
}
@Override
@NotNull
public synchronized Map listModels() {
Map result = new HashMap<>(modelConfigs.size());
for (Map.Entry entry : modelConfigs.entrySet()) {
result.put(entry.getKey(), entry.getValue().getModelConfig());
}
return result;
}
@Override
@NotNull
public RScorer getScorer() {
return rScorer;
}
@Override
public synchronized UUID trainAndAdd(ModelConfig config, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy