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

main.io.github.moonlightsuite.moonlight.offline.algorithms.SpatialOp Maven / Gradle / Ivy

Go to download

MoonLight is a light-weight Java-tool for monitoring temporal, spatial and spatio-temporal properties of distributed complex systems, such as Cyber-Physical Systems and Collective Adaptive Systems.

The newest version!
/*
 * MoonLight: a light-weight framework for runtime monitoring
 * Copyright (C) 2018-2021
 *
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 *
 * 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 io.github.moonlightsuite.moonlight.offline.algorithms;

import io.github.moonlightsuite.moonlight.core.space.DistanceStructure;
import io.github.moonlightsuite.moonlight.core.space.LocationService;
import io.github.moonlightsuite.moonlight.core.space.SpaceIterator;
import io.github.moonlightsuite.moonlight.core.space.SpatialModel;
import io.github.moonlightsuite.moonlight.offline.signal.ParallelSignalCursor;
import io.github.moonlightsuite.moonlight.offline.signal.SpatialTemporalSignal;
import org.jetbrains.annotations.NotNull;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.IntFunction;

import static io.github.moonlightsuite.moonlight.offline.signal.SignalCursor.isNotCompleted;

/**
 * Algorithm for Somewhere and Everywhere Computation
 */
public class SpatialOp {
    private final SpaceIterator spaceItr;
    private final BiFunction, DistanceStructure, IntFunction> op;
    ParallelSignalCursor cursor;
    private SpatialTemporalSignal result;

    public SpatialOp(
            LocationService l,
            Function, DistanceStructure> distance,
            BiFunction, DistanceStructure,
                    IntFunction> operator) {
        op = operator;
        spaceItr = new SpaceIterator<>(l, distance);
    }

    public SpatialTemporalSignal computeUnary(SpatialTemporalSignal s) {
        outputInit(s.getNumberOfLocations());
        if (!spaceItr.isLocationServiceEmpty()) {
            doCompute(s);
        }
        return result;
    }

    private void outputInit(int locations) {
        result = new SpatialTemporalSignal<>(locations);
    }

    private void doCompute(SpatialTemporalSignal s) {
        cursor = s.getSignalCursor(true);
        double t = cursor.getCurrentTime();
        spaceItr.init(t);

        while (!Double.isNaN(t) && isNotCompleted(cursor)) {
            DistanceStructure ds = spaceItr.generateDistanceStructure();
            IntFunction spatialSignal = cursor.getCurrentValue();
            double tNext = cursor.forwardTime();
            addResult(t, tNext, op.apply(spatialSignal, ds));
            spaceItr.forEach(tNext, (itT, itDs) ->
                    addResult(itT, tNext, op.apply(spatialSignal, itDs))
            );
            t = moveSpatialModel(tNext);
        }
    }

    private Double moveSpatialModel(@NotNull Double t) {
        if (spaceItr.isNextSpaceModelAtSameTime(t)) {
            spaceItr.shiftSpatialModel();
        }
        return t;
    }

    protected void addResult(Double start, Double end, IntFunction value) {
        result.add(start, value);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy