com.cloud2bubble.services.FuzzyEngine.java Source code

Java tutorial

Introduction

Here is the source code for com.cloud2bubble.services.FuzzyEngine.java

Source

/**
 *    Copyright (C) 2011 Pedro Mauricio Costa <pm.costa [at] imperial [dot] ac [dot] uk>
 *
 *    This file is part of Cloud2Bubble <http://www.cloud2bubble.com>.
 *
 *     Cloud2Bubble is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Cloud2Bubble 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 Lesser Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser Public License
 *     along with Cloud2Bubble.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.cloud2bubble.services;

import java.util.List;

import net.ponder2.managedobject.Bubble;
import net.ponder2.managedobject.Cloudlet;
import net.sourceforge.jFuzzyLogic.FIS;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import com.cloud2bubble.entities.Environment;
import com.cloud2bubble.entities.Message;
import com.cloud2bubble.entities.QualityOfExperience;

/**
 * Provides the functionality of a Fuzzy Engine.
 * 
 * @author Pedro Mauricio Costa <pm.costa@imperial.ac.uk>
 *
 */
public class FuzzyEngine {

    private static final Log log = LogFactory.getLog(FuzzyEngine.class);

    private final static String QOE_FCL_TPL = "src/fcl/qoe.fcl";

    private final FIS fis;

    public final static String PROFILE_TEMP_COLD_MEAN = "profile_temp_cold_mean";

    public final static String PROFILE_TEMP_COLD_STD = "profile_temp_cold_std";

    public final static String PROFILE_TEMP_WARM_MEAN = "profile_temp_warm_mean";

    public final static String PROFILE_TEMP_WARM_STD = "profile_temp_warm_std";

    public final static String PROFILE_TEMP_HOT_MEAN = "profile_temp_hot_mean";

    public final static String PROFILE_TEMP_HOT_STD = "profile_temp_hot_std";

    private static FuzzyEngine instance;

    private FuzzyEngine() {
        this.fis = FIS.load(QOE_FCL_TPL);
    }

    // TODO this needs to be optimized: large files, etc
    //   private InputStream getBubbleFIS(String fileTemplate, Map<String, List<FunctionPoint>> profile) {
    //      InputStream is;
    //      String template;
    //      try {
    //         is = new FileInputStream(new File(fileTemplate));
    //         template = readInputStreamAsString(is);
    //         
    //         for (Entry<String, List<FunctionPoint>> prop : profile.entrySet()) {
    //            
    //            StringBuffer points = new StringBuffer();
    //            
    //            for (FunctionPoint point : prop.getValue()) {
    //               points.append("(").append(point.getX()).append(",").append(point.getY()).append(") ");
    //            }
    //            
    //            template = template.replace("${" + prop.getKey() + "}", points.toString());
    //         }
    //         
    //         is = new ByteArrayInputStream(template.getBytes());
    //      } catch (Exception e) {
    //         log.error(e);
    //         is = null;
    //      }
    //      
    //      return is;
    //   }

    private String evaluateForTerm(Cloudlet c, Bubble b) {
        Double temp = Double.MIN_NORMAL;
        String term = null;

        // load profile vars
        fis.setVariable(PROFILE_TEMP_COLD_MEAN, b.getProfile().get(PROFILE_TEMP_COLD_MEAN));
        fis.setVariable(PROFILE_TEMP_COLD_STD, b.getProfile().get(PROFILE_TEMP_COLD_STD));
        fis.setVariable(PROFILE_TEMP_WARM_MEAN, b.getProfile().get(PROFILE_TEMP_WARM_MEAN));
        fis.setVariable(PROFILE_TEMP_WARM_STD, b.getProfile().get(PROFILE_TEMP_WARM_STD));
        fis.setVariable(PROFILE_TEMP_HOT_MEAN, b.getProfile().get(PROFILE_TEMP_HOT_MEAN));
        fis.setVariable(PROFILE_TEMP_HOT_STD, b.getProfile().get(PROFILE_TEMP_HOT_STD));

        // load env vars
        if (c.getEnvironment().get(Environment.TEMPERATURE) != null) {
            fis.setVariable(Environment.TEMPERATURE.toString().toLowerCase(),
                    Double.valueOf(c.getEnvironment().get(Environment.TEMPERATURE)));
        } else {
            fis.setVariable(Environment.TEMPERATURE.toString().toLowerCase(), Double.NaN);
        }

        fis.evaluate();

        for (String functionTerm : fis.getVariable(QualityOfExperience.FUNCTION).getLinguisticTerms().keySet()) {
            if (fis.getVariable(QualityOfExperience.FUNCTION).getMembership(functionTerm) > temp) {
                term = functionTerm;
                temp = fis.getVariable(QualityOfExperience.FUNCTION).getMembership(functionTerm);
            }
        }

        return term;
    }

    public Double evaluateQoe(Cloudlet c, Bubble b) {
        // load profile vars
        fis.setVariable(PROFILE_TEMP_COLD_MEAN, b.getProfile().get(PROFILE_TEMP_COLD_MEAN));
        fis.setVariable(PROFILE_TEMP_COLD_STD, b.getProfile().get(PROFILE_TEMP_COLD_STD));
        fis.setVariable(PROFILE_TEMP_WARM_MEAN, b.getProfile().get(PROFILE_TEMP_WARM_MEAN));
        fis.setVariable(PROFILE_TEMP_WARM_STD, b.getProfile().get(PROFILE_TEMP_WARM_STD));
        fis.setVariable(PROFILE_TEMP_HOT_MEAN, b.getProfile().get(PROFILE_TEMP_HOT_MEAN));
        fis.setVariable(PROFILE_TEMP_HOT_STD, b.getProfile().get(PROFILE_TEMP_HOT_STD));

        // load env vars
        if (c.getEnvironment().get(Environment.TEMPERATURE) != null) {
            fis.setVariable(Environment.TEMPERATURE.toString().toLowerCase(),
                    Double.valueOf(c.getEnvironment().get(Environment.TEMPERATURE)));
        } else {
            fis.setVariable(Environment.TEMPERATURE.toString().toLowerCase(), Double.NaN);
        }

        fis.evaluate();

        return fis.getVariable(QualityOfExperience.FUNCTION).defuzzify();
    }

    //   private static String readInputStreamAsString(InputStream in) throws IOException {
    //   
    //       BufferedInputStream bis = new BufferedInputStream(in);
    //       ByteArrayOutputStream buf = new ByteArrayOutputStream();
    //       int result = bis.read();
    //       
    //       while(result != -1) {
    //         byte b = (byte)result;
    //         buf.write(b);
    //         result = bis.read();
    //       }
    //       
    //       return buf.toString();
    //   }

    public static FuzzyEngine getInstance() {
        if (instance == null) {
            instance = new FuzzyEngine();
        }

        return instance;
    }

    public void evaluate(Message msg) {

        if (msg.getType() == Message.MessageType.SUGGESTION) {

            Cloudlet cloudlet = msg.getCloudlets().get(0);
            List<Bubble> bubbles = msg.getBubbles();

            for (Bubble bubble : bubbles) {
                msg.getBubbleEvals()
                        .add(QualityOfExperience.Term.valueOf(evaluateForTerm(cloudlet, bubble).toUpperCase()));
            }

        } else {
            List<Cloudlet> cloudlets = msg.getCloudlets();
            Bubble bubble = msg.getBubbles().get(0);

            for (Cloudlet cloudlet : cloudlets) {
                msg.getCloudletEvals()
                        .add(QualityOfExperience.Term.valueOf(evaluateForTerm(cloudlet, bubble).toUpperCase()));
            }
        }
    }

    public void reset() {

        throw new NotImplementedException();
    }
}