// 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.IIteration; import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance; 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.IStructuringTemporalRelationship; 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 { /** *

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

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

* 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) { ISequenceInstance subsequence = taskFactory .createNewTaskInstance(model); for (int i = startIndex; i <= endIndex; i++) { taskBuilder.addChild(subsequence, parent.get(i)); } return subsequence; } /** *

* 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) { 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; } ITask tempTask = model.getChildren().get(modelindex); //System.out.println("Trying to add " + parent.get(startIndex) // + " to the model instance " + tempTask.getType()); if (tempTask.getType() == "optionality") { if(((IMarkingTemporalRelationship) tempTask).getMarkedTask() == parent.get(startIndex).getTask()) { System.out.println("Adding OptionalInstance " + parent.get(startIndex) + " to " + tempTask.getType()); 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"); IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask); taskBuilder.addChild(subsequence, optional); modelindex++; missedOptionals++; continue; } } else if (tempTask.getType() == "selection") { ISelectionInstance selection = taskFactory.createNewTaskInstance((ISelection) tempTask); ISelection tmpSel = (ISelection)tempTask; if(tmpSel.getChildren().get(0).getType() == "sequence" && tmpSel.getChildren().get(1).getType()=="sequence") { System.out.println("Adding SequenceInstance " + parent.get(startIndex) + " to " + tempTask); ISequenceInstance selseq = null; System.out.println("IDS: " + parent.get(startIndex).getTask().getId() + " " +((ISequence)tmpSel.getChildren().get(0)).getChildren().get(0).getId()); System.out.println("IDS: " + parent.get(startIndex).getTask().getId() + " " +((ISequence)tmpSel.getChildren().get(1)).getChildren().get(0).getId()); 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 { //throw new Exception("Error while creating subsequence of a selection"); System.out.println("Error while creating subsequence of a selection"); } for (int k=0;k * returns the next available id (uses the id counter) *

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

* prevent instantiation *

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