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.os.IBinder; 20import android.util.Log; 21import android.util.Pair; 22import java.io.Serializable; 23import java.util.HashMap; 24import java.util.Map; 25import java.util.List; 26import java.util.Arrays; 27import java.util.HashSet; 28import java.util.Set; 29import java.util.Iterator; 30 31class FeatureAssembly { 32 private static final String TAG = "FeatureAssembly"; 33 private List<String> mPossibleFeatures; 34 private HashSet<String> mUseFeatures; 35 private HashSet<Pair<String, String> > mUsePairedFeatures; 36 private AggregatorManager mAggregatorManager; 37 38 public FeatureAssembly() { 39 mAggregatorManager = AggregatorManager.getInstance(); 40 mPossibleFeatures = Arrays.asList(mAggregatorManager.getListOfFeatures()); 41 mUseFeatures = new HashSet<String>(); 42 mUsePairedFeatures = new HashSet<Pair<String, String> >(); 43 } 44 45 public boolean registerFeature(String s) { 46 if (mPossibleFeatures.contains(s)) { 47 mUseFeatures.add(s); 48 return true; 49 } else { 50 return false; 51 } 52 } 53 54 public boolean registerFeaturePair(String[] features) { 55 if (features.length != 2 || 56 !mPossibleFeatures.contains(features[0]) || 57 !mPossibleFeatures.contains(features[1])) { 58 return false; 59 } else { 60 mUseFeatures.add(features[0]); 61 mUseFeatures.add(features[1]); 62 mUsePairedFeatures.add(Pair.create(features[0], features[1])); 63 return true; 64 } 65 } 66 67 public Set<String> getUsedFeatures() { 68 return (Set) mUseFeatures; 69 } 70 71 public Map<String, String> getFeatureMap() { 72 HashMap<String, String> featureMap = new HashMap<String, String>(); 73 74 Iterator itr = mUseFeatures.iterator(); 75 while(itr.hasNext()) { 76 String f = (String) itr.next(); 77 Map<String, String> features = mAggregatorManager.getDataMap(f); 78 79 // TODO: sanity check for now. 80 if (features.size() > 1) { 81 throw new RuntimeException("Incorrect feature format extracted from aggregator."); 82 } 83 featureMap.putAll(features); 84 } 85 86 if (!mUsePairedFeatures.isEmpty()) { 87 itr = mUsePairedFeatures.iterator(); 88 while(itr.hasNext()) { 89 Pair<String, String> pair = (Pair<String, String>) itr.next(); 90 if (featureMap.containsKey(pair.first) && 91 featureMap.containsKey(pair.second)) { 92 String key = pair.first + Predictor.FEATURE_SEPARATOR + pair.second; 93 String value = featureMap.get(pair.first) + Predictor.FEATURE_SEPARATOR + 94 featureMap.get(pair.second); 95 featureMap.put(key, value); 96 } 97 } 98 } 99 return (Map)featureMap; 100 } 101 102 103 public String augmentFeatureInputString(String s) { 104 String fs = s; 105 Iterator itr = mUseFeatures.iterator(); 106 while(itr.hasNext()) { 107 String f = (String) itr.next(); 108 fs = fs + "+" + mAggregatorManager.getDataMap(f).get(f); 109 } 110 return fs; 111 } 112} 113