// Copyright 2012 Georg-August-Universität Göttingen, Germany // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package de.ugoe.cs.autoquest.tasktrees.temporalrelation; import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship; import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; /** *

* provides some convenience methods for rule application *

* * @author Patrick Harms */ class RuleUtils { /** *

* replaces a sub sequence for a specified range of elements in the provided * task instances list by a sub task instance *

* * @param parent * the list of which the range shall be replaced * @param startIndex * the start index of the range * @param endIndex * the end index of the range (inclusive) * @param model * the task model (required for instantiating the sub sequence) * @param taskFactory * the task factory used for instantiating the sub sequence * @param taskBuilder * the task builder to perform changes in the task structure * * @return the replacement for the range * @throws */ static ISequenceInstance createNewSubSequenceInRange( ITaskInstanceList parent, int startIndex, int endIndex, ISequence model, ITaskFactory taskFactory, ITaskBuilder taskBuilder) { final ISequenceInstance subsequence = taskFactory .createNewTaskInstance(model); // TODO: Debug output /* * System.out.println("PRINTING MODEL: "); for (int i = 0; i < * subsequence.getSequence().getChildren().size(); i++) { * System.out.println(subsequence.getSequence().getChildren().get(i)); * * if (subsequence.getSequence().getChildren().get(i).getType() == * "selection") { for (int j = 0; j < ((ISelection) * subsequence.getSequence().getChildren().get(i)) * .getChildren().size(); j++) { if(((IStructuringTemporalRelationship) * subsequence * .getSequence().getChildren().get(i)).getChildren().get(j).getType() * =="sequence") { ISequence foo = (ISequence) ((ISelection) * (subsequence * .getSequence().getChildren().get(i))).getChildren().get(j); * System.out.println("\t" + foo); for(int k=0; k< * foo.getChildren().size();k++) { System.out.println("\t\t" * +foo.getChildren().get(k)); } System.out.println(); } else{ * System.out.println("\t" + ((ISelection) * subsequence.getSequence().getChildren().get(i)) * .getChildren().get(j)); } * * } } * * } System.out.println(); */ // TODO: This is dirty! missedOptionals = 0; int modelindex = 0; for (int i = startIndex; i <= endIndex; i++) { if (modelindex == model.getChildren().size()) { break; } final ITask tempTask = model.getChildren().get(modelindex); // System.out.println("Trying to add " + parent.get(startIndex) // + " to the model instance " + tempTask); if (tempTask.getType() == "optionality") { if (((IMarkingTemporalRelationship) tempTask).getMarkedTask() == parent .get(startIndex).getTask()) { // System.out.println("Adding OptionalInstance " + // parent.get(startIndex) + " to " + tempTask.getType()); final IOptionalInstance optional = taskFactory .createNewTaskInstance((IOptional) tempTask); taskBuilder.setChild(optional, parent.get(startIndex)); taskBuilder.addChild(subsequence, optional); } else { // System.out.println("Adding Empty optional, not deleting anything from the input sequence"); final IOptionalInstance optional = taskFactory .createNewTaskInstance((IOptional) tempTask); taskBuilder.addChild(subsequence, optional); modelindex++; missedOptionals++; continue; } } else if (tempTask.getType() == "selection") { final ISelectionInstance selection = taskFactory .createNewTaskInstance((ISelection) tempTask); final ISelection tmpSel = (ISelection) tempTask; if ((tmpSel.getChildren().get(0).getType() == "sequence") && (tmpSel.getChildren().get(1).getType() == "sequence")) { ISequenceInstance selseq = null; // The selection I create can just have 2 children if (parent.get(startIndex).getTask().getId() == ((ISequence) tmpSel .getChildren().get(0)).getChildren().get(0).getId()) { selseq = taskFactory .createNewTaskInstance((ISequence) tmpSel .getChildren().get(0)); } else if (parent.get(startIndex).getTask().getId() == ((ISequence) tmpSel .getChildren().get(1)).getChildren().get(0).getId()) { selseq = taskFactory .createNewTaskInstance((ISequence) tmpSel .getChildren().get(1)); } else if ((parent.get(startIndex).getTask().getId() == tmpSel .getChildren().get(0).getId()) || (parent.get(startIndex).getTask().getId() == tmpSel .getChildren().get(1).getId())) { // System.out.println("Session ID: " + // parent.get(startIndex).getTask().getId() + // " tmpSel(0): " + tmpSel.getChildren().get(0).getId() // + " tmpSel(1): " +tmpSel.getChildren().get(1).getId() // ); continue; } for (int k = 0; k < selseq.getSequence().getChildren() .size(); k++) { // System.out.println("Trying to add " + // parent.get(startIndex) + " to " + selseq); taskBuilder.addChild(selseq, parent.get(startIndex)); taskBuilder.removeTaskInstance(parent, startIndex); i++; // System.out.println("I:" + i); } // System.out.println("Trying to add " + selseq + " to " + // tmpSel); taskBuilder.setChild(selection, selseq); taskBuilder.addChild(subsequence, selection); modelindex++; continue; } else { // System.out.println("Trying to adding SelectionInstance " // + parent.get(startIndex) + " to " + tempTask); taskBuilder.setChild(selection, parent.get(startIndex)); taskBuilder.addChild(subsequence, selection); } } else if (tempTask.getType() == "sequence") { // System.out.println("Adding SequenceInstance " + // parent.get(startIndex) + " to " + tempTask); taskBuilder.addChild(subsequence, parent.get(startIndex)); } else if (tempTask.getType() == "iteration") { // System.out.println("Adding IterationInstance " + // parent.get(startIndex) + " to " + tempTask); taskBuilder.addChild(subsequence, parent.get(startIndex)); } else { // System.out.println("Adding EventInstance " + // parent.get(startIndex) + " to " + tempTask); // System.out.println("Foo"); taskBuilder.addChild(subsequence, parent.get(startIndex)); } taskBuilder.removeTaskInstance(parent, startIndex); modelindex++; } taskBuilder.addTaskInstance(parent, startIndex, subsequence); return subsequence; } /** *

* returns the next available id (uses the id counter) *

* * @return the next available id */ static synchronized String getNewId() { return Integer.toString(idCounter++); } /** *

* generates a sub sequence for a specified range of elements in the * provided task instances list. *

* * @param parent * the list of which the range shall be extracted * @param startIndex * the start index of the range * @param endIndex * the end index of the range (inclusive) * @param model * the task model (required for instantiating the sub sequence) * @param taskFactory * the task factory used for instantiating the sub sequence * @param taskBuilder * the task builder to perform changes in the task structure * * @return a task instance representing the requested sub sequence */ static ITaskInstance getSubSequenceInRange(ITaskInstanceList parent, int startIndex, int endIndex, ISequence model, ITaskFactory taskFactory, ITaskBuilder taskBuilder) { final ISequenceInstance subsequence = taskFactory .createNewTaskInstance(model); for (int i = startIndex; i <= endIndex; i++) { taskBuilder.addChild(subsequence, parent.get(i)); } return subsequence; } // Print out the progress static void printProgressPercentage(String message, int count, int size) { if (size > 100) { if (((count % (size / 100)) == 0)) { // Console.traceln(Level.INFO,("Thread" + // Thread.currentThread().getName() + ": " + Math.round((float) // count/size*100))+ "%"); System.out.println(message + " in thread" + Thread.currentThread().getName() + ": " + Math.round(((float) count / size) * 100) + "%"); } } else { // Console.traceln(Level.INFO,("Thread" + // Thread.currentThread().getName() + ": " +Math.round((float) // count/size*100))+ "%"); System.out.println(message + " in thread" + Thread.currentThread().getName() + ": " + Math.round(((float) count / size) * 100) + "%"); } } /** *

* counter for generating unique ids. Starts at 0 for each new program start *

*/ private static int idCounter = 0; public static int missedOptionals = 0; /** *

* prevent instantiation *

*/ private RuleUtils() { // prevent instantiation } }