Skip to content

Commit

Permalink
use buffer append in dot generation
Browse files Browse the repository at this point in the history
equals test added
  • Loading branch information
Felix Reimann committed Jul 4, 2017
1 parent 407a354 commit 44046fd
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 138 deletions.
70 changes: 34 additions & 36 deletions src/main/java/org/jreliability/bdd/BDDs.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class BDDs {
/**
* The platform-independent newline symbol.
*/
protected static String newline = System.getProperty("line.separator");
protected static final String newline = System.getProperty("line.separator");

/**
* Returns all variables (elements) {@code T} included in the {@code BDD}.
Expand Down Expand Up @@ -89,6 +89,8 @@ public static <T> Set<BDD<T>> getNodes(T t, BDD<T> bdd) {
* the comparator ("&lt;","&lt;=","=","&gt;=","&gt;")
* @param rhs
* the right hand side value
* @param provider
* the bdd provider
* @return the BDD representing this linear constraint
*/
public static <T> BDD<T> getBDD(List<Integer> coeffs, List<BDD<T>> vars, LinearTerm.Comparator comp, int rhs,
Expand All @@ -115,8 +117,8 @@ public static <T> BDD<T> getBDD(List<Integer> coeffs, List<BDD<T>> vars, LinearT
BDDConstraint<T> constraint = new BDDConstraint<>(rhs, lits);

/*
* Handle the case that the lhs is empty and is, thus, 0! If 0 >=
* rhs, return true BDD; else return false BDD
* Handle the case that the lhs is empty and is, thus, 0! If 0 >= rhs, return true BDD; else return false
* BDD
*/
if (constraint.getLhs().isEmpty()) {
if (0 >= constraint.getRhs()) {
Expand Down Expand Up @@ -147,6 +149,8 @@ public static <T> BDD<T> getBDD(List<Integer> coeffs, List<BDD<T>> vars, LinearT
* the type of variables
* @param constraint
* the greater-equal constraint
* @param provider
* the bdd provider
* @return the bdd representation of the given constraint
*/
protected static <T> BDD<T> getConstraintBDD(BDDConstraint<T> constraint, BDDProvider<T> provider) {
Expand All @@ -172,8 +176,7 @@ public int compare(Literal<T> o1, Literal<T> o2) {
}

/**
* Returns a graphical representation of the {@code BDD} in the {@code DOT}
* input format.
* Returns a graphical representation of the {@code BDD} in the {@code DOT} input format.
*
* @param <T>
* the type of variable
Expand Down Expand Up @@ -201,9 +204,8 @@ public static <T> String toDot(BDD<T> bdd) {
}

/**
* Calculates the top event of the {@code BDD} based on a
* functionTransformer that delivers for each variable {@code T} a double
* value.
* Calculates the top event of the {@code BDD} based on a functionTransformer that delivers for each variable
* {@code T} a double value.
*
* @param <T>
* the type of variable
Expand Down Expand Up @@ -318,8 +320,8 @@ protected static <T> double evaluate(BDD<T> bdd, Transformer<T, Double> transfor
}

/**
* Returns a {@code greater-equal} constraint represented as a {@code BDD}
* via a recursive procedure proposed by {@code Een & Soerrensson 2006}.
* Returns a {@code greater-equal} constraint represented as a {@code BDD} via a recursive procedure proposed by
* {@code Een & Soerrensson 2006}.
*
* @param <T>
* the type of variables
Expand Down Expand Up @@ -365,8 +367,7 @@ protected static <T> BDD<T> buildConstraintBDD(List<Literal<T>> literals, int rh
}

/**
* Traverses the {@code BDD} to collects all nodes for the {@code DOT}
* representation.
* Traverses the {@code BDD} to collects all nodes for the {@code DOT} representation.
*
* @param <T>
* the type of variables
Expand All @@ -385,14 +386,14 @@ protected static <T> void collectDotNodes(BDD<T> bdd, StringBuffer dot, Map<BDD<
return;
} else if (bdd.isOne()) {
dot.append(
"one [label = \"1\", rank = sink, shape = box, style = filled, color = black, fontcolor = white];"
+ newline);
"one [label = \"1\", rank = sink, shape = box, style = filled, color = black, fontcolor = white];")
.append(newline);
variables.put(bdd, "one");
return;
} else if (bdd.isZero()) {
dot.append(
"zero [label = \"0\", rank = sink, shape = box, style = filled, color = black, fontcolor = white];"
+ newline);
"zero [label = \"0\", rank = sink, shape = box, style = filled, color = black, fontcolor = white];")
.append(newline);
variables.put(bdd, "zero");
return;
}
Expand All @@ -404,16 +405,15 @@ protected static <T> void collectDotNodes(BDD<T> bdd, StringBuffer dot, Map<BDD<
id = id.replaceAll(" |-", "_");
String variable = "n" + id + count;
variables.put(bdd, variable);
dot.append(variable + " [label = \"" + t.toString() + "\", style = filled, fillcolor = gray95, color = black];"
+ newline);
dot.append(variable).append(" [label = \"").append(t.toString())
.append("\", style = filled, fillcolor = gray95, color = black];").append(newline);

collectDotNodes(bdd.high(), dot, variables, counters);
collectDotNodes(bdd.low(), dot, variables, counters);
}

/**
* Traverses the {@code BDD} to collects all edges for the {@code DOT}
* representation.
* Traverses the {@code BDD} to collects all edges for the {@code DOT} representation.
*
* @param <T>
* the type of variable
Expand All @@ -439,8 +439,10 @@ protected static <T> void collectDotEdges(BDD<T> bdd, StringBuffer dot, Map<BDD<
String highVariable = variables.get(high);
String lowVariable = variables.get(low);

dot.append(variable + " -> " + highVariable + " [style = solid, arrowsize = 0.8];" + newline);
dot.append(variable + " -> " + lowVariable + " [style = dashed, arrowsize = 0.8];" + newline);
dot.append(variable).append(" -> ").append(highVariable).append(" [style = solid, arrowsize = 0.8];")
.append(newline);
dot.append(variable).append(" -> ").append(lowVariable).append(" [style = dashed, arrowsize = 0.8];")
.append(newline);

considered.add(bdd);

Expand All @@ -449,8 +451,7 @@ protected static <T> void collectDotEdges(BDD<T> bdd, StringBuffer dot, Map<BDD<
}

/**
* Traverses the {@code BDD} to setup the correct ranks of all nodes
* belonging to the same variable.
* Traverses the {@code BDD} to setup the correct ranks of all nodes belonging to the same variable.
*
* @param <T>
* the type of variable
Expand All @@ -471,7 +472,8 @@ protected static <T> void collectDotMarkers(BDD<T> bdd, StringBuffer dot, Map<T,
String id = t.toString();
id = id.replaceAll(" |-", "_");
String variable = "marker" + id;
dot.append(variable + " [label = \"" + t.toString() + "\", shape = plaintext];" + newline);
dot.append(variable).append(" [label = \"").append(t.toString()).append("\", shape = plaintext];")
.append(newline);
tmpList.add(t);
markers.put(t, variable);
}
Expand All @@ -483,14 +485,13 @@ protected static <T> void collectDotMarkers(BDD<T> bdd, StringBuffer dot, Map<T,
T next = iterator.next();
String currentVariable = markers.get(current);
String nextVariable = markers.get(next);
dot.append(currentVariable + " -> " + nextVariable + " [style = invis];" + newline);
dot.append(currentVariable).append(" -> ").append(nextVariable).append(" [style = invis];").append(newline);
current = next;
}
}

/**
* Traverses the {@code BDD} to setup the correct ranks of all nodes
* belonging to the same variable.
* Traverses the {@code BDD} to setup the correct ranks of all nodes belonging to the same variable.
*
* @param <T>
* the type of variable
Expand All @@ -511,14 +512,13 @@ protected static <T> void collectDotRanks(BDD<T> bdd, StringBuffer dot, Map<BDD<
String variable = entry.getValue();
Set<BDD<T>> nodes = getNodes(t, bdd);

String tmpDot = "{ rank = same; " + variable + "; ";
dot.append("{ rank = same; ").append(variable).append("; ");
for (BDD<T> nodeBDD : nodes) {
String nodeVariable = variables.get(nodeBDD);
tmpDot += nodeVariable + "; ";
dot.append(nodeVariable).append("; ");
}

tmpDot += "}" + newline;
dot.append(tmpDot);
dot.append("}").append(newline);
}
}

Expand Down Expand Up @@ -555,8 +555,7 @@ protected static <T> void collectVariables(BDD<T> bdd, Set<T> variables, Set<BDD
}

/**
* Traverses the {@code BDD} to collect all variables in the current
* variable order of the {@code BDD}.
* Traverses the {@code BDD} to collect all variables in the current variable order of the {@code BDD}.
*
* @param <T>
* the type of variables
Expand All @@ -583,8 +582,7 @@ protected static <T> void collectVariablesSorted(BDD<T> bdd, List<T> variables)
}

/**
* Traverses the {@code BDD} to collect all nodes for a given variable
* {@code T}.
* Traverses the {@code BDD} to collect all nodes for a given variable {@code T}.
*
* @param <T>
* the type of variables
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/org/jreliability/bdd/javabdd/JBDDProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ public class JBDDProvider<T> implements BDDProvider<T> {
* The offset of the variables.
*/
protected int variableOffset = 0;
/**
* The used {@code Type} of real {@code BDD} implementation.
*/
protected Type type;

/**
* The used {@code BDDFactory}.
*/
Expand Down Expand Up @@ -69,8 +66,6 @@ public class JBDDProvider<T> implements BDDProvider<T> {
* the number of variables
*/
public JBDDProvider(Type type, int vars) {
this.type = type;

switch (type) {
case JDD:
factory = JDDFactory.init(200000, 200000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public enum Type {
/**
* The number of initially allocated variables.
*/
protected static int INITIAL_VARIABLES = 10;
protected static final int INITIAL_VARIABLES = 10;
/**
* A map that provides each requested {@code Type} of real {@code BDD} implementation with its specific
* {@code JBDDProvider}.
Expand Down
49 changes: 21 additions & 28 deletions src/main/java/org/jreliability/evaluator/MomentEvaluator.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
/**
* JReliability is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
* JReliability is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* JReliability is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
* JReliability is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Opt4J. If not, see http://www.gnu.org/licenses/.
* You should have received a copy of the GNU Lesser General Public License along with Opt4J. If not, see
* http://www.gnu.org/licenses/.
*/
package org.jreliability.evaluator;

import org.jreliability.function.Function;
import org.jreliability.function.ReliabilityFunction;

/**
* The {@code MomentEvaluator} determines the {@code n}-th {@code Moment} of a
* density function {@code f(x)} given a {@code ReliabilityFunction} {@code
* The {@code MomentEvaluator} determines the {@code n}-th {@code Moment} of a density function {@code f(x)} given a
* {@code ReliabilityFunction} {@code
* R(x)}.
* <p>
* E(X^n)={@code integral_0^infinity x^n f(x) dx}.
* <p>
* It performs an integration from {@code 0} to {@code infinity} using Rombergs
* integration. This is commonly used to derived measures like, e.g., Mean Time
* To Failure (MTTF) (E(X)) and its variance (E(X^2)-E(X)^2).
* It performs an integration from {@code 0} to {@code infinity} using Rombergs integration. This is commonly used to
* derived measures like, e.g., Mean Time To Failure (MTTF) (E(X)) and its variance (E(X^2)-E(X)^2).
*
* @author glass, lukasiewycz
*
Expand All @@ -39,7 +36,7 @@ public class MomentEvaluator implements Evaluator {
* @author lukasiewycz
*
*/
class MomentFunction implements Function {
static class MomentFunction implements Function {

private ReliabilityFunction reliabilityFunction;
private int n;
Expand All @@ -62,6 +59,7 @@ public MomentFunction(ReliabilityFunction reliabilityFunction, int n) {
*
* @see org.jreliability.function.Function#getY(double)
*/
@Override
public double getY(double x) {
return n * Math.pow(x, n - 1) * reliabilityFunction.getY(x);
}
Expand All @@ -79,8 +77,8 @@ public double getY(double x) {
protected final int n;

/**
* Constructs a {@code MomentEvaluator} for the given {@code n}-th moment
* and a maximum error / {@code epsilon} of {@code 1.0E-5}.
* Constructs a {@code MomentEvaluator} for the given {@code n}-th moment and a maximum error / {@code epsilon} of
* {@code 1.0E-5}.
*
* @param n
* the n value
Expand All @@ -90,8 +88,7 @@ public MomentEvaluator(int n) {
}

/**
* Constructs a {@code MomentEvaluator} for the given {@code n}-th moment
* and a maximum error {@code epsilon}.
* Constructs a {@code MomentEvaluator} for the given {@code n}-th moment and a maximum error {@code epsilon}.
*
* @param n
* the n value
Expand All @@ -105,8 +102,7 @@ public MomentEvaluator(int n, double epsilon) {
this.n = n;
this.epsilon = epsilon;
if (n < 1) {
throw new IllegalArgumentException(
"An n-th moment with n < 1 is undefined.");
throw new IllegalArgumentException("An n-th moment with n < 1 is undefined.");
}
}

Expand All @@ -125,13 +121,11 @@ public double evaluate(ReliabilityFunction reliabilityFunction) {
}

/**
* Returns the calculated upper bound that will be used in the integration
* process.
* Returns the calculated upper bound that will be used in the integration process.
*
* @param reliabilityFunction
* the reliabilityFunction
* @return the calculated upper bound that will be used in the integration
* process
* @return the calculated upper bound that will be used in the integration process
*/
public double getUpperBound(ReliabilityFunction reliabilityFunction) {
double upperBound = 0.5;
Expand Down Expand Up @@ -159,8 +153,7 @@ public double getUpperBound(ReliabilityFunction reliabilityFunction) {
* the upper bound
* @return the value of the integral between a and b
*/
protected double integrate(ReliabilityFunction reliabilityFunction,
double a, double b) {
protected double integrate(ReliabilityFunction reliabilityFunction, double a, double b) {
Function f = new MomentFunction(reliabilityFunction, n);

IntegralEvaluator integral = new IntegralEvaluator(epsilon);
Expand Down
Loading

0 comments on commit 44046fd

Please sign in to comment.