<commit_before>/*
 * Licensed to the <NAME> under one or more contributor license
 * agreements.  See the NOTICE file that may be
 * distributed with this work for additional information
 * regarding copyright ownership.  <NAME> licenses this file
 * to you 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 com.mapr.synth.samplers;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.DoubleNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.util.concurrent.AtomicDouble;

import java.io.IOException;
import java.util.Random;

/**
 * Does a random walk where the next step is distributed according to a Normal distribution, but where
 * the standard deviation of the step distribution may not be fixed.
 */
public class RandomWalkSampler extends FieldSampler {
    private static final JsonNode ONE = new DoubleNode(1);
    private static final JsonNode ZERO = new DoubleNode(0);
    private static final int SEED_NOT_SET = new Random(1).nextInt();

    private int seed = SEED_NOT_SET;

    private FieldSampler sd = new FieldSampler() {
        @Override
        public JsonNode sample() {
            return ONE;
        }
    };
    private FieldSampler mean = new FieldSampler() {
        @Override
        public JsonNode sample() {
            return ZERO;
        }
    };

    private Random rand = new Random();
    private boolean verbose = false;

    private AtomicDouble state = new AtomicDouble();

    @Override
    public JsonNode sample() {
        double step = rand.nextGaussian() * sd.sample().asDouble() + mean.sample().asDouble();
        double newState = state.addAndGet(step);

        if (verbose) {
            ObjectNode r = new ObjectNode(JsonNodeFactory.withExactBigDecimals(false));
            r.set("value", new DoubleNode(newState));
            r.set("step", new DoubleNode(step));
            return r;
        } else {
            return new DoubleNode(newState);
        }
    }

    @SuppressWarnings("UnusedDeclaration")
    public void setSeed(int seed) {
        if (seed == SEED_NOT_SET) {
            seed++;
        }
        this.seed = seed;
        init();
    }

    @SuppressWarnings("UnusedDeclaration")
    public void setVerbose(boolean verbose) {
        this.verbose = verbose;
    }

    @SuppressWarnings("UnusedDeclaration")
    public void setS(JsonNode value) throws IOException {
        if (value.isObject()) {
            sd = FieldSampler.newSampler(value.toString());
        } else {
            this.sd = constant(value.asDouble());
        }
        init();
    }

    @SuppressWarnings("UnusedDeclaration")
    public void setPrecision(final JsonNode value) throws IOException {
        if (value.isObject()) {
            sd = new FieldSampler() {
                FieldSampler base = FieldSampler.newSampler(value.toString());

                @Override
                public JsonNode sample() {
                    return new DoubleNode(Math.sqrt(1 / base.sample().asDouble()));
                }
            };
        } else {
            this.sd = constant(Math.sqrt(1 / value.asDouble()));
        }
        init();
    }

    @SuppressWarnings("UnusedDeclaration")
    public void setVariance(final JsonNode value) throws IOException {
        if (value.isObject()) {
            sd = new FieldSampler() {
                FieldSampler base = FieldSampler.newSampler(value.toString());

                @Override
                public JsonNode sample() {
                    return new DoubleNode(Math.sqrt(base.sample().asDouble()));
                }
            };
        } else {
            this.sd = constant(Math.sqrt(value.asDouble()));
        }
        init();
    }

    @SuppressWarnings("UnusedDeclaration")
    public void setSD(final JsonNode value) throws IOException {
        if (value.isObject()) {
            sd = new FieldSampler() {
                FieldSampler base = FieldSampler.newSampler(value.toString());

                @Override
                public JsonNode sample() {
                    return new DoubleNode(base.sample().asDouble());
                }
            };
        } else {
            sd = constant(value.asDouble());
        }        init();
    }

    public void setMean(final JsonNode value) throws IOException {
        if (value.isObject()) {
            mean = FieldSampler.newSampler(value.toString());
        } else {
            mean = constant(value.asDouble());
        }
        init();
    }

    private void init() {
        if (seed != SEED_NOT_SET) {
            rand = new Random(seed);
        }
    }

    private FieldSampler constant(final double v) {
        return new FieldSampler() {
            private DoubleNode sd = new DoubleNode(v);

            @Override
            public JsonNode sample() {
                return sd;
            }
        };
    }
}<commit_msg>Fixed indentation<commit_after>/*
 * Licensed to the <NAME> under one or more contributor license
 * agreements.  See the NOTICE file that may be
 * distributed with this work for additional information
 * regarding copyright ownership.  <NAME> licenses this file
 * to you 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 com.mapr.synth.samplers;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.DoubleNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.util.concurrent.AtomicDouble;

import java.io.IOException;
import java.util.Random;

/**
 * Does a random walk where the next step is distributed according to a Normal distribution, but where
 * the standard deviation of the step distribution may not be fixed.
 */
public class RandomWalkSampler extends FieldSampler {
    private static final JsonNode ONE = new DoubleNode(1);
    private static final JsonNode ZERO = new DoubleNode(0);
    private static final int SEED_NOT_SET = new Random(1).nextInt();

    private int seed = SEED_NOT_SET;

    private FieldSampler sd = new FieldSampler() {
        @Override
        public JsonNode sample() {
            return ONE;
        }
    };
    private FieldSampler mean = new FieldSampler() {
        @Override
        public JsonNode sample() {
            return ZERO;
        }
    };

    private Random rand = new Random();
    private boolean verbose = false;

    private AtomicDouble state = new AtomicDouble();

    @Override
    public JsonNode sample() {
        double step = rand.nextGaussian() * sd.sample().asDouble() + mean.sample().asDouble();
        double newState = state.addAndGet(step);

        if (verbose) {
            ObjectNode r = new ObjectNode(JsonNodeFactory.withExactBigDecimals(false));
            r.set("value", new DoubleNode(newState));
            r.set("step", new DoubleNode(step));
            return r;
        } else {
            return new DoubleNode(newState);
        }
    }

    @SuppressWarnings("UnusedDeclaration")
    public void setSeed(int seed) {
        if (seed == SEED_NOT_SET) {
            seed++;
        }
        this.seed = seed;
        init();
    }

    @SuppressWarnings("UnusedDeclaration")
    public void setVerbose(boolean verbose) {
        this.verbose = verbose;
    }

    @SuppressWarnings("UnusedDeclaration")
    public void setS(JsonNode value) throws IOException {
        if (value.isObject()) {
            sd = FieldSampler.newSampler(value.toString());
        } else {
            this.sd = constant(value.asDouble());
        }
        init();
    }

    @SuppressWarnings("UnusedDeclaration")
    public void setPrecision(final JsonNode value) throws IOException {
        if (value.isObject()) {
            sd = new FieldSampler() {
                FieldSampler base = FieldSampler.newSampler(value.toString());

                @Override
                public JsonNode sample() {
                    return new DoubleNode(Math.sqrt(1 / base.sample().asDouble()));
                }
            };
        } else {
            this.sd = constant(Math.sqrt(1 / value.asDouble()));
        }
        init();
    }

    @SuppressWarnings("UnusedDeclaration")
    public void setVariance(final JsonNode value) throws IOException {
        if (value.isObject()) {
            sd = new FieldSampler() {
                FieldSampler base = FieldSampler.newSampler(value.toString());

                @Override
                public JsonNode sample() {
                    return new DoubleNode(Math.sqrt(base.sample().asDouble()));
                }
            };
        } else {
            this.sd = constant(Math.sqrt(value.asDouble()));
        }
        init();
    }

    @SuppressWarnings("UnusedDeclaration")
    public void setSD(final JsonNode value) throws IOException {
        if (value.isObject()) {
            sd = new FieldSampler() {
                FieldSampler base = FieldSampler.newSampler(value.toString());

                @Override
                public JsonNode sample() {
                    return new DoubleNode(base.sample().asDouble());
                }
            };
        } else {
            sd = constant(value.asDouble());
        }
        init();
    }

    public void setMean(final JsonNode value) throws IOException {
        if (value.isObject()) {
            mean = FieldSampler.newSampler(value.toString());
        } else {
            mean = constant(value.asDouble());
        }
        init();
    }

    private void init() {
        if (seed != SEED_NOT_SET) {
            rand = new Random(seed);
        }
    }

}
{'fraction_non_alphanumeric': 0.09906454630495791, 'fraction_numerical': 0.0016838166510757717, 'mean_word_length': 2.178061831153389, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6887466', 'n_tokens_mistral': 2841, 'n_tokens_neox': 2734, 'n_words': 806}