/* * This file is part of the ONT API. * The contents of this file are subject to the LGPL License, Version 3.0. * Copyright (c) 2020, The University of Manchester, owl.cs group. * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/. * * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above. * 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 com.github.owlcs.tmp; import com.github.owlcs.TestData; import com.github.owlcs.ontapi.OntManagers; import com.github.owlcs.ontapi.Ontology; import com.github.owlcs.ontapi.OntologyManager; import com.github.owlcs.ontapi.internal.AxiomParserProvider; import com.github.owlcs.ontapi.internal.AxiomTranslator; import com.github.owlcs.ontapi.internal.OWLComponentType; import com.github.owlcs.ontapi.internal.OWLContentType; import com.github.owlcs.ontapi.jena.model.OntClass; import com.github.owlcs.ontapi.jena.model.OntModel; import com.github.owlcs.ontapi.jena.model.OntStatement; import com.github.owlcs.ontapi.jena.vocabulary.OWL; import org.apache.jena.rdf.model.Resource; import org.semanticweb.owlapi.model.OWLAxiom; import org.semanticweb.owlapi.model.OWLClass; import java.time.Duration; import java.time.Instant; import java.util.HashSet; import java.util.Set; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; /** * Created by @ssz on 01.03.2020. */ public class AxiomReferencesTmp { public static void main(String... args) throws Exception { Instant s = Instant.now(); System.out.println("START::: " + s); _testReferences(TestData.PIZZA, 10); _testReferences(TestData.FAMILY, 10); _testReferences(TestData.PS, 5); // big: _testReferences(TestData.GALEN, 1); _testReferences(TestData.HP, 1); _testReferences(TestData.TTO, 1); _testReferences(TestData.GO, 1); System.out.println("TOTAL::: " + Duration.between(s, Instant.now())); } private static void _testReferences(TestData data, int num) throws Exception { System.out.println("========================================"); OntologyManager m = OntManagers.createONT(); Ontology o = m.loadOntologyFromOntologyDocument(data.getDocumentSource()); Set classes = o.classesInSignature().collect(Collectors.toSet()); System.out.printf("TEST %s:::: CLASSES=%d ||| AXIOMS=%d |||| iter = %d%n", data, classes.size(), o.getAxiomCount(), num); long count1 = _measure("DEF", num, () -> classes.stream().flatMap(o::referencingAxioms)); long count2 = _measure("GRH", num, () -> classes.stream().flatMap(x -> referencingAxioms(o, x))); if (count1 != count2) throw new IllegalStateException(); } private static long _measure(String pref, int num, Supplier> get) { long count = 0; double d = 0; for (int i = 0; i < num; i++) { Instant s = Instant.now(); count += get.get().distinct().count(); d += getDurationInSeconds(Duration.between(s, Instant.now())); } count /= num; d /= num; System.out.println(pref + "::: " + d + " s."); return count; } public static double getDurationInSeconds(Duration d) { return (double) d.getSeconds() + d.getNano() / 1_000_000_000d; } public static class PizzaT { public static void main(String... args) throws Exception { OntologyManager m = OntManagers.createONT(); Ontology o = m.loadOntologyFromOntologyDocument(TestData.PIZZA.getDocumentSource()); OWLClass clazz = m.getOWLDataFactory() .getOWLClass("http://www.co-ode.org/ontologies/pizza/pizza.owl#MozzarellaTopping"); testRefs(clazz, o); o.classesInSignature().forEach(c -> testRefs(c, o)); } } private static void testRefs(OWLClass clazz, Ontology o) { Set ref1 = o.referencingAxioms(clazz).collect(Collectors.toSet()); Set ref2 = referencingAxioms(o, clazz).collect(Collectors.toSet()); System.out.println(clazz + " => " + ref1.size() + " ||| " + ref2.size()); System.out.println(); for (OWLAxiom a : ref1) { if (ref2.contains(a)) continue; System.out.println("Can't find in propositions : " + a); } for (OWLAxiom a : ref2) { if (ref1.contains(a)) continue; System.out.println("Can't find in original : " + a); } if (ref1.equals(ref2)) return; throw new IllegalArgumentException(); } public static Stream referencingAxioms(Ontology o, OWLClass c) { OntModel m = o.asGraphModel(); OntClass clazz = m.getOntClass(c.getIRI().getIRIString()); Stream candidates = Stream.concat(m.statements(clazz, null, null), m.statements(null, null, clazz).flatMap(AxiomReferencesTmp::roots)); if (OWL.Thing.equals(clazz)) { candidates = Stream.concat(candidates, specialForThing(m).flatMap(AxiomReferencesTmp::roots)); } return candidates.flatMap(s -> translators(s).map(x -> x.toAxiom(s).getOWLObject())); } private static Stream specialForThing(OntModel m) { return Stream.of(OWL.cardinality, OWL.maxCardinality, OWL.minCardinality) .flatMap(p -> m.statements(null, p, null)) .filter(AxiomReferencesTmp::isObjectRestriction); } private static boolean isObjectRestriction(OntStatement s) { return OBJECT_CARDINALITY_TYPES.stream().anyMatch(t -> s.getSubject().canAs(t)); } private static final Set>> OBJECT_CARDINALITY_TYPES = Stream.of(OntClass.ObjectMaxCardinality.class, OntClass.ObjectMinCardinality.class, OntClass.ObjectCardinality.class).collect(Collectors.toSet()); private static Stream> translators(OntStatement s) { return CLASS_TRANSLATORS.stream().filter(x -> x.testStatement(s)); } private static final Set> CLASS_TRANSLATORS = OWLContentType.all() .filter(x -> x.hasComponent(OWLComponentType.CLASS)) .map(OWLContentType::getAxiomType) .map(AxiomParserProvider::get).collect(Collectors.toSet()); private static Stream roots(OntStatement s) { return roots(s.getModel(), s, new HashSet<>()); } private static Stream roots(OntModel m, OntStatement st, Set seen) { Resource s = st.getSubject(); if (s.isURIResource()) { return Stream.of(st); } Set set = m.statements(null, null, s).filter(seen::add).collect(Collectors.toSet()); if (set.isEmpty()) return Stream.of(st); return set.stream().flatMap(x -> roots(m, x, seen)); } }