<commit_before>package org.taverna.component.profile_creator.utils;

import static com.hp.hpl.jena.rdf.model.ModelFactory.createOntologyModel;
import static java.util.Collections.unmodifiableList;
import static org.apache.commons.io.IOUtils.closeQuietly;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.apache.commons.io.IOUtils;

import uk.org.taverna.ns._2012.component.profile.Ontology;
import uk.org.taverna.ns._2012.component.profile.SemanticAnnotation;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;

public class OntologyCollection {
	private final PropertyChangeSupport pcs;
	Map<String, Ontology> onts = new HashMap<>();
	Map<String, OntModel> models = new TreeMap<>();
	private List<PossibleStatement> possibles = new ArrayList<>();

	public OntologyCollection() {
		pcs = new PropertyChangeSupport(this);
	}

	public void addPropertyChangeListener(PropertyChangeListener listener) {
		pcs.addPropertyChangeListener(listener);
	}

	public void removePropertyChangeListener(PropertyChangeListener listener) {
		pcs.removePropertyChangeListener(listener);
	}

	public void addOntology(Ontology ont) throws OntologyCollectionException {
		OntModel old, model = readOntologyFromURI(ont.getId(), ont.getValue());
		onts.put(ont.getId(), ont);
		old = models.put(ont.getId(), model);
		possibles = generateTerms();
		pcs.firePropertyChange(ont.getId(), old, model);
	}

	public void removeOntology(Ontology ont) {
		OntModel model = models.remove(ont.getId());
		if (model != null) {
			onts.remove(ont.getId());
			possibles = generateTerms();
			pcs.firePropertyChange(ont.getId(), model, null);
		}
	}

	public List<PossibleStatement> getPossibleStatements() {
		return unmodifiableList(possibles);
	}

	public class PossibleStatement implements Comparable<PossibleStatement> {
		public final String ontologyId;
		public final String humanReadableForm;
		private final SemanticAnnotation annotation;

		private String name(Resource res) {
			if (res == null)
				return "?";
			Property altLabel = res.getModel().createProperty(
					"http://www.w3.org/2004/02/skos/core#prefLabel");
			for (Statement s : list(res.getModel().listStatements(res,
					altLabel, (String) null)))
				return s.getObject().asLiteral().toString();
			return res.getLocalName();
		}

		PossibleStatement(String id, Resource predicate, Resource object,
				SemanticAnnotation sa) {
			ontologyId = id;
			humanReadableForm = String.format("%s: %s => %s", id,
					name(predicate), name(object));
			annotation = sa;
		}

		@Override
		public int hashCode() {
			return humanReadableForm.hashCode();
		}

		@Override
		public boolean equals(Object o) {
			if (!(o instanceof PossibleStatement))
				return false;
			PossibleStatement ps = (PossibleStatement) o;
			return annotation.getOntology().equals(ps.annotation.getOntology())
					&& annotation.getPredicate().equals(
							ps.annotation.getPredicate())
					&& ((annotation.getClazz() == null && ps.annotation
							.getClazz() == null) || (annotation.getClazz() != null
							&& ps.annotation.getClazz() != null && annotation
							.getClazz().equals(ps.annotation.getClazz())))
					&& ((annotation.getValue() == null && ps.annotation
							.getValue() == null) || (annotation != null && annotation
							.getValue().equals(ps.annotation.getValue())));
		}

		public SemanticAnnotation getAnnotation() {
			SemanticAnnotation sa = new SemanticAnnotation();
			sa.setClazz(annotation.getClazz());
			sa.setOntology(ontologyId);
			sa.setPredicate(annotation.getOntology());
			sa.setValue(annotation.getValue());
			return sa;
		}

		@Override
		public int compareTo(PossibleStatement o) {
			return humanReadableForm.compareTo(o.humanReadableForm);
		}
	}

	private SemanticAnnotation sa(String ontology, RDFNode predicate) {
		SemanticAnnotation sa = new SemanticAnnotation();
		sa.setOntology(ontology);
		sa.setPredicate(predicate.toString());
		return sa;
	}

	private void initClass(SemanticAnnotation sa, RDFNode object) {
		if (object instanceof Individual)
			sa.setClazz(((Individual) object).getOntClass().getURI());
	}

	private boolean initValue(SemanticAnnotation sa, RDFNode object) {<commit_msg>Corrected class information, added it to renderable form.<commit_after>package org.taverna.component.profile_creator.utils;

import static com.hp.hpl.jena.rdf.model.ModelFactory.createOntologyModel;
import static java.util.Collections.unmodifiableList;
import static org.apache.commons.io.IOUtils.closeQuietly;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.apache.commons.io.IOUtils;

import uk.org.taverna.ns._2012.component.profile.Ontology;
import uk.org.taverna.ns._2012.component.profile.SemanticAnnotation;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;

public class OntologyCollection {
	private final PropertyChangeSupport pcs;
	Map<String, Ontology> onts = new HashMap<>();
	Map<String, OntModel> models = new TreeMap<>();
	private List<PossibleStatement> possibles = new ArrayList<>();

	public OntologyCollection() {
		pcs = new PropertyChangeSupport(this);
	}

	public void addPropertyChangeListener(PropertyChangeListener listener) {
		pcs.addPropertyChangeListener(listener);
	}

	public void removePropertyChangeListener(PropertyChangeListener listener) {
		pcs.removePropertyChangeListener(listener);
	}

	public void addOntology(Ontology ont) throws OntologyCollectionException {
		OntModel old, model = readOntologyFromURI(ont.getId(), ont.getValue());
		onts.put(ont.getId(), ont);
		old = models.put(ont.getId(), model);
		possibles = generateTerms();
		pcs.firePropertyChange(ont.getId(), old, model);
	}

	public void removeOntology(Ontology ont) {
		OntModel model = models.remove(ont.getId());
		if (model != null) {
			onts.remove(ont.getId());
			possibles = generateTerms();
			pcs.firePropertyChange(ont.getId(), model, null);
		}
	}

	public List<PossibleStatement> getPossibleStatements() {
		return unmodifiableList(possibles);
	}

	public class PossibleStatement implements Comparable<PossibleStatement> {
		public final String ontologyId;
		public final String humanReadableForm;
		private final SemanticAnnotation annotation;

		private String name(Resource res) {
			if (res == null)
				return "?";
			Property altLabel = res.getModel().createProperty(
					"http://www.w3.org/2004/02/skos/core#prefLabel");
			for (Statement s : list(res.getModel().listStatements(res,
					altLabel, (String) null)))
				return s.getObject().asLiteral().toString();
			return res.getLocalName();
		}

		PossibleStatement(String id, Resource predicate, Resource object,
				SemanticAnnotation sa) {
			ontologyId = id;
			humanReadableForm = String.format("%s: %s => %s::%s", id,
					name(predicate),
					sa.getClazz() == null ? "null" : name(predicate.getModel()
							.createResource(sa.getClazz())), name(object));
			annotation = sa;
		}

		@Override
		public int hashCode() {
			return humanReadableForm.hashCode();
		}

		@Override
		public boolean equals(Object o) {
			if (!(o instanceof PossibleStatement))
				return false;
			PossibleStatement ps = (PossibleStatement) o;
			return annotation.getOntology().equals(ps.annotation.getOntology())
					&& annotation.getPredicate().equals(
							ps.annotation.getPredicate())
					&& ((annotation.getClazz() == null && ps.annotation
							.getClazz() == null) || (annotation.getClazz() != null
							&& ps.annotation.getClazz() != null && annotation
							.getClazz().equals(ps.annotation.getClazz())))
					&& ((annotation.getValue() == null && ps.annotation
							.getValue() == null) || (annotation != null && annotation
							.getValue().equals(ps.annotation.getValue())));
		}

		public SemanticAnnotation getAnnotation() {
			SemanticAnnotation sa = new SemanticAnnotation();
			sa.setClazz(annotation.getClazz());
			sa.setOntology(ontologyId);
			sa.setPredicate(annotation.getOntology());
			sa.setValue(annotation.getValue());
			return sa;
		}

		@Override
		public int compareTo(PossibleStatement o) {
			return humanReadableForm.compareTo(o.humanReadableForm);
		}
	}

	private SemanticAnnotation sa(String ontology, RDFNode predicate) {
		SemanticAnnotation sa = new SemanticAnnotation();
		sa.setOntology(ontology);
		sa.setPredicate(predicate.toString());
		return sa;
	}

	private void initClass(SemanticAnnotation sa, RDFNode object) {
		if (object == null)
			return;
		if (object instanceof Individual)
			sa.setClazz(((Individual) object).getOntClass().getURI());
		else if (object.isResource())
			sa.setClazz(object.asResource().getURI());
	}

	private boolean initValue(SemanticAnnotation sa, RDFNode object) {
{'fraction_non_alphanumeric': 0.12688150318213962, 'fraction_numerical': 0.0030306091524396404, 'mean_word_length': 4.851063829787234, 'pattern_counts': {'":': 0, '<': 19, '<?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': '1799450', 'n_tokens_mistral': 3451, 'n_tokens_neox': 3059, 'n_words': 616}