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 |
|
---|
15 | package de.ugoe.cs.autoquest.usability.rules.metrics;
|
---|
16 |
|
---|
17 | import static de.ugoe.cs.autoquest.usability.taskmodel.filter.types.EventTypeFilter.TEXT_INPUT;
|
---|
18 | import static de.ugoe.cs.autoquest.usability.util.TextInputUtil.aggregateEnteredTextFromTextInputs;
|
---|
19 | import static de.ugoe.cs.autoquest.usability.util.TextInputUtil.characterIsNoLetterOrDigitPredicate;
|
---|
20 |
|
---|
21 | import java.util.List;
|
---|
22 |
|
---|
23 | import com.google.common.base.CharMatcher;
|
---|
24 | import com.google.common.base.Optional;
|
---|
25 | import com.google.common.collect.Multiset;
|
---|
26 |
|
---|
27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
|
---|
28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel;
|
---|
29 | import de.ugoe.cs.autoquest.usability.EvaluationMethodCaller;
|
---|
30 | import de.ugoe.cs.autoquest.usability.result.UsabilityProblemDescription;
|
---|
31 | import de.ugoe.cs.autoquest.usability.result.UsabilityProblemDescriptionResolver;
|
---|
32 | import de.ugoe.cs.autoquest.usability.rules.UsabilityMetric;
|
---|
33 | import de.ugoe.cs.autoquest.usability.rules.UsabilityRule;
|
---|
34 | import de.ugoe.cs.autoquest.usability.taskmodel.filter.FilterResult;
|
---|
35 | import de.ugoe.cs.autoquest.usability.taskmodel.filter.TaskModelFilter;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * <p>
|
---|
39 | * Metric, which calculates the ratio ratio between non letter or digit characters and all entered
|
---|
40 | * characters.
|
---|
41 | * </p>
|
---|
42 | *
|
---|
43 | * @author Alexander Deicke
|
---|
44 | */
|
---|
45 | public class NoLetterOrDigitRatioMetric extends UsabilityRule implements UsabilityMetric {
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * <p>
|
---|
49 | * Constructor. Creates a new {@code NoLetterOrDigitRatioMetric} for a given task model.
|
---|
50 | * </p>
|
---|
51 | *
|
---|
52 | * @param taskModel
|
---|
53 | */
|
---|
54 | public NoLetterOrDigitRatioMetric(ITaskModel taskModel) {
|
---|
55 | super(taskModel);
|
---|
56 | this.name = "NoLetterOrDigitRatio";
|
---|
57 | this.defect =
|
---|
58 | new UsabilityProblemDescriptionResolver().descriptionFor(this.getClass()
|
---|
59 | .getSimpleName());
|
---|
60 | }
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * (non-Javadoc)
|
---|
64 | *
|
---|
65 | * @see de.ugoe.cs.autoquest.usability.rules.UsabilityRule#check()
|
---|
66 | */
|
---|
67 | @Override
|
---|
68 | public Optional<UsabilityProblemDescription> calculate() {
|
---|
69 | FilterResult textInputEvents = extractNodesFromTaskTree();
|
---|
70 | float evaluationMetric = calculateEvaluationMetric(textInputEvents.tasksMatchedFilter());
|
---|
71 | return this.defect.isPresent(evaluationMetric);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | *
|
---|
76 | * <p>
|
---|
77 | * Filters all text input events from task model.
|
---|
78 | * </p>
|
---|
79 | *
|
---|
80 | * @return {@code FilterResult}
|
---|
81 | */
|
---|
82 | private FilterResult extractNodesFromTaskTree() {
|
---|
83 | return new TaskModelFilter().filter(taskModel, TEXT_INPUT);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | *
|
---|
88 | * <p>
|
---|
89 | * Calculates the metric.
|
---|
90 | * </p>
|
---|
91 | *
|
---|
92 | * @param textInputEvents
|
---|
93 | * all text input events
|
---|
94 | * @return ratio between non letter or digit characters and all entered characters
|
---|
95 | */
|
---|
96 | private float calculateEvaluationMetric(List<ITask> textInputEvents) {
|
---|
97 | Multiset<String> enteredTextFragments = aggregateEnteredTextFromTextInputs(textInputEvents);
|
---|
98 | int allCharactersCount = 0;
|
---|
99 | int noLetterOrDigitCount = 0;
|
---|
100 | for (String textFragment : enteredTextFragments.elementSet()) {
|
---|
101 | int occurencesOfTextFragment = enteredTextFragments.count(textFragment);
|
---|
102 | allCharactersCount += CharMatcher.ANY.countIn(textFragment) * occurencesOfTextFragment;
|
---|
103 | noLetterOrDigitCount +=
|
---|
104 | CharMatcher.forPredicate(characterIsNoLetterOrDigitPredicate())
|
---|
105 | .countIn(textFragment) * occurencesOfTextFragment;
|
---|
106 | }
|
---|
107 | return allCharactersCount != 0 ? (float) noLetterOrDigitCount / (float) allCharactersCount
|
---|
108 | : 0;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * (non-Javadoc)
|
---|
113 | *
|
---|
114 | * @see
|
---|
115 | * de.ugoe.cs.autoquest.usability.rules.UsabilityRule#callEvaluationMetho(de.ugoe.cs.autoquest
|
---|
116 | * .usability.EvaluationMethodCaller)
|
---|
117 | */
|
---|
118 | @Override
|
---|
119 | public Optional<UsabilityProblemDescription> callEvaluationMethod(EvaluationMethodCaller evaluationMethodCaller)
|
---|
120 | {
|
---|
121 | return evaluationMethodCaller.check(this);
|
---|
122 | }
|
---|
123 |
|
---|
124 | }
|
---|