<commit_before>/*
 * Copyright (c) 2013. 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.commons.io;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.DC;
import com.hp.hpl.jena.vocabulary.RDF;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.ac.open.kmi.iserve.commons.model.*;
import uk.ac.open.kmi.iserve.commons.vocabulary.MSM;
import uk.ac.open.kmi.iserve.commons.vocabulary.MSM_WSDL;
import uk.ac.open.kmi.iserve.commons.vocabulary.SAWSDL;
import uk.ac.open.kmi.iserve.commons.vocabulary.WSMO_LITE;

import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * ServiceReaderImpl
 * TODO: Provide Description
 * <p/>
 * Author: <NAME> (KMi - The Open University)
 * Date: 01/06/2013
 * Time: 12:23
 */
public class ServiceReaderImpl implements ServiceReader {

    private static final Logger log = LoggerFactory.getLogger(ServiceReaderImpl.class);

    OntModel model;

    /**
     * Read a stream in RDF and return the corresponding set of services
     * as Java Objects
     *
     * @param in The input stream of MSM services
     * @return The collection of Services parsed from the stream
     */
    @Override
    public List<Service> parse(InputStream in, Syntax syntax) {

        try {
            // create an empty model
            model = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM);
            // Parse the stream into a model
            model.read(in, null, syntax.getName());
            return parseService(model);
        } finally {
            if (model != null)
                model.close();
        }
    }

    public ArrayList<Service> parseService(OntModel model) {
        ArrayList<Service> result = new ArrayList<Service>();
        // Get the services
        Service service;
        Individual individual;
        ExtendedIterator<Individual> services = model.listIndividuals(MSM.Service);
        while (services.hasNext()) {
            individual = services.next();
            try {
                service = obtainService(individual);
                if (service != null) {
                    result.add(service);
                }
            } catch (URISyntaxException e) {
                log.error("Incorrect URI while parsing service.", e);
            }

        }<commit_msg>Moved OntModel within the method.<commit_after>/*
 * Copyright (c) 2013. 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.commons.io;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.DC;
import com.hp.hpl.jena.vocabulary.RDF;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.ac.open.kmi.iserve.commons.model.*;
import uk.ac.open.kmi.iserve.commons.vocabulary.MSM;
import uk.ac.open.kmi.iserve.commons.vocabulary.MSM_WSDL;
import uk.ac.open.kmi.iserve.commons.vocabulary.SAWSDL;
import uk.ac.open.kmi.iserve.commons.vocabulary.WSMO_LITE;

import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * ServiceReaderImpl
 * TODO: Provide Description
 * <p/>
 * Author: <NAME> (KMi - The Open University)
 * Date: 01/06/2013
 * Time: 12:23
 */
public class ServiceReaderImpl implements ServiceReader {

    private static final Logger log = LoggerFactory.getLogger(ServiceReaderImpl.class);

    /**
     * Read a stream in RDF and return the corresponding set of services
     * as Java Objects
     *
     * @param in The input stream of MSM services
     * @return The collection of Services parsed from the stream
     */
    @Override
    public List<Service> parse(InputStream in, Syntax syntax) {

        OntModel model = null;
        List<Service> result = new ArrayList<Service>();
        try {
            // create an empty model
            model = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM);
            // Parse the stream into a model
            model.read(in, null, syntax.getName());
            result = parseService(model);
        } finally {
            if (model != null)
                model.close();
        }

        return result;
    }

    public ArrayList<Service> parseService(OntModel model) {
        ArrayList<Service> result = new ArrayList<Service>();
        // Get the services
        Service service;
        Individual individual;
        ExtendedIterator<Individual> services = model.listIndividuals(MSM.Service);
        while (services.hasNext()) {
            individual = services.next();
            try {
                service = obtainService(individual);
                if (service != null) {
                    result.add(service);
                }
            } catch (URISyntaxException e) {
                log.error("Incorrect URI while parsing service.", e);
            }

        }
{'fraction_non_alphanumeric': 0.10109415934658653, 'fraction_numerical': 0.006780705809832023, 'mean_word_length': 2.738479262672811, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5221481', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1860, 'n_words': 527}