de.learnlib.oracle.membership.SULOracle Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of learnlib-membership-oracles Show documentation
Show all versions of learnlib-membership-oracles Show documentation
A collection of membership oracles
/* Copyright (C) 2013-2018 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.membership;
import java.util.Collection;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import de.learnlib.api.SUL;
import de.learnlib.api.exception.SULException;
import de.learnlib.api.oracle.MembershipOracle.MealyMembershipOracle;
import de.learnlib.api.query.Query;
import net.automatalib.words.Word;
import net.automatalib.words.WordBuilder;
/**
* A wrapper around a system under learning (SUL).
*
* This membership oracle is thread-safe. Thread-safety is obtained in either of the following ways:
- if the
* {@link SUL} can be {@link SUL#fork() forked}, each thread from which {@link #processQueries(Collection)} will
* maintain a {@link ThreadLocal thread-local} fork of the SUL, which is used for processing queries.
* - otherwise, if the SUL is not forkable, accesses to the SUL in {@link #processQueries(Collection)} will be
* synchronized explicitly.
*
* @author Falk Howar
* @author Malte Isberner
*/
@ParametersAreNonnullByDefault
public class SULOracle implements MealyMembershipOracle {
private final SUL sul;
private final ThreadLocal> localSul;
public SULOracle(SUL sul) {
this.sul = sul;
if (sul.canFork()) {
this.localSul = ThreadLocal.withInitial(sul::fork);
} else {
this.localSul = null;
}
}
@Override
public void processQueries(Collection extends Query>> queries) {
if (localSul != null) {
processQueries(localSul.get(), queries);
} else {
synchronized (sul) {
processQueries(sul, queries);
}
}
}
private static void processQueries(SUL sul, Collection extends Query>> queries) {
for (Query> q : queries) {
Word output = answerQuery(sul, q.getPrefix(), q.getSuffix());
q.answer(output);
}
}
@Nonnull
private static Word answerQuery(SUL sul, Word prefix, Word suffix) throws SULException {
sul.pre();
try {
// Prefix: Execute symbols, don't record output
for (I sym : prefix) {
sul.step(sym);
}
// Suffix: Execute symbols, outputs constitute output word
WordBuilder wb = new WordBuilder<>(suffix.length());
for (I sym : suffix) {
wb.add(sul.step(sym));
}
return wb.toWord();
} finally {
sul.post();
}
}
}