source: trunk/autoquest-core-usageprofiles/src/main/java/de/ugoe/cs/autoquest/usageprofiles/DeterministicFiniteAutomaton.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 2.9 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.usageprofiles;
16
17import java.util.Collection;
18import java.util.LinkedList;
19import java.util.List;
20import java.util.Random;
21
22import de.ugoe.cs.autoquest.eventcore.Event;
23
24/**
25 * <p>
26 * Implements a Deterministic Finite Automata (DFA) capable of random session generation. It is a
27 * special case of a first-order Markov model, where the transition probability is equally high for
28 * all following states.
29 * </p>
30 *
31 * @author Steffen Herbold
32 * @version 1.0
33 */
34public class DeterministicFiniteAutomaton extends FirstOrderMarkovModel {
35
36    /**
37     * <p>
38     * Id for object serialization.
39     * </p>
40     */
41    private static final long serialVersionUID = 1L;
42
43    /**
44     * <p>
45     * Constructor. Creates a new DeterministicFiniteAutomaton.
46     * </p>
47     *
48     * @param r
49     *            random number generator used by probabilistic methods of the class
50     */
51    public DeterministicFiniteAutomaton(Random r) {
52        super(r);
53    }
54
55    /**
56     * <p>
57     * Calculates the proability of the next state. Each of the following states in the automaton is
58     * equally probable.
59     * </p>
60     *
61     * @see de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess#getProbability(java.util.List,
62     *      de.ugoe.cs.autoquest.eventcore.Event)
63     */
64    @Override
65    public double getProbability(List<Event> context, Event symbol) {
66        if (context == null) {
67            throw new IllegalArgumentException("context must not be null");
68        }
69        if (symbol == null) {
70            throw new IllegalArgumentException("symbol must not be null");
71        }
72        double result = 0.0d;
73
74        List<Event> contextCopy;
75        if (context.size() >= trieOrder) {
76            contextCopy =
77                new LinkedList<Event>(context.subList(context.size() - trieOrder + 1,
78                                                      context.size()));
79        }
80        else {
81            contextCopy = new LinkedList<Event>(context);
82        }
83
84        Collection<Event> followers = trie.getFollowingSymbols(contextCopy);
85
86        if (followers.size() != 0 && followers.contains(symbol)) {
87            result = 1.0d / followers.size();
88        }
89
90        return result;
91    }
92
93}
Note: See TracBrowser for help on using the repository browser.