<commit_before>/**
 * 
 */
package org.semanticweb.elk.ore;

/*
 * #%L
 * ELK Command Line Interface
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2013 Department of Computer Science, University of Oxford
 * %%
 * 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.
 * #L%
 */

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.semanticweb.elk.loading.AxiomLoader;
import org.semanticweb.elk.loading.ElkLoadingException;
import org.semanticweb.elk.loading.Owl2StreamLoader;
import org.semanticweb.elk.owl.exceptions.ElkException;
import org.semanticweb.elk.owl.interfaces.ElkClass;
import org.semanticweb.elk.owl.interfaces.ElkNamedIndividual;
import org.semanticweb.elk.owl.parsing.Owl2ParserFactory;
import org.semanticweb.elk.owl.parsing.javacc.Owl2FunctionalStyleParserFactory;
import org.semanticweb.elk.reasoner.ElkInconsistentOntologyException;
import org.semanticweb.elk.reasoner.Reasoner;
import org.semanticweb.elk.reasoner.ReasonerFactory;
import org.semanticweb.elk.reasoner.stages.SimpleStageExecutor;
import org.semanticweb.elk.reasoner.taxonomy.model.InstanceTaxonomy;
import org.semanticweb.elk.reasoner.taxonomy.model.Taxonomy;

/**
 * The CLI tool for the ORE 2013 contest which uses the ELK's native OWL FSS
 * parser and includes the parsing/loading/indexing time in the total wall time.
 * 
 * @author <NAME>
 * 
 *         <EMAIL>
 */
public class NativeRunner {
	
	enum Task {
		CLASSIFICATION {
		
			@Override
			public String toString() {
				return "classification";
			}
		},
		
		CONSISTENCY {
		
			@Override
			public String toString() {
				return "consistency";
			}
		},
		
		REALISATION {
			
			@Override
			public String toString() {
				return "realisation";
			}
		},
	};

	/**
	 * Arguments are as follows: 0 - name of the reasoning task (SAT, query,
	 * classification, consistency) 1 - ontology path 2 - output path 3 -
	 * concept URI, in case of SAT
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		Task task = validateArgs(args);
		// help
		if (task == null) {
			printHelp();
			return;
		}

		NativeRunner runner = new NativeRunner();
		
		runner.run(args, task);
	}
	
	void run(String[] args, Task task) throws Exception {
		File input = getOutputFile(args[1]);
		File output = getOutputFile(args[2]);
		// create reasoner
		ReasonerFactory reasoningFactory = new ReasonerFactory();
		Owl2ParserFactory parserFactory = new Owl2FunctionalStyleParserFactory();
		AxiomLoader loader = new Owl2StreamLoader(parserFactory, input);
		Reasoner reasoner = reasoningFactory.createReasoner(loader,
				new SimpleStageExecutor());

		try {
			loadOntology(reasoner);
			
			long ts = System.currentTimeMillis();

			System.out.println("Started " + task.toString() + " on " + input);

			switch (task) {
			case CLASSIFICATION:
				Taxonomy<ElkClass> taxonomy = reasoner.getTaxonomyQuietly();

				printTime(ts);

				writeClassTaxonomyToFile(output, taxonomy);

				printCompleted(task, input);

				break;
			case REALISATION:
				InstanceTaxonomy<ElkClass, ElkNamedIndividual> instanceTaxonomy = reasoner.getInstanceTaxonomyQuietly();

				printTime(ts);

				writeInstanceTaxonomyToFile(output, instanceTaxonomy);

				printCompleted(task, input);

				break;				
			case CONSISTENCY:
				boolean isConsistent = reasoner.isInconsistent();

				printTime(ts);

				writeStringToFile(output, String.valueOf(!isConsistent), false);

				printCompleted(task, input);

				break;
			default:
			}
		} catch (ElkLoadingException e) {
			System.err
					.println("ELK could not parse the ontology. Please, make sure that it is in the OWL 2 Functional Style Syntax.");
		} finally {
			reasoner.shutdown();
		}
	}

	protected void loadOntology(Reasoner reasoner) throws ElkException {
		// no eager loading so it will be done later and included in measurements
	}

	private File getOutputFile(String path) {
		File file = new File(path);

		try {
			if (!file.isAbsolute()) {<commit_msg>elk-ore: output messages are displayed in case of exceptions<commit_after>/**
 * 
 */
package org.semanticweb.elk.ore;

/*
 * #%L
 * ELK Command Line Interface
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2013 Department of Computer Science, University of Oxford
 * %%
 * 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.
 * #L%
 */

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.semanticweb.elk.loading.AxiomLoader;
import org.semanticweb.elk.loading.Owl2StreamLoader;
import org.semanticweb.elk.owl.exceptions.ElkException;
import org.semanticweb.elk.owl.interfaces.ElkClass;
import org.semanticweb.elk.owl.interfaces.ElkNamedIndividual;
import org.semanticweb.elk.owl.parsing.Owl2ParserFactory;
import org.semanticweb.elk.owl.parsing.javacc.Owl2FunctionalStyleParserFactory;
import org.semanticweb.elk.reasoner.ElkInconsistentOntologyException;
import org.semanticweb.elk.reasoner.Reasoner;
import org.semanticweb.elk.reasoner.ReasonerFactory;
import org.semanticweb.elk.reasoner.stages.SimpleStageExecutor;
import org.semanticweb.elk.reasoner.taxonomy.model.InstanceTaxonomy;
import org.semanticweb.elk.reasoner.taxonomy.model.Taxonomy;

/**
 * The CLI tool for the ORE 2013 contest which uses the ELK's native OWL FSS
 * parser and includes the parsing/loading/indexing time in the total wall time.
 * 
 * @author <NAME>
 * 
 *         <EMAIL>
 */
public class NativeRunner {
	
	enum Task {
		CLASSIFICATION {
		
			@Override
			public String toString() {
				return "classification";
			}
		},
		
		CONSISTENCY {
		
			@Override
			public String toString() {
				return "consistency";
			}
		},
		
		REALISATION {
			
			@Override
			public String toString() {
				return "realisation";
			}
		},
	};

	/**
	 * Arguments are as follows: 0 - name of the reasoning task (SAT, query,
	 * classification, consistency) 1 - ontology path 2 - output path 3 -
	 * concept URI, in case of SAT
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		Task task = validateArgs(args);
		// help
		if (task == null) {
			printHelp();
			return;
		}

		NativeRunner runner = new NativeRunner();
		
		runner.run(args, task);
	}
	
	void run(String[] args, Task task) throws Exception {
		File input = getOutputFile(args[1]);
		File output = getOutputFile(args[2]);
		// create reasoner
		ReasonerFactory reasoningFactory = new ReasonerFactory();
		Owl2ParserFactory parserFactory = new Owl2FunctionalStyleParserFactory();
		AxiomLoader loader = new Owl2StreamLoader(parserFactory, input);
		Reasoner reasoner = reasoningFactory.createReasoner(loader,
				new SimpleStageExecutor());

		boolean printedStarted = false, printedTime = false;
		
		long ts = System.currentTimeMillis();
		
		try {
			loadOntology(reasoner);
			
			ts = System.currentTimeMillis();

			System.out.println("Started " + task.toString() + " on " + input);
			printedStarted = true;

			switch (task) {
			case CLASSIFICATION:
				Taxonomy<ElkClass> taxonomy = reasoner.getTaxonomyQuietly();

				printTime(ts);
				printedTime = true;

				writeClassTaxonomyToFile(output, taxonomy);

				break;
			case REALISATION:
				InstanceTaxonomy<ElkClass, ElkNamedIndividual> instanceTaxonomy = reasoner.getInstanceTaxonomyQuietly();

				printTime(ts);
				printedTime = true;

				writeInstanceTaxonomyToFile(output, instanceTaxonomy);

				break;				
			case CONSISTENCY:
				boolean isConsistent = reasoner.isInconsistent();

				printTime(ts);
				printedTime = true;

				writeStringToFile(output, String.valueOf(!isConsistent), false);

				break;
			default:
			}
		} catch (ElkException e) {
			System.err.println("ELK error: " + e);
			if (!printedStarted) {
				ts = System.currentTimeMillis();
				System.out.println("Started " + task.toString() + " on " + input);				
			}
			if (!printedTime) {
				printTime(ts);
			}			
		} finally {
			printCompleted(task, input);
			reasoner.shutdown();
		}
	}

	protected void loadOntology(Reasoner reasoner) throws ElkException {
		// no eager loading so it will be done later and included in measurements
	}

	private File getOutputFile(String path) {
		File file = new File(path);

		try {
			if (!file.isAbsolute()) {
{'fraction_non_alphanumeric': 0.10270154373927959, 'fraction_numerical': 0.006110634648370497, 'mean_word_length': 3.7914740626605035, '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': '1119085', 'n_tokens_mistral': 3319, 'n_tokens_neox': 2822, 'n_words': 769}