File: RAutomaton.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 (153 lines) | stat: -rw-r--r-- 4,524 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* This file is part of the FaCT++ DL reasoner
Copyright (C) 2006-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 "RAutomaton.h"
#include "tRole.h"

/// check whether transition is TopRole one
bool
RATransition :: isTop ( void ) const
{
	return label.size() == 1 && unlikely(label.front()->isTop());
}

/// set up state transitions: no more additions to the structure
void
RAStateTransitions :: setup ( RAState state, size_t nRoles, bool data )
{
	from = state;
	DataRole = data;
	ApplicableRoles.ensureMaxSetSize(nRoles);
	// fills the set of recognisable roles
	for ( const_iterator p = begin(), p_end = end(); p != p_end; ++p )
		for ( RATransition::const_iterator q = (*p)->begin(), q_end = (*p)->end(); q != q_end; ++q )
			ApplicableRoles.add((*q)->getIndex());
}

/// add information from TRANS to existing transition between the same states. @return false if no such transition found
bool
RAStateTransitions :: addToExisting ( const RATransition* trans )
{
	RAState to = trans->final();
	bool tEmpty = trans->empty();
	for ( iterator p = Base.begin(), p_end = Base.end(); p != p_end; ++p )
		if ( (*p)->final() == to && (*p)->empty() == tEmpty )
		{	// found existing transition
			(*p)->addIfNew(*trans);
			return true;
		}
	// no transition from->to found
	return false;
}

void
RATransition::Print ( std::ostream& o, RAState from ) const
{
	o << "\n" << from << " -- ";
	if ( empty() )
		o << "e";
	else
	{
		const_iterator p = label.begin();
		o << '"' << (*p)->getName() << '"';

		for ( ++p; p != label.end(); ++p )
			o << ",\"" << (*p)->getName() << '"';
	}
	o << " -> " << final();
}

void
RoleAutomaton :: addCopy ( const RoleAutomaton& RA )
{
	for ( RAState i = 0; i < RA.size(); ++i )
	{
		RAState from = map[i];
		RAStateTransitions& RST = Base[from];
		const RAStateTransitions& RSTOrig = RA[i];

		if ( RSTOrig.empty() )
			continue;

		for ( RAStateTransitions::const_iterator p = RSTOrig.begin(), p_end = RSTOrig.end(); p != p_end; ++p )
		{
			RAState to = (*p)->final();
			RATransition* trans = new RATransition(map[to]);
			checkTransition ( from, trans->final() );
			trans->add(**p);

			// try to merge transitions going to the original final state
			if ( to == 1 && RST.addToExisting(trans) )
				delete trans;
			else
				RST.add(trans);
		}
	}
}

/// init internal map according to RA size, with new initial state from chainState and final (FRA) states
void
RoleAutomaton :: initMap ( size_t RASize, RAState fRA )
{
	map.resize(RASize);
	// new state in the automaton
	RAState newState = (RAState) size()-1;

	// fill initial state; it is always known in the automata
	map[0] = iRA;

	// fills the final state; if it is not known -- adjust newState
	if ( fRA >= size() )
	{
		fRA = (RAState) size();	// make sure we don't create an extra unused state
		++newState;
	}
	map[1] = fRA;

	// check transitions as it may turns out to be a single transition
	checkTransition ( iRA, fRA );

	// set new initial state
	iRA = fRA;

	// fills the rest of map
	for ( unsigned int i = 2; i < RASize; ++i )
		map[i] = ++newState;

	// reserve enough space for the new automaton
	ensureState(newState);
}

/// add an Automaton to the chain that would start from the iRA; OSAFE shows the safety of a previous automaton in a chain
bool
RoleAutomaton :: addToChain ( const RoleAutomaton& RA, bool oSafe, RAState fRA )
{
	fpp_assert(!isCompleted());
	bool needFinalTrans = ( fRA < size() && !RA.isOSafe() );
	// we can skip transition if chaining automata are i- and o-safe
	if ( !oSafe && !RA.isISafe() )
		nextChainTransition(newState());
	// check whether we need an output transition
	initMap ( RA.size(), needFinalTrans ? (RAState) size() : fRA );
	addCopy(RA);
	if ( needFinalTrans )
		nextChainTransition(fRA);

	return RA.isOSafe();
}