source: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/RuleUtils.java @ 1645

Last change on this file since 1645 was 1645, checked in by rkrimmel, 10 years ago

Trying to replace event instances in the sessions

File size: 4.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.tasktrees.temporalrelation;
16
17import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance;
19import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
21import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
22import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory;
23
24/**
25 * <p>
26 * provides some convenience methods for rule application
27 * </p>
28 *
29 * @author Patrick Harms
30 */
31class RuleUtils {
32   
33    /**
34     * <p>
35     * counter for generating unique ids. Starts at 0 for each new program start
36     * </p>
37     */
38    private static int idCounter = 0;
39
40    /**
41     * <p>
42     * generates a sub sequence for a specified range of elements in the provided task instances
43     * list.
44     * </p>
45     *
46     * @param parent      the list of which the range shall be extracted
47     * @param startIndex  the start index of the range
48     * @param endIndex    the end index of the range (inclusive)
49     * @param model       the task model (required for instantiating the sub sequence)
50     * @param taskFactory the task factory used for instantiating the sub sequence
51     * @param taskBuilder the task builder to perform changes in the task structure
52     *
53     * @return a task instance representing the requested sub sequence
54     */
55    static ITaskInstance getSubSequenceInRange(ITaskInstanceList parent,
56                                               int               startIndex,
57                                               int               endIndex,
58                                               ISequence         model,
59                                               ITaskFactory      taskFactory,
60                                               ITaskBuilder      taskBuilder)
61    {
62        ISequenceInstance subsequence = taskFactory.createNewTaskInstance(model);
63
64        for (int i = startIndex; i <= endIndex; i++) {
65            taskBuilder.addChild(subsequence, parent.get(i));
66        }
67
68        return subsequence;
69    }
70
71    /**
72     * <p>
73     * replaces a sub sequence for a specified range of elements in the provided task instances
74     * list by a sub task instance
75     * </p>
76     *
77     * @param parent      the list of which the range shall be replaced
78     * @param startIndex  the start index of the range
79     * @param endIndex    the end index of the range (inclusive)
80     * @param model       the task model (required for instantiating the sub sequence)
81     * @param taskFactory the task factory used for instantiating the sub sequence
82     * @param taskBuilder the task builder to perform changes in the task structure
83     *
84     * @return the replacement for the range
85     */
86    static ISequenceInstance createNewSubSequenceInRange(ITaskInstanceList parent,
87                                                         int               startIndex,
88                                                         int               endIndex,
89                                                         ISequence         model,
90                                                         ITaskFactory      taskFactory,
91                                                         ITaskBuilder      taskBuilder)
92    {
93        ISequenceInstance subsequence = taskFactory.createNewTaskInstance(model);
94       
95       
96       
97        System.out.println("Got model: " + model.toString());
98       
99       
100       
101       
102       
103        System.out.println(parent);
104        //System.out.println(parent.get(startIndex));
105        for (int i = startIndex; i <= endIndex; i++) {
106                System.out.println("Trying to add "+ parent.get(startIndex) + " to the model");
107            taskBuilder.addChild(subsequence, parent.get(startIndex));
108           
109            taskBuilder.removeTaskInstance(parent, startIndex);
110        }
111
112        taskBuilder.addTaskInstance(parent, startIndex, subsequence);
113
114        return subsequence;
115    }
116
117    /**
118     * <p>
119     * returns the next available id (uses the id counter)
120     * </p>
121     *
122     * @return the next available id
123     */
124    static synchronized String getNewId() {
125        return Integer.toString(idCounter++);
126    }
127
128    /**
129     * <p>
130     * prevent instantiation
131     * </p>
132     */
133    private RuleUtils() {
134        // prevent instantiation
135    }
136
137}
Note: See TracBrowser for help on using the repository browser.