summaryrefslogtreecommitdiff
path: root/bordeaux/service/src/android/bordeaux/services/BordeauxPredictor.java
blob: ac46af11f5208ffa00df556ffd74b5e67ef0f132 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.bordeaux.services;

import android.bordeaux.services.IPredictor;
import android.content.Context;
import android.os.RemoteException;
import android.util.Log;
import android.util.Pair;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

/** Predictor for the Learning framework.
 */
public class BordeauxPredictor {
    static final String TAG = "BordeauxPredictor";
    static final String PREDICTOR_NOTAVAILABLE = "Predictor is not available.";
    private Context mContext;
    private String mName;
    private IPredictor mPredictor;

    public BordeauxPredictor(Context context) {
        mContext = context;
        mName = "defaultPredictor";
        mPredictor = BordeauxManagerService.getPredictor(context, mName);
    }

    public BordeauxPredictor(Context context, String name) {
        mContext = context;
        mName = name;
        mPredictor = BordeauxManagerService.getPredictor(context, mName);
    }

    public boolean reset() {
        if (!retrievePredictor()){
            Log.e(TAG, "reset: " + PREDICTOR_NOTAVAILABLE);
            return false;
        }
        try {
            mPredictor.resetPredictor();
            return true;
        } catch (RemoteException e) {
        }
        return false;
    }

    public boolean retrievePredictor() {
        if (mPredictor == null) {
            mPredictor = BordeauxManagerService.getPredictor(mContext, mName);
        }
        if (mPredictor == null) {
            Log.e(TAG, "retrievePredictor: " + PREDICTOR_NOTAVAILABLE);
            return false;
        }
        return true;
    }

    public void addSample(String sampleName) {
        if (!retrievePredictor())
            throw new RuntimeException(PREDICTOR_NOTAVAILABLE);
        try {
            mPredictor.pushNewSample(sampleName);
        } catch (RemoteException e) {
            Log.e(TAG,"Exception: pushing a new example");
            throw new RuntimeException(PREDICTOR_NOTAVAILABLE);
        }
    }

    public ArrayList<Pair<String, Float> > getTopSamples() {
        return getTopSamples(0);
    }

    public ArrayList<Pair<String, Float> > getTopSamples(int topK) {
        try {
            ArrayList<StringFloat> topList =
                    (ArrayList<StringFloat>) mPredictor.getTopCandidates(topK);

            ArrayList<Pair<String, Float> > topSamples =
                    new ArrayList<Pair<String, Float> >(topList.size());
            for (int i = 0; i < topList.size(); ++i) {
                topSamples.add(new Pair<String, Float>(topList.get(i).key, topList.get(i).value));
            }
            return topSamples;
        } catch(RemoteException e) {
            Log.e(TAG,"Exception: getTopSamples");
            throw new RuntimeException(PREDICTOR_NOTAVAILABLE);
        }
    }

    public boolean setParameter(String key, String value) {
        if (!retrievePredictor())
            throw new RuntimeException(PREDICTOR_NOTAVAILABLE);
        try {
            return mPredictor.setPredictorParameter(key, value);
        } catch (RemoteException e) {
            Log.e(TAG,"Exception: setting predictor parameter");
            throw new RuntimeException(PREDICTOR_NOTAVAILABLE);
        }
    }
}