Context-detection API for Android developed as a university project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.co.md87.android.common;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. /**
  26. *
  27. * @author chris
  28. */
  29. public class Aggregator {
  30. static final double DELTA = 0.25;
  31. static final double THRESHOLD = 0.5;
  32. private final HashMap<String, HashMap<String, Double>> scores
  33. = new HashMap<String, HashMap<String, Double>>() {{
  34. put("", new HashMap<String, Double>() {{
  35. put("null", 0.5d);
  36. put("CLASSIFIED", 0.5d);
  37. }});
  38. put("CLASSIFIED", new HashMap<String, Double>() {{
  39. put("null", 0.2d);
  40. put("DANCING", 0.2d);
  41. put("WALKING", 0.2d);
  42. put("VEHICLE", 0.2d);
  43. put("IDLE", 0.2d);
  44. }});
  45. put("CLASSIFIED/WALKING", new HashMap<String, Double>() {{
  46. put("null", 0.5d);
  47. put("STAIRS", 0.5d);
  48. }});
  49. put("CLASSIFIED/VEHICLE", new HashMap<String, Double>() {{
  50. put("null", 0.333d);
  51. put("CAR", 0.333d);
  52. put("BUS", 0.333d);
  53. }});
  54. put("CLASSIFIED/IDLE", new HashMap<String, Double>() {{
  55. put("null", 0.333d);
  56. put("STANDING", 0.333d);
  57. put("SITTING", 0.333d);
  58. }});
  59. put("CLASSIFIED/WALKING/STAIRS", new HashMap<String, Double>() {{
  60. put("null", 0.333d);
  61. put("UP", 0.333d);
  62. put("DOWN", 0.333d);
  63. }});
  64. }};
  65. public void addClassification(final String classification) {
  66. String path = "";
  67. for (String part : classification.split("/")) {
  68. if (!scores.containsKey(path)) {
  69. throw new RuntimeException("Path not found: " + path
  70. + " (classification: " + classification + ")");
  71. }
  72. updateScores(scores.get(path), part);
  73. path = path + (path.length() == 0 ? "" : "/") + part;
  74. }
  75. if (scores.containsKey(path)) {
  76. // This classification has children which we're not using
  77. // e.g. we've received CLASSIFIED/WALKING, but we're not walking
  78. // up or down stairs
  79. updateScores(scores.get(path), "null");
  80. }
  81. }
  82. void updateScores(final Map<String, Double> map, final String target) {
  83. for (Map.Entry<String, Double> entry : map.entrySet()) {
  84. //Log.d(getClass().getName(), "Score for " + entry.getKey() + " was: " + entry.getValue());
  85. entry.setValue(entry.getValue() * (1 - DELTA));
  86. if (entry.getKey().equals(target)) {
  87. entry.setValue(entry.getValue() + DELTA);
  88. }
  89. //Log.d(getClass().getName(), "Score for " + entry.getKey() + " is now: " + entry.getValue());
  90. }
  91. }
  92. public String getClassification() {
  93. String path = "";
  94. do {
  95. final Map<String, Double> map = scores.get(path);
  96. double best = THRESHOLD;
  97. String bestPath = "null";
  98. for (Map.Entry<String, Double> entry : map.entrySet()) {
  99. if (entry.getValue() >= best) {
  100. best = entry.getValue();
  101. bestPath = entry.getKey();
  102. }
  103. }
  104. path = path + (path.length() == 0 ? "" : "/") + bestPath;
  105. } while (scores.containsKey(path));
  106. return path.replaceAll("(^CLASSIFIED)?/?null$", "");
  107. }
  108. }