ru.avicomp.ontapi.jena.model.OntCE Maven / Gradle / Ivy
Show all versions of ontapi Show documentation
/*
* This file is part of the ONT API.
* The contents of this file are subject to the LGPL License, Version 3.0.
* Copyright (c) 2018, Avicomp Services, AO
*
* 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 ru.avicomp.ontapi.jena.model;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.RDFList;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.vocabulary.RDFS;
import ru.avicomp.ontapi.jena.OntJenaException;
import ru.avicomp.ontapi.jena.vocabulary.OWL;
import ru.avicomp.ontapi.jena.vocabulary.RDF;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* A common interface for any Class Expressions (both named and anonymous).
* Examples of rdf-patterns see here.
*
* Created by szuev on 01.11.2016.
* @see OntClass
* @see 2.1 Class Expressions
* @see 8 Class Expressions
*/
public interface OntCE extends OntObject {
/**
* Creates an anonymous individual which is of this class type.
*
* @return {@link OntIndividual.Anonymous}
* @see OntIndividual#attachClass(OntCE)
*/
OntIndividual.Anonymous createIndividual();
/**
* Creates a named individual which is of this class type.
*
* @param uri, String not null
* @return {@link OntIndividual.Named}
* @see OntIndividual#attachClass(OntCE)
*/
OntIndividual.Named createIndividual(String uri);
/**
* Lists all individuals,
* i.e. subjects from class-assertion statements {@code a rdf:type C}.
*
* @return Stream of {@link OntIndividual}s
*/
default Stream individuals() {
return getModel().statements(null, RDF.type, this)
.map(OntStatement::getSubject).map(s -> s.as(OntIndividual.class));
}
/**
* Lists all properties attached to the class in a {@code rdfs:domain} statement.
* The property is considered as attached if
* it and the class expression are both included in property domain axiom description:
*
* - {@code R rdfs:domain C} - {@code R} is a data property {@code C} - this class expression
* - {@code P rdfs:domain C} - {@code P} is an object property expression, {@code C} - this class expression
* - {@code A rdfs:domain U} - {@code A} is annotation property, {@code U} is IRI, this class expression
*
*
* @return Stream of {@link OntPE}s
* @see OntPE#domain()
*/
default Stream properties() {
return getModel().statements(null, RDFS.domain, this)
.map(OntStatement::getSubject)
.filter(s -> s.canAs(OntPE.class))
.map(s -> s.as(OntPE.class));
}
/**
* Returns all super classes.
*
* @return Stream of {@link OntCE}s.
*/
default Stream subClassOf() {
return objects(RDFS.subClassOf, OntCE.class);
}
/**
* Adds a super class.
*
* @param superClass {@link OntCE}
* @return {@link OntStatement}
*/
default OntStatement addSubClassOf(OntCE superClass) {
return addStatement(RDFS.subClassOf, superClass);
}
/**
* Removes a super class.
*
* @param superClass {@link OntCE}
*/
default void removeSubClassOf(OntCE superClass) {
remove(RDFS.subClassOf, superClass);
}
/**
* Returns all disjoint classes.
* The statement patter to search for is {@code C1 owl:disjointWith C2}.
*
* @return Stream of {@link OntCE}s
* @see OntDisjoint.Classes
*/
default Stream disjointWith() {
return objects(OWL.disjointWith, OntCE.class);
}
/**
* Adds a disjoint class.
*
* @param other {@link OntCE}
* @return {@link OntStatement}
* @see OntDisjoint.Classes
*/
default OntStatement addDisjointWith(OntCE other) {
return addStatement(OWL.disjointWith, other);
}
/**
* Removes a disjoint class.
*
* @param other {@link OntCE}
* @see OntDisjoint.Classes
*/
default void removeDisjointWith(OntCE other) {
remove(OWL.disjointWith, other);
}
/**
* Lists all equivalent classes.
*
* @return Stream of {@link OntCE}s
* @see OntDT#equivalentClass()
*/
default Stream equivalentClass() {
return objects(OWL.equivalentClass, OntCE.class);
}
/**
* Adds new equivalent class.
*
* @param other {@link OntCE}
* @return {@link OntStatement}
* @see OntDT#addEquivalentClass(OntDR)
*/
default OntStatement addEquivalentClass(OntCE other) {
return addStatement(OWL.equivalentClass, other);
}
/**
* Removes an equivalent class.
*
* @param other {@link OntCE}
* @see OntDT#removeEquivalentClass(OntDR)
*/
default void removeEquivalentClass(OntCE other) {
remove(OWL.equivalentClass, other);
}
/**
* Creates a HasKey logical construction as {@link OntList ontology list} of {@link OntDOP Object or Data Property Expression}s
* that is attached to this Class Expression using the predicate {@link OWL#hasKey owl:hasKey}.
* The resulting rdf-list will consist of all the elements of the specified collection in the same order but with exclusion of duplicates.
* Note: {@code null}s in collection will cause {@link NullPointerException NullPointerException}.
* For additional information about HasKey logical construction see
* 9.5 Keys specification.
*
* @param objectProperties {@link Collection} (preferably {@link Set})of {@link OntOPE object property expression}s
* @param dataProperties {@link Collection} (preferably {@link Set})of {@link OntNDP data property expression}s
* @return {@link OntList} of {@link OntDOP}s
* @since 1.3.0
*/
OntList createHasKey(Collection objectProperties, Collection dataProperties);
/**
* Creates a HasKey logical construction as {@link OntList ontology list} and returns statement {@code C owl:hasKey (P1 ... Pm R1 ... Rn)}
* to allow the addition of annotations.
* About RDF Graph annotation specification see, for example,
* 2.3.1 Axioms that Generate a Main Triple.
*
* @param properties Array of {@link OntDOP}s without {@code null}s
* @return {@link OntStatement}
* @since 1.3.0
*/
OntStatement addHasKey(OntDOP... properties);
/**
* Finds a HasKey logical construction attached to this class expression by the specified rdf-node in the form of {@link OntList}.
*
* @param list {@link RDFNode}
* @return Optional around {@link OntList} of {@link OntDOP data and object property expression}s
* @since 1.3.0
*/
Optional> findHasKey(RDFNode list);
/**
* Lists all HasKey {@link OntList ontology list}s that are attached to this class expression
* on predicate {@link OWL#hasKey owl:hasKey}.
*
* @return Stream of {@link OntList}s with parameter-type {@code OntDOP}
* @since 1.3.0
*/
Stream> listHasKeys();
/**
* Deletes the given HasKey list including its annotations
* with predicate {@link OWL#hasKey owl:hasKey} for this resource from its associated model.
*
* @param list {@link RDFNode} can be {@link OntList} or {@link RDFList}
* @throws OntJenaException if the list is not found
* @since 1.3.0
*/
void removeHasKey(RDFNode list);
/**
* Lists all key properties.
* I.e. returns all object- and datatype- properties which belong to
* the {@code C owl:hasKey (P1 ... Pm R1 ... Rn)} statements,
* where {@code C} - this class expression, {@code P} is a property expression, and {@code R} is a data(-type) property.
* If there are several []-lists in the model that satisfy these conditions,
* all their content will be merged into the one distinct stream.
*
* @return distinct Stream of {@link OntOPE}s and {@link OntNDP}s
* @see #listHasKeys()
* @deprecated use {@code listHasKeys()} with filtering instead
*/
@Deprecated
default Stream hasKey() {
return listHasKeys().flatMap(OntList::members);
}
/**
* Creates an {@code owl:hasKey} statement.
*
* @param objectProperties the collection of {@link OntOPE}s
* @param dataProperties the collection of {@link OntNDP}s
* @return {@link OntStatement}
* @deprecated redundant method: use {@code createHasKey(objectProperties, dataProperties)} instead
*/
@Deprecated
default OntStatement addHasKey(Collection objectProperties, Collection dataProperties) {
return createHasKey(objectProperties, dataProperties).getRoot();
}
/**
* Deletes all HasKey list including its annotations
* with predicate {@link OWL#hasKey owl:hasKey} for this resource from its associated model.
*
* @throws OntJenaException if the list is not found
* @since 1.3.0
*/
default void clearHasKeys() {
listHasKeys().collect(Collectors.toSet()).forEach(this::removeHasKey);
}
/**
* Removes all key properties.
* I.e. removes all statements with their content from a {@code owl:hasKey} axiom.
*
* @see #clearHasKeys()
* @deprecated this method does not take into account possible annotations of HasKey statement, use instead {@code clearHasKeys()}
*/
@Deprecated
void removeHasKey();
/*
* ============================
* All known Class Expressions:
* ============================
*/
interface ObjectSomeValuesFrom extends ComponentRestrictionCE {
}
interface DataSomeValuesFrom extends ComponentRestrictionCE {
}
interface ObjectAllValuesFrom extends ComponentRestrictionCE {
}
interface DataAllValuesFrom extends ComponentRestrictionCE {
}
interface ObjectHasValue extends ComponentRestrictionCE {
}
interface DataHasValue extends ComponentRestrictionCE {
}
interface ObjectMinCardinality extends CardinalityRestrictionCE {
}
interface DataMinCardinality extends CardinalityRestrictionCE {
}
interface ObjectMaxCardinality extends CardinalityRestrictionCE {
}
interface DataMaxCardinality extends CardinalityRestrictionCE {
}
interface ObjectCardinality extends CardinalityRestrictionCE {
}
interface DataCardinality extends CardinalityRestrictionCE {
}
interface HasSelf extends RestrictionCE, ONProperty {
}
interface UnionOf extends ComponentsCE {
}
interface OneOf extends ComponentsCE {
}
interface IntersectionOf extends ComponentsCE {
}
interface ComplementOf extends OntCE, Value {
}
interface NaryDataAllValuesFrom extends NaryRestrictionCE {
}
interface NaryDataSomeValuesFrom extends NaryRestrictionCE {
}
/*
* ===========================
* Abstract class expressions:
* ===========================
*/
interface ComponentsCE extends OntCE, Components {
}
interface CardinalityRestrictionCE extends Cardinality, ComponentRestrictionCE {
}
interface ComponentRestrictionCE extends RestrictionCE, ONProperty, Value {
}
interface NaryRestrictionCE extends RestrictionCE, ONProperties, Value {
}
interface RestrictionCE extends OntCE {
}
/*
* ============================
* Common technical interfaces:
* ============================
*/
interface ONProperty {
P getOnProperty();
void setOnProperty(P p);
}
interface ONProperties
{
/**
* Gets the ONT-List that contains resources of type {@link P}.
*
* @return {@link OntList}
* @since 1.3.0
*/
OntList
getList();
default Stream
onProperties() {
return getList().members();
}
default void setOnProperties(Collection
properties) {
getList().clear().addAll(properties);
}
}
interface Components {
/**
* Gets the ONT-List that contains resources of type {@link O}.
*
* @return {@link OntList}
* @since 1.3.0
*/
OntList getList();
default Stream components() {
return getList().members();
}
default void setComponents(Collection components) {
getList().clear().addAll(components);
}
}
interface Value {
O getValue();
void setValue(O value);
}
interface Cardinality {
int getCardinality();
void setCardinality(int cardinality);
/**
* Determines if this restriction is qualified.
* Qualified cardinality restrictions are defined to be cardinality restrictions
* that have fillers which aren't TOP (owl:Thing or rdfs:Literal).
* An object restriction is unqualified if it has a filler that is owl:Thing.
* A data restriction is unqualified if it has a filler which is the top data type (rdfs:Literal).
*
* @return {@code true} if this restriction is qualified, or {@code false} if this restriction is unqualified.
*/
boolean isQualified();
}
}