001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 package graphlab.platform.preferences; 005 006 import graphlab.platform.attribute.NotifiableAttributeSetImpl; 007 import graphlab.platform.core.BlackBoard; 008 import graphlab.platform.lang.ArrayX; 009 import graphlab.platform.preferences.lastsettings.StorableOnExit; 010 import graphlab.platform.preferences.lastsettings.UserModifiableProperty; 011 012 import java.lang.reflect.Field; 013 import java.util.HashMap; 014 import java.util.HashSet; 015 016 /** 017 * @author Rouzbeh Ebrahimi 018 */ 019 public class Preferences implements StorableOnExit { 020 021 public static final String NAME = "__Prefferences"; 022 023 static { 024 categories = new HashMap<Object, String>(); 025 } 026 027 public static HashMap<Object, String> categories; 028 029 public HashSet<NotifiableAttributeSetImpl> setOfAttributes = new HashSet<NotifiableAttributeSetImpl>(); 030 public HashSet<AbstractPreference> set = new HashSet<AbstractPreference>(); 031 032 public void putNewSetOfAttributes(AbstractPreference ap) { 033 setOfAttributes.add(ap.attributeSet); 034 set.add(ap); 035 } 036 037 public Preferences(BlackBoard bb) { 038 bb.setData("Preferences", this); 039 GraphPreferences.pref = this; 040 041 } 042 043 public void retrieveEveryItem() { 044 HashSet<Object> objects = SETTINGS.getRegisteredObjects(); 045 HashMap<String, GraphPreferences> gPrefs = new HashMap<String, GraphPreferences>(); 046 for (Object o : objects) { 047 if (o instanceof UserDefinedEligiblity) { 048 UserDefinedEligiblity um = (UserDefinedEligiblity) o; 049 GraphPreferences gp = um.GraphPrefFactory(); 050 gp.defineAttributes(um.defineEligibleValuesForSettings(new HashMap<Object, ArrayX>())); 051 } else { 052 String key = Preferences.categories.get(o); 053 if (!gPrefs.containsKey(key)) { 054 GraphPreferences gp = new GraphPreferences(Preferences.categories.get(o), o, Preferences.categories.get(o)); 055 HashMap<Object, Object> eligiblesValues = detectEligibleValues(gPrefs, o); 056 gp.defineAttributes(eligiblesValues, true); 057 gPrefs.put(key, gp); 058 } else { 059 GraphPreferences gpref = gPrefs.get(key); 060 gpref.addObject(o); 061 HashMap<Object, Object> eligiblesValues = detectEligibleValues(gPrefs, o); 062 gpref.defineAttributes(eligiblesValues, true); 063 } 064 } 065 } 066 } 067 068 private HashMap<Object, Object> detectEligibleValues(HashMap<String, GraphPreferences> gprefs, Object o) { 069 HashMap<Object, Object> eligiblesValues = new HashMap<Object, Object>(); 070 HashMap<Object, Object> exceptionalEligiblesValues = new HashMap<Object, Object>(); 071 for (Field f : o.getClass().getFields()) { 072 UserModifiableProperty anot = f.getAnnotation(UserModifiableProperty.class); 073 if (anot != null) { 074 try { 075 if (anot.obeysAncestorCategory()) { 076 eligiblesValues.put(anot.displayName(), f.get(o)); 077 } else { 078 if (gprefs.containsKey(anot.category())) { 079 GraphPreferences gp = gprefs.get(anot.category()); 080 gp.addObject(o); 081 exceptionalEligiblesValues.put(anot.displayName(), f.get(o)); 082 gp.defineAttributes(exceptionalEligiblesValues, true); 083 084 } else { 085 GraphPreferences gp = new GraphPreferences(anot.category(), o, anot.category()); 086 exceptionalEligiblesValues.put(anot.displayName(), f.get(o)); 087 gp.defineAttributes(exceptionalEligiblesValues, true); 088 gprefs.put(anot.category(), gp); 089 } 090 } 091 } catch (IllegalAccessException e) { 092 e.printStackTrace(); 093 } 094 } 095 } 096 return eligiblesValues; 097 } 098 }