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

main.io.github.moonlightsuite.moonlight.util.Utils 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.util;

import io.github.moonlightsuite.moonlight.core.base.Pair;
import io.github.moonlightsuite.moonlight.core.space.LocationService;
import io.github.moonlightsuite.moonlight.core.space.SpatialModel;
import io.github.moonlightsuite.moonlight.offline.signal.Signal;
import io.github.moonlightsuite.moonlight.offline.signal.SpatialTemporalSignal;
import io.github.moonlightsuite.moonlight.space.GraphModel;
import io.github.moonlightsuite.moonlight.space.LocationServiceList;
import io.github.moonlightsuite.moonlight.space.RegularGridModel;

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

/**
 * @author loreti
 */
public class Utils {

    private Utils() {
        //utility class
    }

    public static  Signal createSignal(double start, double end, double dt, Function f) {
        Signal signal = new Signal<>();
        double time = start;
        while (time <= end) {
            signal.add(time, f.apply(time));
            time += dt;
        }
        signal.endAt(end);
        return signal;
    }


    public static  SpatialTemporalSignal createSpatioTemporalSignal(int size, double start, double dt, double end, BiFunction f) {
        SpatialTemporalSignal s = new SpatialTemporalSignal<>(size);
        double time = start;
        while (time < end) {
            double current = time;
            s.add(time, (i -> f.apply(current, i)));
            time += dt;
        }
        s.add(end, (i -> f.apply(end, i)));
        return s;
    }

    public static  SpatialTemporalSignal createSpatioTemporalSignalFromGrid(int rowLength, int columnLength, double start, double dt, double end, BiFunction, T> f) {
        SpatialTemporalSignal s = new SpatialTemporalSignal<>(rowLength * columnLength);
        double time = start;
        while (time < end) {
            double current = time;
            s.add(time, (i -> f.apply(current, gridLocationOf(i, rowLength, columnLength))));
            time += dt;
        }
        s.add(end, (i -> f.apply(end, gridLocationOf(i, rowLength, columnLength))));
        return s;
    }

    public static Pair gridLocationOf(int i, int rows, int columns) {
        int r = i / columns;
        int c = i % columns;
        if ((r >= rows) || (c >= columns)) {
            throw new IllegalArgumentException();
        }
        return new Pair<>(r, c);
    }

    public static  SpatialModel createSpatialModel(int size, Map, T> edges) {
        return createSpatialModel(size, (i, j) -> edges.get(new Pair<>(i, j)));
    }

    public static  SpatialModel createSpatialModel(int size, BiFunction edges) {
        GraphModel model = new GraphModel<>(size);
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                T value = edges.apply(i, j);
                if (value != null) {
                    model.add(i, value, j);
                }
            }
        }
        return model;
    }

    public static  SpatialModel createGridModelAsGraph(int rows, int columns, boolean directed, T w) {
        int size = rows * columns;
        GraphModel model = new GraphModel<>(size);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                if (i + 1 < rows) {
                    model.add(gridIndexOf(i, j, columns), w, gridIndexOf(i + 1, j, columns));
                    if (!directed) {
                        model.add(gridIndexOf(i + 1, j, columns), w, gridIndexOf(i, j, columns));
                    }
                }
                if (j + 1 < columns) {
                    model.add(gridIndexOf(i, j, columns), w, gridIndexOf(i, j + 1, columns));
                    if (!directed) {
                        model.add(gridIndexOf(i, j + 1, columns), w, gridIndexOf(i, j, columns));
                    }
                }
            }
        }
        return model;
    }

    public static int gridIndexOf(int r, int c, int columns) {
        return r * columns + c;
    }

    public static  RegularGridModel createGridModel(int rows, int cols, T w) {
        return new RegularGridModel<>(rows, cols, w);
    }

    public static  GraphModel createGraphFromMatlabData(int nodes,
                                                              int[][] edges,
                                                              T[] weights) {
        final int SOURCE = 0;
        final int DESTINATION = 1;
        GraphModel space = new GraphModel<>(nodes);
        if (edges.length != weights.length)
            throw new IllegalArgumentException("Mismatching edges provided");

        for (int i = 0; i < edges.length; i++) {
            space.add(edges[i][SOURCE] - 1,     //Matlab indices fix
                    weights[i],
                    edges[i][DESTINATION] - 1); //Matlab indices fix
        }

        return space;
    }

    public static  LocationService
    createLocationServiceFromTimesAndModels(double[] times,
                                            SpatialModel[] models) {
        LocationServiceList locSvc = new LocationServiceList<>();
        if (times.length != models.length)
            throw new IllegalArgumentException("Mismatched arguments provided");

        for (int i = 0; i < times.length; i++) {
            locSvc.add(times[i], models[i]);
        }

        return locSvc;
    }


    public static LocationService createLocServiceFromSetMatrix(Object[] cgraph1) {
        double[][] matrix;
        LocationServiceList locService = new LocationServiceList<>();
        for (int k = 0; k < cgraph1.length; k++) {
            double t = ((float) k);
            matrix = (double[][]) cgraph1[(int) Math.floor(t)];
            int size = matrix.length;
            GraphModel graphModel = new GraphModel<>(size);
            for (int i = 0; i < matrix.length; i++) {
                for (int j = i + 1; j < matrix[i].length; j++) {
                    graphModel.add(i, matrix[i][j], j);
                    graphModel.add(j, matrix[j][i], i);
                }
            }
            locService.add(t, graphModel);
        }
        return locService;
    }

    public static LocationService createLocServiceStatic(double start, double dt, double end, SpatialModel graph) {
        LocationServiceList locService = new LocationServiceList<>();
        double time = start;
        while (time < end) {
            double current = time;
            locService.add(time, graph);
            time += dt;
        }
        locService.add(end, graph);
        return locService;
    }

    public static LocationService
    createLocServiceStaticFromTimeTraj(double[] time,
                                       SpatialModel graph) {
        LocationServiceList locService = new LocationServiceList<>();
        for (double v : time) {
            locService.add(v, graph);
        }
        return locService;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy