|
1
|
|
|
/* |
|
2
|
|
|
* |
|
3
|
|
|
* Copyright (C) 2009-2017 Julian Mendez |
|
4
|
|
|
* |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of jcel. |
|
7
|
|
|
* |
|
8
|
|
|
* |
|
9
|
|
|
* The contents of this file are subject to the GNU Lesser General Public License |
|
10
|
|
|
* version 3 |
|
11
|
|
|
* |
|
12
|
|
|
* |
|
13
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
14
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
15
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
16
|
|
|
* (at your option) any later version. |
|
17
|
|
|
* |
|
18
|
|
|
* This program is distributed in the hope that it will be useful, |
|
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21
|
|
|
* GNU Lesser General Public License for more details. |
|
22
|
|
|
* |
|
23
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25
|
|
|
* |
|
26
|
|
|
* |
|
27
|
|
|
* Alternatively, the contents of this file may be used under the terms |
|
28
|
|
|
* of the Apache License, Version 2.0, in which case the |
|
29
|
|
|
* provisions of the Apache License, Version 2.0 are applicable instead of those |
|
30
|
|
|
* above. |
|
31
|
|
|
* |
|
32
|
|
|
* |
|
33
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
34
|
|
|
* you may not use this file except in compliance with the License. |
|
35
|
|
|
* You may obtain a copy of the License at |
|
36
|
|
|
* |
|
37
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
38
|
|
|
* |
|
39
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
40
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
41
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
42
|
|
|
* See the License for the specific language governing permissions and |
|
43
|
|
|
* limitations under the License. |
|
44
|
|
|
* |
|
45
|
|
|
*/ |
|
46
|
|
|
|
|
47
|
|
|
package de.tudresden.inf.lat.jcel.ontology.normalization; |
|
48
|
|
|
|
|
49
|
|
|
import java.util.ArrayList; |
|
50
|
|
|
import java.util.Collection; |
|
51
|
|
|
import java.util.HashSet; |
|
52
|
|
|
import java.util.Iterator; |
|
53
|
|
|
import java.util.Objects; |
|
54
|
|
|
import java.util.Set; |
|
55
|
|
|
|
|
56
|
|
|
import de.tudresden.inf.lat.jcel.coreontology.axiom.NormalizedIntegerAxiom; |
|
57
|
|
|
import de.tudresden.inf.lat.jcel.coreontology.axiom.NormalizedIntegerAxiomFactory; |
|
58
|
|
|
import de.tudresden.inf.lat.jcel.coreontology.datatype.IntegerAxiom; |
|
59
|
|
|
import de.tudresden.inf.lat.jcel.coreontology.datatype.IntegerEntityManager; |
|
60
|
|
|
import de.tudresden.inf.lat.jcel.ontology.axiom.complex.IntegerSubClassOfAxiom; |
|
61
|
|
|
import de.tudresden.inf.lat.jcel.ontology.axiom.extension.IntegerOntologyObjectFactory; |
|
62
|
|
|
import de.tudresden.inf.lat.jcel.ontology.datatype.IntegerClass; |
|
63
|
|
|
import de.tudresden.inf.lat.jcel.ontology.datatype.IntegerClassExpression; |
|
64
|
|
|
import de.tudresden.inf.lat.jcel.ontology.datatype.IntegerObjectIntersectionOf; |
|
65
|
|
|
import de.tudresden.inf.lat.jcel.ontology.datatype.IntegerObjectPropertyExpression; |
|
66
|
|
|
import de.tudresden.inf.lat.jcel.ontology.datatype.IntegerObjectSomeValuesFrom; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* |
|
70
|
|
|
* @author Julian Mendez |
|
71
|
|
|
*/ |
|
72
|
|
|
public class NormalizerSubClassOf implements NormalizationRule { |
|
73
|
|
|
|
|
74
|
|
|
private final IntegerOntologyObjectFactory ontologyObjectFactory; |
|
75
|
|
|
|
|
76
|
|
|
public NormalizerSubClassOf(IntegerOntologyObjectFactory factory) { |
|
77
|
|
|
this.ontologyObjectFactory = factory; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
@Override |
|
81
|
|
|
public Set<IntegerAxiom> apply(IntegerAxiom axiom) { |
|
82
|
|
|
Objects.requireNonNull(axiom); |
|
83
|
|
|
Set<IntegerAxiom> ret = new HashSet<>(); |
|
84
|
|
|
if (axiom instanceof IntegerSubClassOfAxiom) { |
|
85
|
|
|
Collection<NormalizedIntegerAxiom> normalizedAxioms = simplify((IntegerSubClassOfAxiom) axiom); |
|
86
|
|
|
normalizedAxioms.forEach(normalizedAxiom -> { |
|
87
|
|
|
ret.add(normalizedAxiom); |
|
88
|
|
|
}); |
|
89
|
|
|
} |
|
90
|
|
|
return ret; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private IntegerEntityManager getIdGenerator() { |
|
94
|
|
|
return getOntologyObjectFactory().getEntityManager(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private NormalizedIntegerAxiomFactory getNormalizedAxiomFactory() { |
|
98
|
|
|
return getOntologyObjectFactory().getNormalizedAxiomFactory(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private Integer getObjectPropertyId(IntegerObjectPropertyExpression propExpr) { |
|
102
|
|
|
return propExpr.accept(new ObjectPropertyIdFinder(getIdGenerator())); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public IntegerOntologyObjectFactory getOntologyObjectFactory() { |
|
106
|
|
|
return this.ontologyObjectFactory; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private Collection<NormalizedIntegerAxiom> simplify(IntegerSubClassOfAxiom axiom) { |
|
110
|
|
|
Collection<NormalizedIntegerAxiom> ret = new ArrayList<>(); |
|
111
|
|
|
IntegerClassExpression subClass = axiom.getSubClass(); |
|
112
|
|
|
IntegerClassExpression superClass = axiom.getSuperClass(); |
|
113
|
|
|
|
|
114
|
|
|
if (subClass.isLiteral() && superClass.isLiteral()) { |
|
115
|
|
|
ret.add(getNormalizedAxiomFactory().createGCI0Axiom(((IntegerClass) subClass).getId(), |
|
116
|
|
|
((IntegerClass) superClass).getId(), axiom.getAnnotations())); |
|
117
|
|
|
|
|
118
|
|
|
} else if (!subClass.isLiteral() && superClass.isLiteral() && (subClass instanceof IntegerObjectIntersectionOf) |
|
119
|
|
|
&& subClass.hasOnlyClasses()) { |
|
120
|
|
|
|
|
121
|
|
|
IntegerObjectIntersectionOf intersection = (IntegerObjectIntersectionOf) subClass; |
|
122
|
|
|
Set<IntegerClassExpression> operands = intersection.getOperands(); |
|
123
|
|
|
if (operands.size() == 0) { |
|
124
|
|
|
ret.add(getNormalizedAxiomFactory().createGCI0Axiom(IntegerEntityManager.topClassId, |
|
125
|
|
|
((IntegerClass) superClass).getId(), axiom.getAnnotations())); |
|
126
|
|
|
|
|
127
|
|
|
} else if (operands.size() == 1) { |
|
128
|
|
|
ret.add(getNormalizedAxiomFactory().createGCI0Axiom(((IntegerClass) operands.iterator().next()).getId(), |
|
129
|
|
|
((IntegerClass) superClass).getId(), axiom.getAnnotations())); |
|
130
|
|
|
|
|
131
|
|
|
} else if (operands.size() == 2) { |
|
132
|
|
|
Iterator<IntegerClassExpression> it = operands.iterator(); |
|
133
|
|
|
int leftSubClassId = ((IntegerClass) it.next()).getId(); |
|
134
|
|
|
int rightSubClassId = ((IntegerClass) it.next()).getId(); |
|
135
|
|
|
int superClassId = ((IntegerClass) superClass).getId(); |
|
136
|
|
|
ret.add(getNormalizedAxiomFactory().createGCI1Axiom(leftSubClassId, rightSubClassId, superClassId, |
|
137
|
|
|
axiom.getAnnotations())); |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} else if (subClass.isLiteral() && !superClass.isLiteral() |
|
142
|
|
|
&& (superClass instanceof IntegerObjectSomeValuesFrom) && superClass.hasOnlyClasses()) { |
|
143
|
|
|
|
|
144
|
|
|
IntegerObjectSomeValuesFrom restriction = (IntegerObjectSomeValuesFrom) superClass; |
|
145
|
|
|
IntegerClass filler = (IntegerClass) restriction.getFiller(); |
|
146
|
|
|
Integer property = getObjectPropertyId(restriction.getProperty()); |
|
147
|
|
|
ret.add(getNormalizedAxiomFactory().createGCI2Axiom(((IntegerClass) subClass).getId(), property, |
|
148
|
|
|
filler.getId(), axiom.getAnnotations())); |
|
149
|
|
|
|
|
150
|
|
|
} else if (!subClass.isLiteral() && superClass.isLiteral() && (subClass instanceof IntegerObjectSomeValuesFrom) |
|
151
|
|
|
&& subClass.hasOnlyClasses()) { |
|
152
|
|
|
|
|
153
|
|
|
IntegerObjectSomeValuesFrom restriction = (IntegerObjectSomeValuesFrom) subClass; |
|
154
|
|
|
IntegerClass filler = (IntegerClass) restriction.getFiller(); |
|
155
|
|
|
Integer property = getObjectPropertyId(restriction.getProperty()); |
|
156
|
|
|
ret.add(getNormalizedAxiomFactory().createGCI3Axiom(property, filler.getId(), |
|
157
|
|
|
((IntegerClass) superClass).getId(), axiom.getAnnotations())); |
|
158
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
return ret; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|