All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.learnlib.oracle.parallelism.StaticParallelOracleBuilder Maven / Gradle / Ivy

There is a newer version: 0.17.0
Show newest version
/* 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> oracles;
    private final Supplier> 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> oracles) {
        Preconditions.checkArgument(!oracles.isEmpty(), "No oracles specified");
        this.oracles = oracles;
        this.oracleSupplier = null;
    }

    public StaticParallelOracleBuilder(Supplier> 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> 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);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy