| 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 |
# distutils: language = c++ |
| 21 |
# cython: c_string_type=unicode, c_string_encoding=utf8, embedsignature=True, language_level=3 |
| 22 |
|
| 23 |
from libcpp cimport bool |
| 24 |
from libcpp.string cimport string |
| 25 |
from cython.operator cimport dereference, postincrement |
| 26 |
from libcpp.set cimport set |
| 27 |
|
| 28 |
cdef extern from "<vector>" namespace "std": |
| 29 |
cdef cppclass vector[T]: |
| 30 |
cppclass iterator: |
| 31 |
T operator*() |
| 32 |
iterator operator++() |
| 33 |
bint operator==(iterator) |
| 34 |
bint operator!=(iterator) |
| 35 |
|
| 36 |
T& operator[](int) |
| 37 |
int size() |
| 38 |
iterator begin() |
| 39 |
iterator end() |
| 40 |
|
| 41 |
cdef extern from "<iostream>" namespace "std": |
| 42 |
cdef cppclass ostream: |
| 43 |
ostream& write(const char*, int) except + |
| 44 |
|
| 45 |
cdef cppclass istream: |
| 46 |
istream& read(const char*, int) except + |
| 47 |
|
| 48 |
cdef extern from "<iostream>" namespace "std::ios_base": |
| 49 |
cdef cppclass open_mode: |
| 50 |
pass |
| 51 |
cdef open_mode binary |
| 52 |
|
| 53 |
cdef extern from "<fstream>" namespace "std": |
| 54 |
cdef cppclass ofstream(ostream): |
| 55 |
ofstream(const char*) except + |
| 56 |
ofstream(const char*, open_mode) except + |
| 57 |
|
| 58 |
cdef cppclass ifstream(istream): |
| 59 |
ifstream(const char*) except + |
| 60 |
ifstream(const char*, open_mode) except + |
| 61 |
|
| 62 |
cdef extern from 'tNamedEntry.h': |
| 63 |
cdef cppclass TNamedEntry: |
| 64 |
TNamedEntry(string) except + |
| 65 |
string getName() |
| 66 |
|
| 67 |
cdef extern from 'taxNamEntry.h': |
| 68 |
cdef cppclass ClassifiableEntry(TNamedEntry): |
| 69 |
ClassifiableEntry() except + |
| 70 |
bool isSystem() |
| 71 |
bool isTop() |
| 72 |
bool isBottom() |
| 73 |
TNamedEntity *getEntity() |
| 74 |
|
| 75 |
cdef bool skip(const ClassifiableEntry *obj): |
| 76 |
return obj.isSystem() or obj.isBottom() |
| 77 |
|
| 78 |
cdef extern from 'tDLAxiom.h': |
| 79 |
cdef cppclass TDLAxiom: |
| 80 |
TDLAxiom() except + |
| 81 |
|
| 82 |
cdef extern from 'tConcept.h': |
| 83 |
cdef cppclass TConcept(ClassifiableEntry): |
| 84 |
TConcept(string) except + |
| 85 |
bool isSingleton() |
| 86 |
|
| 87 |
cdef extern from 'tIndividual.h': |
| 88 |
cdef cppclass TRelatedMap: |
| 89 |
TRelatedMap() except + |
| 90 |
|
| 91 |
cdef cppclass TIndividual(TConcept): |
| 92 |
TIndividual(string) except + |
| 93 |
|
| 94 |
cdef extern from 'tRole.h': |
| 95 |
cdef cppclass TRole: |
| 96 |
TRole(string) except + |
| 97 |
|
| 98 |
cdef extern from 'tIndividual.h' namespace 'TRelatedMap': |
| 99 |
ctypedef vector[TIndividual*] CIVec |
| 100 |
|
| 101 |
cdef extern from 'tDLExpression.h': |
| 102 |
cdef cppclass TNamedEntity: |
| 103 |
string getName() |
| 104 |
|
| 105 |
cdef cppclass TDLObjectRoleName: |
| 106 |
TDLObjectRoleName(string) except + |
| 107 |
string getName() |
| 108 |
|
| 109 |
cdef cppclass TDLExpression: |
| 110 |
pass |
| 111 |
|
| 112 |
cdef cppclass TDLRoleExpression(TDLExpression): |
| 113 |
pass |
| 114 |
|
| 115 |
cdef cppclass TDLObjectRoleComplexExpression(TDLRoleExpression): |
| 116 |
pass |
| 117 |
|
| 118 |
cdef cppclass TDLObjectRoleExpression(TDLObjectRoleComplexExpression): |
| 119 |
pass |
| 120 |
|
| 121 |
cdef cppclass TDLObjectRoleName(TDLObjectRoleExpression): |
| 122 |
pass |
| 123 |
|
| 124 |
cdef cppclass TDLDataExpression: |
| 125 |
pass |
| 126 |
|
| 127 |
cdef cppclass TDLDataValue(TDLDataExpression): |
| 128 |
pass |
| 129 |
|
| 130 |
cdef cppclass TDLDataTypeExpression(TDLDataExpression): |
| 131 |
pass |
| 132 |
|
| 133 |
cdef cppclass TDLDataTypeName(TDLDataTypeExpression): |
| 134 |
pass |
| 135 |
|
| 136 |
cdef cppclass TDLConceptExpression(TDLExpression): |
| 137 |
pass |
| 138 |
|
| 139 |
cdef cppclass TDLConceptName(TDLConceptExpression): |
| 140 |
string getName() |
| 141 |
|
| 142 |
cdef cppclass TDLIndividualExpression(TDLExpression): |
| 143 |
pass |
| 144 |
|
| 145 |
cdef cppclass TDLIndividualName(TDLIndividualExpression): |
| 146 |
TDLIndividualName(string) except + |
| 147 |
string getName() |
| 148 |
|
| 149 |
cdef cppclass TDLDataRoleExpression(TDLExpression): |
| 150 |
pass |
| 151 |
|
| 152 |
cdef cppclass TDLDataRoleName(TDLDataRoleExpression): |
| 153 |
string getName() |
| 154 |
|
| 155 |
cdef extern from 'tExpressionManager.h': |
| 156 |
cdef cppclass TExpressionManager: |
| 157 |
TDLConceptName* Concept(string) |
| 158 |
TDLIndividualExpression* Individual(string) |
| 159 |
TDLDataRoleName* DataRole(string) |
| 160 |
TDLObjectRoleName* ObjectRole(string) |
| 161 |
TDLDataExpression* DataTop() |
| 162 |
TDLDataExpression* DataBottom() |
| 163 |
TDLDataTypeName* DataType(string) |
| 164 |
TDLObjectRoleExpression* ObjectRoleTop() |
| 165 |
TDLDataRoleExpression* DataRoleTop() |
| 166 |
|
| 167 |
TDLConceptExpression* Top() |
| 168 |
TDLConceptExpression* Bottom() |
| 169 |
TDLConceptExpression* Value(const TDLObjectRoleExpression*, const TDLIndividualExpression*) |
| 170 |
TDLConceptExpression* Value(const TDLDataRoleExpression*, const TDLDataValue*) |
| 171 |
TDLConceptExpression* Exists(const TDLObjectRoleExpression*, const TDLConceptExpression*) |
| 172 |
TDLConceptExpression* Exists(const TDLDataRoleExpression*, const TDLDataExpression*) |
| 173 |
TDLConceptExpression* Forall(const TDLObjectRoleExpression*, const TDLConceptExpression*) |
| 174 |
TDLConceptExpression* Forall(const TDLDataRoleExpression*, const TDLDataExpression*) |
| 175 |
TDLObjectRoleExpression* Inverse(const TDLObjectRoleExpression*) |
| 176 |
TDLObjectRoleComplexExpression* Compose() |
| 177 |
|
| 178 |
TDLConceptExpression* And() |
| 179 |
TDLConceptExpression* Or() |
| 180 |
TDLConceptExpression* OneOf() |
| 181 |
TDLConceptExpression* Not(const TDLConceptExpression*) |
| 182 |
|
| 183 |
TDLConceptExpression* Cardinality(unsigned int, const TDLObjectRoleExpression*, const TDLConceptExpression*) |
| 184 |
TDLConceptExpression* Cardinality(unsigned int, const TDLDataRoleExpression*, const TDLDataExpression*) |
| 185 |
TDLConceptExpression* MinCardinality(unsigned int, const TDLObjectRoleExpression*, const TDLConceptExpression*) |
| 186 |
TDLConceptExpression* MinCardinality(unsigned int, const TDLDataRoleExpression*, const TDLDataExpression*) |
| 187 |
TDLConceptExpression* MaxCardinality(unsigned int, const TDLObjectRoleExpression*, const TDLConceptExpression*) |
| 188 |
TDLConceptExpression* MaxCardinality(unsigned int, const TDLDataRoleExpression*, const TDLDataExpression*) |
| 189 |
|
| 190 |
const TDLDataValue* DataValue(string, const TDLDataTypeExpression*); |
| 191 |
|
| 192 |
void newArgList() |
| 193 |
void addArg(const TDLExpression*) |
| 194 |
|
| 195 |
|
| 196 |
#cdef extern from 'Kernel.h' namespace 'ReasoningKernel': |
| 197 |
# ctypedef vector[TNamedEntry*] IndividualSet |
| 198 |
|
| 199 |
cdef extern from 'taxVertex.h': |
| 200 |
cdef cppclass TaxonomyVertex: |
| 201 |
TaxonomyVertex() except + |
| 202 |
const ClassifiableEntry* getPrimer() |
| 203 |
vector[TaxonomyVertex*].iterator begin(bool) |
| 204 |
vector[TaxonomyVertex*].iterator end(bool) |
| 205 |
|
| 206 |
cdef extern from 'parser.h': |
| 207 |
cdef cppclass DLLispParser: |
| 208 |
DLLispParser(istream*, ReasoningKernel*) except + |
| 209 |
void Parse() |
| 210 |
|
| 211 |
cdef extern from 'ifOptions.h': |
| 212 |
cdef cppclass ifOptionSet: |
| 213 |
ifOptionSet() except + |
| 214 |
bool setOption(const string, const string) |
| 215 |
|
| 216 |
cdef extern from 'Kernel.h': |
| 217 |
cdef cppclass ReasoningKernel: |
| 218 |
ReasoningKernel() except + |
| 219 |
TExpressionManager* getExpressionManager() |
| 220 |
ifOptionSet* getOptions() |
| 221 |
|
| 222 |
TDLAxiom* equalORoles() |
| 223 |
TDLAxiom* equalDRoles() |
| 224 |
TDLAxiom* setSymmetric(TDLObjectRoleExpression*) |
| 225 |
TDLAxiom* setTransitive(TDLObjectRoleExpression*) |
| 226 |
TDLAxiom* setIrreflexive(TDLObjectRoleExpression*) |
| 227 |
TDLAxiom* setOFunctional(TDLObjectRoleExpression*) |
| 228 |
TDLAxiom* setDFunctional(TDLDataRoleExpression*) |
| 229 |
TDLAxiom* setInverseFunctional(TDLObjectRoleExpression*) |
| 230 |
TDLAxiom* relatedTo(TDLIndividualExpression*, TDLObjectRoleExpression*, TDLIndividualExpression*) |
| 231 |
TDLAxiom* relatedToNot(TDLIndividualExpression*, TDLObjectRoleComplexExpression*, TDLIndividualExpression*) |
| 232 |
TDLAxiom* instanceOf(TDLIndividualExpression*, TDLConceptExpression* C) |
| 233 |
bool isSameIndividuals(const TDLIndividualExpression*, const TDLIndividualExpression*) |
| 234 |
bool isInstance(TDLIndividualExpression*, TDLConceptExpression* C) |
| 235 |
TDLAxiom* valueOf(TDLIndividualExpression*, TDLDataRoleExpression*, TDLDataValue*) |
| 236 |
TDLAxiom* impliesORoles(TDLObjectRoleComplexExpression*, TDLObjectRoleExpression*) |
| 237 |
TDLAxiom* impliesDRoles(TDLDataRoleExpression*, TDLDataRoleExpression*) |
| 238 |
bool isSubRoles(const TDLObjectRoleExpression*, const TDLObjectRoleExpression*) |
| 239 |
bool isSubRoles(const TDLDataRoleExpression*, const TDLDataRoleExpression*) |
| 240 |
TDLAxiom* setODomain(TDLObjectRoleExpression*, TDLConceptExpression*) |
| 241 |
TDLAxiom* setORange(TDLObjectRoleExpression*, TDLConceptExpression*) |
| 242 |
TDLAxiom* setDDomain(TDLDataRoleExpression*, TDLConceptExpression*) |
| 243 |
TDLAxiom* setDRange(TDLDataRoleExpression*, TDLDataExpression*) |
| 244 |
TDLAxiom* setInverseRoles(TDLObjectRoleExpression*, TDLObjectRoleExpression*) |
| 245 |
|
| 246 |
TDLAxiom* equalConcepts() |
| 247 |
TDLAxiom* disjointConcepts() |
| 248 |
TDLAxiom* disjointUnion(TDLConceptExpression*) |
| 249 |
TDLAxiom* processSame() |
| 250 |
TDLAxiom* processDifferent() |
| 251 |
TDLAxiom* impliesConcepts(TDLConceptExpression*, TDLConceptExpression*) |
| 252 |
bool isSubsumedBy(TDLConceptExpression*, TDLConceptExpression*) |
| 253 |
#TIndividual* getIndividual(TDLIndividualExpression*, char*) |
| 254 |
#TRole* getRole(TDLObjectRoleExpression*, char*) |
| 255 |
#CIVec& getRelated(TIndividual*, TRole*) |
| 256 |
#void getRoleFillers(TDLIndividualExpression*, TDLObjectRoleExpression*, IndividualSet&) |
| 257 |
CIVec& getRoleFillers(TDLIndividualExpression*, TDLObjectRoleExpression*) |
| 258 |
void getTriples(const string&, const string&, const string&, set[vector[string]]&) |
| 259 |
TaxonomyVertex* setUpCache(TDLConceptExpression*) |
| 260 |
|
| 261 |
void realiseKB() except + |
| 262 |
void classifyKB() except + |
| 263 |
bool isKBConsistent() |
| 264 |
bool isKBPreprocessed() |
| 265 |
bool isKBRealised() |
| 266 |
|
| 267 |
void dumpLISP(ostream) |
| 268 |
|
| 269 |
cdef class Expression: |
| 270 |
cdef const TDLExpression *_obj |
| 271 |
|
| 272 |
cdef class IndividualExpr(Expression): |
| 273 |
cdef TDLIndividualExpression *c_obj(self): |
| 274 |
return <TDLIndividualExpression*>self._obj |
| 275 |
|
| 276 |
cdef class Individual(IndividualExpr): |
| 277 |
@property |
| 278 |
def name(self): |
| 279 |
return (<TDLIndividualName*>self.c_obj()).getName() |
| 280 |
|
| 281 |
def __repr__(self): |
| 282 |
return '<{} object at {}: {}>'.format(self.__class__.__name__, id(self), self.name) |
| 283 |
|
| 284 |
cdef class ConceptExpr(Expression): |
| 285 |
cdef TDLConceptExpression *c_obj(self): |
| 286 |
return <TDLConceptExpression*>self._obj |
| 287 |
|
| 288 |
cdef class Concept(ConceptExpr): |
| 289 |
@property |
| 290 |
def name(self): |
| 291 |
return (<TDLConceptName*>self.c_obj()).getName() |
| 292 |
|
| 293 |
def __repr__(self): |
| 294 |
return '<{} object at {}: {}>'.format(self.__class__.__name__, id(self), self.name) |
| 295 |
|
| 296 |
cdef class ObjectRoleExpr(Expression): |
| 297 |
cdef TDLObjectRoleExpression *c_obj(self): |
| 298 |
return <TDLObjectRoleExpression*>self._obj |
| 299 |
|
| 300 |
cdef class ObjectRole(ObjectRoleExpr): |
| 301 |
@property |
| 302 |
def name(self): |
| 303 |
return (<TDLObjectRoleName*>self.c_obj()).getName() |
| 304 |
|
| 305 |
def __repr__(self): |
| 306 |
return '<{} object at {}: {}>'.format(self.__class__.__name__, id(self), self.name) |
| 307 |
|
| 308 |
cdef class DataRoleExpr(Expression): |
| 309 |
cdef TDLDataRoleExpression *c_obj(self): |
| 310 |
return <TDLDataRoleExpression*>self._obj |
| 311 |
|
| 312 |
cdef class DataRole(DataRoleExpr): |
| 313 |
@property |
| 314 |
def name(self): |
| 315 |
return (<TDLDataRoleName*>self.c_obj()).getName() |
| 316 |
|
| 317 |
def __repr__(self): |
| 318 |
return '<{} object at {}: {}>'.format(self.__class__.__name__, id(self), self.name) |
| 319 |
|
| 320 |
cdef class DataExpr: |
| 321 |
cdef const TDLDataExpression *c_obj |
| 322 |
|
| 323 |
#cdef class DataValue: |
| 324 |
# cdef const TDLDataValue *c_obj |
| 325 |
|
| 326 |
cdef class DataType: |
| 327 |
cdef TDLDataTypeName *c_obj |
| 328 |
|
| 329 |
cdef class Reasoner: |
| 330 |
cdef ReasoningKernel *c_kernel |
| 331 |
cdef TExpressionManager *c_mgr |
| 332 |
|
| 333 |
cdef readonly DataType type_int |
| 334 |
cdef readonly DataType type_float |
| 335 |
cdef readonly DataType type_str |
| 336 |
cdef readonly DataType type_bool |
| 337 |
cdef readonly DataType type_datetime_long |
| 338 |
cdef readonly dict _cache |
| 339 |
|
| 340 |
def __cinit__(self): |
| 341 |
self.c_kernel = new ReasoningKernel() |
| 342 |
self.c_mgr = self.c_kernel.getExpressionManager() |
| 343 |
|
| 344 |
def __init__(self, **kwds): |
| 345 |
self.type_int = self.data_type('http://www.w3.org/2001/XMLSchema#integer') |
| 346 |
self.type_float = self.data_type('http://www.w3.org/2001/XMLSchema#float') |
| 347 |
self.type_str = self.data_type('http://www.w3.org/2001/XMLSchema#string') |
| 348 |
self.type_bool = self.data_type('http://www.w3.org/2001/XMLSchema#boolean') |
| 349 |
self.type_datetime_long = self.data_type('http://www.w3.org/2001/XMLSchema#dateTimeAsLong') |
| 350 |
# set options |
| 351 |
cdef string name |
| 352 |
cdef string value |
| 353 |
for k, v in kwds.items(): |
| 354 |
name = k.encode('UTF-8') |
| 355 |
value = v.encode('UTF-8') |
| 356 |
if self.c_kernel.getOptions().setOption(name, value): |
| 357 |
raise ValueError(f'Error setting value {v} for option {k}. \n' |
| 358 |
'Either option does not exist or value is of incorrect type') |
| 359 |
|
| 360 |
self._cache = {} # it should be weakref dict |
| 361 |
|
| 362 |
def parse_lisp(self, str fn): |
| 363 |
cdef ifstream* in_file = new ifstream(fn.encode(), binary) |
| 364 |
cdef DLLispParser *parser = new DLLispParser(in_file, self.c_kernel) |
| 365 |
try: |
| 366 |
parser.Parse() |
| 367 |
finally: |
| 368 |
del in_file |
| 369 |
|
| 370 |
def __dealloc__(self): |
| 371 |
del self.c_kernel |
| 372 |
|
| 373 |
cdef _get(self, type T, TDLExpression *c_obj): |
| 374 |
return instance(self._cache, T, c_obj) |
| 375 |
|
| 376 |
cdef _to_cls_expr(self, const ClassifiableEntry *obj): |
| 377 |
# FIXME: or use obj.getEntity()? |
| 378 |
if obj.isTop(): |
| 379 |
return self.concept_top() |
| 380 |
else: |
| 381 |
return self.concept(obj.getName()) |
| 382 |
|
| 383 |
def _find_o_role_items(self, ObjectRoleExpr r, bool top): |
| 384 |
# TODO: duplicate code, see _find_d_role_items |
| 385 |
|
| 386 |
cdef TDLConceptExpression *start |
| 387 |
cdef TaxonomyVertex *node |
| 388 |
cdef const ClassifiableEntry *obj |
| 389 |
cdef vector[TaxonomyVertex*].iterator it |
| 390 |
|
| 391 |
start = self.c_mgr.Top() if top else self.c_mgr.Bottom() |
| 392 |
node = self.c_kernel.setUpCache(self.c_mgr.Exists(r.c_obj(), start)) |
| 393 |
obj = <const ClassifiableEntry*>node.getPrimer() |
| 394 |
if not skip(obj): |
| 395 |
yield self._to_cls_expr(obj) |
| 396 |
|
| 397 |
it = node.begin(True) |
| 398 |
while it != node.end(True): |
| 399 |
obj = <const ClassifiableEntry*>dereference(it).getPrimer() |
| 400 |
if skip(obj): |
| 401 |
postincrement(it) |
| 402 |
continue |
| 403 |
yield self._to_cls_expr(obj) |
| 404 |
postincrement(it) |
| 405 |
|
| 406 |
def _find_d_role_items(self, DataRoleExpr r, bool top): |
| 407 |
# TODO: duplicate code, see _find_o_role_items |
| 408 |
|
| 409 |
cdef TDLDataExpression *start |
| 410 |
cdef TaxonomyVertex *node |
| 411 |
cdef const ClassifiableEntry *obj |
| 412 |
cdef vector[TaxonomyVertex*].iterator it |
| 413 |
|
| 414 |
start = self.c_mgr.DataTop() if top else self.c_mgr.DataBottom() |
| 415 |
node = self.c_kernel.setUpCache(self.c_mgr.Exists(r.c_obj(), start)) |
| 416 |
|
| 417 |
it = node.begin(True) |
| 418 |
while it != node.end(True): |
| 419 |
obj = <const ClassifiableEntry*>dereference(it).getPrimer() |
| 420 |
yield self._to_cls_expr(obj) |
| 421 |
postincrement(it) |
| 422 |
|
| 423 |
def _arg_list(self, items): |
| 424 |
self.c_mgr.newArgList() |
| 425 |
for e in items: |
| 426 |
self.c_mgr.addArg((<Expression>e)._obj) |
| 427 |
|
| 428 |
# |
| 429 |
# concepts |
| 430 |
# |
| 431 |
def concept(self, name not None): |
| 432 |
return self._get(Concept, self.c_mgr.Concept(name.encode())) |
| 433 |
|
| 434 |
def concept_top(self): |
| 435 |
return self._get(ConceptExpr, self.c_mgr.Top()) |
| 436 |
|
| 437 |
def concept_bottom(self): |
| 438 |
return self._get(ConceptExpr, self.c_mgr.Bottom()) |
| 439 |
|
| 440 |
def implies_concepts(self, ConceptExpr subcls, ConceptExpr parent): |
| 441 |
""" |
| 442 |
Make first class a subclass of the second. |
| 443 |
|
| 444 |
:param subcls: Derived class (subclass). |
| 445 |
:param parent: Parent class (superclass) |
| 446 |
""" |
| 447 |
self.c_kernel.impliesConcepts(subcls.c_obj(), parent.c_obj()) |
| 448 |
|
| 449 |
def is_subsumed_by(self, ConceptExpr c1, ConceptExpr c2): |
| 450 |
return self.c_kernel.isSubsumedBy(c1.c_obj(), c2.c_obj()) |
| 451 |
|
| 452 |
def equal_concepts(self, *classes): |
| 453 |
self._arg_list(classes) |
| 454 |
self.c_kernel.equalConcepts() |
| 455 |
|
| 456 |
def disjoint_concepts(self, *classes): |
| 457 |
self._arg_list(classes) |
| 458 |
self.c_kernel.disjointConcepts() |
| 459 |
|
| 460 |
def union(self, *classes): |
| 461 |
self._arg_list(classes) |
| 462 |
return self._get(ConceptExpr, self.c_mgr.Or()) |
| 463 |
|
| 464 |
def intersection(self, *classes): |
| 465 |
self._arg_list(classes) |
| 466 |
return self._get(ConceptExpr, self.c_mgr.And()) |
| 467 |
|
| 468 |
# |
| 469 |
# individuals |
| 470 |
# |
| 471 |
def individual(self, name not None): |
| 472 |
return self._get(Individual, self.c_mgr.Individual(name.encode())) |
| 473 |
|
| 474 |
def instance_of(self, IndividualExpr i, ConceptExpr c): |
| 475 |
self.c_kernel.instanceOf(i.c_obj(), c.c_obj()) |
| 476 |
|
| 477 |
def one_of(self, *instances): |
| 478 |
self._arg_list(instances) |
| 479 |
return self._get(ConceptExpr, self.c_mgr.OneOf()) |
| 480 |
|
| 481 |
def complement_of(self, ConceptExpr c): |
| 482 |
return self._get(ConceptExpr, self.c_mgr.Not(c.c_obj())) |
| 483 |
|
| 484 |
def same_individuals(self, instances): |
| 485 |
self._arg_list(instances) |
| 486 |
self.c_kernel.processSame() |
| 487 |
|
| 488 |
def different_individuals(self, *instances): |
| 489 |
self._arg_list(instances) |
| 490 |
self.c_kernel.processDifferent() |
| 491 |
|
| 492 |
def is_same_individuals(self, IndividualExpr i1, IndividualExpr i2): |
| 493 |
return self.c_kernel.isSameIndividuals(i1.c_obj(), i2.c_obj()) |
| 494 |
|
| 495 |
def is_instance(self, IndividualExpr i, ConceptExpr c): |
| 496 |
return self.c_kernel.isInstance(i.c_obj(), c.c_obj()) |
| 497 |
|
| 498 |
# |
| 499 |
# object roles |
| 500 |
# |
| 501 |
def object_role_top(self): |
| 502 |
return self._get(ObjectRole, self.c_mgr.ObjectRoleTop()) |
| 503 |
|
| 504 |
def object_role(self, name not None): |
| 505 |
return self._get(ObjectRole, self.c_mgr.ObjectRole(name.encode())) |
| 506 |
|
| 507 |
def set_o_domain(self, ObjectRoleExpr r, ConceptExpr c): |
| 508 |
self.c_kernel.setODomain(r.c_obj(), c.c_obj()) |
| 509 |
|
| 510 |
def get_o_domain(self, ObjectRoleExpr r): |
| 511 |
yield from self._find_o_role_items(r, True) |
| 512 |
|
| 513 |
def get_o_range(self, ObjectRoleExpr r): |
| 514 |
rev = self._get(ObjectRoleExpr, self.c_mgr.Inverse((r.c_obj()))) |
| 515 |
yield from self._find_o_role_items(rev, True) |
| 516 |
|
| 517 |
def set_o_range(self, ObjectRoleExpr r, ConceptExpr c): |
| 518 |
self.c_kernel.setORange(r.c_obj(), c.c_obj()) |
| 519 |
|
| 520 |
def o_value(self, ObjectRoleExpr r, IndividualExpr i): |
| 521 |
return self._get(ConceptExpr, self.c_mgr.Value(r.c_obj(), i.c_obj())) |
| 522 |
|
| 523 |
def o_exists(self, ObjectRoleExpr r, ConceptExpr c): |
| 524 |
return self._get(ConceptExpr, self.c_mgr.Exists(r.c_obj(), c.c_obj())) |
| 525 |
|
| 526 |
def o_forall(self, ObjectRoleExpr r, ConceptExpr c): |
| 527 |
return self._get(ConceptExpr, self.c_mgr.Forall(r.c_obj(), c.c_obj())) |
| 528 |
|
| 529 |
def o_cardinality(self, unsigned int n, ObjectRoleExpr r, ConceptExpr c): |
| 530 |
return self._get(ConceptExpr, self.c_mgr.Cardinality(n, r.c_obj(), c.c_obj())) |
| 531 |
|
| 532 |
def min_o_cardinality(self, unsigned int n, ObjectRoleExpr r, ConceptExpr c): |
| 533 |
return self._get(ConceptExpr, self.c_mgr.MinCardinality(n, r.c_obj(), c.c_obj())) |
| 534 |
|
| 535 |
def max_o_cardinality(self, unsigned int n, ObjectRoleExpr r, ConceptExpr c): |
| 536 |
return self._get(ConceptExpr, self.c_mgr.MaxCardinality(n, r.c_obj(), c.c_obj())) |
| 537 |
|
| 538 |
def equal_o_roles(self, *roles): |
| 539 |
self._arg_list(roles) |
| 540 |
self.c_kernel.equalORoles() |
| 541 |
|
| 542 |
def implies_o_roles(self, ObjectRoleExpr r1, ObjectRoleExpr r2): |
| 543 |
self.c_kernel.impliesORoles(r1.c_obj(), r2.c_obj()) |
| 544 |
|
| 545 |
def is_sub_o_role(self, ObjectRoleExpr r1, ObjectRoleExpr r2): |
| 546 |
return self.c_kernel.isSubRoles(r1.c_obj(), r2.c_obj()) |
| 547 |
|
| 548 |
def set_symmetric(self, ObjectRoleExpr r): |
| 549 |
self.c_kernel.setSymmetric(r.c_obj()) |
| 550 |
|
| 551 |
def set_transitive(self, ObjectRoleExpr r): |
| 552 |
self.c_kernel.setTransitive(r.c_obj()) |
| 553 |
|
| 554 |
def set_irreflexive(self, ObjectRoleExpr r): |
| 555 |
self.c_kernel.setIrreflexive(r.c_obj()) |
| 556 |
|
| 557 |
def set_o_functional(self, ObjectRoleExpr r): |
| 558 |
self.c_kernel.setOFunctional(r.c_obj()) |
| 559 |
|
| 560 |
def set_inverse_functional(self, ObjectRoleExpr r): |
| 561 |
self.c_kernel.setInverseFunctional(r.c_obj()) |
| 562 |
|
| 563 |
def inverse(self, ObjectRoleExpr r): |
| 564 |
return self._get(ObjectRoleExpr, self.c_mgr.Inverse(r.c_obj())) |
| 565 |
|
| 566 |
def compose(self, *roles): |
| 567 |
""" |
| 568 |
Compose chain of roles. |
| 569 |
|
| 570 |
:param roles: Collection of object roles. |
| 571 |
""" |
| 572 |
self._arg_list(roles) |
| 573 |
return self._get(ObjectRoleExpr, self.c_mgr.Compose()) |
| 574 |
|
| 575 |
def set_inverse_roles(self, ObjectRoleExpr r1, ObjectRoleExpr r2): |
| 576 |
self.c_kernel.setInverseRoles(r1.c_obj(), r2.c_obj()) |
| 577 |
|
| 578 |
def related_to(self, IndividualExpr i1, ObjectRoleExpr r, IndividualExpr i2): |
| 579 |
self.c_kernel.relatedTo(i1.c_obj(), r.c_obj(), i2.c_obj()) |
| 580 |
|
| 581 |
def related_to_not(self, IndividualExpr i1, ObjectRoleExpr r, IndividualExpr i2): |
| 582 |
self.c_kernel.relatedToNot(i1.c_obj(), r.c_obj(), i2.c_obj()) |
| 583 |
|
| 584 |
def get_role_fillers(self, IndividualExpr i, ObjectRoleExpr r): |
| 585 |
cdef CIVec data = self.c_kernel.getRoleFillers(i.c_obj(), r.c_obj()) |
| 586 |
|
| 587 |
for k in range(data.size()): |
| 588 |
yield self.individual(data[k].getName()) |
| 589 |
|
| 590 |
def get_instances(self, ConceptExpr c): |
| 591 |
cdef const TConcept *obj |
| 592 |
cdef TaxonomyVertex *node = self.c_kernel.setUpCache(c.c_obj()) |
| 593 |
cdef vector[TaxonomyVertex*].iterator it = node.begin(False) |
| 594 |
obj = <const TConcept*>dereference(it).getPrimer() |
| 595 |
if obj.isBottom(): |
| 596 |
return |
| 597 |
|
| 598 |
while it != node.end(False): |
| 599 |
obj = <const TConcept*>dereference(it).getPrimer() |
| 600 |
if obj.isSingleton(): |
| 601 |
yield self.individual(obj.getName()) |
| 602 |
else: |
| 603 |
yield from self.get_instances(self.concept(obj.getName())) |
| 604 |
postincrement(it) |
| 605 |
|
| 606 |
def get_triples(self, string s, string p, string o): |
| 607 |
cdef set[vector[string]] c_triples |
| 608 |
|
| 609 |
self.c_kernel.getTriples(s, p, o, c_triples) |
| 610 |
|
| 611 |
return ((triple[0], triple[1], triple[2], triple[3]) for triple in c_triples) |
| 612 |
# |
| 613 |
# data roles |
| 614 |
# |
| 615 |
def data_role_top(self): |
| 616 |
return self._get(DataRole, self.c_mgr.DataRoleTop()) |
| 617 |
|
| 618 |
def data_role(self, name not None): |
| 619 |
return self._get(DataRole, self.c_mgr.DataRole(name.encode())) |
| 620 |
|
| 621 |
def data_top(self): |
| 622 |
cdef DataExpr result = DataExpr.__new__(DataExpr) |
| 623 |
result.c_obj = self.c_mgr.DataTop() |
| 624 |
return result |
| 625 |
|
| 626 |
def get_d_domain(self, DataRoleExpr r): |
| 627 |
yield from self._find_d_role_items(r, True) |
| 628 |
|
| 629 |
def data_type(self, name not None): |
| 630 |
cdef DataType result = DataType.__new__(DataType) |
| 631 |
result.c_obj = self.c_mgr.DataType(name.encode()) |
| 632 |
return result |
| 633 |
|
| 634 |
def equal_d_roles(self, *roles): |
| 635 |
self._arg_list(roles) |
| 636 |
self.c_kernel.equalDRoles() |
| 637 |
|
| 638 |
def set_d_domain(self, DataRoleExpr r, ConceptExpr c): |
| 639 |
self.c_kernel.setDDomain(r.c_obj(), c.c_obj()) |
| 640 |
|
| 641 |
def set_d_range(self, DataRoleExpr r, DataType t): |
| 642 |
self.c_kernel.setDRange(r.c_obj(), t.c_obj) |
| 643 |
|
| 644 |
def d_cardinality(self, unsigned int n, DataRoleExpr r, DataExpr d): |
| 645 |
return self._get(ConceptExpr, self.c_mgr.Cardinality(n, r.c_obj(), d.c_obj)) |
| 646 |
|
| 647 |
def max_d_cardinality(self, unsigned int n, DataRoleExpr r, DataExpr d): |
| 648 |
return self._get(ConceptExpr, self.c_mgr.MaxCardinality(n, r.c_obj(), d.c_obj)) |
| 649 |
|
| 650 |
def implies_d_roles(self, DataRoleExpr r1, DataRoleExpr r2): |
| 651 |
self.c_kernel.impliesDRoles(r1.c_obj(), r2.c_obj()) |
| 652 |
|
| 653 |
def is_sub_d_role(self, DataRoleExpr r1, DataRoleExpr r2): |
| 654 |
return self.c_kernel.isSubRoles(r1.c_obj(), r2.c_obj()) |
| 655 |
|
| 656 |
def set_d_functional(self, DataRoleExpr r): |
| 657 |
self.c_kernel.setDFunctional(r.c_obj()) |
| 658 |
|
| 659 |
# def d_value(self, DataRoleExpr r, DataValue d): |
| 660 |
# return self._get(ConceptExpr, self.c_mgr.Value(r.c_obj(), d.c_obj())) |
| 661 |
|
| 662 |
# def data_value(self, string v, DataType t): |
| 663 |
# cdef DataExpr result = DataExpr.__new__(DataExpr) |
| 664 |
# result.c_obj = self.c_mgr.DataValue(v, t.c_obj) |
| 665 |
# return result |
| 666 |
|
| 667 |
def value_of_int(self, IndividualExpr i, DataRoleExpr r, int v): |
| 668 |
value = self.c_mgr.DataValue(str(v).encode(), self.type_int.c_obj) |
| 669 |
self.c_kernel.valueOf(i.c_obj(), r.c_obj(), value) |
| 670 |
|
| 671 |
def value_of_str(self, IndividualExpr i, DataRoleExpr r, str v): |
| 672 |
value = self.c_mgr.DataValue(v.encode(), self.type_str.c_obj) |
| 673 |
self.c_kernel.valueOf(i.c_obj(), r.c_obj(), value) |
| 674 |
|
| 675 |
def value_of_float(self, IndividualExpr i, DataRoleExpr r, float v): |
| 676 |
value = self.c_mgr.DataValue(str(v).encode(), self.type_float.c_obj) |
| 677 |
self.c_kernel.valueOf(i.c_obj(), r.c_obj(), value) |
| 678 |
|
| 679 |
def value_of_bool(self, IndividualExpr i, DataRoleExpr r, bool v): |
| 680 |
value = self.c_mgr.DataValue(str(v).encode(), self.type_bool.c_obj) |
| 681 |
self.c_kernel.valueOf(i.c_obj(), r.c_obj(), value) |
| 682 |
|
| 683 |
# |
| 684 |
# general |
| 685 |
# |
| 686 |
def realise(self): |
| 687 |
self.c_kernel.realiseKB() |
| 688 |
|
| 689 |
def classify(self): |
| 690 |
self.c_kernel.classifyKB() |
| 691 |
|
| 692 |
def is_consistent(self): |
| 693 |
return self.c_kernel.isKBConsistent() |
| 694 |
|
| 695 |
def is_preprocessed(self): |
| 696 |
return self.c_kernel.isKBPreprocessed() |
| 697 |
|
| 698 |
def is_realised(self): |
| 699 |
return self.c_kernel.isKBRealised() |
| 700 |
|
| 701 |
def dump(self, str fn): |
| 702 |
cdef ofstream *out = new ofstream(fn.encode(), binary) |
| 703 |
try: |
| 704 |
self.c_kernel.dumpLISP(out[0]) |
| 705 |
finally: |
| 706 |
del out |
| 707 |
|
| 708 |
cdef instance(dict cache, type T, const TDLExpression *c_obj): |
| 709 |
cdef Expression result |
| 710 |
|
| 711 |
key = <long>c_obj |
| 712 |
result = cache.get(key) |
| 713 |
|
| 714 |
if result is None: |
| 715 |
result = T.__new__(T) |
| 716 |
result._obj = c_obj |
| 717 |
cache[key] = result |
| 718 |
assert result is not None |
| 719 |
return result |
| 720 |
|
| 721 |
# vim: sw=4:et:ai |