de.learnlib.oracle.parallelism.StaticParallelOracleBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of learnlib-parallelism Show documentation
Show all versions of learnlib-parallelism Show documentation
Support for parallel membership queries and thread-safe caches
/* Copyright (C) 2013-2019 TU Dortmund
* This file is part of LearnLib, http://www.learnlib.de/.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.learnlib.oracle.parallelism;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Supplier;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import com.google.common.base.Preconditions;
import de.learnlib.api.oracle.MembershipOracle;
import de.learnlib.oracle.parallelism.ParallelOracle.PoolPolicy;
/**
* A builder for a {@link StaticParallelOracle}.
*
* @param
* input symbol type
* @param
* output type
*
* @author Malte Isberner
*/
@ParametersAreNonnullByDefault
public class StaticParallelOracleBuilder {
private final Collection extends MembershipOracle> oracles;
private final Supplier extends MembershipOracle> oracleSupplier;
@Nonnegative
private int minBatchSize = StaticParallelOracle.MIN_BATCH_SIZE;
@Nonnegative
private int numInstances = StaticParallelOracle.NUM_INSTANCES;
@Nonnull
private PoolPolicy poolPolicy = StaticParallelOracle.POOL_POLICY;
public StaticParallelOracleBuilder(Collection extends MembershipOracle> oracles) {
Preconditions.checkArgument(!oracles.isEmpty(), "No oracles specified");
this.oracles = oracles;
this.oracleSupplier = null;
}
public StaticParallelOracleBuilder(Supplier extends MembershipOracle> oracleSupplier) {
this.oracles = null;
this.oracleSupplier = oracleSupplier;
}
@Nonnull
public StaticParallelOracleBuilder withMinBatchSize(@Nonnegative int minBatchSize) {
this.minBatchSize = minBatchSize;
return this;
}
@Nonnull
public StaticParallelOracleBuilder withPoolPolicy(PoolPolicy policy) {
this.poolPolicy = policy;
return this;
}
@Nonnull
public StaticParallelOracleBuilder withNumInstances(@Nonnegative int numInstances) {
this.numInstances = numInstances;
return this;
}
@Nonnull
public StaticParallelOracle create() {
Collection extends MembershipOracle> oracleInstances;
if (oracles != null) {
oracleInstances = oracles;
} else {
List> oracleList = new ArrayList<>(numInstances);
for (int i = 0; i < numInstances; i++) {
oracleList.add(oracleSupplier.get());
}
oracleInstances = oracleList;
}
return new StaticParallelOracle<>(oracleInstances, minBatchSize, poolPolicy);
}
}