diff --git a/reana.iml b/reana.iml new file mode 100644 index 0000000..120f59d --- /dev/null +++ b/reana.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/fdtmc/FDTMC.java b/src/fdtmc/FDTMC.java index ef21282..2f3919f 100644 --- a/src/fdtmc/FDTMC.java +++ b/src/fdtmc/FDTMC.java @@ -13,20 +13,19 @@ public class FDTMC { - public static final String INITIAL_LABEL = "initial"; - public static final String SUCCESS_LABEL = "success"; - public static final String ERROR_LABEL = "error"; + public static final String INITIAL_LABEL = "initial"; + public static final String SUCCESS_LABEL = "success"; + public static final String ERROR_LABEL = "error"; private Set states; - private State initialState; - private State successState; - private State errorState; + private State initialState; + private State successState; + private State errorState; private String variableName; private int index; private Map> transitionSystem; private Map> interfaces; - public FDTMC() { states = new LinkedHashSet(); initialState = null; @@ -70,105 +69,110 @@ public State createState(String label) { return temp; } - public State createInitialState() { - State initial = createState(); - setInitialState(initial); - return initial; - } + public State createInitialState() { + State initial = createState(); + setInitialState(initial); + return initial; + } - private void setInitialState(State initialState) { - this.initialState = initialState; - initialState.setLabel(INITIAL_LABEL); - } + private void setInitialState(State initialState) { + this.initialState = initialState; + initialState.setLabel(INITIAL_LABEL); + } - public State getInitialState() { - return initialState; - } + public State getInitialState() { + return initialState; + } - public State createSuccessState() { - State success = createState(); - setSuccessState(success); - createTransition(success, success, "", "1.0"); - return success; - } + public State createSuccessState() { + State success = createState(); + setSuccessState(success); + createTransition(success, success, "", "1.0"); + return success; + } - private void setSuccessState(State successState) { - this.successState = successState; - successState.setLabel(SUCCESS_LABEL); - } + private void setSuccessState(State successState) { + this.successState = successState; + successState.setLabel(SUCCESS_LABEL); + } - public State getSuccessState() { - return successState; - } + public State getSuccessState() { + return successState; + } - public State createErrorState() { - State error = createState(); - setErrorState(error); - createTransition(error, error, "", "1.0"); - return error; - } + public State createErrorState() { + State error = createState(); + setErrorState(error); + createTransition(error, error, "", "1.0"); + return error; + } - private void setErrorState(State errorState) { - this.errorState = errorState; - errorState.setLabel(ERROR_LABEL); - } + private void setErrorState(State errorState) { + this.errorState = errorState; + errorState.setLabel(ERROR_LABEL); + } - public State getErrorState() { - return errorState; - } + public State getErrorState() { + return errorState; + } - public Transition createTransition(State source, State target, String action, String reliability) { - if (source == null) { - return null; - } + public Transition createTransition(State source, State target, + String action, String reliability) { + if (source == null) { + return null; + } - List l = transitionSystem.get(source); - if (l == null) { - l = new LinkedList(); + List transitionsList = transitionSystem.get(source); + if (transitionsList == null) { + transitionsList = new LinkedList(); } - Transition newTransition = new Transition(source, target, action, reliability); - boolean success = l.add(newTransition); - transitionSystem.put(source, l); + Transition newTransition = new Transition(source, target, action, + reliability); + boolean success = transitionsList.add(newTransition); + transitionSystem.put(source, transitionsList); return success ? newTransition : null; } /** * Creates an explicit interface to another FDTMC. - * - * An interface is an FDTMC fragment with 3 states (initial, success, and error) - * and 2 transitions (initial to success with probability {@code id} and initial - * to error with probability 1 - {@code id}). - * - * @param id Identifier of the FDTMC to be abstracted away. - * @param initial Initial state of the interface. - * @param success Success state of the interface. - * @param error Error state of the interface. + * + * An interface is an FDTMC fragment with 3 states (initial, success, and + * error) and 2 transitions (initial to success with probability {@code id} + * and initial to error with probability 1 - {@code id}). + * + * @param id + * Identifier of the FDTMC to be abstracted away. + * @param initial + * Initial state of the interface. + * @param success + * Success state of the interface. + * @param error + * Error state of the interface. */ - public Interface createInterface(String id, State initial, State success, State error) { - Transition successTransition = createTransition(initial, success, "", id); - Transition errorTransition = createTransition(initial, error, "", "1 - " + id); - Interface newInterface = new Interface(id, - initial, - success, - error, - successTransition, - errorTransition); - - List interfaceOccurrences = null; - if (interfaces.containsKey(id)) { - interfaceOccurrences = interfaces.get(id); - } else { - interfaceOccurrences = new LinkedList(); - interfaces.put(id, interfaceOccurrences); - } - interfaceOccurrences.add(newInterface); - return newInterface; + public Interface createInterface(String id, State initial, State success, + State error) { + Transition successTransition = createTransition(initial, success, "", + id); + Transition errorTransition = createTransition(initial, error, "", + "1 - " + id); + Interface newInterface = new Interface(id, initial, success, error, + successTransition, errorTransition); + + List interfaceOccurrences = null; + if (interfaces.containsKey(id)) { + interfaceOccurrences = interfaces.get(id); + } else { + interfaceOccurrences = new LinkedList(); + interfaces.put(id, interfaceOccurrences); + } + interfaceOccurrences.add(newInterface); + return newInterface; } public State getStateByLabel(String label) { - Iterator it = states.iterator(); - while (it.hasNext()){ + Iterator it = states.iterator(); + while (it.hasNext()) { State s = it.next(); if (s.getLabel().equals(label)) return s; @@ -177,251 +181,319 @@ public State getStateByLabel(String label) { } public Transition getTransitionByActionName(String action) { - //para cada Lista de adjacencias de cada nodo - Collection> stateAdjacencies = transitionSystem.values(); - Iterator> iteratorStateAdjacencies = stateAdjacencies.iterator(); + Collection> stateAdjacencies = transitionSystem + .values(); + Iterator> iteratorStateAdjacencies = stateAdjacencies + .iterator(); while (iteratorStateAdjacencies.hasNext()) { List transitions = iteratorStateAdjacencies.next(); - //Percorrer a lista de transicoes e comparar os labels das transicoes - Iterator iteratorTransitions = transitions.iterator(); - while (iteratorTransitions.hasNext()) { - Transition t = iteratorTransitions.next(); - if (t.getActionName().equals(action)) - return t; - } + Transition temporaryTransition = compareTransitionsLabels( + transitions, action); + return temporaryTransition; } return null; + } + private Transition compareTransitionsLabels(List transitions, + String action) { + Iterator iteratorTransitions = transitions.iterator(); + while (iteratorTransitions.hasNext()) { + Transition temporaryTransition = iteratorTransitions.next(); + if (temporaryTransition.getActionName().equals(action)) + return temporaryTransition; + } + return null; + } @Override public String toString() { String msg = new String(); Set tmpStates = this.transitionSystem.keySet(); - Iterator itStates = tmpStates.iterator(); + Iterator itStates = tmpStates.iterator(); + + msg = createTrasitionList(itStates, msg); + + return msg; + } + + private String createTrasitionList(Iterator itStates, String msg) { while (itStates.hasNext()) { State temp = itStates.next(); List transitionList = this.transitionSystem.get(temp); if (transitionList != null) { - Iterator itTransitions = transitionList.iterator(); - while (itTransitions.hasNext()) { - Transition t = itTransitions.next(); - msg += temp.getVariableName() + "=" + temp.getIndex() + ((temp.getLabel() != null) ? "(" + temp.getLabel() + ")" : "") + - " --- " + t.getActionName() + " / " + t.getProbability() + - " ---> " + t.getTarget().getVariableName() + "=" + t.getTarget().getIndex() + ((t.getTarget().getLabel() != null) ? "(" + t.getTarget().getLabel() + ")" : "") + "\n"; - } + Iterator itTransitions = transitionList.iterator(); + msg = createMessage(itTransitions, temp, msg); } } + + return msg; + } + + private String createMessage(Iterator itTransitions, + State temp, String msg) { + while (itTransitions.hasNext()) { + Transition t = itTransitions.next(); + msg += temp.getVariableName() + + "=" + + temp.getIndex() + + ((temp.getLabel() != null) ? "(" + temp.getLabel() + ")" + : "") + + " --- " + + t.getActionName() + + " / " + + t.getProbability() + + " ---> " + + t.getTarget().getVariableName() + + "=" + + t.getTarget().getIndex() + + ((t.getTarget().getLabel() != null) ? "(" + + t.getTarget().getLabel() + ")" : "") + "\n"; + } return msg; } /** - * Two FDTMCs are deemed equal whenever: - * - their states are equal; - * - their initial, success, and error states are equal; - * - the transitions with concrete values are equal; - * - the transitions with variable names have equal source and target states; and - * - the abstracted interfaces are equal. + * Two FDTMCs are deemed equal whenever: - their states are equal; - their + * initial, success, and error states are equal; - the transitions with + * concrete values are equal; - the transitions with variable names have + * equal source and target states; and - the abstracted interfaces are + * equal. */ @Override public boolean equals(Object obj) { - if (obj != null && obj instanceof FDTMC) { - FDTMC other = (FDTMC) obj; - LinkedList> thisInterfaces = new LinkedList>(interfaces.values()); - LinkedList> otherInterfaces = new LinkedList>(other.interfaces.values()); - return states.equals(other.states) - && getInitialState().equals(other.getInitialState()) - && getSuccessState().equals(other.getSuccessState()) - && getErrorState().equals(other.getErrorState()) - && transitionSystem.equals(other.transitionSystem) - && thisInterfaces.equals(otherInterfaces); - } - return false; + if (obj != null && obj instanceof FDTMC) { + FDTMC other = (FDTMC) obj; + LinkedList> thisInterfaces = new LinkedList>( + interfaces.values()); + LinkedList> otherInterfaces = new LinkedList>( + other.interfaces.values()); + + // Variables to test Equality + final boolean isStateEqual = states.equals(other.states); + final boolean isInitialStateEqual = getInitialState().equals( + other.getInitialState()); + final boolean isSuccessStateEqual = getSuccessState().equals( + other.getSuccessState()); + final boolean isErrorStateEqual = getErrorState().equals( + other.getErrorState()); + final boolean isTrasitionSystemEqual = transitionSystem + .equals(other.transitionSystem); + final boolean isInterfaceEqual = thisInterfaces + .equals(otherInterfaces); + + return compareBooleanValuesOfVariables(isStateEqual, + isInitialStateEqual, isSuccessStateEqual, + isErrorStateEqual, isTrasitionSystemEqual, isInterfaceEqual); + + } + return false; + } + + private Boolean compareBooleanValuesOfVariables(Boolean isStateEqual, + Boolean isInitialStateEqual, Boolean isSuccessStateEqual, + Boolean isErrorStateEqual, Boolean isTrasitionSystemEqual, + Boolean isInterfaceEqual) { + + return isStateEqual && isInitialStateEqual && isSuccessStateEqual + && isErrorStateEqual && isTrasitionSystemEqual + && isInterfaceEqual; } @Override - public int hashCode() { - return states.hashCode() + transitionSystem.hashCode() + interfaces.hashCode(); - } + public int hashCode() { + return states.hashCode() + transitionSystem.hashCode() + + interfaces.hashCode(); + } - public Map> getTransitions() { + public Map> getTransitions() { return transitionSystem; } /** - * Inlines the given FDTMCs whenever there is an interface corresponding - * to the string in the respective index. - * + * Inlines the given FDTMCs whenever there is an interface corresponding to + * the string in the respective index. + * * @param indexedModels - * @return a new FDTMC which represents this one with the ones specified - * in {@code indexedModels} inlined. + * @return a new FDTMC which represents this one with the ones specified in + * {@code indexedModels} inlined. */ - public FDTMC inline(Map indexedModels) { - FDTMC inlined = new FDTMC(); - Map statesMapping = copyForInlining(inlined); - - for (Map.Entry> entry: interfaces.entrySet()) { - String dependencyId = entry.getKey(); - if (indexedModels.containsKey(dependencyId)) { - FDTMC fragment = indexedModels.get(dependencyId); - for (Interface iface: entry.getValue()) { - inlined.inlineInterface(iface, - fragment, - statesMapping); - } - } - } - return inlined; - } + public FDTMC inline(Map indexedModels) { + FDTMC inlined = new FDTMC(); - /** - * Inlines the given FDTMCs whenever there is an interface corresponding - * to the string in the respective index. - * - * This method maintains the variability notion by using the same abstraction - * id of the interface as an encoding of presence (i.e., a "switch" on whether - * or not to take the transitions of the inlined model). - * - * @param indexedModels - * @return a new FDTMC which represents this one with the ones specified - * in {@code indexedModels} inlined. - */ - public FDTMC inlineWithVariability(Map indexedModels) { - FDTMC inlined = new FDTMC(); - Map statesMapping = copyForInlining(inlined); - - for (Map.Entry> entry: interfaces.entrySet()) { - String dependencyId = entry.getKey(); - if (indexedModels.containsKey(dependencyId)) { - FDTMC fragment = indexedModels.get(dependencyId); - for (Interface iface: entry.getValue()) { - inlined.inlineInterfaceWithVariability(iface, - fragment, - statesMapping); - } - } - } - return inlined; - } + inlined = scrollInterfaceList(inlined, indexedModels, false); - /** - * Prepares {@code destination} FDTMC to be an inlined version of this one. - * @param destination - * @return a mapping from states in this FDTMC to the corresponding states - * in the copied one ({@code destination}). - */ - private Map copyForInlining(FDTMC destination) { - destination.variableName = this.getVariableName(); - - Map statesMapping = destination.inlineStates(this); - destination.setInitialState(statesMapping.get(this.getInitialState())); - destination.setSuccessState(statesMapping.get(this.getSuccessState())); - destination.setErrorState(statesMapping.get(this.getErrorState())); - - destination.inlineTransitions(this, statesMapping); - return statesMapping; - } + return inlined; + } - /** - * Inlines all states from {@code fdtmc} stripped of their labels. - * @param fdtmc - * @return - */ - private Map inlineStates(FDTMC fdtmc) { - Map statesOldToNew = new HashMap(); - for (State state: fdtmc.getStates()) { - State newState = this.createState(); - statesOldToNew.put(state, newState); - } - return statesOldToNew; - } + private FDTMC scrollInterfaceList(FDTMC inlined, + Map indexedModels, + Boolean isInlineInterfaceWithVariability) { - /** - * Inlines all transitions from {@code fdtmc} that are not part of an interface. - * - * @param fdtmc - * @param statesOldToNew - */ - private void inlineTransitions(FDTMC fdtmc, Map statesOldToNew) { - Set interfaceTransitions = fdtmc.getInterfaceTransitions(); - for (Map.Entry> entry : fdtmc.getTransitions().entrySet()) { - State newSource = statesOldToNew.get(entry.getKey()); - List transitions = entry.getValue(); - if (transitions != null) { - for (Transition transition : transitions) { - if (!interfaceTransitions.contains(transition)) { - State newTarget = statesOldToNew.get(transition.getTarget()); - createTransition(newSource, - newTarget, - transition.getActionName(), - transition.getProbability()); - } - } - } - } - } + Map statesMapping = copyForInlining(inlined); - private void inlineInterface(Interface iface, FDTMC fragment, Map statesMapping) { - Map fragmentStatesMapping = this.inlineStates(fragment); - this.inlineTransitions(fragment, fragmentStatesMapping); - - State initialInlined = iface.getInitial(); - State initialFragment = fragment.getInitialState(); - State successInlined = iface.getSuccess(); - State successFragment = fragment.getSuccessState(); - State errorInlined = iface.getError(); - State errorFragment = fragment.getErrorState(); - - this.createTransition(statesMapping.get(initialInlined), - fragmentStatesMapping.get(initialFragment), - "", - "1"); - this.createTransition(fragmentStatesMapping.get(successFragment), - statesMapping.get(successInlined), - "", - "1"); - if (errorFragment != null) { - this.createTransition(fragmentStatesMapping.get(errorFragment), - statesMapping.get(errorInlined), - "", - "1"); - } - } + for (Map.Entry> entry : interfaces.entrySet()) { + String dependencyId = entry.getKey(); + iterateModelsDependencies(inlined, indexedModels, + isInlineInterfaceWithVariability, statesMapping, entry, + dependencyId); + } - private void inlineInterfaceWithVariability(Interface iface, FDTMC fragment, Map statesMapping) { - Map fragmentStatesMapping = this.inlineStates(fragment); - this.inlineTransitions(fragment, fragmentStatesMapping); - - State initialInlined = iface.getInitial(); - State initialFragment = fragment.getInitialState(); - State successInlined = iface.getSuccess(); - State successFragment = fragment.getSuccessState(); - State errorInlined = iface.getError(); - State errorFragment = fragment.getErrorState(); - - this.createTransition(statesMapping.get(initialInlined), - fragmentStatesMapping.get(initialFragment), - "", - iface.getAbstractedId()); - this.createTransition(statesMapping.get(initialInlined), - statesMapping.get(successInlined), - "", - "1 - " + iface.getAbstractedId()); - this.createTransition(fragmentStatesMapping.get(successFragment), - statesMapping.get(successInlined), - "", - "1"); - if (errorFragment != null) { - this.createTransition(fragmentStatesMapping.get(errorFragment), - statesMapping.get(errorInlined), - "", - "1"); - } - } + return inlined; + } + + private void iterateModelsDependencies(FDTMC inlined, + Map indexedModels, + Boolean isInlineInterfaceWithVariability, + Map statesMapping, + Map.Entry> entry, String dependencyId) { + if (indexedModels.containsKey(dependencyId)) { + FDTMC fragment = indexedModels.get(dependencyId); + for (Interface iface : entry.getValue()) { + checkForInlineInterfaceType(inlined, + isInlineInterfaceWithVariability, statesMapping, + fragment, iface); + } + } + } + + private void checkForInlineInterfaceType(FDTMC inlined, + Boolean isInlineInterfaceWithVariability, + Map statesMapping, FDTMC fragment, Interface iface) { + if (!isInlineInterfaceWithVariability) { + inlined.inlineInterface(iface, fragment, statesMapping); + } else { + inlined.inlineInterfaceWithVariability(iface, fragment, + statesMapping); + } + } + + /** + * Inlines the given FDTMCs whenever there is an interface corresponding to + * the string in the respective index. + * + * This method maintains the variability notion by using the same + * abstraction id of the interface as an encoding of presence (i.e., a + * "switch" on whether or not to take the transitions of the inlined model). + * + * @param indexedModels + * @return a new FDTMC which represents this one with the ones specified in + * {@code indexedModels} inlined. + */ + public FDTMC inlineWithVariability(Map indexedModels) { + FDTMC inlined = new FDTMC(); + + inlined = scrollInterfaceList(inlined, indexedModels, true); + + return inlined; + } + + /** + * Prepares {@code destination} FDTMC to be an inlined version of this one. + * + * @param destination + * @return a mapping from states in this FDTMC to the corresponding states + * in the copied one ({@code destination}). + */ + private Map copyForInlining(FDTMC destination) { + destination.variableName = this.getVariableName(); + + Map statesMapping = destination.inlineStates(this); + destination.setInitialState(statesMapping.get(this.getInitialState())); + destination.setSuccessState(statesMapping.get(this.getSuccessState())); + destination.setErrorState(statesMapping.get(this.getErrorState())); + + destination.inlineTransitions(this, statesMapping); + return statesMapping; + } + + /** + * Inlines all states from {@code fdtmc} stripped of their labels. + * + * @param fdtmc + * @return + */ + private Map inlineStates(FDTMC fdtmc) { + Map statesOldToNew = new HashMap(); + for (State state : fdtmc.getStates()) { + State newState = this.createState(); + statesOldToNew.put(state, newState); + } + return statesOldToNew; + } + + /** + * Inlines all transitions from {@code fdtmc} that are not part of an + * interface. + * + * @param fdtmc + * @param statesOldToNew + */ + private void inlineTransitions(FDTMC fdtmc, Map statesOldToNew) { + Set interfaceTransitions = fdtmc.getInterfaceTransitions(); + for (Map.Entry> entry : fdtmc.getTransitions() + .entrySet()) { + State newSource = statesOldToNew.get(entry.getKey()); + List transitions = entry.getValue(); + if (transitions != null) { + for (Transition transition : transitions) { + if (!interfaceTransitions.contains(transition)) { + State newTarget = statesOldToNew.get(transition + .getTarget()); + createTransition(newSource, newTarget, + transition.getActionName(), + transition.getProbability()); + } + } + } + } + } + + private void inlineInterface(Interface iface, FDTMC fragment, + Map statesMapping) { + chooseInlineInterface(iface, fragment, statesMapping, false); + } + + private void chooseInlineInterface(Interface iface, FDTMC fragment, + Map statesMapping, Boolean isWithVariability) { + + Map fragmentStatesMapping = this.inlineStates(fragment); + this.inlineTransitions(fragment, fragmentStatesMapping); + + State initialInlined = iface.getInitial(); + State initialFragment = fragment.getInitialState(); + State successInlined = iface.getSuccess(); + State successFragment = fragment.getSuccessState(); + State errorInlined = iface.getError(); + State errorFragment = fragment.getErrorState(); + String ifaceID = "1"; + + if (isWithVariability) { + ifaceID = iface.getAbstractedId(); + this.createTransition(statesMapping.get(initialInlined), + statesMapping.get(successInlined), "", + "1 - " + ifaceID); + } + + this.createTransition(statesMapping.get(initialInlined), + fragmentStatesMapping.get(initialFragment), "", ifaceID); + this.createTransition(fragmentStatesMapping.get(successFragment), + statesMapping.get(successInlined), "", "1"); - private Set getInterfaceTransitions() { + if (errorFragment != null) { + this.createTransition(fragmentStatesMapping.get(errorFragment), + statesMapping.get(errorInlined), "", "1"); + } + } + + private void inlineInterfaceWithVariability(Interface iface, + FDTMC fragment, Map statesMapping) { + chooseInlineInterface(iface, fragment, statesMapping, true); + } + + private Set getInterfaceTransitions() { Set transitions = new HashSet(); interfaces.values().stream().flatMap(List::stream) .forEach(iface -> { @@ -430,5 +502,4 @@ private Set getInterfaceTransitions() { }); return transitions; } - } diff --git a/src/fdtmc/InitialStateInlineInterface.java b/src/fdtmc/InitialStateInlineInterface.java new file mode 100644 index 0000000..bf21e35 --- /dev/null +++ b/src/fdtmc/InitialStateInlineInterface.java @@ -0,0 +1,33 @@ +package fdtmc; + +public class InitialStateInlineInterface { + + private Interface iface; + private FDTMC fragment; + + public State getInitialInlined() { + return this.iface.getInitial(); + } + + public State getInitialFragment() { + return this.fragment.getInitialState(); + } + public State getSuccessInlined() { + return this.iface.getSuccess(); + } + public State getSuccessFragment() { + return this.fragment.getSuccessState(); + } + public State getErrorInlined() { + return this.iface.getError(); + } + public State getErrorFragment() { + return this.fragment.getErrorState(); + } + + public InitialStateInlineInterface(Interface iface, FDTMC fragment) { + this.iface = iface; + this.fragment = fragment; + } + +} diff --git a/src/tool/RDGNode.java b/src/tool/RDGNode.java index 180097c..3287d9b 100644 --- a/src/tool/RDGNode.java +++ b/src/tool/RDGNode.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map; +import com.sun.org.apache.bcel.internal.generic.NEW; import fdtmc.FDTMC; @@ -100,13 +101,14 @@ public static String getNextId() { */ @Override public boolean equals(Object obj) { + boolean isEqual = false; if (obj != null && obj instanceof RDGNode) { RDGNode other = (RDGNode) obj; - return this.getPresenceCondition().equals(other.getPresenceCondition()) + isEqual = this.getPresenceCondition().equals(other.getPresenceCondition()) && this.getFDTMC().equals(other.getFDTMC()) && this.getDependencies().equals(other.getDependencies()); } - return false; + return isEqual; } @Override @@ -143,21 +145,26 @@ public List getDependenciesTransitiveClosure() throws CyclicRdgExceptio * @throws CyclicRdgException */ private void topoSortVisit(RDGNode node, Map marks, List sorted) throws CyclicRdgException { - if (marks.containsKey(node) && marks.get(node) == false) { + if (marks.containsKey(node) && !marks.get(node)) { // Visiting temporarily marked node -- this means a cyclic dependency! throw new CyclicRdgException(); } else if (!marks.containsKey(node)) { // Mark node temporarily (cycle detection) marks.put(node, false); - for (RDGNode child: node.getDependencies()) { - topoSortVisit(child, marks, sorted); - } + visitChildrenNodes(node, marks, sorted); // Mark node permanently (finished sorting branch) marks.put(node, true); sorted.add(node); } } + private void visitChildrenNodes(RDGNode node, Map marks, + List sorted) throws CyclicRdgException { + for (RDGNode child: node.getDependencies()) { + topoSortVisit(child, marks, sorted); + } + } + /** * Computes the number of paths from source nodes to every known node. * @return A map associating an RDGNode to the corresponding number @@ -177,7 +184,7 @@ public Map getNumberOfPaths() throws CyclicRdgException { // TODO Parameterize topological sort of RDG. private static Map numPathsVisit(RDGNode node, Map marks, Map> cache) throws CyclicRdgException { - if (marks.containsKey(node) && marks.get(node) == false) { + if (marks.containsKey(node) && !marks.get(node)) { // Visiting temporarily marked node -- this means a cyclic dependency! throw new CyclicRdgException(); } else if (!marks.containsKey(node)) { @@ -211,16 +218,21 @@ private static Map numPathsVisit(RDGNode node, Map sumPaths(Map pathsCountA, Map pathsCountB) { Map numberOfPaths = new HashMap(pathsCountA); for (Map.Entry entry: pathsCountB.entrySet()) { - RDGNode node = entry.getKey(); - Integer count = entry.getValue(); - if (numberOfPaths.containsKey(node)) { - count += numberOfPaths.get(node); - } - numberOfPaths.put(node, count); + numberOfPathsForNode(numberOfPaths, entry); } return numberOfPaths; } + private static void numberOfPathsForNode(Map numberOfPaths, + Map.Entry entry) { + RDGNode node = entry.getKey(); + Integer count = entry.getValue(); + if (numberOfPaths.containsKey(node)) { + count += numberOfPaths.get(node); + } + numberOfPaths.put(node, count); + } + /** * Returns the first RDG node (in crescent order of creation time) which is similar * to the one provided. @@ -230,12 +242,18 @@ private static Map sumPaths(Map pathsCountA, * @return a similar RDG node or null in case there is none. */ public static RDGNode getSimilarNode(RDGNode target) { + RDGNode rdgNode = null; for (RDGNode candidate: nodesInCreationOrder) { - if (candidate != target && candidate.equals(target)) { - return candidate; - } + rdgNode = checkSimilarNode(target, rdgNode, candidate); + } + return rdgNode; + } + + private static RDGNode checkSimilarNode(RDGNode target, RDGNode rdgNode, RDGNode candidate) { + if (candidate != target && candidate.equals(target)) { + rdgNode = candidate; } - return null; + return rdgNode; } }