source: trunk/autoquest-core-usability-evaluation/src/main/java/de/ugoe/cs/autoquest/usability/rules/metrics/NoLetterOrDigitRatioMetric.java @ 1217

Last change on this file since 1217 was 1217, checked in by adeicke, 11 years ago
  • Added proper formating and JavaDoc?.
  • Several renaming refactorings.
  • Property svn:mime-type set to text/plain
File size: 4.8 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.usability.rules.metrics;
16
17import static de.ugoe.cs.autoquest.usability.taskmodel.filter.types.EventTypeFilter.TEXT_INPUT;
18import static de.ugoe.cs.autoquest.usability.util.TextInputUtil.aggregateEnteredTextFromTextInputs;
19import static de.ugoe.cs.autoquest.usability.util.TextInputUtil.characterIsNoLetterOrDigitPredicate;
20
21import java.util.List;
22
23import com.google.common.base.CharMatcher;
24import com.google.common.base.Optional;
25import com.google.common.collect.Multiset;
26
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel;
29import de.ugoe.cs.autoquest.usability.EvaluationMethodCaller;
30import de.ugoe.cs.autoquest.usability.result.UsabilityProblemDescriptionResolver;
31import de.ugoe.cs.autoquest.usability.result.UsabilityProblemDescription;
32import de.ugoe.cs.autoquest.usability.rules.UsabilityMetric;
33import de.ugoe.cs.autoquest.usability.rules.UsabilityRule;
34import de.ugoe.cs.autoquest.usability.taskmodel.filter.FilterResult;
35import de.ugoe.cs.autoquest.usability.taskmodel.filter.IterativeDFSFilterStrategy;
36import de.ugoe.cs.autoquest.usability.taskmodel.filter.types.TaskModelFilter;
37
38/**
39 * <p>
40 * Metric, which calculates the ratio ratio between non letter or digit characters and all entered
41 * characters.
42 * </p>
43 *
44 * @author Alexander Deicke
45 */
46public class NoLetterOrDigitRatioMetric extends UsabilityRule implements UsabilityMetric {
47
48    /**
49     * <p>
50     * Constructor. Creates a new {@code NoLetterOrDigitRatioMetric} for a given task model.
51     * </p>
52     *
53     * @param taskModel
54     */
55    public NoLetterOrDigitRatioMetric(ITaskModel taskModel) {
56        super(taskModel);
57        this.name = "NoLetterOrDigitRatio";
58        this.defect =
59            new UsabilityProblemDescriptionResolver().descriptionFor(this.getClass()
60                .getSimpleName());
61    }
62
63    /*
64     * (non-Javadoc)
65     *
66     * @see de.ugoe.cs.autoquest.usability.rules.UsabilityRule#check()
67     */
68    @Override
69    public Optional<UsabilityProblemDescription> calculate() {
70        FilterResult textInputEvents = extractNodesFromTaskTree();
71        float evaluationMetric = calculateEvaluationMetric(textInputEvents.tasksMatchedFilter());
72        return this.defect.isPresent(evaluationMetric);
73    }
74
75    /**
76     *
77     * <p>
78     * Filters all text input events from task model.
79     * </p>
80     *
81     * @return {@code FilterResult}
82     */
83    private FilterResult extractNodesFromTaskTree() {
84        return new TaskModelFilter(new IterativeDFSFilterStrategy()).filterByEventType(TEXT_INPUT)
85            .from(this.taskModel);
86    }
87
88    /**
89     *
90     * <p>
91     * Calculates the metric.
92     * </p>
93     *
94     * @param textInputEvents
95     *            all text input events
96     * @return ratio between non letter or digit characters and all entered characters
97     */
98    private float calculateEvaluationMetric(List<ITask> textInputEvents) {
99        Multiset<String> enteredTextFragments = aggregateEnteredTextFromTextInputs(textInputEvents);
100        int allCharactersCount = 0;
101        int noLetterOrDigitCount = 0;
102        for (String textFragment : enteredTextFragments.elementSet()) {
103            int occurencesOfTextFragment = enteredTextFragments.count(textFragment);
104            allCharactersCount += CharMatcher.ANY.countIn(textFragment) * occurencesOfTextFragment;
105            noLetterOrDigitCount +=
106                CharMatcher.forPredicate(characterIsNoLetterOrDigitPredicate())
107                    .countIn(textFragment) * occurencesOfTextFragment;
108        }
109        return allCharactersCount != 0 ? (float) noLetterOrDigitCount / (float) allCharactersCount
110            : 0;
111    }
112
113    /*
114     * (non-Javadoc)
115     *
116     * @see
117     * de.ugoe.cs.autoquest.usability.rules.UsabilityRule#callEvaluationMetho(de.ugoe.cs.autoquest
118     * .usability.EvaluationMethodCaller)
119     */
120    @Override
121    public Optional<UsabilityProblemDescription> callEvaluationMethod(EvaluationMethodCaller evaluationMethodCaller)
122    {
123        return evaluationMethodCaller.check(this);
124    }
125
126}
Note: See TracBrowser for help on using the repository browser.