Mercurial > hg > Members > kono > jpf-core
changeset 20:b1790909ebb1
changed EventForest to derive from EventTree, which kills three birds with one
stone - guaranteeing there is a default tree, being able to initialize
a concrete forest with one method (createRoot), and being able to use both
EventTree and EventForest with a ForestCGFactory, i.e. without changes to the
config other than the event tree class name
slight refactoring of EventTree (createTree() -> createRoot()) to make it more
obvious how the result is used
author | Peter Mehlitz <pcmehlitz@gmail.com> |
---|---|
date | Mon, 06 Apr 2015 12:08:03 -0700 |
parents | 9fea3d8da9aa |
children | caa0924e093d |
files | src/main/gov/nasa/jpf/util/event/Event.java src/main/gov/nasa/jpf/util/event/EventChoiceGenerator.java src/main/gov/nasa/jpf/util/event/EventConstructor.java src/main/gov/nasa/jpf/util/event/EventForest.java src/main/gov/nasa/jpf/util/event/EventTree.java src/tests/gov/nasa/jpf/test/mc/data/EventGeneratorTest.java src/tests/gov/nasa/jpf/util/event/EventTreeTest.java |
diffstat | 7 files changed, 118 insertions(+), 76 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/gov/nasa/jpf/util/event/Event.java Wed Apr 01 12:14:15 2015 -0700 +++ b/src/main/gov/nasa/jpf/util/event/Event.java Mon Apr 06 12:08:03 2015 -0700 @@ -207,7 +207,15 @@ } } - + + public Event clone(){ + try { + return (Event) super.clone(); + } catch (CloneNotSupportedException cnsx){ + throw new RuntimeException("Event clone failed"); + } + } + public Event deepClone(){ try { Event e = (Event)super.clone();
--- a/src/main/gov/nasa/jpf/util/event/EventChoiceGenerator.java Wed Apr 01 12:14:15 2015 -0700 +++ b/src/main/gov/nasa/jpf/util/event/EventChoiceGenerator.java Mon Apr 06 12:08:03 2015 -0700 @@ -168,7 +168,7 @@ @Override public int getTotalNumberOfChoices () { - return base.getNumberOfAlternatives(); + return base.getNumberOfAlternatives() + 1; // include base itself } @Override
--- a/src/main/gov/nasa/jpf/util/event/EventConstructor.java Wed Apr 01 12:14:15 2015 -0700 +++ b/src/main/gov/nasa/jpf/util/event/EventConstructor.java Mon Apr 06 12:08:03 2015 -0700 @@ -24,21 +24,21 @@ * this factors out constructor methods so that they can be used inside of * EventTrees and EventForests */ -public abstract class EventConstructor { +public interface EventConstructor { //--- overridable event factory method to facilitate creation of specialized event classes - protected Event event (String name){ + default Event event (String name) { return new Event(name, this); } - protected Event event (String name, Object... arguments){ + default Event event (String name, Object... arguments){ return new Event(name, arguments, this); } //--- compound constructors that create sets of events - protected Event alternatives (Event... events){ + default Event alternatives (Event... events){ Event last = events[0]; for (int i = 1; i < events.length; i++) { Event e = events[i]; @@ -48,7 +48,7 @@ return events[0]; } - protected Event sequence (Event... events) { + default Event sequence (Event... events) { Event base = events[0]; for (int i = 1; i < events.length; i++) { @@ -57,7 +57,7 @@ return base; } - protected Event iteration (int count, Event... events) { + default Event iteration (int count, Event... events) { Event seq = sequence(events); Event[] it = new Event[count]; @@ -72,7 +72,7 @@ /** * an alterative of all combinations of the specified events (regardless of order) */ - protected Event anyCombination (Event... events){ + default Event anyCombination (Event... events){ int n = events.length; int max = 0; for (int i=0; i<n; i++){ @@ -102,7 +102,7 @@ } - protected void generatePermutation (int length, Event[] events, Event anchor, Event perm){ + default void generatePermutation (int length, Event[] events, Event anchor, Event perm){ if (length == 0){ anchor.addAlternative(perm); @@ -131,7 +131,7 @@ * generate tree with all event permutations without repetitions. * <2do> this is not particularly optimized */ - protected Event anyPermutation (Event... events){ + default Event anyPermutation (Event... events){ Event a = new NoEvent(); generatePermutation( events.length, events, a, null); return a.getAlt();
--- a/src/main/gov/nasa/jpf/util/event/EventForest.java Wed Apr 01 12:14:15 2015 -0700 +++ b/src/main/gov/nasa/jpf/util/event/EventForest.java Mon Apr 06 12:08:03 2015 -0700 @@ -22,47 +22,49 @@ /** * a forest of named event trees + * * This class mostly exists for the purpose of tree construction, which happens from respective ctors like * - * MyEventForest (){ - * addDefault( - * sequence( - * event(..), - * .. - * ); - * - * addTree( "someState", - * sequence( - * event(..), - * ... - * ); - * - * addTree( "someOtherState", - * ... + * EventForest myForest = new EventForest(){ + * @Override + * protected Event createRoot(){ + * addRoot("someState", + * sequence( + * event(..), + * ... + * ); + * + * addRoot("someOtherState", + * ... + * + * return sequence( ... ); // default tree * } * - * Used by CompoundEventChoiceGenerator */ -public abstract class EventForest extends EventConstructor { +public abstract class EventForest extends EventTree { + + protected HashMap<String,Event> rootMap; + + //--- construction - protected Event defaultTree; - protected HashMap<String,Event> map = new HashMap<String,Event>(); - - // map to be populated by subclass ctors - - protected void add (String name, Event root){ - map.put(name, root); + /** + * usually called from createRootEvent() + */ + public void addRoot (String name, Event nextRoot){ + if (rootMap == null){ + rootMap = new HashMap<>(); + } + rootMap.put(name, nextRoot); } - - protected void addDefault( Event root){ - defaultTree = root; - } - - public Event getDefault(){ - return defaultTree; - } - - public Event get (String name){ - return map.get(name); + + + //--- accessors + + public Event getRoot (String name) { + if (rootMap != null) { + return rootMap.get(name); + } else { + return null; + } } }
--- a/src/main/gov/nasa/jpf/util/event/EventTree.java Wed Apr 01 12:14:15 2015 -0700 +++ b/src/main/gov/nasa/jpf/util/event/EventTree.java Mon Apr 06 12:08:03 2015 -0700 @@ -25,24 +25,56 @@ * an abstract class that creates Event trees * * While there is no need to provide specialized Event types or additional event - * constructors, concrete subclasses have to provide a createEventTree() implementation. + * constructors, concrete subclasses have to provide a createRoot() implementation. * * A typical implementation looks like this - * - * public Event createEventTree1() { - * return sequence( + * + * class MyTree extends EventTree { + * @Override + * public Event createRoot() { + * return sequence( + * event("a"), + * alternatives( + * event("1"), + * iteration(2, + * event("x") + * ) + * ), + * event("b") + * ); + * } + * } + * + * or alternatively + * class MyTree extends EventTree { + * MyTree(){ + * root = sequence( + * event("a"), + * alternative( + * event("1), + * event("2") + * ) + * ); + * } + * } + * + * or alternatively as an anonymous class + * + * EventTree myTree = new EventTree( + * sequence( * event("a"), * alternatives( - * event("1"), - * iteration(2, - * event("x") - * ) + * event("1"), + * iteration(2, + * event("x") + * ) * ), * event("b") + * ) * ); - * } + * */ -public class EventTree extends EventConstructor { +public class EventTree implements EventConstructor { public static final String CONFIG_KEY = "event.tree.class"; @@ -51,13 +83,13 @@ /** * this is our purpose in life, which has to be provided by concrete subclasses */ - public Event createEventTree() { + public Event createRoot() { // nothing here, needs to be overridden by subclass to populate tree return null; } - protected EventTree (){ - root = createEventTree(); + protected EventTree () { + root = createRoot(); } protected EventTree (Event root){
--- a/src/tests/gov/nasa/jpf/test/mc/data/EventGeneratorTest.java Wed Apr 01 12:14:15 2015 -0700 +++ b/src/tests/gov/nasa/jpf/test/mc/data/EventGeneratorTest.java Mon Apr 06 12:08:03 2015 -0700 @@ -46,7 +46,7 @@ } @Override - public Event createEventTree() { + public Event createRoot() { return sequence( event("a"), @@ -103,7 +103,7 @@ } @Override - public Event createEventTree() { + public Event createRoot() { Event[] options = { event("A"), event("B"), event("C") }; return anyCombination(options); @@ -130,7 +130,7 @@ } @Override - public Event createEventTree(){ + public Event createRoot(){ return sequence( event("a"),
--- a/src/tests/gov/nasa/jpf/util/event/EventTreeTest.java Wed Apr 01 12:14:15 2015 -0700 +++ b/src/tests/gov/nasa/jpf/util/event/EventTreeTest.java Mon Apr 06 12:08:03 2015 -0700 @@ -65,7 +65,7 @@ static class SimpleTree extends EventTree { @Override - public Event createEventTree() { + public Event createRoot() { return sequence( event("a"), @@ -97,7 +97,7 @@ //-------------------------------------------------------------------- public static class CombinationTree extends EventTree { @Override - public Event createEventTree() { + public Event createRoot() { return anyCombination( event("a"), event("b"), @@ -138,7 +138,7 @@ static class SimpleCombinationTree extends EventTree { @Override - public Event createEventTree() { + public Event createRoot() { return anyCombination( event("a"), event("b") @@ -159,7 +159,7 @@ static class EmbeddedCombinationTree extends EventTree { @Override - public Event createEventTree() { + public Event createRoot() { return sequence( event("1"), anyCombination( @@ -182,7 +182,7 @@ //-------------------------------------------------------------------- static class DT extends EventTree { @Override - public Event createEventTree() { + public Event createRoot() { return sequence( event("a"), alternatives( @@ -215,7 +215,7 @@ //-------------------------------------------------------------------- static class PermutationTree extends EventTree { @Override - public Event createEventTree(){ + public Event createRoot(){ return anyPermutation( event("a"), event("b"), @@ -246,7 +246,7 @@ //-------------------------------------------------------------------- static class AddPathTree extends EventTree { @Override - public Event createEventTree(){ + public Event createRoot(){ return sequence( event("a"), event("b"), @@ -273,7 +273,7 @@ //------------------------------------------------------------------- static class MT1 extends EventTree { @Override - public Event createEventTree(){ + public Event createRoot(){ return sequence( event("a"), event("b"), @@ -284,7 +284,7 @@ static class MT2 extends EventTree { @Override - public Event createEventTree(){ + public Event createRoot(){ return sequence( event("1"), event("2"), @@ -295,7 +295,7 @@ static class MT3 extends EventTree { @Override - public Event createEventTree(){ + public Event createRoot(){ return sequence( event("X"), event("Y") @@ -344,7 +344,7 @@ //------------------------------------------------------------------- static class SMT1 extends EventTree { @Override - public Event createEventTree(){ + public Event createRoot(){ return sequence( event("a"), event("b") @@ -354,7 +354,7 @@ static class SMT2 extends EventTree { @Override - public Event createEventTree(){ + public Event createRoot(){ return sequence( event("1"), event("2") @@ -379,7 +379,7 @@ //------------------------------------------------------------------- static class RT1 extends EventTree { @Override - public Event createEventTree(){ + public Event createRoot(){ return sequence( event("a"), event("b") @@ -389,7 +389,7 @@ static class RT2 extends EventTree { @Override - public Event createEventTree(){ + public Event createRoot(){ return sequence( event("1"), event("2")