<commit_before>/*
 * Copyright (c) 2014. Knowledge Media Institute - The Open University
 *
 * 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 uk.ac.open.kmi.iserve.sal.manager.impl;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.rdf.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.ac.open.kmi.iserve.core.ConfigurationProperty;
import uk.ac.open.kmi.iserve.core.iServeProperty;
import uk.ac.open.kmi.iserve.sal.events.Event;
import uk.ac.open.kmi.iserve.sal.events.OntologyCreatedEvent;
import uk.ac.open.kmi.iserve.sal.exception.SalException;
import uk.ac.open.kmi.iserve.sal.manager.IntegratedComponent;
import uk.ac.open.kmi.iserve.sal.manager.NfpManager;
import uk.ac.open.kmi.iserve.sal.manager.SparqlGraphStoreManager;
import uk.ac.open.kmi.iserve.sal.util.caching.Cache;
import uk.ac.open.kmi.iserve.sal.util.caching.CacheFactory;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Created by <NAME> on 27/05/2014.
 */
@Singleton
public class NfpManagerSparql extends IntegratedComponent implements NfpManager {

    private static Cache<String, ConcurrentMap<String, Set<String>>> propertyValueCache;
    private Logger logger = LoggerFactory.getLogger(NfpManagerSparql.class);
    private String sparqlEndpoint;
    private SparqlGraphStoreManager graphStoreManager;

    @Inject
    public NfpManagerSparql(EventBus eventBus,
                            @iServeProperty(ConfigurationProperty.ISERVE_URL) String iServeUri,
                            @iServeProperty(ConfigurationProperty.NFP_SPARQL_QUERY) String sparqlQueryEndpoint,
                            @iServeProperty(ConfigurationProperty.NFP_SPARQL_UPDATE) String sparqlUpdateEndpoint,
                            @iServeProperty(ConfigurationProperty.NFP_SPARQL_SERVICE) String sparqlServiceEndpoint,
                            CacheFactory cacheFactory,
                            SparqlGraphStoreFactory sparqlGraphStoreFactory) throws SalException {
        super(eventBus, iServeUri);
        this.sparqlEndpoint = sparqlQueryEndpoint;
        propertyValueCache = cacheFactory.createInMemoryCache("nfp");

        Set<URI> defaultModelsToLoad = ImmutableSet.of();


        // Configuration for quick retrieval of ontologies by resolving them to local files.
        ImmutableMap.Builder<String, String> mappingsBuilder = ImmutableMap.builder();

        // Configuration for avoiding the import of certain files
        ImmutableSet<String> ignoredImports = ImmutableSet.of();

        this.graphStoreManager = sparqlGraphStoreFactory.create(sparqlQueryEndpoint, sparqlUpdateEndpoint,
                sparqlServiceEndpoint, defaultModelsToLoad, mappingsBuilder.build(), ignoredImports);
        logger.debug("Created NfpManagerSparql");
    }


    @Override
    public void shutdown() {

    }

    @Override
    public void createPropertyValue(URI resource, URI property, Object value) throws SalException {
        OntModel model = ModelFactory.createOntologyModel();
        RDFNode object;
        if (value instanceof URI) {
            object = model.createResource(((URI) value).toASCIIString());
            if (graphStoreManager.fetchAndStore((URI) value)) {
                getEventBus().post(new OntologyCreatedEvent(new Date(), (URI) value));
            }
        } else {
            object = model.createTypedLiteral(value);
        }

        if (graphStoreManager.fetchAndStore(property)) {
            getEventBus().post(new OntologyCreatedEvent(new Date(), property));
        }

        Statement triple = model.createStatement(model.createResource(resource.toASCIIString()), model.createProperty(property.toASCIIString()), object);
        model.add(triple);
        URI graphUri = graphStoreManager.getGraphUriByResource(resource);
        if (graphUri == null) {
            throw new SalException("The requested resource does not exist");
        }
        graphStoreManager.addModelToGraph(graphUri, model);
        propertyValueCache.remove(resource.toASCIIString());
    }

    @Override
    public void createPropertyValuesOfResource(Map<URI, Object> propertyValueMap, URI resource) throws SalException {
        for (URI property : propertyValueMap.keySet()) {
            createPropertyValue(resource, property, propertyValueMap.get(property));
        }
    }

    @Override
    public void createPropertyValueOfResources(Map<URI, Object> resourceValueMap, URI property) throws SalException {
        for (URI resource : resourceValueMap.keySet()) {
            createPropertyValue(resource, property, resourceValueMap.get(resource));
        }<commit_msg>Fixed issue about storage of remote ontologies<commit_after>/*
 * Copyright (c) 2014. Knowledge Media Institute - The Open University
 *
 * 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 uk.ac.open.kmi.iserve.sal.manager.impl;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.rdf.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.ac.open.kmi.iserve.core.ConfigurationProperty;
import uk.ac.open.kmi.iserve.core.iServeProperty;
import uk.ac.open.kmi.iserve.sal.events.Event;
import uk.ac.open.kmi.iserve.sal.events.OntologyCreatedEvent;
import uk.ac.open.kmi.iserve.sal.exception.SalException;
import uk.ac.open.kmi.iserve.sal.manager.IntegratedComponent;
import uk.ac.open.kmi.iserve.sal.manager.NfpManager;
import uk.ac.open.kmi.iserve.sal.manager.SparqlGraphStoreManager;
import uk.ac.open.kmi.iserve.sal.util.caching.Cache;
import uk.ac.open.kmi.iserve.sal.util.caching.CacheFactory;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Created by <NAME> on 27/05/2014.
 */
@Singleton
public class NfpManagerSparql extends IntegratedComponent implements NfpManager {

    private static Cache<String, ConcurrentMap<String, Set<String>>> propertyValueCache;
    private Logger logger = LoggerFactory.getLogger(NfpManagerSparql.class);
    private String sparqlEndpoint;
    private SparqlGraphStoreManager graphStoreManager;

    @Inject
    public NfpManagerSparql(EventBus eventBus,
                            @iServeProperty(ConfigurationProperty.ISERVE_URL) String iServeUri,
                            @iServeProperty(ConfigurationProperty.NFP_SPARQL_QUERY) String sparqlQueryEndpoint,
                            @iServeProperty(ConfigurationProperty.NFP_SPARQL_UPDATE) String sparqlUpdateEndpoint,
                            @iServeProperty(ConfigurationProperty.NFP_SPARQL_SERVICE) String sparqlServiceEndpoint,
                            CacheFactory cacheFactory,
                            SparqlGraphStoreFactory sparqlGraphStoreFactory) throws SalException {
        super(eventBus, iServeUri);
        this.sparqlEndpoint = sparqlQueryEndpoint;
        propertyValueCache = cacheFactory.createInMemoryCache("nfp");

        Set<URI> defaultModelsToLoad = ImmutableSet.of();


        // Configuration for quick retrieval of ontologies by resolving them to local files.
        ImmutableMap.Builder<String, String> mappingsBuilder = ImmutableMap.builder();

        // Configuration for avoiding the import of certain files
        ImmutableSet<String> ignoredImports = ImmutableSet.of();

        this.graphStoreManager = sparqlGraphStoreFactory.create(sparqlQueryEndpoint, sparqlUpdateEndpoint,
                sparqlServiceEndpoint, defaultModelsToLoad, mappingsBuilder.build(), ignoredImports);
        logger.debug("Created NfpManagerSparql");
    }


    @Override
    public void shutdown() {

    }

    @Override
    public void createPropertyValue(URI resource, URI property, Object value) throws SalException {
        OntModel model = ModelFactory.createOntologyModel();
        RDFNode object;
        if (value instanceof URI) {
            object = model.createResource(((URI) value).toASCIIString());
            if (graphStoreManager.fetchAndStore((URI) value)) {
                getEventBus().post(new OntologyCreatedEvent(new Date(), (URI) value));
            }
        } else {
            object = model.createTypedLiteral(value);
        }

        Statement triple = model.createStatement(model.createResource(resource.toASCIIString()), model.createProperty(property.toASCIIString()), object);
        model.add(triple);
        URI graphUri = graphStoreManager.getGraphUriByResource(resource);
        if (graphUri == null) {
            throw new SalException("The requested resource does not exist");
        }
        graphStoreManager.addModelToGraph(graphUri, model);
        propertyValueCache.remove(resource.toASCIIString());
    }

    @Override
    public void createPropertyValuesOfResource(Map<URI, Object> propertyValueMap, URI resource) throws SalException {
        for (URI property : propertyValueMap.keySet()) {
            createPropertyValue(resource, property, propertyValueMap.get(property));
        }
    }

    @Override
    public void createPropertyValueOfResources(Map<URI, Object> resourceValueMap, URI property) throws SalException {
        for (URI resource : resourceValueMap.keySet()) {
            createPropertyValue(resource, property, resourceValueMap.get(resource));
        }
{'fraction_non_alphanumeric': 0.09255254735749503, 'fraction_numerical': 0.0031139174811867488, 'mean_word_length': 3.61741214057508, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '6893844', 'n_tokens_mistral': 3264, 'n_tokens_neox': 3201, 'n_words': 710}