Skip to content

Commit

Permalink
implemented sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
eric.bodden committed Sep 10, 2012
1 parent 7aac20b commit 31b4c93
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;

import com.google.common.collect.HashMultiset;
Expand Down Expand Up @@ -106,6 +107,35 @@ public abstract class AbstractSyncingFSMMonitorTemplate<L, K, V, A extends Abstr
public boolean isCompatibleWith(IVariableBinding<K,V> other) { return false; };
};

/**
* We use this random source to produce coin flips that tell us whether to turn sampling
* on or off during any given sampling period. We use a deterministic seed for
* reproducibility.
*/
protected final Random random = new Random(0L);

/**
* The length of any sampling period.
*/
protected final int samplingPeriod;

/**
* The length of any skip period.
*/
protected final int skipPeriod;

/**
* The current phase of the sampling period, where 0 is the start of the period.
*/
protected int phase;

/**
* This boolean tells us whether or not we will process events in the current period.
*/
protected boolean processEventsInCurrentPeriod;

protected final Set<ISymbol<L, K>> criticalSymbols;

/**
* @param delegate The monitor template this syncing monitor template is based on. The template will remain unmodified.
* @param max The maximal number of skipped events.
Expand All @@ -114,6 +144,9 @@ public AbstractSyncingFSMMonitorTemplate(OpenFSMMonitorTemplate<L, K, V> delegat
this.delegate = delegate;
this.MAX = max;
initialize();
this.samplingPeriod = samplingPeriod();
this.skipPeriod = (int) (1.0d/samplingRate() - 1) * samplingPeriod;
this.criticalSymbols = criticalSymbols();
}

/**
Expand Down Expand Up @@ -319,40 +352,51 @@ protected void fillAlphabet(IAlphabet<AbstractionAndSymbol, K> alphabet) {
* @param binding the current events's binding
*/
public void maybeProcessEvent(L symbolLabel, IVariableBinding<K,V> binding) {
// While skipping the events e_1 to e_n, we intersect those event's bindings. E.g. c=c1 and i=i1 intersects to (c=c1 && i=i1). If we get two contradicting bindings during the process then we intersect to "false".
//
// Then at event e (which we monitor), if the intersection is not "false" but some other binding b then we dispatch to b's monitor. If it is false then...?

// boolean compatible = binding.isCompatibleWith(intersectionOfSkippedBindings);
// if(compatible) {
// binding = intersectionOfSkippedBindings = binding.computeJoinWith(intersectionOfSkippedBindings);
// } else {
// intersectionOfSkippedBindings = INCOMPATIBLE_BINDING;
// }

ISymbol<L, K> symbol = delegate.getAlphabet().getSymbolByLabel(symbolLabel);
if(shouldMonitor(symbol,binding,skippedSymbols)) {
if(!didMonitorLastEvent) reenableTime++;
// if(compatible)
processEvent(new AbstractionAndSymbol(abstraction(skippedSymbols), symbol), binding);
skippedSymbols.clear();
didMonitorLastEvent = true;
// intersectionOfSkippedBindings.clear(); //reset binding
} else {
if(skippedSymbols.size()>MAX) throw new InternalError("MAX is "+MAX+" but skipped "+skippedSymbols.size()+" events!");
skippedSymbols.add(symbol);
didMonitorLastEvent = false;
}
}

/**
* Returns the lenght of a sampling period for this monitor template. This size may actually depend on the
* size or structure of the delegate, i.e., the property to be monitored.
*/
abstract protected int samplingPeriod();

/**
* This is the rate at which we sample, given as a number from 0 to 1.
*/
abstract protected double samplingRate();

/**
* Returns the set of critical symbols for the monitored property. Such symbols will always be monitored,
* no matter whether or not sampling is enabled.
*/
abstract protected Set<ISymbol<L, K>> criticalSymbols();

/**
* Determines whether the current event should be monitored.
* @param symbol the current event's symbol
* @param binding the current events's binding
* @param skippedSymbols the multiset of symbols of events skipped so far
* @return
*/
protected abstract boolean shouldMonitor(ISymbol<L, K> symbol, IVariableBinding<K, V> binding, Multiset<ISymbol<L, K>> skippedSymbols);
protected boolean shouldMonitor(ISymbol<L, K> symbol, IVariableBinding<K, V> binding, Multiset<ISymbol<L, K>> skippedSymbols) {
if(phase==0) {
processEventsInCurrentPeriod = random.nextBoolean();
}
int periodLength = processEventsInCurrentPeriod ? samplingPeriod : skipPeriod;
phase = (phase+1) % periodLength;
return processEventsInCurrentPeriod || criticalSymbols.contains(symbol);
}


public class AbstractionAndSymbol {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.bodden.mopbox.finitestate.sync;

import java.util.Collections;
import java.util.Set;

import de.bodden.mopbox.finitestate.OpenFSMMonitorTemplate;
import de.bodden.mopbox.generic.ISymbol;

public abstract class DefaultSyncingFSMMonitorTemplate<L, K, V, A extends AbstractSyncingFSMMonitorTemplate<L,K,V,A>.SymbolMultisetAbstraction> extends AbstractSyncingFSMMonitorTemplate<L, K, V, A> {

public DefaultSyncingFSMMonitorTemplate(OpenFSMMonitorTemplate<L, K, V> delegate, int max) {
super(delegate, max);
}

@Override
protected int samplingPeriod() {
return delegate.getStates().size()+2;
}

@Override
protected double samplingRate() {
return 0.2d;
}

@Override
protected Set<ISymbol<L, K>> criticalSymbols() {
return Collections.emptySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* of missed events.
*/
public abstract class FullSyncingTemplate<L, K, V>
extends AbstractSyncingFSMMonitorTemplate<L, K, V, FullSyncingTemplate<L,K,V>.FullAbstraction>{
extends DefaultSyncingFSMMonitorTemplate<L, K, V, FullSyncingTemplate<L,K,V>.FullAbstraction>{

public FullSyncingTemplate(OpenFSMMonitorTemplate<L, K, V> delegate, int max) {
super(delegate, max);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* An {@link AbstractSyncingFSMMonitorTemplate} that models the gap of events missed as a multiset of missed events.
*/
public abstract class MultisetSyncingTemplate<L, K, V>
extends AbstractSyncingFSMMonitorTemplate<L, K, V, MultisetSyncingTemplate<L,K,V>.AbstractionAsMultiset>{
extends DefaultSyncingFSMMonitorTemplate<L, K, V, MultisetSyncingTemplate<L,K,V>.AbstractionAsMultiset>{

public MultisetSyncingTemplate(OpenFSMMonitorTemplate<L, K, V> delegate, int max) {
super(delegate, max);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* and kind of missed events.
*/
public abstract class NumberAndSymbolSetSyncingTemplate<L, K, V>
extends AbstractSyncingFSMMonitorTemplate<L, K, V, NumberAndSymbolSetSyncingTemplate<L,K,V>.AbstractionBySizeAndSymbols>{
extends DefaultSyncingFSMMonitorTemplate<L, K, V, NumberAndSymbolSetSyncingTemplate<L,K,V>.AbstractionBySizeAndSymbols>{

public NumberAndSymbolSetSyncingTemplate(OpenFSMMonitorTemplate<L, K, V> delegate, int max) {
super(delegate, max);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* of missed events.
*/
public abstract class NumberSyncingTemplate<L, K, V>
extends AbstractSyncingFSMMonitorTemplate<L, K, V, NumberSyncingTemplate<L,K,V>.AbstractionBySize>{
extends DefaultSyncingFSMMonitorTemplate<L, K, V, NumberSyncingTemplate<L,K,V>.AbstractionBySize>{

public NumberSyncingTemplate(OpenFSMMonitorTemplate<L, K, V> delegate, int max) {
super(delegate, max);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* An {@link AbstractSyncingFSMMonitorTemplate} that models the gap of events missed by the set of symbols that were missed.
*/
public abstract class SymbolSetSyncingTemplate<L, K, V>
extends AbstractSyncingFSMMonitorTemplate<L, K, V, SymbolSetSyncingTemplate<L,K,V>.AbstractionBySymbolSet>{
extends DefaultSyncingFSMMonitorTemplate<L, K, V, SymbolSetSyncingTemplate<L,K,V>.AbstractionBySymbolSet>{

public SymbolSetSyncingTemplate(OpenFSMMonitorTemplate<L, K, V> delegate, int max) {
super(delegate, max);
Expand Down

0 comments on commit 31b4c93

Please sign in to comment.