<commit_before>/**
 * Licensed to the Austrian Association for Software Tool Integration (AASTI)
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. The AASTI licenses this file to you 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.openengsb.core.ekb.graph.orient.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import org.openengsb.core.api.model.ModelDescription;
import org.openengsb.core.ekb.api.ModelGraph;
import org.openengsb.core.ekb.api.transformation.TransformationDescription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.db.graph.OGraphDatabase;
import com.orientechnologies.orient.core.index.OIndexes;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;

/**
 * The EKB model graph is an implementation of the model graph. It uses a graph database as storage.
 * 
 * It usually get filled by two components: the TransformationEngineService and the ModelRegistryService. The
 * TransformationEngineService inserts all transformations it get saved as new edges into the graph database. The
 * ModelRegistry notifies the graph whenever new models get available or models get unavailable.
 */
public final class OrientModelGraph implements ModelGraph {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrientModelGraph.class);
    private static final String ACTIVE_FIELD = "isActive";
    private static final String FILENAME = "filename";
    private OGraphDatabase graph;
    private Map<String, TransformationDescription> descriptions;
    private AtomicLong counter;

    public OrientModelGraph() {
        startup();
        descriptions = new HashMap<String, TransformationDescription>();
        counter = new AtomicLong(0L);
    }

    private void startup() {
        ClassLoader origClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            ClassLoader orientClassLoader = OIndexes.class.getClassLoader();
            Thread.currentThread().setContextClassLoader(orientClassLoader);
            graph = new OGraphDatabase("memory:ekbgraphdb").create();
        } finally {
            Thread.currentThread().setContextClassLoader(origClassLoader);
        }
        Orient.instance().removeShutdownHook();
        graph.createVertexType("Models");
    }

    /**
     * Shuts down the graph database
     */
    public void shutdown() {
        ClassLoader origClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            ClassLoader orientClassLoader = OIndexes.class.getClassLoader();
            Thread.currentThread().setContextClassLoader(orientClassLoader);
            graph.close();
        } finally {
            Thread.currentThread().setContextClassLoader(origClassLoader);
        }
    }

    /**
     * Deletes all edges and nodes from the graph database. This method is used in the tests to have at every test the
     * same start situation.
     */
    public void cleanDatabase() {
        for (ODocument edge : graph.browseEdges()) {
            edge.delete();
        }
        for (ODocument node : graph.browseVertices()) {
            node.delete();
        }
    }

    @Override
    public void addModel(ModelDescription model) {
        ODocument node = getModel(model.toString());
        if (node == null) {
            node = graph.createVertex("Models");
            setIdFieldValue(node, model.toString());
        }
        setActiveFieldValue(node, true);
        node.save();
        LOGGER.debug("Added model {} to the graph database", model);
    }

    @Override
    public void removeModel(ModelDescription model) {
        ODocument node = getModel(model.toString());
        if (node == null) {
            LOGGER.warn("Couldn't remove model {} since it wasn't present in the graph database", model);
            return;
        }
        setActiveFieldValue(node, false);
        node.save();
        LOGGER.debug("Removed model {} from the graph database", model);
    }

    @Override
    public void addTransformation(TransformationDescription description) {
        checkTransformationDescriptionId(description);
        ODocument source = getOrCreateModel(description.getSourceModel().toString());
        ODocument target = getOrCreateModel(description.getTargetModel().toString());
        ODocument edge = graph.createEdge(source, target);
        setIdFieldValue(edge, description.getId());
        if (description.getFileName() != null) {
            setFilenameFieldValue(edge, description.getFileName());
        }
        edge.save();<commit_msg>[OPENENGSB-3302] now the property connections are saved in the edges of the ekb model graph<commit_after>/**
 * Licensed to the Austrian Association for Software Tool Integration (AASTI)
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. The AASTI licenses this file to you 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.openengsb.core.ekb.graph.orient.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;

import org.openengsb.core.api.model.ModelDescription;
import org.openengsb.core.ekb.api.ModelGraph;
import org.openengsb.core.ekb.api.transformation.TransformationDescription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.db.graph.OGraphDatabase;
import com.orientechnologies.orient.core.index.OIndexes;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;

/**
 * The EKB model graph is an implementation of the model graph. It uses a graph database as storage.
 * 
 * It usually get filled by two components: the TransformationEngineService and the ModelRegistryService. The
 * TransformationEngineService inserts all transformations it get saved as new edges into the graph database. The
 * ModelRegistry notifies the graph whenever new models get available or models get unavailable.
 */
public final class OrientModelGraph implements ModelGraph {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrientModelGraph.class);
    private static final String ACTIVE_FIELD = "isActive";
    private static final String FILENAME = "filename";
    private OGraphDatabase graph;
    private Map<String, TransformationDescription> descriptions;
    private AtomicLong counter;

    public OrientModelGraph() {
        startup();
        descriptions = new HashMap<String, TransformationDescription>();
        counter = new AtomicLong(0L);
    }

    private void startup() {
        ClassLoader origClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            ClassLoader orientClassLoader = OIndexes.class.getClassLoader();
            Thread.currentThread().setContextClassLoader(orientClassLoader);
            graph = new OGraphDatabase("memory:ekbgraphdb").create();
        } finally {
            Thread.currentThread().setContextClassLoader(origClassLoader);
        }
        Orient.instance().removeShutdownHook();
        graph.createVertexType("Models");
    }

    /**
     * Shuts down the graph database
     */
    public void shutdown() {
        ClassLoader origClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            ClassLoader orientClassLoader = OIndexes.class.getClassLoader();
            Thread.currentThread().setContextClassLoader(orientClassLoader);
            graph.close();
        } finally {
            Thread.currentThread().setContextClassLoader(origClassLoader);
        }
    }

    /**
     * Deletes all edges and nodes from the graph database. This method is used in the tests to have at every test the
     * same start situation.
     */
    public void cleanDatabase() {
        for (ODocument edge : graph.browseEdges()) {
            edge.delete();
        }
        for (ODocument node : graph.browseVertices()) {
            node.delete();
        }
    }

    @Override
    public void addModel(ModelDescription model) {
        ODocument node = getModel(model.toString());
        if (node == null) {
            node = graph.createVertex("Models");
            setIdFieldValue(node, model.toString());
        }
        setActiveFieldValue(node, true);
        node.save();
        LOGGER.debug("Added model {} to the graph database", model);
    }

    @Override
    public void removeModel(ModelDescription model) {
        ODocument node = getModel(model.toString());
        if (node == null) {
            LOGGER.warn("Couldn't remove model {} since it wasn't present in the graph database", model);
            return;
        }
        setActiveFieldValue(node, false);
        node.save();
        LOGGER.debug("Removed model {} from the graph database", model);
    }

    @Override
    public void addTransformation(TransformationDescription description) {
        checkTransformationDescriptionId(description);
        ODocument source = getOrCreateModel(description.getSourceModel().toString());
        ODocument target = getOrCreateModel(description.getTargetModel().toString());
        ODocument edge = graph.createEdge(source, target);
        setIdFieldValue(edge, description.getId());
        if (description.getFileName() != null) {
            setFilenameFieldValue(edge, description.getFileName());
        }
        Map<String, Set<String>> connections = description.getPropertyConnections();
        if (connections != null) {
            for (Map.Entry<String, Set<String>> entry : connections.entrySet()) {
                StringBuilder builder = new StringBuilder();
                for (String property : entry.getValue()) {
                    if (builder.length() != 0) {
                        builder.append(",");
                    }
                    builder.append(property);
                }
                edge.field(entry.getKey(), builder.toString());
            }
        }
        edge.save();
{'fraction_non_alphanumeric': 0.09105845728121757, 'fraction_numerical': 0.0016430300933932896, 'mean_word_length': 3.117123531505874, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3409135', 'n_tokens_mistral': 2904, 'n_tokens_neox': 2868, 'n_words': 864}