1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.bordeaux.services; 18 19import android.bordeaux.services.IPredictor; 20import android.content.Context; 21import android.os.RemoteException; 22import android.util.Log; 23 24import java.util.ArrayList; 25import java.util.List; 26import java.util.HashMap; 27import java.util.Map; 28 29/** Predictor for the Learning framework. 30 */ 31public class BordeauxPredictor { 32 static final String TAG = "BordeauxPredictor"; 33 static final String PREDICTOR_NOTAVAILABLE = "Predictor is not available."; 34 private Context mContext; 35 private String mName; 36 private IPredictor mPredictor; 37 38 public boolean retrievePredictor() { 39 if (mPredictor == null) 40 mPredictor = BordeauxManagerService.getPredictor(mContext, mName); 41 if (mPredictor == null) { 42 Log.e(TAG, PREDICTOR_NOTAVAILABLE); 43 return false; 44 } 45 return true; 46 } 47 48 public BordeauxPredictor(Context context) { 49 mContext = context; 50 mName = "defaultPredictor"; 51 mPredictor = BordeauxManagerService.getPredictor(context, mName); 52 } 53 54 public BordeauxPredictor(Context context, String name) { 55 mContext = context; 56 mName = name; 57 mPredictor = BordeauxManagerService.getPredictor(context, mName); 58 } 59 60 public boolean reset() { 61 if (!retrievePredictor()){ 62 Log.e(TAG, PREDICTOR_NOTAVAILABLE); 63 return false; 64 } 65 try { 66 mPredictor.ResetPredictor(); 67 return true; 68 } catch (RemoteException e) { 69 } 70 return false; 71 } 72 73 public void pushSample(String s) { 74 if (!retrievePredictor()) 75 throw new RuntimeException(PREDICTOR_NOTAVAILABLE); 76 try { 77 mPredictor.pushNewSample(s); 78 } catch (RemoteException e) { 79 Log.e(TAG,"Exception: pushing a new example"); 80 throw new RuntimeException(PREDICTOR_NOTAVAILABLE); 81 } 82 } 83 84 public float getProbability(String s) { 85 if (!retrievePredictor()) 86 throw new RuntimeException(PREDICTOR_NOTAVAILABLE); 87 try { 88 return mPredictor.getSampleProbability(s); 89 } catch (RemoteException e) { 90 Log.e(TAG,"Exception: getting sample probability"); 91 throw new RuntimeException(PREDICTOR_NOTAVAILABLE); 92 } 93 } 94 95 public boolean setParameter(String key, String value) { 96 if (!retrievePredictor()) 97 throw new RuntimeException(PREDICTOR_NOTAVAILABLE); 98 try { 99 return mPredictor.setPredictorParameter(key, value); 100 } catch (RemoteException e) { 101 Log.e(TAG,"Exception: setting predictor parameter"); 102 throw new RuntimeException(PREDICTOR_NOTAVAILABLE); 103 } 104 } 105} 106