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;
23import android.util.Pair;
24
25import java.util.ArrayList;
26import java.util.List;
27import java.util.HashMap;
28import java.util.Map;
29
30/** Predictor for the Learning framework.
31 */
32public class BordeauxPredictor {
33    static final String TAG = "BordeauxPredictor";
34    static final String PREDICTOR_NOTAVAILABLE = "Predictor is not available.";
35    private Context mContext;
36    private String mName;
37    private IPredictor mPredictor;
38
39    public BordeauxPredictor(Context context) {
40        mContext = context;
41        mName = "defaultPredictor";
42        mPredictor = BordeauxManagerService.getPredictor(context, mName);
43    }
44
45    public BordeauxPredictor(Context context, String name) {
46        mContext = context;
47        mName = name;
48        mPredictor = BordeauxManagerService.getPredictor(context, mName);
49    }
50
51    public boolean reset() {
52        if (!retrievePredictor()){
53            Log.e(TAG, "reset: " + PREDICTOR_NOTAVAILABLE);
54            return false;
55        }
56        try {
57            mPredictor.resetPredictor();
58            return true;
59        } catch (RemoteException e) {
60        }
61        return false;
62    }
63
64    public boolean retrievePredictor() {
65        if (mPredictor == null) {
66            mPredictor = BordeauxManagerService.getPredictor(mContext, mName);
67        }
68        if (mPredictor == null) {
69            Log.e(TAG, "retrievePredictor: " + PREDICTOR_NOTAVAILABLE);
70            return false;
71        }
72        return true;
73    }
74
75    public void addSample(String sampleName) {
76        if (!retrievePredictor())
77            throw new RuntimeException(PREDICTOR_NOTAVAILABLE);
78        try {
79            mPredictor.pushNewSample(sampleName);
80        } catch (RemoteException e) {
81            Log.e(TAG,"Exception: pushing a new example");
82            throw new RuntimeException(PREDICTOR_NOTAVAILABLE);
83        }
84    }
85
86    public ArrayList<Pair<String, Float> > getTopSamples() {
87        return getTopSamples(0);
88    }
89
90    public ArrayList<Pair<String, Float> > getTopSamples(int topK) {
91        try {
92            ArrayList<StringFloat> topList =
93                    (ArrayList<StringFloat>) mPredictor.getTopCandidates(topK);
94
95            ArrayList<Pair<String, Float> > topSamples =
96                    new ArrayList<Pair<String, Float> >(topList.size());
97            for (int i = 0; i < topList.size(); ++i) {
98                topSamples.add(new Pair<String, Float>(topList.get(i).key, topList.get(i).value));
99            }
100            return topSamples;
101        } catch(RemoteException e) {
102            Log.e(TAG,"Exception: getTopSamples");
103            throw new RuntimeException(PREDICTOR_NOTAVAILABLE);
104        }
105    }
106
107    public boolean setParameter(String key, String value) {
108        if (!retrievePredictor())
109            throw new RuntimeException(PREDICTOR_NOTAVAILABLE);
110        try {
111            return mPredictor.setPredictorParameter(key, value);
112        } catch (RemoteException e) {
113            Log.e(TAG,"Exception: setting predictor parameter");
114            throw new RuntimeException(PREDICTOR_NOTAVAILABLE);
115        }
116    }
117}
118