001/*
002 * Java Genetic Algorithm Library (jenetics-7.1.0).
003 * Copyright (c) 2007-2022 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020package io.jenetics.prog.regression;
021
022import static java.util.Objects.requireNonNull;
023
024import java.lang.reflect.Array;
025import java.util.Arrays;
026import java.util.Collection;
027import java.util.List;
028import java.util.Objects;
029import java.util.function.Function;
030import java.util.stream.Stream;
031
032import io.jenetics.ext.util.Tree;
033
034import io.jenetics.prog.op.Op;
035
036/**
037 * This class holds the actual sample values which are used for the symbolic
038 * regression example. This class is <em>thread-safe</em> and can be used in a
039 * <em>producer-consumer</em> setup. You can add single sample values
040 * ({@link #add(Sample)}) or a list ({@link #addAll(Collection)}) of new values.
041 * These values will be made available for evaluation after an explicit call of
042 * the {@link #publish()} method.
043 *
044 * @implNote
045 * This class is thread-safe.
046 *
047 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
048 * @version 6.0
049 * @since 6.0
050 */
051public final class SampleBuffer<T> implements Sampling<T> {
052
053        private final RingBuffer _buffer;
054
055        private volatile SampleList<T> _snapshot = null;
056
057        public SampleBuffer(final int capacity) {
058                _buffer = new RingBuffer(capacity);
059        }
060
061        /**
062         * Adding a new sample point to the buffer. <em>You need to explicitly
063         * call {@link #publish()} to make it available for the {@link #eval(Tree)}
064         * method.</em>
065         *
066         * @param sample the sample point to add
067         * @throws NullPointerException if the given {@code sample} point is
068         *         {@code null}
069         */
070        public void add(final Sample<T> sample) {
071                _buffer.add(requireNonNull(sample));
072        }
073
074        /**
075         * The the given sample points to the buffer.  <em>You need to explicitly
076         * call {@link #publish()} to make it available for the {@link #eval(Tree)}
077         * method.</em>
078         *
079         * @param samples the samples to add to the buffer
080         * @throws NullPointerException if the given {@code samples} is {@code null}
081         */
082        public void addAll(final Collection<? extends Sample<T>> samples) {
083                samples.forEach(Objects::requireNonNull);
084                _buffer.addAll(samples);
085        }
086
087        /**
088         * Making the current sample points available for the {@link #eval(Tree)}
089         * function.
090         *
091         * @return the number of <em>published</em> sample points
092         */
093        @SuppressWarnings({"unchecked", "rawtypes"})
094        public int publish() {
095                final Object[] values = _buffer.snapshot();
096
097                SampleList<T> snapshot = null;
098                if (values != null && values.length > 0) {
099                        final List samples = Arrays.asList(values);
100                        snapshot = new SampleList(samples);
101                }
102
103                try {
104                        return snapshot != null ? snapshot.size() : 0;
105                } finally {
106                        _snapshot = snapshot;
107                }
108        }
109
110        /**
111         * Return the currently <em>published</em> sample points.
112         *
113         * @see #publish()
114         *
115         * @return the currently <em>published</em> sample points
116         */
117        List<Sample<T>> samples() {
118                final SampleList<T> snapshot = _snapshot;
119                return snapshot != null ? snapshot : List.of();
120        }
121
122        @Override
123        public Result<T> eval(final Tree<? extends Op<T>, ?> program) {
124                requireNonNull(program);
125
126                final SampleList<T> snapshot = _snapshot;
127                return snapshot != null && !snapshot.isEmpty()
128                        ? snapshot.eval(program)
129                        : null;
130        }
131
132        @Override
133        public Result<T> eval(final Function<? super T[], ? extends T> function) {
134                requireNonNull(function);
135
136                final SampleList<T> snapshot = _snapshot;
137                return snapshot != null && !snapshot.isEmpty()
138                        ? snapshot.eval(function)
139                        : null;
140        }
141
142}