| /** |
| ******************************************************************************** |
| * Copyright (c) 2020 Eclipse APP4MC contributors. |
| * |
| * This program and the accompanying materials are made |
| * available under the terms of the Eclipse Public License 2.0 |
| * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| ******************************************************************************** |
| */ |
| |
| package org.eclipse.app4mc.atdb; |
| |
| import java.sql.PreparedStatement; |
| import java.sql.SQLException; |
| import java.util.Arrays; |
| import java.util.Collections; |
| import java.util.List; |
| import java.util.Objects; |
| import java.util.Optional; |
| import java.util.stream.Collectors; |
| import java.util.stream.Stream; |
| |
| public class ATDBConnection extends DBConnection { |
| |
| private static final String METAINFORMATION_QUERY = "SELECT value FROM metaInformation WHERE name = ?;"; |
| private static final String ENTITY_BY_TYPE_QUERY = "SELECT name FROM entity WHERE entityTypeId = (SELECT id FROM entityType WHERE entityType.name = ?);"; |
| private static final String EVENT_BY_TYPE_QUERY = "SELECT name FROM event WHERE eventTypeId = (SELECT id FROM eventType WHERE eventType.name = ?);"; |
| private static final String ENTITY_PROPERTY_ENTITYREF_QUERY = "SELECT (SELECT name FROM entity WHERE id = value) AS entities " |
| + "FROM entity AS sourceEntity, propertyValue WHERE sourceEntity.name = ? " |
| + "AND entityTypeId = (SELECT id FROM entityType WHERE entityType.name = ?) " |
| + "AND propertyValue.entityId = sourceEntity.id " |
| + "AND propertyValue.propertyId = (SELECT id FROM property WHERE name = ?);"; |
| private static final String ENTITY_PROPERTY_VALUE_QUERY = "SELECT propertyValue.value " |
| + "FROM entity AS sourceEntity, propertyValue WHERE sourceEntity.name = ? " |
| + "AND entityTypeId = (SELECT id FROM entityType WHERE entityType.name = ?) " |
| + "AND propertyValue.entityId = sourceEntity.id " |
| + "AND propertyValue.propertyId = (SELECT id FROM property WHERE name = ?);"; |
| private static final String EVENTCHAIN_EVENTREF_QUERY = "SELECT (SELECT GROUP_CONCAT((SELECT name FROM event WHERE id = value), ', ') " |
| + "FROM propertyValue WHERE entityId = ecEntity.id AND propertyId = (SELECT id FROM property WHERE name = ?)) AS evName " |
| + "FROM entity AS ecEntity WHERE ecEntity.name = ? AND entityTypeId = (SELECT id FROM entityType WHERE entityType.name = 'EC');"; |
| private static final String METRICS_QUERY = "SELECT name FROM metric;"; |
| private static final String METRICVALUE_QUERY = "SELECT value FROM entityMetricValue WHERE entityId = (SELECT id FROM entity WHERE name = ?) " |
| + "AND metricId = (SELECT id FROM metric WHERE name = ?);"; |
| |
| public ATDBConnection(String dbFile) throws SQLException { |
| super(dbFile); |
| } |
| |
| public ATDBConnection(String dbFile, boolean writeAccess) throws SQLException { |
| super(dbFile, writeAccess); |
| } |
| |
| private String getFirstStringFromPreparedQuery(final String query, final List<String> parameters, final String defaultValue) throws SQLException { |
| return getStringStreamFromPreparedQuery(query, parameters).findFirst().orElse(defaultValue); |
| } |
| |
| private Stream<String> getStringStreamFromPreparedQuery(final String query, final List<String> parameters) throws SQLException { |
| final PreparedStatement prepStmt = super.getPrepareQueryFor(query); |
| for(int i = 0; i < parameters.size(); i++) { |
| prepStmt.setString(i + 1, parameters.get(i)); |
| } |
| return super.queryAndMapToStream(prepStmt, rs -> rs.getString(1)).filter(Objects::nonNull); |
| } |
| |
| private List<String> getStringListFromPreparedQuery(final String query, final List<String> parameters) throws SQLException { |
| return getStringStreamFromPreparedQuery(query, parameters).collect(Collectors.toList()); |
| } |
| |
| private String getMetaInfo(final String metaInfoKey, final String defaultValue) throws SQLException { |
| return getFirstStringFromPreparedQuery(METAINFORMATION_QUERY, Arrays.asList(metaInfoKey), defaultValue); |
| } |
| |
| /** |
| * Returns the time base of this ATDB. |
| * |
| * @return The time base unit. If not set, "?" will be returned. |
| * @throws SQLException |
| */ |
| public String getTimeBase() throws SQLException { |
| return getMetaInfo("timeBase", "?"); |
| } |
| |
| private List<String> getAllEntitiesByType(final String entityTypeName) throws SQLException { |
| return getStringListFromPreparedQuery(ENTITY_BY_TYPE_QUERY, Arrays.asList(entityTypeName)); |
| } |
| |
| /** |
| * Returns all ECU names of this ATDB. |
| * @return A list of all ECU names. |
| * @throws SQLException |
| */ |
| public List<String> getAllECUs() throws SQLException { |
| return getAllEntitiesByType("ECU"); |
| } |
| |
| /** |
| * Returns all processor names of this ATDB for the given ecu name. |
| * @param ecuName The ECU name of which we want to retrieve all processor names. |
| * @return A list of all processor names for the given ECU. |
| * @throws SQLException |
| */ |
| public List<String> getProcessorsOfECU(final String ecuName) throws SQLException { |
| return getStringListFromPreparedQuery(ENTITY_PROPERTY_ENTITYREF_QUERY, Arrays.asList(ecuName, "ECU", "processors")); |
| } |
| |
| /** |
| * Returns all processor names of this ATDB. |
| * @return A list of all processor names. |
| * @throws SQLException |
| */ |
| public List<String> getAllProcessors() throws SQLException { |
| return getAllEntitiesByType("Processor"); |
| } |
| |
| /** |
| * Returns all core names of this ATDB for the given processor name. |
| * @param processorName The processor name of which we want to retrieve all core names. |
| * @return A list of all core names for the given processor. |
| * @throws SQLException |
| */ |
| public List<String> getCoresOfProcessor(final String processorName) throws SQLException { |
| return getStringListFromPreparedQuery(ENTITY_PROPERTY_ENTITYREF_QUERY, Arrays.asList(processorName, "Processor", "cores")); |
| } |
| |
| /** |
| * Returns all core names of this ATDB. |
| * @return A list of all core names. |
| * @throws SQLException |
| */ |
| public List<String> getAllCores() throws SQLException { |
| return getAllEntitiesByType("C"); |
| } |
| |
| /** |
| * Returns the frequency in Hz for the given core name. |
| * @param coreName The core name of which we want to retrieve the frequency. |
| * @return The frequency in Hz as string (can be parsed into a long). Empty, if none was found. |
| * @throws SQLException |
| */ |
| public String getCoreFrequencyInHz(final String coreName) throws SQLException { |
| return getFirstStringFromPreparedQuery(ENTITY_PROPERTY_VALUE_QUERY, Arrays.asList(coreName, "C", "frequencyInHz"), ""); |
| } |
| |
| /** |
| * Returns all task names of this ATDB. |
| * @return A list of all task names. |
| * @throws SQLException |
| */ |
| public List<String> getAllTasks() throws SQLException { |
| return getAllEntitiesByType("T"); |
| } |
| |
| /** |
| * Returns all ISR (Interrupt Service Routine) names of this ATDB. |
| * @return A list of all ISR names. |
| * @throws SQLException |
| */ |
| public List<String> getAllISRs() throws SQLException { |
| return getAllEntitiesByType("I"); |
| } |
| |
| /** |
| * Returns the name of the core that executes the process specified by the process name. |
| * @param processName The process name of which we want to retrieve the executing core. |
| * @return The core name on which the process is executed. Empty, if none was found. |
| * @throws SQLException |
| */ |
| public String getExecutingCoreOfProcess(final String processName) throws SQLException { |
| final Optional<String> coreOfTask = getStringStreamFromPreparedQuery(ENTITY_PROPERTY_ENTITYREF_QUERY, Arrays.asList(processName, |
| "T", "executingCore")).findFirst(); |
| if (!coreOfTask.isPresent()) { |
| return getFirstStringFromPreparedQuery(ENTITY_PROPERTY_ENTITYREF_QUERY, Arrays.asList(processName, "I", "executingCore"), ""); |
| } else { |
| return coreOfTask.get(); |
| } |
| } |
| |
| /** |
| * Returns all runnable names of this ATDB that are called by the given process name. |
| * @param processName The process name of which we want to retrieve all runnable names. |
| * @return A list of all runnable names called by the given process. |
| * @throws SQLException |
| */ |
| public List<String> getRunnablesOfProcess(final String processName) throws SQLException { |
| return Stream.concat( |
| getStringStreamFromPreparedQuery(ENTITY_PROPERTY_ENTITYREF_QUERY, Arrays.asList(processName, "T", "runnables")), |
| getStringStreamFromPreparedQuery(ENTITY_PROPERTY_ENTITYREF_QUERY, Arrays.asList(processName, "I", "runnables")) |
| ).collect(Collectors.toList()); |
| } |
| |
| /** |
| * Returns all runnable names of this ATDB. |
| * @return A list of all runnable names. |
| * @throws SQLException |
| */ |
| public List<String> getAllRunnables() throws SQLException { |
| return getAllEntitiesByType("R"); |
| } |
| |
| /** |
| * Returns all label names of this ATDB that are read by the given runnable name. |
| * @param runnableName The runnable name of which we want to retrieve all read labels. |
| * @return A list of all label names read by the given runnable. |
| * @throws SQLException |
| */ |
| public List<String> getLabelsReadByRunnable(final String runnableName) throws SQLException { |
| return getStringListFromPreparedQuery(ENTITY_PROPERTY_ENTITYREF_QUERY, Arrays.asList(runnableName, "R", "readSignals")); |
| } |
| |
| /** |
| * Returns all label names of this ATDB that are written by the given runnable name. |
| * @param runnableName The runnable name of which we want to retrieve all written labels. |
| * @return A list of all label names written by the given runnable. |
| * @throws SQLException |
| */ |
| public List<String> getLabelsWrittenByRunnable(final String runnableName) throws SQLException { |
| return getStringListFromPreparedQuery(ENTITY_PROPERTY_ENTITYREF_QUERY, Arrays.asList(runnableName, "R", "writtenSignals")); |
| } |
| |
| /** |
| * Returns all stimulus names of this ATDB. |
| * @return A list of all stimulus names. |
| * @throws SQLException |
| */ |
| public List<String> getAllStimuli() throws SQLException { |
| return getAllEntitiesByType("STI"); |
| } |
| |
| /** |
| * Returns all label names of this ATDB. |
| * @return A list of all label names. |
| * @throws SQLException |
| */ |
| public List<String> getAllLabels() throws SQLException { |
| return getAllEntitiesByType("SIG"); |
| } |
| |
| /** |
| * Returns the initial value for the given label name. |
| * @param labelName The label name of which we want to retrieve the initial value. |
| * @return The initial value as string. Empty, if none was found. |
| * @throws SQLException |
| */ |
| public String getLabelInitialValue(final String labelName) throws SQLException { |
| return getFirstStringFromPreparedQuery(ENTITY_PROPERTY_VALUE_QUERY, Arrays.asList(labelName, "SIG", "initialValue"), ""); |
| } |
| |
| /** |
| * Returns all event chain names of this ATDB. |
| * @return A list of all event chain names. |
| * @throws SQLException |
| */ |
| public List<String> getAllEventChains() throws SQLException { |
| return getAllEntitiesByType("EC"); |
| } |
| |
| /** |
| * Returns all event (specification, not from the actual trace!) names of this ATDB of the given event type. |
| * @param eventTypeName The event type name of which the returned event names shall be. |
| * @return A list of all event names. |
| * @throws SQLException |
| */ |
| public List<String> getAllEventsByType(final String eventTypeName) throws SQLException { |
| return getStringListFromPreparedQuery(EVENT_BY_TYPE_QUERY, Arrays.asList(eventTypeName)); |
| } |
| |
| /** |
| * Returns the stimulus event name for the given event chain name. If there are multiple stimulus events, they will be separated by a ', '. |
| * @param eventChainName The name of the event chain from which we want to know the stimulus event. |
| * @return The stimulus event name. Empty, if none was found. |
| * @throws SQLException |
| */ |
| public String getEventChainStimulus(final String eventChainName) throws SQLException { |
| return getFirstStringFromPreparedQuery(EVENTCHAIN_EVENTREF_QUERY, Arrays.asList("ecStimulus", eventChainName), ""); |
| } |
| |
| /** |
| * Returns the response event name for the given event chain name. If there are multiple response events, they will be separated by a ', '. |
| * @param eventChainName The name of the event chain from which we want to know the response event. |
| * @return The response event name. Empty, if none was found. |
| * @throws SQLException |
| */ |
| public String getEventChainResponse(final String eventChainName) throws SQLException { |
| return getFirstStringFromPreparedQuery(EVENTCHAIN_EVENTREF_QUERY, Arrays.asList("ecResponse", eventChainName), ""); |
| } |
| |
| /** |
| * Returns the names of the item event chains for the given event chain name. |
| * @param eventChainName The name of the event chain from which we want to know the item event chain names. |
| * @return The item event chain names. |
| * @throws SQLException |
| */ |
| public List<String> getEventChainItems(final String eventChainName) throws SQLException { |
| return getStringListFromPreparedQuery(ENTITY_PROPERTY_ENTITYREF_QUERY, Arrays.asList(eventChainName, "EC", "items")); |
| } |
| |
| /** |
| * Returns all metric names contained in this ATDB, some of which are specific to certain entity types. |
| * @return All metric names. |
| * @throws SQLException |
| */ |
| public List<String> getAllMetrics() throws SQLException { |
| return getStringListFromPreparedQuery(METRICS_QUERY, Collections.emptyList()); |
| } |
| |
| /** |
| * Returns the value of the metric for the given entity and metric names. |
| * @param entityName The name of the entity from which we want to know the metric value. |
| * @param metricName The name of the metric from which we want to know the value for the specified entity. |
| * @return The value for the metric and entity. Empty, if none is found. |
| * @throws SQLException |
| */ |
| public String getValueForMetricAndEntity(final String entityName, final String metricName) throws SQLException { |
| return getFirstStringFromPreparedQuery(METRICVALUE_QUERY, Arrays.asList(entityName, metricName), ""); |
| } |
| |
| } |