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

org.streampipes.manager.monitoring.runtime.SimilarStreamFinder Maven / Gradle / Ivy

There is a newer version: 0.65.0
Show newest version
/*
 * Copyright 2018 FZI Forschungszentrum Informatik
 *
 * 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 org.streampipes.manager.monitoring.runtime;

import org.streampipes.model.SpDataStream;
import org.streampipes.model.client.pipeline.Pipeline;
import org.streampipes.model.graph.DataSourceDescription;
import org.streampipes.model.quality.MeasurementCapability;
import org.streampipes.model.quality.MeasurementObject;
import org.streampipes.storage.management.StorageDispatcher;
import org.streampipes.storage.management.StorageManager;

import java.util.ArrayList;
import java.util.List;

public class SimilarStreamFinder {

	private Pipeline pipeline;
	
	private List similarStreams;
	
	public SimilarStreamFinder(String pipelineId) {
		this.pipeline = StorageDispatcher.INSTANCE.getNoSqlStore().getPipelineStorageAPI().getPipeline(pipelineId);
		this.similarStreams = new ArrayList<>();
	}
	
	public boolean isReplacable() {
		if (pipeline.getStreams().size() > 1 || pipeline.getStreams().size() == 0) return false;
		else {
			return isSimilarStreamAvailable();
		}
	}

	private boolean isSimilarStreamAvailable() {
		
		List seps = StorageManager.INSTANCE.getStorageAPI().getAllSEPs();
		List streams = getEventStreams(seps);
		
		SpDataStream pipelineInputStream = getStream();
		List pipelineInputStreamCapabilities = pipelineInputStream.getMeasurementCapability();
		List pipelineInputStreamMeasurementObject = pipelineInputStream.getMeasurementObject();
		
		for(SpDataStream stream : streams) {
			if (!stream.getElementId().equals(pipelineInputStream.getElementId())) {
				if (matchesStream(pipelineInputStreamCapabilities, pipelineInputStreamMeasurementObject, stream.getMeasurementCapability(), stream.getMeasurementObject())) {
					similarStreams.add(stream);
				}
			}
		}
		
		return similarStreams.size() > 0;
	}
	
	private boolean matchesStream(
			List pipelineInputStreamCapabilities,
			List pipelineInputStreamMeasurementObject,
			List measurementCapability,
			List measurementObject) {
		return matchesCapability(pipelineInputStreamCapabilities, measurementCapability) &&
				matchesObject(pipelineInputStreamMeasurementObject, measurementObject);
	}

	private boolean matchesObject(
			List pipelineInputStreamMeasurementObject,
			List measurementObject) {
		if (pipelineInputStreamMeasurementObject == null | measurementObject == null) return false;
		else return pipelineInputStreamMeasurementObject.stream().allMatch(p -> measurementObject.stream().anyMatch(mc -> mc.getMeasuresObject().toString().equals(p.getMeasuresObject().toString())));
	}

	private boolean matchesCapability(
			List pipelineInputStreamCapabilities,
			List measurementCapability) {
		if (pipelineInputStreamCapabilities == null || measurementCapability == null) return false;
		else return pipelineInputStreamCapabilities.stream().allMatch(p -> measurementCapability.stream().anyMatch(mc -> mc.getCapability().toString().equals(p.getCapability().toString())));
	}

	private SpDataStream getStream() {
		String streamId = pipeline.getStreams().get(0).getElementId();
		
		return StorageManager.INSTANCE.getStorageAPI().getEventStreamById(streamId);
	}
	
	private List getEventStreams(List seps) {
		List result = new ArrayList<>();
		for(DataSourceDescription sep : seps) {
			result.addAll(sep.getSpDataStreams());
		}
		return result;
	}

	public List getSimilarStreams() {
		return similarStreams;
	}
	
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy