Debian Codegraph @

FaCT++.Python/pyfactxx/tests/test_query_instances.py

raw symbols SCIP
1 #
2 # pyfactxx - Python interface to FaCT++ reasoner
3 #
4 # Copyright (C) 2016-2018 by Artur Wroblewski <[email protected]>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19
20
21 def test_instances_query(reasoner):
22 """Test getting instances"""
23 cls = reasoner.concept('CLS')
24 for v in 'abc':
25 i = reasoner.individual(v)
26 reasoner.instance_of(i, cls)
27
28 reasoner.realise()
29
30 names = [i.name for i in reasoner.get_instances(cls)]
31 assert ['a', 'b', 'c'] == names
32
33
34 def test_instances_query_from_object_role_domain(reasoner):
35 """Test getting instances of a class, which is a domain of an object role.
36 """
37 cls = reasoner.concept('CLS')
38 r = reasoner.object_role('R')
39 reasoner.set_o_domain(r, cls)
40
41 for v in 'abc':
42 i = reasoner.individual(v)
43 reasoner.instance_of(i, cls)
44
45 reasoner.realise()
46
47 # get role domain
48 value = next(reasoner.get_o_domain(r))
49
50 # get instances of the domain
51 names = [i.name for i in reasoner.get_instances(value)]
52 assert ['a', 'b', 'c'] == names
53
54
55 def test_instances_query_of_superclass(reasoner):
56 """Test getting instances of a superclass.
57 """
58 person = reasoner.concept('Person')
59 child = reasoner.concept('Child')
60 reasoner.implies_concepts(child, person)
61
62 a = reasoner.individual('a')
63 reasoner.instance_of(a, child)
64
65 reasoner.realise()
66 items = reasoner.get_instances(person)
67
68 assert ['a'] == [i.name for i in items]
69
70
71 def test_instances_query_of_class_hierarchy(reasoner):
72 """Test getting instances of a class hierarchy.
73 """
74 person = reasoner.concept('Person')
75 child = reasoner.concept('Child')
76 reasoner.implies_concepts(child, person)
77
78 p1 = reasoner.individual('p1')
79 p2 = reasoner.individual('p2')
80 reasoner.instance_of(p1, person)
81 reasoner.instance_of(p2, person)
82
83 c1 = reasoner.individual('c1')
84 c2 = reasoner.individual('c2')
85 reasoner.instance_of(c1, child)
86 reasoner.instance_of(c2, child)
87
88 reasoner.realise()
89 items = reasoner.get_instances(person)
90
91 assert ['c1', 'c2', 'p1', 'p2'] == [i.name for i in items]
92
93
94 def test_instances_query_of_class_hierarchy_last_empty(reasoner):
95 """Test getting instances of a class hierarchy with last class having no
96 instances.
97 """
98 person = reasoner.concept('Person')
99 child = reasoner.concept('Child')
100 baby = reasoner.concept('Baby')
101 reasoner.implies_concepts(child, person)
102 reasoner.implies_concepts(baby, child)
103
104 p1 = reasoner.individual('p1')
105 p2 = reasoner.individual('p2')
106 reasoner.instance_of(p1, person)
107 reasoner.instance_of(p2, person)
108
109 c1 = reasoner.individual('c1')
110 c2 = reasoner.individual('c2')
111 reasoner.instance_of(c1, child)
112 reasoner.instance_of(c2, child)
113
114 reasoner.realise()
115 items = reasoner.get_instances(person)
116
117 assert ['c1', 'c2', 'p1', 'p2'] == [i.name for i in items]
118
119 # vim: sw=4:et:ai