From 170f632ef8c5df57d856c923467e04c386689299 Mon Sep 17 00:00:00 2001
From: Konstantin Blaschke <blaschke@fortiss.org>
Date: Wed, 31 May 2023 10:46:15 +0200
Subject: [PATCH] # WARNING: head commit changed in the meantime

Setting up Metric extraction plugin for AF3
Issue-Ref: 4310
Issue-Url: https://git.fortiss.org/af3/af3/-/issues/4310
Signed-off-by: Konstantin Blaschke <blaschke@fortiss.org>
---
 .../META-INF/MANIFEST.MF                      |  10 ++
 .../ext/quality/AF3QualityActivator.java      |  54 +++++++
 .../ext/quality/service/IMetricProvider.java  |  55 +++++++
 .../ext/quality/service/IMetricService.java   |  32 ++++
 .../ext/quality/service/MetricService.java    | 138 ++++++++++++++++++
 .../META-INF/MANIFEST.MF                      |   3 +-
 .../internal/ToolingKernelInternal.java       |   3 +
 7 files changed, 294 insertions(+), 1 deletion(-)
 create mode 100644 org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/AF3QualityActivator.java
 create mode 100644 org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/service/IMetricProvider.java
 create mode 100644 org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/service/IMetricService.java
 create mode 100644 org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/service/MetricService.java

diff --git a/org.fortiss.tooling.ext.quality/META-INF/MANIFEST.MF b/org.fortiss.tooling.ext.quality/META-INF/MANIFEST.MF
index c1706adaf..3584c3ee8 100644
--- a/org.fortiss.tooling.ext.quality/META-INF/MANIFEST.MF
+++ b/org.fortiss.tooling.ext.quality/META-INF/MANIFEST.MF
@@ -5,3 +5,13 @@ Bundle-SymbolicName: org.fortiss.tooling.ext.quality
 Bundle-Version: 1.0.0.qualifier
 Automatic-Module-Name: org.fortiss.tooling.ext.quality
 Bundle-RequiredExecutionEnvironment: JavaSE-11
+Import-Package: org.eclipse.core.runtime;version="3.5.0",
+ org.eclipse.emf.ecore,
+ org.eclipse.emf.ecore.xml.type,
+ org.fortiss.tooling.kernel.extension.data,
+ org.fortiss.tooling.kernel.introspection,
+ org.fortiss.tooling.kernel.service,
+ org.fortiss.tooling.kernel.service.base,
+ org.fortiss.tooling.kernel.utils,
+ org.osgi.framework
+Export-Package: org.fortiss.tooling.ext.quality.service
diff --git a/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/AF3QualityActivator.java b/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/AF3QualityActivator.java
new file mode 100644
index 000000000..97f276ca6
--- /dev/null
+++ b/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/AF3QualityActivator.java
@@ -0,0 +1,54 @@
+package org.fortiss.tooling.ext.quality;
+
+/*-------------------------------------------------------------------------+
+| Copyright 2023 fortiss GmbH                                              |
+|                                                                          |
+| 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.                                           |
++--------------------------------------------------------------------------*/
+
+import org.eclipse.core.runtime.Plugin;
+import org.osgi.framework.BundleContext;
+
+
+/**
+ * The activator class controls the plug-in life cycle.
+ * 
+ * @author blaschke
+ */
+public class AF3QualityActivator extends Plugin {
+	/** The plug-in ID. */
+	public static final String PLUGIN_ID = AF3QualityActivator.class.getPackage().getName(); // $NON-NLS-1$
+
+	/** The shared instance. */
+	private static AF3QualityActivator plugin;
+
+	/** {@inheritDoc} */
+	@Override
+	public void start(BundleContext context) throws Exception {
+		super.start(context);
+		plugin = this;
+		System.out.println("[Plugin] " + PLUGIN_ID + " started.");
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public void stop(BundleContext context) throws Exception {
+		plugin = null;
+		super.stop(context);
+	}
+
+	/** Returns the shared instance. */
+	public static AF3QualityActivator getDefault() {
+		return plugin;
+	}
+}
diff --git a/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/service/IMetricProvider.java b/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/service/IMetricProvider.java
new file mode 100644
index 000000000..830382c9e
--- /dev/null
+++ b/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/service/IMetricProvider.java
@@ -0,0 +1,55 @@
+/*-------------------------------------------------------------------------+
+| Copyright 2011 fortiss GmbH                                              |
+|                                                                          |
+| 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.fortiss.tooling.ext.quality.service;
+
+import java.util.Map;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.xml.type.AnyType;
+import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
+import org.fortiss.tooling.kernel.service.base.IObjectAware;
+
+/**
+ * Interface for the migration provider implementations.
+ * <P>
+ * migration provider extensions are handled by {@link IMetricService}.
+ * 
+ * @author blaschke
+ */
+public interface IMetricProvider extends IObjectAware<ITopLevelElement> {
+
+	/**
+	 * Determines whether migration provider is needed for the given
+	 * model element.
+	 * 
+	 * The parameter "unknownFeatures" returns a map indicating the features that are not recognized
+	 * in the model. This can be useful to detect features coming from old models and can be then
+	 * translated to the new model by a migrator.
+	 */
+	boolean needMigration(ITopLevelElement modelElement, Map<EObject, AnyType> unknownFeatures);
+
+	/**
+	 * Applies the provider to the given element.
+	 * 
+	 * The parameter "unknownFeatures" indicates the features that are not recognized
+	 * in the model. This can be useful to detect features coming from old models and can be then
+	 * translated to the new model by a migrator.
+	 * 
+	 * @return all unknown features that have successfully been migrated.
+	 */
+	Map<EObject, AnyType> migrate(ITopLevelElement modelElement,
+			Map<EObject, AnyType> unknownFeatures);
+}
\ No newline at end of file
diff --git a/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/service/IMetricService.java b/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/service/IMetricService.java
new file mode 100644
index 000000000..b5cc5feeb
--- /dev/null
+++ b/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/service/IMetricService.java
@@ -0,0 +1,32 @@
+/*-------------------------------------------------------------------------+
+| Copyright 2011 fortiss GmbH                                              |
+|                                                                          |
+| 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.fortiss.tooling.ext.quality.service;
+
+
+/**
+ * The migration service checks and upgrades the old models from the last release.
+ * 
+ * @author mou
+ */
+public interface IMetricService {
+	/** Returns the service instance. */
+	public static IMetricService getInstance() {
+		return MetricService.getInstance();
+	}
+
+	/** Registers the metric provider with the service. */
+	void registerMetricProvider(IMetricProvider provider, Class<?> modelElementClass);
+}
diff --git a/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/service/MetricService.java b/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/service/MetricService.java
new file mode 100644
index 000000000..d7f16e362
--- /dev/null
+++ b/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/service/MetricService.java
@@ -0,0 +1,138 @@
+/*-------------------------------------------------------------------------+
+| Copyright 2013 fortiss GmbH                                              |
+|                                                                          |
+| 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.fortiss.tooling.ext.quality.service;
+
+import static java.util.Collections.emptyList;
+import static org.fortiss.tooling.kernel.utils.EcoreUtils.getFirstChildWithType;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map.Entry;
+import org.eclipse.emf.ecore.EObject;
+import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
+import org.fortiss.tooling.kernel.introspection.IIntrospectionDetailsItem;
+import org.fortiss.tooling.kernel.introspection.IIntrospectionItem;
+import org.fortiss.tooling.kernel.introspection.IIntrospectiveKernelService;
+import org.fortiss.tooling.kernel.service.IKernelIntrospectionSystemService;
+import org.fortiss.tooling.kernel.service.IMigrationService;
+import org.fortiss.tooling.kernel.service.base.ObjectAwareServiceBase;
+
+/**
+ * This class implements the {@link IMigrationService} interface.
+ * 
+ * @author blaschke
+ */
+public class MetricService extends ObjectAwareServiceBase<IMetricProvider> implements IIntrospectiveKernelService, IMetricService{
+	/** The singleton instance. */
+	private static final MetricService INSTANCE = new MetricService();
+
+	/** Returns singleton instance of the service. */
+	public static MetricService getInstance() {
+		return INSTANCE;
+	}
+
+	/** The connector extension point ID. */
+	private static final String EXTENSION_POINT_NAME =
+			"org.fortiss.tooling.ext.quality.metricService";
+
+	/** The connector configuration element name. */
+	private static final String CONFIGURATION_ELEMENT_NAME = "metricService";
+
+	/** The connector attribute name. */
+	private static final String ATTRIBUTE_NAME = "metricService";
+
+	/** */
+	@Override
+	public void startService() {
+		IKernelIntrospectionSystemService.getInstance().registerService(this);
+	}
+
+	/** Registers the migration provider with the service. */
+	@Override
+	public void registerMetricProvider(IMetricProvider provider, Class<?> modelElementClass) {
+		addHandler(modelElementClass, provider);
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public String getIntrospectionDescription() {
+		return getIntrospectionLabel() +
+				"\n\nThis service allows the tracking of metrical development in modeling." +
+				"\n the service outputs the current metric analysis of the model and stores" +
+				"\n the data within logs for later history analysis." +
+				"\n\nThe service extension point is '" + EXTENSION_POINT_NAME + "'.";
+	}
+
+
+	/** Get all suitable {@link IMetricProvider} for the given input. */
+	@SuppressWarnings("unused")
+	private List<IMetricProvider> getProviders(ITopLevelElement input) {
+		List<IMetricProvider> providers = new ArrayList<>();
+		EObject migElement = input.getRootModelElement();
+
+		for(Entry<Class<?>, List<IMetricProvider>> migEntry : handlerMap.entrySet()) {
+			if(getFirstChildWithType(migElement, migEntry.getKey()) != null) {
+				providers.addAll(migEntry.getValue());
+			}
+		}
+
+		return providers;
+	}
+
+
+	/** */
+	@Override
+	protected String getExtensionPointName() {
+		return EXTENSION_POINT_NAME;
+	}
+
+
+	/** */
+	@Override
+	protected String getConfigurationElementName() {
+		return CONFIGURATION_ELEMENT_NAME;
+	}
+
+
+	/** */
+	@Override
+	protected String getHandlerClassAttribute() {
+		return ATTRIBUTE_NAME;
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public String getIntrospectionLabel() {
+		return "Metric Service";
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public List<IIntrospectionItem> getIntrospectionItems() {
+		return emptyList();
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean showInIntrospectionNavigation() {
+		return true;
+	}
+
+	@SuppressWarnings("javadoc")
+	@Override
+	public IIntrospectionDetailsItem getDetailsItem() {
+		return null;
+	}
+}
diff --git a/org.fortiss.tooling.kernel/META-INF/MANIFEST.MF b/org.fortiss.tooling.kernel/META-INF/MANIFEST.MF
index cd96ea055..2f54d7bd5 100644
--- a/org.fortiss.tooling.kernel/META-INF/MANIFEST.MF
+++ b/org.fortiss.tooling.kernel/META-INF/MANIFEST.MF
@@ -41,4 +41,5 @@ Export-Package: org.fortiss.tooling.kernel; uses:="org.eclipse.core.runtime,
  org.fortiss.tooling.kernel.utils; uses:="org.eclipse.emf.ecore,
    org.eclipse.core.resources"
 Automatic-Module-Name: org.fortiss.tooling.kernel
-Import-Package: com.google.common.collect
+Import-Package: com.google.common.collect,
+ org.fortiss.tooling.ext.quality.service
diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/ToolingKernelInternal.java b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/ToolingKernelInternal.java
index afbd2ccb5..6033329e6 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/ToolingKernelInternal.java
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/ToolingKernelInternal.java
@@ -17,6 +17,7 @@ package org.fortiss.tooling.kernel.internal;
 
 import static org.fortiss.tooling.kernel.ToolingKernel.printPhase;
 
+import org.fortiss.tooling.ext.quality.service.MetricService;
 import org.fortiss.tooling.kernel.internal.storage.eclipse.EclipseResourceStorageService;
 
 /**
@@ -42,6 +43,7 @@ public final class ToolingKernelInternal {
 		PrototypeService.getInstance().initializeService();
 		TransformationService.getInstance().initializeService();
 		TutorialService.getInstance().initializeService();
+		MetricService.getInstance().initializeService();
 		printPhase("Tooling Kernel initialized.");
 	}
 
@@ -60,6 +62,7 @@ public final class ToolingKernelInternal {
 		PersistencyService.getInstance().startService();
 		PrototypeService.getInstance().startService();
 		TransformationService.getInstance().startService();
+		MetricService.getInstance().startService();
 		printPhase("Tooling Kernel started.");
 	}
 }
-- 
GitLab