main.io.github.moonlightsuite.moonlight.online.algorithms.SpatialOp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moonlight-engine Show documentation
Show all versions of moonlight-engine Show documentation
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.online.algorithms;
import io.github.moonlightsuite.moonlight.core.signal.Sample;
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.online.signal.TimeChain;
import io.github.moonlightsuite.moonlight.online.signal.Update;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.IntStream;
import static io.github.moonlightsuite.moonlight.online.signal.Update.asTimeChain;
public class SpatialOp
, S, R extends Comparable> {
private final SpaceIterator spaceIterator;
private final BiFunction, DistanceStructure, IntFunction> op;
private List>> results;
private int locations;
public SpatialOp(@NotNull LocationService locationService,
Function,
DistanceStructure> distance,
BiFunction,
DistanceStructure,
IntFunction> operator) {
this.op = operator;
checkLocationServiceValidity(locationService);
spaceIterator = new SpaceIterator<>(locationService, distance);
}
private void checkLocationServiceValidity(LocationService locSvc) {
if (locSvc.isEmpty())
throw new UnsupportedOperationException("The location Service " +
"must not be empty!");
}
public List>> computeUnary(Update> u) {
locations = u.getValue().size();
T t = u.getStart();
spaceIterator.init(t);
T tNext = spaceIterator.fromNextSpaceOrFallback(u.getEnd());
doCompute(t, tNext, u.getValue());
return results;
}
protected void doCompute(T t, T tNext, List value) {
DistanceStructure f = spaceIterator.generateDistanceStructure();
tNext = spaceIterator.fromNextSpaceOrFallback(tNext);
results = new ArrayList<>();
addResult(t, tNext, op.apply(value::get, f));
T finalTNext = tNext;
spaceIterator.forEach(tNext, (itT, itDs) ->
addResult(itT, finalTNext, op.apply(value::get, itDs))
);
}
protected void addResult(T start, T end, IntFunction value) {
results.add(new Update<>(start, end, computeSS(value, locations)));
}
private List computeSS(IntFunction spatialSignal, int size) {
return IntStream.range(0, size)
.boxed()
.map(spatialSignal::apply)
.toList();
}
public TimeChain> computeUnaryChain(TimeChain> ups) {
TimeChain> resultsChain = new TimeChain<>(ups.getEnd());
final int LAST = ups.size() - 1;
locations = ups.getFirst().getValue().size();
spaceIterator.init(ups.getStart());
for (int i = 0; i < ups.size(); i++) {
Sample> up = ups.get(i);
T t = up.getStart();
T tNext = i != LAST ? ups.get(i + 1).getStart() : ups.getEnd();
doCompute(t, tNext, up.getValue());
asTimeChain(results).forEach(resultsChain::add);
}
return resultsChain;
}
}