Completed
Branch master (2d821a)
by Julian
49:45
created

simplify(IntegerSubClassOfAxiom)   F

Complexity

Conditions 18

Size

Total Lines 52
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 18
c 1
b 0
f 0
dl 0
loc 52
rs 1.2
eloc 39

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like de.tudresden.inf.lat.jcel.ontology.normalization.NormalizerSubClassOf.simplify(IntegerSubClassOfAxiom) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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