Skip to content

Commit

Permalink
ComposableEventSets upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
michbarsinai committed Mar 4, 2019
1 parent cf2f567 commit 453b025
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 77 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ a link to this page somewhere in the documentation/system about section.

## Change Log for the BPjs Library.

### 2019-03-05
* :arrow_up: `ComposableEventSet` refactored to have `equals` and `hashCode` that can hold during verification.

### 2019-02-10
* :arrow_up: Improved hash functions for `ContinuationProgramState` (affects b-thread sync snapshots as well).

Expand Down
4 changes: 2 additions & 2 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.github.bthink-bgu</groupId>
<artifactId>BPjs</artifactId>
<name>BPjs</name>
<version>0.10.0-SNAPSHOT</version>
<version>0.10.1-SNAPSHOT</version>
<description>Provides a runtime for behavioral programs written in Javascript. It can
run stand-alone (from the commmandline) or be embedded in larger
JVM-based systems.</description>
Expand Down Expand Up @@ -260,8 +260,8 @@
</snapshotRepository>
</distributionManagement>
<properties>
<java.version>1.8</java.version>
<exec.mainClass>il.ac.bgu.cs.bp.bpjs.mains.BPJsCliRunner</exec.mainClass>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.github.bthink-bgu</groupId>
<artifactId>BPjs</artifactId>
<version>0.10.0</version>
<version>0.10.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>BPjs</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ protected Violation dfsUsingStack(DfsTraversalNode aStartNode, ExecutorService e
if (isDebugMode()) {
System.out.println("-pop!-");
}
pop();
DfsTraversalNode p = pop();
if ( p.getEventIterator().hasNext() ) {
throw new IllegalStateException("Still having some events to traverse: " + p.getEventIterator().next() );
}

} else {
// go deeper
if (isDebugMode()) {
Expand Down Expand Up @@ -225,9 +229,10 @@ protected DfsTraversalNode getUnvisitedNextNode(DfsTraversalNode src, ExecutorSe
final BEvent nextEvent = src.getEventIterator().next();
DfsTraversalNode possibleNextNode = src.getNextNode(nextEvent, execSvc);
visitedEdgeCount++;
if (visited.isVisited(possibleNextNode.getSystemState()) ) {

BProgramSyncSnapshot pns = possibleNextNode.getSystemState();
if (visited.isVisited(pns) ) {
// Found a possible cycle
BProgramSyncSnapshot pns = possibleNextNode.getSystemState();

for ( int idx=0; idx<currentPath.size(); idx++) {
DfsTraversalNode nd = currentPath.get(idx);
Expand Down Expand Up @@ -319,9 +324,10 @@ private void push(DfsTraversalNode n) {
}
}

private void pop() {
currentPath.remove(currentPath.size() - 1);
private DfsTraversalNode pop() {
DfsTraversalNode popped = currentPath.remove(currentPath.size() - 1);
trace.pop();
return popped;
}

private int pathLength() {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/il/ac/bgu/cs/bp/bpjs/model/BEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import static java.util.stream.Collectors.joining;
import java.util.stream.Stream;
import org.mozilla.javascript.ConsString;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

/**
* A base class for events. Each event has a name and optional data, which is a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import il.ac.bgu.cs.bp.bpjs.model.BEvent;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import static java.util.stream.Collectors.joining;


/**
Expand All @@ -24,6 +27,111 @@
*/
public abstract class ComposableEventSet implements EventSet {

public static final class Not extends ComposableEventSet {
private final EventSet negated;

public Not(EventSet negated) {
this.negated = negated;
}

@Override
public boolean contains(BEvent event) {
return ! negated.contains(event);
}

@Override
public String toString() {
return "not(" + negated.toString() +")";
}

@Override
public int hashCode(){
return 17*negated.hashCode();
}

@Override
public boolean equals( Object other ) {
if ( this == other ) return true;
if ( this == null ) return false;
if ( other instanceof Not ) {
return ((Not)other).negated.equals(negated);
} else {
return false;
}
}
}

public static final class AnyOf extends ComposableEventSet {
private final Set<EventSet> events;

public AnyOf(Set<EventSet> events) {
this.events = events;
}

@Override
public boolean contains(BEvent event) {
return events.stream().anyMatch( es->es.contains(event) );
}

@Override
public String toString() {
return "anyOf(" + events.stream().map(es->es.toString())
.sorted().collect( joining(",")) +")";
}

@Override
public int hashCode(){
return 17*events.hashCode();
}

@Override
public boolean equals( Object other ) {
if ( this == other ) return true;
if ( this == null ) return false;
if ( other instanceof AnyOf ) {
return ((AnyOf)other).events.equals(events);
} else {
return false;
}
}
}

public static final class AllOf extends ComposableEventSet {
private final Set<EventSet> events;

public AllOf(Set<EventSet> events) {
this.events = events;
}

@Override
public boolean contains(BEvent event) {
return events.stream().allMatch( es->es.contains(event) );
}

@Override
public String toString() {
return "allOf(" + events.stream().map(es->es.toString())
.sorted().collect( joining(",")) +")";
}

@Override
public int hashCode(){
return 19*events.hashCode();
}

@Override
public boolean equals( Object other ) {
if ( this == other ) return true;
if ( this == null ) return false;
if ( other instanceof AllOf ) {
return ((AllOf)other).events.equals(events);
} else {
return false;
}
}
}


public static ComposableEventSet theEventSet( final EventSet ifce ) {
if ( ifce==null ) throw new IllegalArgumentException("eventset cannot be null");
if ( ifce instanceof ComposableEventSet ) {
Expand All @@ -44,90 +152,37 @@ public String toString() {

public static ComposableEventSet not( final EventSet ifce ) {
if ( ifce==null ) throw new IllegalArgumentException("eventset cannot be null");
return new ComposableEventSet() {
@Override
public boolean contains(BEvent event) {
return ! ifce.contains(event);
}
@Override
public String toString() {
return "not(" + ifce.toString() +")";
}};
return new Not(ifce);
}

public static ComposableEventSet anyOf( final Collection<EventSet> ifces) {
if ( ifces==null ) throw new IllegalArgumentException("eventset collection cannot be null");
return new ComposableEventSet() {
@Override
public boolean contains(BEvent event) {
return ifces.stream().anyMatch(ifce->ifce.contains(event) );
}
@Override
public String toString() {
return "anyOf(" + Arrays.asList(ifces).toString() +")";
}};
return new AnyOf( new HashSet<>(ifces) );
}

public static ComposableEventSet anyOf( final EventSet... ifces) {
if ( ifces==null ) throw new IllegalArgumentException("eventset collection cannot be null");
return new ComposableEventSet() {
@Override
public boolean contains(BEvent event) {
for ( EventSet ifce : ifces ) {
if ( ifce.contains(event) ) return true;
}
return false;
}
@Override
public String toString() {
return "anyOf(" + Arrays.asList(ifces).toString() +")";
}};
return new AnyOf( new HashSet<>(Arrays.asList(ifces)) );
}

public static ComposableEventSet allOf( final EventSet... ifces) {
public static ComposableEventSet allOf( final Collection<EventSet> ifces) {
if ( ifces==null ) throw new IllegalArgumentException("eventset collection cannot be null");
return new ComposableEventSet() {
@Override
public boolean contains(BEvent event) {
for ( EventSet ifce : ifces ) {
if ( ! ifce.contains(event) ) return false;
}
return true;
}
@Override
public String toString() {
return "allOf(" + Arrays.asList(ifces).toString() +")";
}};
return new AllOf(new HashSet<>(ifces));
}

public static ComposableEventSet allOf( final EventSet... ifces) {
if ( ifces==null ) throw new IllegalArgumentException("eventset collection cannot be null");
return new AllOf(new HashSet<>(Arrays.asList(ifces)));
}

public ComposableEventSet and( final EventSet ifce ) {
if ( ifce==null ) throw new IllegalArgumentException("eventset cannot be null");
return new ComposableEventSet() {
@Override
public boolean contains(BEvent event) {
return ifce.contains(event) && ComposableEventSet.this.contains(event);
}

@Override
public String toString() {
return "(" + ifce.toString() +") and (" + ComposableEventSet.this.toString() +")";
}

};
return new AllOf(makeSet(this, ifce));
}

public ComposableEventSet or( final EventSet ifce ) {
if ( ifce==null ) throw new IllegalArgumentException("eventset cannot be null");
return new ComposableEventSet() {
@Override
public boolean contains(BEvent event) {
return ifce.contains(event) || ComposableEventSet.this.contains(event);
}

@Override
public String toString() {
return "(" + ifce.toString() +") or (" + ComposableEventSet.this.toString() +")";
}};
return new AnyOf( makeSet(this, ifce) );
}

public ComposableEventSet xor( final EventSet ifce ) {
Expand Down Expand Up @@ -173,6 +228,13 @@ public String toString() {
}


private static Set<EventSet> makeSet( ComposableEventSet es1, EventSet es2 ) {
Set<EventSet> retVal = new HashSet<>(2);
retVal.add(es1);
retVal.add(es2);
return retVal;
}

@Override
public boolean equals( Object o ) {
if ( o == this ) return true;
Expand Down

0 comments on commit 453b025

Please sign in to comment.