File: KnowledgeExplorer.cpp

package info (click to toggle)
fact%2B%2B 1.6.5~dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 4,496 kB
  • sloc: cpp: 28,000; java: 22,674; xml: 3,268; makefile: 102; ansic: 61; sh: 3
file content (134 lines) | stat: -rw-r--r-- 5,190 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* This file is part of the FaCT++ DL reasoner
Copyright (C) 2011-2015 Dmitry Tsarkov and The University of Manchester
Copyright (C) 2015-2016 Dmitry Tsarkov

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "KnowledgeExplorer.h"
#include "dlCompletionTree.h"
#include "dlTBox.h"

		/// init c'tor
KnowledgeExplorer :: KnowledgeExplorer ( const TBox* box, TExpressionManager* pEM )
	: D2I ( box->getDag(), pEM )
{
	// init all concepts
	for ( TBox::c_const_iterator c = box->c_begin(), c_end = box->c_end(); c != c_end; ++c )
		addE ( Cs, *c );
	// init all individuals
	for ( TBox::i_const_iterator i = box->i_begin(), i_end = box->i_end(); i != i_end; ++i )
		addE ( Is, *i );
	RoleMaster::const_iterator r, r_end;
	// init all object roles
	for ( r = box->getORM()->begin(), r_end = box->getORM()->end(); r != r_end; ++r )
	{
		TRole* R = *r;
		addE ( ORs, R );
		for ( TRole::const_iterator p = R->begin_anc(), p_end = R->end_anc(); p != p_end; ++p )
			ORs.add ( R, *p );
	}
	// init all data roles
	for ( r = box->getDRM()->begin(), r_end = box->getDRM()->end(); r != r_end; ++r )
	{
		TRole* R = *r;
		addE ( DRs, R );
		for ( TRole::const_iterator p = R->begin_anc(), p_end = R->end_anc(); p != p_end; ++p )
			DRs.add ( R, *p );
	}
}

/// add concept-like entity E (possibly with synonyms) to CONCEPTS
void
KnowledgeExplorer :: addC ( const TDLExpression* e )
{
	// check named concepts
	const TDLConceptName* C = dynamic_cast<const TDLConceptName*>(e);
	if ( C != NULL )
	{
		for ( EE2Map<TDLConceptName>::iterator p = Cs.begin(C), p_end = Cs.end(C); p != p_end; ++p )
			if ( unlikely(*p == NULL) )
				std::cerr << "Null found while processing class " << C->getName() << "\n";
			else
				Concepts.push_back(*p);
		return;
	}
	// check named individuals
	const TDLIndividualName* I = dynamic_cast<const TDLIndividualName*>(e);
	if ( I != NULL )
	{
		for ( EE2Map<TDLIndividualName>::iterator p = Is.begin(I), p_end = Is.end(I); p != p_end; ++p )
			if ( unlikely(*p == NULL) )
				std::cerr << "Null found while processing individual " << I->getName() << "\n";
			else
				Concepts.push_back(*p);
		return;
	}
	Concepts.push_back(e);
}

// couldn't get it as a method, so
#define addAll(S) do { Roles.insert(S.begin(),S.end()); } while(0)

const KnowledgeExplorer::TCGRoleSet&
KnowledgeExplorer :: getDataRoles ( const TCGNode* node, bool onlyDet )
{
	Roles.clear();
	for ( DlCompletionTree::const_edge_iterator p = node->begin(), p_end = node->end(); p != p_end; ++p )
		if ( likely(!(*p)->isIBlocked()) && (*p)->getArcEnd()->isDataNode() && (!onlyDet || (*p)->getDep().empty()) )
			addAll(DRs.get((*p)->getRole()->getEntity()));
	return Roles;
}
/// build the set of object neighbours of a NODE; incoming edges are counted iff NEEDINCOMING is true
const KnowledgeExplorer::TCGRoleSet&
KnowledgeExplorer :: getObjectRoles ( const TCGNode* node, bool onlyDet, bool needIncoming )
{
	Roles.clear();
	for ( DlCompletionTree::const_edge_iterator p = node->begin(), p_end = node->end(); p != p_end; ++p )
		if ( likely(!(*p)->isIBlocked()) && !(*p)->getArcEnd()->isDataNode() && (!onlyDet || (*p)->getDep().empty()) && (needIncoming || (*p)->isSuccEdge() ) )
			addAll(ORs.get((*p)->getRole()->getEntity()));
	return Roles;
}
/// build the set of neighbours of a NODE via role ROLE; put the resulting list into RESULT
const KnowledgeExplorer::TCGNodeVec&
KnowledgeExplorer :: getNeighbours ( const TCGNode* node, const TRole* R )
{
	Nodes.clear();
	for ( DlCompletionTree::const_edge_iterator p = node->begin(), p_end = node->end(); p != p_end; ++p )
		if ( likely(!(*p)->isIBlocked()) && (*p)->isNeighbour(R) )
			Nodes.push_back((*p)->getArcEnd());
	return Nodes;
}
/// put into RESULT all the data expressions from the NODE label
const KnowledgeExplorer::TCGItemVec&
KnowledgeExplorer :: getLabel ( const TCGNode* node, bool onlyDet )
{
	// prepare D2I translator
	D2I.ensureDagSize();
	DlCompletionTree::const_label_iterator p, p_end;
	bool data = node->isDataNode();
	Concepts.clear();
	for ( p = node->beginl_sc(), p_end = node->endl_sc(); p != p_end; ++p )
		if ( !onlyDet || p->getDep().empty() )
			addC(D2I.getExpr(p->bp(),data));
	for ( p = node->beginl_cc(), p_end = node->endl_cc(); p != p_end; ++p )
		if ( !onlyDet || p->getDep().empty() )
			addC(D2I.getExpr(p->bp(),data));
	return Concepts;
}

/// @return blocker of a blocked node NODE or NULL if node is not blocked
const KnowledgeExplorer::TCGNode*
KnowledgeExplorer :: getBlocker ( const TCGNode* node ) const { return node->getBlocker(); }