Passed
Push — master ( e0b928...937cde )
by Julian
03:04 queued 12s
created

applyRule(ClassifierStatus,int,int,int)   B

Complexity

Conditions 7

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 28
c 1
b 0
f 0
dl 0
loc 44
rs 7.808
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.core.completion.ext;
48
49
import java.util.HashSet;
50
import java.util.Objects;
51
import java.util.Optional;
52
import java.util.Set;
53
54
import de.tudresden.inf.lat.jcel.core.completion.common.ClassifierStatus;
55
import de.tudresden.inf.lat.jcel.core.completion.common.CompletionRuleMonitor;
56
import de.tudresden.inf.lat.jcel.core.completion.common.RObserverRule;
57
import de.tudresden.inf.lat.jcel.core.graph.VNode;
58
import de.tudresden.inf.lat.jcel.core.graph.VNodeImpl;
59
import de.tudresden.inf.lat.jcel.coreontology.datatype.IntegerEntityManager;
60
61
/**
62
 * 
63
 * <ul>
64
 * <li>CR-9 (optimized) : <b>if</b> <u>(r<sub>1</sub>, x, y<sub>1</sub>) &isin;
65
 * R</u>, (r<sub>2</sub>, x, y<sub>2</sub>) &isin; R, &hellip;, (r<sub>n</sub>,
66
 * x, y<sub>n</sub>) &isin; R, <br>
67
 * r<sub>1</sub> \u2291<sub><i>T</i></sub> s, r<sub>2</sub> \u2291
68
 * <sub><i>T</i></sub> s, &hellip;, r<sub>n</sub> \u2291<sub><i>T</i></sub> s, y
69
 * <sub>i</sub> = (\u22A4 , &psi;<sub>i</sub>) for 1 &le; i &le; n, y
70
 * <sub>i</sub> &ne; y<sub>j</sub> for 1 &le; i &lt; j &le; n, f(s) <br>
71
 * <b>then</b> v := (\u22A4 , &psi;<sub>1</sub> &cup; &hellip; &cup; &psi;
72
 * <sub>n</sub>) <br>
73
 * &nbsp;&nbsp;&nbsp;&nbsp; <b>if</b> v &notin; V <b>then</b> V := V &cup; {v}
74
 * <br>
75
 * &nbsp;&nbsp;&nbsp;&nbsp; S := S &cup; {(v, k) | (y<sub>1</sub>, k) &isin; S}
76
 * &cup; &hellip; &cup; {(v, k) | (y<sub>n</sub>, k) &isin; S} <br>
77
 * &nbsp;&nbsp;&nbsp;&nbsp; R := R &cup; {(r<sub>1</sub>, x, v)}</li>
78
 * </ul>
79
 * <br>
80
 * 
81
 * @author Julian Mendez
82
 */
83
public class CR9RExtOptRule implements RObserverRule {
84
85
	/**
86
	 * Constructs a new completion rule CR-9.
87
	 */
88
	public CR9RExtOptRule() {
89
	}
90
91
	@Override
92
	public boolean apply(ClassifierStatus status, int property, int leftClass, int rightClass) {
93
		Objects.requireNonNull(status);
94
		return applyRule(status, property, leftClass, rightClass);
95
	}
96
97
	private boolean applyRule(ClassifierStatus status, int r1, int x, int y) {
98
		CompletionRuleMonitor ret = new CompletionRuleMonitor();
99
		Optional<VNode> optPsiNode = status.getNode(y);
100
		if (!optPsiNode.isPresent()) {
101
			throw new IllegalStateException("Node not found in internal structure '" + y + "'.");
102
		}
103
		if (optPsiNode.get().getClassId() == IntegerEntityManager.topClassId) {
104
105
			Set<Integer> valid = new HashSet<>();
106
			valid.add(y);
107
			status.getObjectPropertiesWithFunctionalAncestor(r1).forEach(r2 -> {
108
				status.getSecondByFirst(r2, x).forEach(yi -> {
109
					Optional<VNode> optPhiNode = status.getNode(yi);
110
					if (!optPhiNode.isPresent()) {
111
						throw new IllegalStateException("Node not found in internal structure '" + yi + "'.");
112
					}
113
					if (optPhiNode.get().getClassId() == IntegerEntityManager.topClassId) {
114
						valid.add(yi);
115
					}
116
				});
117
			});
118
119
			if (valid.size() > 1) {
120
				VNodeImpl newNode = new VNodeImpl(IntegerEntityManager.topClassId);
121
				valid.forEach(yi -> {
122
					Optional<VNode> optPhiNode = status.getNode(yi);
123
					if (!optPsiNode.isPresent()) {
124
						throw new IllegalStateException("Node not found in internal structure '" + yi + "'.");
125
					}
126
					newNode.addExistentialsOf(optPhiNode.get());
127
				});
128
				int v = status.createOrGetNodeId(newNode);
129
				valid.forEach(yi -> {
130
131
					status.getSubsumers(yi).forEach(p -> {
132
						ret.or(status.addNewSEntry(v, p));
133
					});
134
135
					ret.or(status.addNewREntry(r1, x, v));
136
				});
137
			}
138
139
		}
140
		return ret.get();
141
	}
142
143
	@Override
144
	public boolean equals(Object o) {
145
		return (Objects.nonNull(o)) && getClass().equals(o.getClass());
146
	}
147
148
	@Override
149
	public int hashCode() {
150
		return getClass().hashCode();
151
	}
152
153
	@Override
154
	public String toString() {
155
		return getClass().getSimpleName();
156
	}
157
158
}
159