Search in sources :

Example 36 with Range

use of org.apache.commons.lang3.Range in project RecurrentComplex by Ivorforce.

the class FactorMatch method consider.

@Override
public List<Pair<LineSelection, Float>> consider(WorldCache cache, LineSelection considerable, @Nullable IvBlockCollection blockCollection, Set<BlockPos> surface, StructurePlaceContext context) {
    if (blockCollection == null)
        throw new IllegalArgumentException("Missing a block collection!");
    List<Pair<LineSelection, Float>> consideration = new ArrayList<>();
    int[] size = StructureBoundingBoxes.size(context.boundingBox);
    BlockPos lowerCoord = StructureBoundingBoxes.min(context.boundingBox);
    Set<BlockPos.MutableBlockPos> sources = BlockAreas.streamMutablePositions(blockCollection.area()).filter(p -> sourceMatcher.evaluate(() -> blockCollection.getBlockState(p))).map(p -> new BlockPos.MutableBlockPos(context.transform.apply(p, size).add(lowerCoord.getX(), 0, lowerCoord.getZ()))).collect(Collectors.toSet());
    for (IntegerRange range : (Iterable<IntegerRange>) considerable.streamSections(null, true)::iterator) {
        Float curConformity = null;
        int lastY = range.getMax();
        int end = range.getMin();
        for (int y = lastY; y >= end; y--) {
            int finalY = y;
            sources.forEach(p -> p.move(EnumFacing.UP, finalY));
            float conformity = weight(cache, sources, requiredConformity);
            sources.forEach(p -> p.move(EnumFacing.DOWN, finalY));
            if (curConformity == null) {
                curConformity = conformity;
                lastY = y;
            } else if (!DoubleMath.fuzzyEquals(conformity, curConformity, 0.01)) {
                consideration.add(Pair.of(LineSelection.fromRange(IntegerRanges.from(lastY, y + 1), true), weight(curConformity)));
                curConformity = conformity;
                lastY = y;
            }
        }
        if (curConformity != null)
            consideration.add(Pair.of(LineSelection.fromRange(IntegerRanges.from(lastY, end), true), weight(curConformity)));
    }
    return consideration;
}
Also used : IvBlockCollection(ivorius.ivtoolkit.blocks.IvBlockCollection) BlockExpression(ivorius.reccomplex.utils.expression.BlockExpression) BlockAreas(ivorius.ivtoolkit.blocks.BlockAreas) TableDataSource(ivorius.reccomplex.gui.table.datasource.TableDataSource) StructureBoundingBoxes(ivorius.ivtoolkit.world.chunk.gen.StructureBoundingBoxes) ArrayList(java.util.ArrayList) Pair(org.apache.commons.lang3.tuple.Pair) Side(net.minecraftforge.fml.relauncher.Side) RecurrentComplex(ivorius.reccomplex.RecurrentComplex) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) PositionedBlockExpression(ivorius.reccomplex.utils.expression.PositionedBlockExpression) JsonUtils(ivorius.reccomplex.json.JsonUtils) Nullable(javax.annotation.Nullable) ExpressionCache(ivorius.reccomplex.utils.algebra.ExpressionCache) DoubleMath(com.google.common.math.DoubleMath) TableDataSourceFactorMatch(ivorius.reccomplex.gui.editstructure.placer.TableDataSourceFactorMatch) WorldCache(ivorius.ivtoolkit.world.WorldCache) EnumFacing(net.minecraft.util.EnumFacing) Set(java.util.Set) LineSelection(ivorius.ivtoolkit.util.LineSelection) BlockPos(net.minecraft.util.math.BlockPos) Collectors(java.util.stream.Collectors) TableNavigator(ivorius.reccomplex.gui.table.TableNavigator) List(java.util.List) Type(java.lang.reflect.Type) TableDelegate(ivorius.reccomplex.gui.table.TableDelegate) IntegerRanges(ivorius.reccomplex.utils.IntegerRanges) com.google.gson(com.google.gson) IntegerRange(ivorius.ivtoolkit.gui.IntegerRange) IntegerRange(ivorius.ivtoolkit.gui.IntegerRange) ArrayList(java.util.ArrayList) BlockPos(net.minecraft.util.math.BlockPos) Pair(org.apache.commons.lang3.tuple.Pair)

Example 37 with Range

use of org.apache.commons.lang3.Range in project metron by apache.

the class WindowProcessorTest method testRepeatWithWeekdayExclusion.

@Test
public void testRepeatWithWeekdayExclusion() throws ParseException {
    Window w = WindowProcessor.process("30 minute window every 24 hours from 7 days ago excluding weekdays");
    Date now = new Date();
    // avoid DST impacts if near Midnight
    now.setHours(6);
    List<Range<Long>> intervals = w.toIntervals(now.getTime());
    assertEquals(2, intervals.size());
}
Also used : Range(org.apache.commons.lang3.Range) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 38 with Range

use of org.apache.commons.lang3.Range in project metron by apache.

the class WindowProcessorTest method testSparse.

@Test
public void testSparse() {
    for (String text : new String[] { "30 minute window every 1 hour from 2 hours ago to 30 minutes ago", "30 minute window every 1 hour starting from 2 hours ago to 30 minutes ago", "30 minute window every 1 hour starting from 2 hours ago until 30 minutes ago", "30 minute window for every 1 hour starting from 2 hours ago until 30 minutes ago" }) {
        Window w = WindowProcessor.process(text);
        /*
    A window size of 30 minutes
    Starting 2 hour ago and continuing until 30 minutes ago
    window 1: ( now - 2 hour, now - 2 hour + 30 minutes)
    window 2: (now - 1 hour, now - 1 hour + 30 minutes)
     */
        Date now = new Date();
        List<Range<Long>> intervals = w.toIntervals(now.getTime());
        assertEquals(2, intervals.size());
        assertEquals(now.getTime() - TimeUnit.HOURS.toMillis(2), intervals.get(0).getMinimum());
        assertEquals(now.getTime() - TimeUnit.HOURS.toMillis(2) + TimeUnit.MINUTES.toMillis(30), intervals.get(0).getMaximum());
        assertEquals(now.getTime() - TimeUnit.HOURS.toMillis(1), intervals.get(1).getMinimum());
        assertEquals(now.getTime() - TimeUnit.HOURS.toMillis(1) + TimeUnit.MINUTES.toMillis(30), intervals.get(1).getMaximum());
    }
}
Also used : Range(org.apache.commons.lang3.Range) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 39 with Range

use of org.apache.commons.lang3.Range in project metron by apache.

the class WindowProcessorTest method testBaseCase.

@Test
public void testBaseCase() {
    for (String text : new String[] { "1 hour", "1 hour(s)", "1 hours" }) {
        Window w = WindowProcessor.process(text);
        Date now = new Date();
        List<Range<Long>> intervals = w.toIntervals(now.getTime());
        assertEquals(1, intervals.size());
        assertEquals(now.getTime(), (long) intervals.get(0).getMaximum());
        assertEquals(now.getTime() - TimeUnit.HOURS.toMillis(1), (long) intervals.get(0).getMinimum());
    }
}
Also used : Range(org.apache.commons.lang3.Range) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 40 with Range

use of org.apache.commons.lang3.Range in project metron by apache.

the class WindowProcessorTest method testRepeatTilNow.

@Test
public void testRepeatTilNow() {
    Window w = WindowProcessor.process("30 minute window every 1 hour from 3 hours ago");
    /*
    A window size of 30 minutes
    Starting 3 hours ago and continuing until now
    window 1: ( now - 3 hour, now - 3 hour + 30 minutes)
    window 2: ( now - 2 hour, now - 2 hour + 30 minutes)
    window 3: ( now - 1 hour, now - 1 hour + 30 minutes)
     */
    Date now = new Date();
    List<Range<Long>> intervals = w.toIntervals(now.getTime());
    assertEquals(3, intervals.size());
    assertTimeEquals(now.getTime() - TimeUnit.HOURS.toMillis(3), intervals.get(0).getMinimum());
    assertTimeEquals(now.getTime() - TimeUnit.HOURS.toMillis(3) + TimeUnit.MINUTES.toMillis(30), intervals.get(0).getMaximum());
    assertTimeEquals(now.getTime() - TimeUnit.HOURS.toMillis(2), intervals.get(1).getMinimum());
    assertTimeEquals(now.getTime() - TimeUnit.HOURS.toMillis(2) + TimeUnit.MINUTES.toMillis(30), intervals.get(1).getMaximum());
    assertTimeEquals(now.getTime() - TimeUnit.HOURS.toMillis(1), intervals.get(2).getMinimum());
    assertTimeEquals(now.getTime() - TimeUnit.HOURS.toMillis(1) + TimeUnit.MINUTES.toMillis(30), intervals.get(2).getMaximum());
}
Also used : Range(org.apache.commons.lang3.Range) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

List (java.util.List)30 HashMap (java.util.HashMap)24 Map (java.util.Map)24 ArrayList (java.util.ArrayList)23 Collectors (java.util.stream.Collectors)21 StringUtils (org.apache.commons.lang3.StringUtils)20 LoggerFactory (org.slf4j.LoggerFactory)17 Pair (org.apache.commons.lang3.tuple.Pair)16 Logger (org.slf4j.Logger)16 Set (java.util.Set)15 IOException (java.io.IOException)14 Optional (java.util.Optional)12 Range (org.apache.commons.lang3.Range)11 Test (org.junit.jupiter.api.Test)11 java.util (java.util)10 Date (java.util.Date)10 Lists (com.google.common.collect.Lists)9 HashSet (java.util.HashSet)9 ExecutorService (java.util.concurrent.ExecutorService)9 Collection (java.util.Collection)8