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

applyRule(ClassifierStatus,int,int,int)   B

Complexity

Conditions 6

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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