1/*  
    2% ===================================================================
    3% File 'mpred_type_constraints.pl'
    4% Purpose: For Emulation of OpenCyc for SWI-Prolog
    5% Maintainer: Douglas Miles
    6% Contact: $Author: dmiles $@users.sourceforge.net ;
    7% Version: 'interface' 1.0.0
    8% Revision:  $Revision: 1.9 $
    9% Revised At:   $Date: 2002/06/27 14:13:20 $
   10% ===================================================================
   11% File used as storage place for all predicates which change as
   12% the world is run.
   13%
   14%
   15% Dec 13, 2035
   16% Douglas Miles
   17*/
   18
   19
   20%:- if(( ( \+ ((current_prolog_flag(logicmoo_include,Call),Call))) )).
   21:- module(mpred_motel,[ getLibraries/0]).   22%:- endif.
   23:- style_check(-singleton).   24
   25assert_logged(A):-assert(A).
   26asserta_logged(A):-asserta(A).
   27assertz_logged(A):-assertz(A).
   28:- op(700,fy,skipped).   29skipped(G):- nop(G).
   30/*
   31:- discontiguous testAllMotelExamples/1. 
   32:- discontiguous testMotel/1. 
   33:- discontiguous tryGoal/1.
   34*/
   35:- system:op(1199,fx,('==>')).   36:- system:op(1190,xfx,('::::')).   37:- system:op(1180,xfx,('==>')).   38:- system:op(1170,xfx,('<==>')).   39:- system:op(1160,xfx,('<-')).   40
   41:- system:op(1150,xfx,('=>')).   42:- system:op(1140,xfx,('<=')).   43:- system:op(1130,xfx,('<=>')).   44:-  system:((
   45 op(1199,fx,('==>')), 
   46 op(1190,xfx,('::::')),
   47 op(1180,xfx,('==>')),
   48 op(1170,xfx,'<==>'),  
   49 op(1160,xfx,('<-')),
   50 op(1150,xfx,'=>'),
   51 op(1140,xfx,'<='),
   52 op(1130,xfx,'<=>'), 
   53 op(600,yfx,'&'), 
   54 op(600,yfx,'v'),
   55 op(350,xfx,'xor'),
   56 op(300,fx,'~'),
   57 op(300,fx,'-'))).   58
   59
   60:- meta_predicate prolog_statistics_time(0).   61:- meta_predicate setupTest(*,0).   62
   63:-  system:op(200,yfx,('`')).   64:-  system:op(600,yfx,('&')).   65:-  system:op(600,yfx,('v')).   66:-  system:op(350,xfx,('xor')).   67:-  system:op(300,fx,('~')).   68:-  system:op(300,fx,('-')).   69:- user:use_module(library(logicmoo_common)).   70:- user:use_module(library(logicmoo_utils)).   71
   72/*	
   73
   74
   75                         INSTALLING MOTEL
   76			================
   77
   78The MOTEL distribution contains one compressed tar file, which
   79includes the MOTEL system. To install the sytem execute the following 
   80steps:
   81
   821. Uncompress the compressed tar file
   83
   84prompt(1)% uncompress motel.tar.Z
   85
   862. Extract the source file and documentation file from the tar file
   87
   88prompt(2)% tar xvf motel.tar
   89
   90This results in the files 	
   91README      ---	brief description how the system can be used
   92int.c       --- Part of the interface between Lucid Common Lisp and Sicstus Prolog
   93int.o       --- Part of the interface between Lucid Common Lisp and Sicstus Prolog
   94int.pl	    --- Prolog code of the interface between Lucid Common Lisp and Sicstus Prolog
   95motel.lisp  ---	Lisp   code of the interface between Lucid Common Lisp and Sicstus Prolog
   96motel.pl    ---	MOTEL source code	
   97motel.dvi   --- MOTEL user manual 
   98nh.dvi      --- Introduction to modal terminological logics
   99
  100After starting your PROLOG system you have to consult the source file.
  101
  102prompt(3)% sicstus
  103SICStus 2.1 #5: Tue Jul 21 16:16:49 MET DST 1992
  104| ?- consult(motel).
  105consulting motel.pl...
  106motel.pl consulted, 5600 msec 329168 bytes
  107
  108yes
  109| ?-
  110
  111Now you can work with the MOTEL system as described in the user manual.
  112
  113To use the interface between Lucid Common Lisp and SICStus Prolog, you 
  114have to modify the file motel.lisp. At the beginning it contains two
  115setq-commands:
  116
  117(setq *consult-motel-string* "['/usr/local/motel/motel.pl'].")
  118(setq *prolog-executable* "/usr/local/sicstus2.1/sicstus")
  119(setq *int_dot_pl* "/HG/hiwis/timm/lucid/int.pl")
  120
  121You should replace `/usr/local/motel/motel.pl` with the filename
  122of your installation of the motel.pl file. Furthermore you should
  123replace `/usr/local/sicstus2.1/sicstus` with the filename of you
  124PROLOG system. The variable `*int_dot_pl*` contains the location 
  125of the file `int.pl` included in the distribution
  126
  127Now you can load this file after you have started Lucid Common Lisp:
  128
  129prompt(4)% lucid
  130;;; Lucid Common Lisp/SPARC
  131;;; Application Environment Version 4.0.0, 6 July 1990
  132;;; Copyright (C) 1985, 1986, 1987, 1988, 1989, 1990, 1991 by Lucid, Inc.
  133;;; All Rights Reserved
  134;;;
  135;;; This software product contains confidential and trade secret information
  136;;; belonging to Lucid, Inc.  It may not be copied for any reason other than
  137;;; for archival and backup purposes.
  138;;;
  139;;; Lucid and Lucid Common Lisp are trademarks of Lucid, Inc.  Other brand
  140;;; or product names are trademarks or registered trademarks of their
  141;;; respective holders.
  142
  143> (load "motel.lisp")
  144;;; Loading source file "motel.lisp"
  145;;; Warning: File "motel.lisp" does not begin with IN-PACKAGE.
  146    Loading into package "USER"
  147#P"/usr/local/motel/src/motel/motel.lisp"
  148> 
  149
  150Then you are able to work with the interface between Lucid Lisp and
  151SICSTUS Prolog as described the appendix of the MOTEL user manual.
  152
  153*/
  154/**********************************************************************
  155 *
  156 * @(#) dynamicDef.pl 1.16@(#)
  157 *
  158 */
  159
  160:- multifile(call_u_lm/1).  161:- dynamic(call_u_lm/1).  162
  163% !! Remember: Any changes to the following list should be carefully
  164%              reflected in     clearEnvironment
  165%                       and     saveEnvironment.
  166
  167% The following predicates belong to the translated terminologial 
  168% axioms.
  169:- multifile(in/9).  170:- dynamic(in/9).  171:- multifile(kb_in/10).  172:- dynamic(kb_in/10).  173:- multifile(eq/9).  174:- dynamic(eq/9).  175:- multifile(constraint/8).  176:- dynamic(constraint/8).  177:- multifile(rel/5).  178:- dynamic(rel/5).  179% The following predicates are used for additional informations about
  180% the terminology and the world description.
  181:- multifile((attribute)/5).  182:- dynamic((attribute)/5).  183
  184:- multifile(axiom/3).  185:- dynamic(axiom/3).  186:- multifile(closed/5).  187:- dynamic(closed/5).  188:- multifile(compiledPredicate/2).  189:- dynamic(compiledPredicate/2).  190:- multifile(conceptElement/7).  191:- dynamic(conceptElement/7).  192:- multifile(conceptEqualSets/6).  193:- dynamic(conceptEqualSets/6).  194:- multifile(conceptHierarchy/3).  195:- dynamic(conceptHierarchy/3).  196:- multifile(conceptName/4).  197:- dynamic(conceptName/4).  198
  199
  200:- multifile(conceptName1/4).  201:- dynamic(conceptName1/4).  202
  203
  204:- multifile(conceptSubsets/6).  205:- dynamic(conceptSubsets/6).  206:- multifile(environment/3).  207:- dynamic(environment/3).  208:- multifile(given_change/4).  209:- dynamic(given_change/4).  210:- multifile(given_inflLink/4).  211:- dynamic(given_inflLink/4).  212:- multifile(modalAxioms/6).  213:- dynamic(modalAxioms/6).  214:- multifile(roleAttributes/5).  215:- dynamic(roleAttributes/5).  216:- multifile(roleDefault/4).  217:- dynamic(roleDefault/4).  218:- multifile(roleDefNr/4).  219:- dynamic(roleDefNr/4).  220:- multifile(roleDomain/4).  221:- dynamic(roleDomain/4).  222:- multifile(roleElement/8).  223:- dynamic(roleElement/8).  224:- multifile(roleEqualSets/6).  225:- dynamic(roleEqualSets/6).  226:- multifile(roleHierarchy/3).  227:- dynamic(roleHierarchy/3).  228:- multifile(roleName/4).  229:- dynamic(roleName/4).  230:- multifile(roleName1/4).  231:- dynamic(roleName1/4).  232:- multifile(roleNr/5).  233:- dynamic(roleNr/5).  234:- multifile(roleRange/4).  235:- dynamic(roleRange/4).  236:- multifile(roleSubsets/6).  237:- dynamic(roleSubsets/6).  238
  239:- multifile(sub/5).  240:- dynamic(sub/5).  241:- multifile(succ/5).  242:- dynamic(succ/5).  243:- multifile(nsub/5).  244:- dynamic(nsub/5).  245
  246% The following predicates are used during computations only.
  247:- multifile(abductiveDerivation/3).  248:- dynamic(abductiveDerivation/3).  249:- multifile(consistencyDerivation/3).  250:- dynamic(consistencyDerivation/3).  251:- multifile(hypothesis/1).  252:- dynamic(hypothesis/1).  253:- multifile(inconsistencyCheck/3).  254:- dynamic(inconsistencyCheck/3).  255:- multifile(motel_option/2).  256:- dynamic(motel_option/2).  257:- multifile(nsub3/2).  258:- dynamic(nsub3/2).  259:- multifile(sub3/2).  260:- dynamic(sub3/2).  261:- multifile(succ3/2).  262:- dynamic(succ3/2).  263%:- multifile(value/2).
  264%:- dynamic(value/2).
  265% Predicates which are no longer needed
  266%:- multifile(falsum/2).
  267%:- dynamic(falsum/2).
  268%:- multifile(numb/1).
  269%:- dynamic(numb/1).
  270:- op(1200,xfx,<==).  271
  272 :- meta_predicate doFileGoal(0).  273 :- meta_predicate performQuery(*,0,0).  274 :- meta_predicate setofOrNil(?,^,-).  275 :- meta_predicate bagofOrNil(?,^,-).  276 :- meta_predicate tryGoal(0).  277 :- meta_predicate doClashTest(0).  278 :- meta_predicate runTest(*,0).  279 :- meta_predicate mapGoal(0,*,*).  280 :- meta_predicate doboth(0,0).  281 :- meta_predicate assert2(0).  282 :- meta_predicate callList(0).  283 :- meta_predicate assert1(0).  284 :- meta_predicate try(0).  285 :- meta_predicate unexpand_role(*,*,0,*).  286
  287
  288:- multifile( clause/1).  289:- multifile( conceptElement/6).  290:- multifile( conceptEqualSets/5).  291:- multifile( conceptSubsets/5).  292:- multifile( constructEqHead/20).  293:- multifile( constructMLHead/20).  294:- multifile( cont1a/5).  295:- multifile( convertInConsequence/10).  296:- multifile( default_change/3).  297:- multifile( eq/19).  298:- multifile( noDouble/2).  299:- multifile( prolog_flag/3).  300:- multifile( roleAll/9).  301:- multifile( roleEqualSets/5).  302:- multifile( roleName1/4).  303:- multifile( roleSubsets/5).  304:- multifile( roleTripel/6).  305
  306:- dynamic( clause/1).  307:- dynamic( conceptElement/6).  308:- dynamic( conceptSubsets/5).  309
  310:- dynamic( conceptEqualSets/5).  311:- dynamic( conceptSubsets/4).  312
  313
  314:- dynamic( constructEqHead/20).  315:- dynamic( constructMLHead/20).  316:- dynamic( cont1a/5).  317:- dynamic( convertInConsequence/10).  318:- dynamic( default_change/3).  319:- dynamic( eq/19).  320:- dynamic( noDouble/2).  321
  322:- dynamic( roleAll/9).  323:- dynamic( roleEqualSets/5).  324:- dynamic( roleName1/4).  325:- dynamic( roleSubsets/5).  326:- dynamic( roleTripel/6).  327
  328/**********************************************************************
  329 *
  330 * @(#) sets.pl 1.1@(#)
  331 *
  332 */
  333
  334%   member(?Element, ?Set)
  335%   is true when Set is a list, and Element occurs in it.  It may be used
  336%   to test for an element or to enumerate all the elements by backtracking.
  337%   Indeed, it may be used to generate the Set!
  338/*
  339member(X, [X|_]    ).
  340member(X, [_,X|_]  ).
  341member(X, [_,_,X|_]).
  342member(X, [_,_,_|L]) :-
  343        member(X, L).
  344*/
  345%   reverseList(+List1,-List2
  346%   reverses the list List1 to get List2
  347
  348reverseList([],[]) :- !.
  349reverseList([H|T],L2) :-
  350	reverseList(T,L1),
  351	append(L1,[H],L2),
  352	!.
  353
  354%   memberchk(+Element, +Set)
  355%   means the same thing, but may only be used to test whether a known
  356%   Element occurs in a known Set.  In return for this limited use, it
  357%   is more efficient than member/2 when it is applicable.
  358/*
  359memberchk(X, L) :- 
  360	nonvar(X), 
  361	nonvar(L),
  362	memberchk1(X,L).
  363
  364memberchk1(X, [X|_]    ) :- !.
  365memberchk1(X, [_,X|_]  ) :- !.
  366memberchk1(X, [_,_,X|_]) :- !.
  367memberchk1(X, [_,_,_|L]) :-
  368	memberchk1(X, L).
  369*/
  370%   nonmember(+Element, +Set)
  371%   means that Element does not occur in Set.  It does not make sense
  372%   to instantiate Element in any way, as there are infinitely many
  373%   terms which do not occur in any given set.  Nor can we generate
  374%   Set; there are infinitely many sets not containing a given Element.
  375%   Read it as "the given Element does not occur in the given list Set".
  376%   This code was suggested by Bruce Hakami; seven versions of this
  377%   operation were benchmarked and this found to be the fastest.
  378%   The old code was for DEC-10 Prolog, which did not compile (\+)/1.
  379
  380nonmember(Element, Set) :-
  381	nonvar(Element),
  382	nonvar(Set),
  383	\+ (member(Element, Set)).
  384
  385%   intersection_motel(+Set1, +Set2, ?Intersection)
  386%   is true when all three arguments are lists representing sets,
  387%   and Intersection contains every element of Set1 which is also
  388%   an element of Set2, the order of elements in Intersection
  389%   being the same as in Set1.  That is, Intersection represents
  390%   the intersection_motel of the sets represented by Set1 and Set2.
  391%   If Set2 is a partial list, Intersection will be empty, which
  392%   is not, of course, correct.  If Set1 is a partial list, this
  393%   predicate will run away on backtracking.  Set1 and Set2 should
  394%   both be proper lists, but this is not checked.  Duplicates in
  395%   Set1 may survive in Intersection.  It is worthy of note that
  396%   if Set1 is an ordset, Intersection is an ordset, despite Set2.
  397
  398intersection_motel([], _, []).
  399intersection_motel([Element|Elements], Set, Intersection) :-
  400	memberchk(Element, Set),
  401	!,
  402	Intersection = [Element|Rest],
  403	intersection_motel(Elements, Set, Rest).
  404intersection_motel([_|Elements], Set, Intersection) :-
  405	intersection_motel(Elements, Set, Intersection).
  406
  407
  408
  409%   intersection_motel(+ListOfSets, ?Intersection)
  410%   is true when Intersection is the intersection_motel of all the sets in
  411%   ListOfSets.  The order of elements in Intersection is taken from
  412%   the first set in ListOfSets.  This has been turned inside out to
  413%   minimise the storage turnover.
  414
  415intersection_motel([Set|Sets], Intersection) :-
  416	intersection1(Set, Sets, Intersection).
  417
  418intersection1([], _, []).
  419intersection1([Element|Elements], Sets, Intersection) :-
  420	memberchk_all(Sets, Element),
  421	!,
  422	Intersection = [Element|Rest],
  423	intersection1(Elements, Sets, Rest).
  424intersection1([_|Elements], Sets, Intersection) :-
  425	intersection1(Elements, Sets, Intersection).
  426
  427memberchk_all([], _).
  428memberchk_all([Set|Sets], Element) :-
  429	memberchk(Element, Set),
  430	memberchk_all(Sets, Element).
  431
  432%   motel_subtract(+Set1, +Set2, ?Difference)
  433%   is like intersect, but this time it is the elements of Set1 which
  434%   *are* in Set2 that are deleted.  Note that duplicated Elements of
  435%   Set1 which are not in Set2 are retained in Difference.
  436
  437motel_subtract([], _, []).
  438motel_subtract([Element|Elements], Set, Difference) :-
  439	memberchk(Element, Set),
  440	!,
  441	motel_subtract(Elements, Set, Difference).
  442motel_subtract([Element|Elements], Set, [Element|Difference]) :-
  443	motel_subtract(Elements, Set, Difference).
  444
  445%   motel_union(+Set1, +Set2, ?Union)
  446%   is true when motel_subtract(Set1,Set2,Diff) and append(Diff,Set2,Union),
  447%   that is, when Union is the elements of Set1 that do not occur in
  448%   Set2, followed by all the elements of Set2.
  449
  450motel_union([], Union, Union).
  451motel_union([Element|Elements], Set, Union) :-
  452	memberchk(Element, Set),
  453	!,
  454	motel_union(Elements, Set, Union).
  455motel_union([Element|Elements], Set, [Element|Union]) :-
  456	motel_union(Elements, Set, Union).
  457
  458%   motel_union(+ListOfSets, ?Union)
  459%   is true when Union is the motel_union of all sets in ListOfSets.
  460
  461motel_union([],[]).
  462motel_union([Set1],Set1).
  463motel_union([Set1,Set2|Sets],Union) :-
  464	motel_union(Set1,Set2,Set),
  465	motel_union([Set|Sets],Union).
  466
  467
  468%   list_to_set(+List, ?Set)
  469%   is true when List and Set are lists, and Set has the same elements
  470%   as List in the same order, except that it contains no duplicates.
  471%   The two are thus equal considered as sets.  If you really want to
  472%   convert a list to a set, list_to_ord_set is faster, but this way
  473%   preserves as much of the original ordering as possible.
  474%   If List contains several copies of an element X, only the LAST
  475%   copy of X is retained.  If you want to convert a List to a Set,
  476%   retaining the FIRST copy of repeated elements, call
  477%	symdiff([], List, Set)
  478/*
  479list_to_set([], []).
  480list_to_set([Head|Tail], Set) :-
  481	memberchk(Head, Tail),
  482	!,
  483	list_to_set(Tail, Set).
  484list_to_set([Head|Tail], [Head|Set]) :-
  485	list_to_set(Tail, Set).
  486*/
  487
  488%   deleteInList(+List, +Kill, ?Residue)
  489%   is true when List is a list, in which Kill may or may not occur, and
  490%   Residue is a copy of List with all elements equal to Kill deleted.
  491%   To extract a single copy of Kill, use select(Kill, List, Residue).
  492%   If List is not proper, deleteInList/3 will FAIL.  Kill and the elements of
  493%   List should be sufficiently instantiated for \= to be sound.
  494
  495deleteInList(-, _, _) :- !, fail.		% reject partial lists
  496deleteInList([], _, []).
  497deleteInList([Kill|Tail], Kill, Residue) :- !,
  498	deleteInList(Tail, Kill, Residue).
  499deleteInList([Head|Tail], Kill, [Head|Residue]) :-
  500    %	Head \= Kill,
  501	deleteInList(Tail, Kill, Residue).
  502
  503
  504
  505motel_subset([],_S2) :- !.
  506motel_subset([E1|S1],S2) :-
  507	\+(\+(member(E1,S2))),
  508	motel_subset(S1,S2),
  509	!.
  510
  511equalset(S1,S2) :-
  512	motel_subset(S1,S2),
  513	motel_subset(S2,S1),
  514	!.
  515
  516
  517%----------------------------------------------------------------------	
  518%   Module : lists
  519%   Authors: Bob Welham, Lawrence Byrd, and Richard A. O'Keefe
  520%   Updated: 10/25/90
  521%   Defines: list processing utilities
  522%   SeeAlso: library(motel_flatten)
  523
  524%   Adapted from shared code written by the same authors; all changes
  525%   Copyright (C) 1987, Quintus Computer Systems, Inc.  All rights reserved.
  526
  527%   perm(+List, ?Perm)
  528%   is true when List and Perm are permutations of each other.  The main
  529%   use of perm/2 is to generate permutations.  You should not use this
  530%   predicate in new programs; use permutation_motel/2 instead.  List must be
  531%   a proper list.  Perm may be partly instantiated.
  532
  533perm([], []).
  534perm([X|Xs], Ys1) :-
  535	perm(Xs, Ys),
  536	motel_insert(Ys, X, Ys1).
  537
  538
  539motel_insert(L, X, [X|L]).
  540motel_insert([H|T], X, [H|L]) :-
  541	motel_insert(T, X, L).
  542
  543%   permutation_motel(?List, ?Perm)
  544%   is true when List and Perm are permuations of each other.
  545%   Unlike perm/2, it will work even when List is not a proper list.
  546%   It even acts in a marginally sensible way when Perm isn't proper
  547%   either, but it will still backtrack forever.
  548%   Be careful: this is quite efficient, but the number of permutations of an
  549%   N-element list is N!, and even for a 7-element list that is 5040.
  550
  551permutation_motel(List, Perm) :-
  552	permutation_motel(List, Perm, Perm).
  553
  554permutation_motel([], [], []).
  555permutation_motel([X|Xs], Ys1, [_|Zs]) :-
  556	permutation_motel(Xs, Ys, Zs),
  557	motel_insert(Ys, X, Ys1).
  558
  559
  560
  561/**********************************************************************
  562 *
  563 * @(#) lib.pl 1.7@(#)
  564 *
  565 */
  566
  567/* POPLOG PROLOG LIBRARIES
  568 */
  569
  570/* Library gensym
  571 * import:  gensym(_,_)
  572 */
  573
  574/* QUINTUS PROLOG LIBRARIES
  575 */
  576
  577/* Library strings
  578 * import:  gensym(_,_)
  579 */
  580
  581
  582/**********************************************************************
  583 *
  584 * COUNTER
  585 *
  586 */
  587
  588/**********************************************************************
  589 *
  590 * setCounter(+Counter,+Value)
  591 * creates a new counter Counter with value Value.
  592 *
  593 */
  594
  595setCounter(Counter,N) :- flag(Counter,_,N).
  596
  597/**********************************************************************
  598 *
  599 * addCounter(+Counter,+Value)
  600 * adds Value to the current value of counter Counter.
  601 *
  602 */
  603 
  604addCounter(Counter,N) :-  flag(Counter,M,M+N).
  605
  606/**********************************************************************
  607 *
  608 * getCounter(+Counter,-Value)
  609 * retrieves the current value Value of counter Counter.
  610 *
  611 */
  612
  613getCounter(Counter,N) :- flag(Counter,N,N).
  614      
  615
  616/**********************************************************************
  617 *
  618 * writes(+List)
  619 * put each character in List.
  620 *
  621 */
  622
  623writes([]) :- !.
  624writes([H|T]) :- put(H), writes(T).
  625
  626/***********************************************************************
  627 *
  628 * printTime(+G)
  629 * execute goal G and report the runtime the execution needed.
  630 * Only available for SICStus Prolog and Quintus Prolog.
  631 *
  632 */
  633
  634:- meta_predicate printTime(0).  635:- meta_predicate printTime(0,*).  636
  637printTime(G) :-
  638	(currentProlog(poplog) ; currentProlog(macprolog)),
  639	!,
  640	call(G),
  641	!.
  642printTime(G) :-
  643	!,
  644	getRuntime(T0),
  645	printTime(G,T0).
  646
  647printTime(G,T0) :-
  648	call(G),
  649	getRuntime(T1),
  650	T is T1 - T0,
  651	format('Total runtime ~3d sec.~n', [T]).
  652printTime(_,T0) :-
  653	getRuntime(T1),
  654	T is T1 - T0,
  655	format('Total runtime ~3d sec.~n', [T]),
  656	!,
  657	fail.
  658
  659/**********************************************************************
  660 *
  661 * simple_term(X) 
  662 * it contrast to the usage in the Quintus Prolog user manual we
  663 * call a term `simple` if it is either an atom or a variable.
  664 * This predicate succeeds iff X is a simple term in this sense.
  665 *
  666 */
  667
  668simple_term(X) :-
  669	var(X),
  670	!.
  671simple_term(X) :-
  672	atomic(X),
  673	!.
  674
  675/**********************************************************************
  676 *
  677 * LIBRARY HANDLING
  678 *
  679 */
  680
  681loadLibraries(sicstus) :-
  682	assertz_logged((gensym(Prefix, V) :-
  683	var(V),
  684	atomic(Prefix),
  685	(   retract(gensym_counter(Prefix, M))
  686	;   M = 0
  687	),
  688	N is M+1,
  689	asserta_logged(gensym_counter(Prefix, N)),
  690	name(Prefix,P1),
  691	name(N,N1),
  692	append(P1,N1,V1),
  693	name(V,V1),
  694	!)),
  695	assertz_logged((getTwoRandomNumbers(RT,CT) :-
  696	statistics(runtime,[RT,CT]))),
  697	assertz_logged((getRuntime(RT) :-
  698	statistics(runtime,[RT|_]))),
  699	assertz_logged((append([],L2,L2))),
  700	assertz_logged((append([A1|L1],L2,[A1|L3]) :-
  701	append(L1,L2,L3))),
  702	assertz_logged((not(Goal) :- call(\+ Goal))),
  703	assertz_logged((once(Goal) :- Goal, !)),
  704	assertz_logged((ask(A1) :- deduce(A1))),
  705	assertz_logged((ask(A1,A2) :- deduce(A1,A2))),
  706	assertz_logged((ask(A1,A2,A3) :- deduce(A1,A2,A3))),
  707	assertz_logged((ask(A1,A2,A3,A4) :- deduce(A1,A2,A3,A4))),
  708	assertz_logged((map(A1,A2,A3) :- hop_map(A1,A2,A3))),
  709	assertz_logged((map(A1,A2,A3,A4) :- hop_map(A1,A2,A3,A4))),
  710	!.
  711loadLibraries(eclipse) :-
  712	assertz_logged((gensym(Prefix, V) :-
  713	var(V),
  714	atomic(Prefix),
  715	(   retract(gensym_counter(Prefix, M))
  716	;   M = 0
  717	),
  718	N is M+1,
  719	asserta_logged(gensym_counter(Prefix, N)),
  720	name(Prefix,P1),
  721	name(N,N1),
  722	append(P1,N1,V1),
  723	name(V,V1),
  724	!)),
  725	assertz_logged((getTwoRandomNumbers(RT,CT) :-
  726	statistics(runtime,[RT,CT]))),
  727	assertz_logged((getRuntime(RT) :-
  728	statistics(runtime,[RT|_]))),
  729	assertz_logged((append([],L2,L2))),
  730	assertz_logged((append([A1|L1],L2,[A1|L3]) :-
  731	append(L1,L2,L3))),
  732	assertz_logged((ask(A1) :- deduce(A1))),
  733	assertz_logged((ask(A1,A2) :- deduce(A1,A2))),
  734	assertz_logged((ask(A1,A2,A3) :- deduce(A1,A2,A3))),
  735	assertz_logged((ask(A1,A2,A3,A4) :- deduce(A1,A2,A3,A4))),
  736	assertz_logged((map(A1,A2,A3) :- hop_map(A1,A2,A3))),
  737	assertz_logged((map(A1,A2,A3,A4) :- hop_map(A1,A2,A3,A4))),
  738	!.
  739loadLibraries(swiprolog) :-
  740	assertz_logged((ask(A1) :- deduce(A1))),
  741	assertz_logged((ask(A1,A2) :- deduce(A1,A2))),
  742	assertz_logged((ask(A1,A2,A3) :- deduce(A1,A2,A3))),
  743	assertz_logged((ask(A1,A2,A3,A4) :- deduce(A1,A2,A3,A4))),
  744	assertz_logged((map(A1,A2,A3) :- hop_map(A1,A2,A3))),
  745	assertz_logged((map(A1,A2,A3,A4) :- hop_map(A1,A2,A3,A4))),
  746	assertz_logged((portray(not(F)) :- display(not(F)))),
  747	assertz_logged((getTwoRandomNumbers(RT,CT) :-
  748	statistics(cputime,RT1), RT is (ceil(RT1 * 100000)) mod 100000, statistics(atoms,CT))),
  749	assertz_logged((getRuntime(RT) :-
  750	statistics(cputime,RT1), RT is ceil(RT1 * 1000))),
  751	%index(kb_in(1,0,0,0,1,1,0,0,0,0)),
  752	%index(eq(1,0,0,1,1,0,0,0,0)),
  753	%index(constraint(1,0,0,1,0,0,0,0)),
  754	assertz_logged((retractall_head(Head) :- retract(Head), fail)),
  755	assertz_logged((retractall_head(Head) :- retract((Head :- _Body)), fail)),
  756	assertz_logged((retractall_head(_))),
  757	!.
  758loadLibraries(poplog) :-
  759	op(600,xfy,':'),
  760	assertz_logged((gensym(Prefix, V) :-
  761	var(V),
  762	atomic(Prefix),
  763	(   retract(gensym_counter(Prefix, M))
  764	;   M = 0
  765	),
  766	N is M+1,
  767	asserta_logged(gensym_counter(Prefix, N)),
  768	name(Prefix,P1),
  769	name(N,N1),
  770	append(P1,N1,V1),
  771	name(V,V1),
  772	!)),
  773	assertz_logged((append([],L2,L2))),
  774	assertz_logged((append([A1|L1],L2,[A1|L3]) :-
  775	append(L1,L2,L3))),
  776	assertz_logged((ask(A1) :- deduce(A1))),
  777	assertz_logged((ask(A1,A2) :- deduce(A1,A2))),
  778	assertz_logged((ask(A1,A2,A3) :- deduce(A1,A2,A3))),
  779	assertz_logged((ask(A1,A2,A3,A4) :- deduce(A1,A2,A3,A4))),
  780	assertz_logged((map(A1,A2,A3) :- hop_map(A1,A2,A3))),
  781	assertz_logged((map(A1,A2,A3,A4) :- hop_map(A1,A2,A3,A4))),
  782	assertz_logged((once(Goal) :- Goal, !)),
  783	assertz_logged((saveMOTEL(F) :- save_program(F))),
  784	!.
  785loadLibraries(quintus) :-
  786	assertz_logged((gensym(Prefix, V) :-
  787	var(V),
  788	atomic(Prefix),
  789	(   retract(gensym_counter(Prefix, M))
  790	;   M = 0
  791	),
  792	N is M+1,
  793	asserta_logged(gensym_counter(Prefix, N)),
  794	name(Prefix,P1),
  795	name(N,N1),
  796	append(P1,N1,V1),
  797	name(V,V1),
  798	!)),
  799	assertz_logged((getTwoRandomNumbers(RT,CT) :-
  800	statistics(runtime,[RT,CT]))),
  801	assertz_logged((getRuntime(RT) :-
  802	statistics(runtime,[RT|_]))),
  803	assertz_logged((not(Goal) :- call(\+ Goal))),
  804	assertz_logged((once(Goal) :- Goal, !)),
  805	assertz_logged((ask(A1) :- deduce(A1))),
  806	assertz_logged((ask(A1,A2,A3,A4) :- deduce(A1,A2,A3,A4))),
  807	assertz_logged((ask(A1,A2) :- deduce(A1,A2))),
  808	assertz_logged((ask(A1,A2,A3) :- deduce(A1,A2,A3))),
  809	assertz_logged((map(A1,A2,A3) :- hop_map(A1,A2,A3))),
  810	assertz_logged((map(A1,A2,A3,A4) :- hop_map(A1,A2,A3,A4))),
  811	assertz_logged((saveMOTEL(F) :- save_program(F))),
  812	!.
  813loadLibraries(macprolog) :-
  814	op(600,xfy,':'),
  815	!.
  816
  817testForMacprolog(others) :-
  818	current_op(_X,_Y,':'),
  819	!,
  820	fail.
  821testForMacprolog(macprolog) :-
  822	unknown(_X,fail),
  823	!.
  824
  825getLibraries :-
  826	testForMacprolog(_),
  827	!,
  828	asserta_logged(currentProlog(macprolog)),
  829	version('MOTEL-0.4 Tue Aug 04 15:00:00 MET 1992'),
  830	loadLibraries(macprolog).
  831:- if(false).  832getLibraries :-
  833	current_op(1190,fx,delay),
  834	!,
  835	sicstus,
  836	asserta_logged(currentProlog(eclipse)),
  837	set_flag(variable_names,off),
  838	loadLibraries(eclipse).
  839:- endif.  840getLibraries :-
  841	current_op(_X,_Y,?),
  842	style_check(-singleton),
  843	!,
  844	asserta_logged(currentProlog(swiprolog)),
  845	style_check(-discontiguous),
  846	loadLibraries(swiprolog).
  847getLibraries :-
  848	setof((X,Y),prolog_flag(X,Y),L),
  849	member((single_var,_Z),L),
  850	!,
  851	asserta_logged(currentProlog(quintus)),
  852	version('MOTEL-0.4 Tue Aug 04 15:00:00 MET 1992'),
  853	prolog_flag(single_var,_,off),
  854	loadLibraries(quintus).
  855getLibraries :-
  856	prolog_flag(_X,_Y),
  857	!,
  858	asserta_logged(currentProlog(sicstus)),
  859	version('MOTEL-0.4 Tue Aug 04 15:00:00 MET 1992'),
  860	prolog_flag(single_var_warnings,_,off),
  861	prolog_flag(compiling,_,fastcode),
  862	prolog_flag(unknown,_,fail),
  863%	asserta_logged(foreign_file('int.o',[int_init])),
  864%	asserta_logged(foreign(int_init,int_init)),
  865%	load_foreign_files(['int.o'],[]),
  866%	int_init,
  867	loadLibraries(sicstus).
  868getLibraries :-
  869	tell('/tmp/v1'), version, told,
  870	!,
  871	asserta_logged(currentProlog(poplog)),
  872	version('MOTEL-0.4 Tue Aug 04 15:00:00 MET 1992'),
  873	loadLibraries(poplog).
  874
  875/**********************************************************************
  876 *
  877 * OPTIONS
  878 *
  879 */
  880
  881/***********************************************************************
  882 *
  883 * setOption(+Option,+Set)
  884 * set motel_option Option to value Set.
  885 *
  886 */
  887
  888setOption(Option,Set) :-
  889	retractall_head(motel_option(Option,_)),
  890	asserta_logged(motel_option(Option,Set)),
  891	!.
  892
  893/**********************************************************************
  894 *
  895 * ifOption(+Option,+Set,+Goal)
  896 * executes Goal if the current value of Option is Set otherwise
  897 * the predicate suceeds.
  898 *
  899 */
  900
  901:- meta_predicate ifOption(*,*,0).  902ifOption(Option,Set,Goal) :-
  903	motel_option(Option,Set),
  904	call(Goal),
  905	!.
  906ifOption(_,_,_) :-
  907	!.
  908
  909retractallEnv(Env,Pred/Arity) :-
  910	constructHead(Env,Pred/Arity,Head),
  911	retractall_head(Head), 
  912	!.
  913
  914% :- ensure_loaded('/opt/PrologMUD/pack/logicmoo_base/prolog/logicmoo_utils').
  915
  916:- getLibraries.  917
  918
  919
  920
  921% Copyright (C) 1993 Patrick Brandmeier
  922%                    Ullrich Hustadt
  923%                    Renate  Schmidt
  924%                    Jan     Timm
  925
  926% This file is part of the MOTEL distribution.
  927
  928% MOTEL is free software; you can redistribute it and/or modify
  929% it under the terms of the GNU General Public License as published by
  930% the Free Software Foundation; either version 1, or (at your motel_option)
  931% any later version.
  932
  933% MOTEL is distributed in the hope that it will be useful,
  934% but WITHOUT ANY WARRANTY; without even the implied warranty of
  935% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  936% GNU General Public License for more details.
  937/**********************************************************************
  938 *
  939 * @(#) abduction.pl 1.2@(#)
  940 *
  941 */
  942
  943getAbductionHyps(L,[]) :-
  944	var(L),
  945	!.
  946getAbductionHyps([],[]) :-
  947	!.
  948getAbductionHyps([in(Env,RN,modal(MS),C,X,A1,A2,A3,A4)|L1],
  949	[in(Env,RN,modal(MS),C,X,A1,A2,A3,A4)|L2]) :-
  950	!,
  951	getAbductionHyps(L1,L2).
  952
  953doMinimalityCheck(GL1,[in(Env,RN,modal(MS),C,X,_A1,_A2,_A3,_A4)|GL2]) :-
  954	append(GL1,GL2,GL),
  955	HYPS = [or(GL),rl([]),fl(H3)],
  956	constructMLCall(Env,rn(_AX3,_RN3,_S3,_O3),bodyMC(MS),headMC(MS),
  957	                C,X,HYPS,[],CALLS,_PT35,Goal),
  958	not(Goal),
  959	doMinimalityCheck([in(Env,RN,modal(MS),C,X,_A1,_A2,_A3,_A4)|GL1],
  960	                   GL2),
  961	!.
  962doMinimalityCheck(_GL1,[]) :-
  963	!.
  964	
  965
  966doConsistencyCheck(GL1,[in(Env,RN,modal(MS),C,X,_A1,_A2,_A3,_A4)|GL2]) :-
  967	append(GL1,GL2,GL),
  968	HYPS = [or(GL),rl([]),fl(H3)],
  969	normalizeNot(not(C),C1),
  970	constructMLCall(Env,rn(_AX3,_RN3,_S3,_O3),bodyMC(MS),headMC(MS),
  971	                C1,X,HYPS,[],CALLS,_PT35,Goal),
  972	not(Goal),
  973	doConsistencyCheck([in(Env,RN,modal(MS),C,X,_A1,_A2,_A3,_A4)|GL1],
  974	                   GL2),
  975	!.
  976doConsistencyCheck(_GL1,[]) :-
  977	!.
  978	
  979
  980	
  981/**********************************************************************
  982 *
  983 * @(#) callStack.pl 1.4@(#)
  984 *
  985 */
  986
  987/**********************************************************************
  988 *
  989 * THE CALL STACK 
  990 * is a list of elements of the following form:
  991 * - true 
  992 * - in(rn(AX,RN,_,_),modal(MS),C,X,hyp(HYPS))
  993 * - eq(rn(AX,RN,_,_),modal(MS),X,Y,hyp(HYPS))
  994 * - constraint(rn(AX,RN,_,_),MS,(card,app(_F:R,X),Rel,N),hyp(HYPS))
  995 *
  996 */
  997
  998/***********************************************************************
  999 * 
 1000 * cCS(+CallStack,Call)
 1001 * succeeds if the 'top' call on CallStack is not already contained
 1002 * elsewhere in CallStack and Call is not already contained in CallStack.
 1003 * This predicate is used to prevent nontermination.
 1004 *
 1005 */
 1006
 1007cCS([],_) :- !.
 1008cCS(IL,A1) :-
 1009%	print('trying '), print(A1), nl,
 1010	noAxiom(A1,IL),
 1011%	IL = [I1|IL1],
 1012	noDouble(IL),
 1013%	printAxiom(A1), nl,
 1014%	print('------------------------------------------------------------'),
 1015%	nl,
 1016	not(clashCS([A1|IL])),
 1017	!.
 1018
 1019testEqualAbductiveHypotheses(D1,D2) :-
 1020	currentEnvironment(Env),
 1021	abductiveDerivation(Env,D1,HL1),
 1022	abductiveDerivation(Env,D2,HL2),
 1023	!,
 1024	equalset(HL1,HL2),
 1025	!.
 1026testEqualAbductiveHypotheses(_D1,_D2) :-
 1027	!.
 1028
 1029testEqualHypotheses(H1,H2) :-
 1030%	equalset(H1,H2),
 1031	!.
 1032
 1033% To prove in(C,X) it is not allowed to use another in-clause generated 
 1034% from the same axiom
 1035sameAxiom(AX,_RN1,MS1,in(C1,X1),HYPS1,D1,
 1036          in(rn(AX,_RN2,_,_),modal(MS2),C2,X2,hyp(HYPS2),ab(D2))) :- 
 1037	not(not(X1 = X2)),
 1038	not(not(C1 = C2)),
 1039	testEqualAbductiveHypotheses(D1,D2),
 1040	testEqualHypotheses(HYPS1,HYPS2),
 1041	equalWorlds(MS1,MS2), !.
 1042sameAxiom(AX,_RN1,MS1,in(_C,X1),HYPS1,_D1,
 1043          eq(rn(AX,_RN2,_,_),modal(MS2),X2,_,hyp(HYPS2))) :- 
 1044	nonvar(X1),
 1045	nonvar(X2),
 1046	not(not(X1 = X2)),
 1047	testEqualHypotheses(HYPS1,HYPS2),
 1048	equalWorlds(MS1,MS2), !.
 1049sameAxiom(AX,_RN1,MS1,in(_C,X1),HYPS1,_D1,
 1050          eq(rn(AX,_RN2,_,_),modal(MS2),_,X2,hyp(HYPS2))) :- 
 1051	not(not(X1 = X2)),
 1052	testEqualHypotheses(HYPS1,HYPS2),
 1053	equalWorlds(MS1,MS2), !.
 1054% To prove in(C,X) it is not allowed to use a constraint-clause generated 
 1055% from the same axiom
 1056sameAxiom(AX,_RN1,MS1,in(_C,X1),HYPS1,_D1,
 1057          constraint(rn(AX,_RN2,_,_),MS2,(card,app(_,X2),_Rel,_N),hyp(HYPS2))) :- 
 1058	not(not(X1 = X2)),
 1059	testEqualHypotheses(HYPS1,HYPS2),
 1060	equalWorlds(MS1,MS2), !.
 1061sameAxiom(AX,_RN1,MS1,in(_C,X1),HYPS1,_D1,
 1062          solveConstraint(rn(AX,_RN2,_,_),MS2,(card,app(_,X2),_Rel,_N),hyp(HYPS2))) :- 
 1063	not(not(X1 = X2)),
 1064	testEqualHypotheses(HYPS1,HYPS2),
 1065	equalWorlds(MS1,MS2), !.
 1066sameAxiom(AX,_RN1,MS1,eq(X1,_Y),HYPS1,_D1,
 1067          in(rn(AX,_RN2,_,_),modal(MS2),_C,X2,hyp(HYPS2),ab(_D2))) :- 
 1068	nonvar(X1),
 1069	nonvar(X2),
 1070	not(not(X1 = X2)),
 1071	testEqualHypotheses(HYPS1,HYPS2),
 1072	equalWorlds(MS1,MS2), !.
 1073sameAxiom(AX,_RN1,MS1,eq(_X,Y1),HYPS1,_D1,
 1074          in(rn(AX,_RN2,_,_),modal(MS2),_C,Y2,hyp(HYPS2),ab(_D2))) :- 
 1075	not(not(Y1 = Y2)),
 1076	testEqualHypotheses(HYPS1,HYPS2),
 1077	equalWorlds(MS1,MS2), !.
 1078sameAxiom(AX,_RN1,MS1,eq(X1,Y1),HYPS1,_D1,
 1079          eq(rn(AX,_RN2,_,_),modal(MS2),X2,Y2,hyp(HYPS2))) :- 
 1080	not(not(X1 = X2)),
 1081	not(not(Y1 = Y2)),
 1082	testEqualHypotheses(HYPS1,HYPS2),
 1083	equalWorlds(MS1,MS2), !.
 1084sameAxiom(AX,_RN1,MS1,eq(X1,app(_Y1)),HYPS1,_D1,
 1085          constraint(rn(AX,_RN2,_,_),MS2,(card,app(_,X2),_Rel,_N),hyp(HYPS2))) :-   
 1086	not(not(X1 = X2)),
 1087	testEqualHypotheses(HYPS1,HYPS2),
 1088	equalWorlds(MS1,MS2), 
 1089	!.
 1090sameAxiom(AX,_RN1,MS1,eq(_X,app(_F1:R1,Y1)),HYPS1,_D1,
 1091          constraint(rn(AX,_RN2,_,_),MS2,(card,app(_F2:R2,Y2),_Rel,_N),hyp(HYPS2))) :-
 1092	not(not(R1 = R2)),
 1093	not(not(Y1 = Y2)),
 1094	testEqualHypotheses(HYPS1,HYPS2),
 1095	equalWorlds(MS1,MS2), !.
 1096sameAxiom(AX,_RN1,MS1,eq(X1,app(_Y1)),HYPS1,_D1,
 1097          solveConstraint(rn(AX,_RN2,_,_),MS2,(card,app(_,X2),_Rel,_N),hyp(HYPS2))) :-   
 1098	not(not(X1 = X2)),
 1099	testEqualHypotheses(HYPS1,HYPS2),
 1100	equalWorlds(MS1,MS2), 
 1101	!.
 1102sameAxiom(AX,_RN1,MS1,eq(_X,app(_F1:R1,Y1)),HYPS1,_D1,
 1103          solveConstraint(rn(AX,_RN2,_,_),MS2,(card,app(_F2:R2,Y2),_Rel,_N),hyp(HYPS2))) :-
 1104	not(not(R1 = R2)),
 1105	not(not(Y1 = Y2)),
 1106	testEqualHypotheses(HYPS1,HYPS2),
 1107	equalWorlds(MS1,MS2), !.
 1108sameAxiom(AX,_RN1,MS1,eq(_X,Y1),HYPS1,_D1,
 1109          solveConstraint(rn(AX,_RN2,_,_),MS2,(card,app(_,Y2),_Rel,_N),hyp(HYPS2))) :-
 1110	not(not(Y1 = Y2)),
 1111	testEqualHypotheses(HYPS1,HYPS2),
 1112	equalWorlds(MS1,MS2), !.
 1113sameAxiom(AX,_RN1,MS1,c(X1,_,_),HYPS1,_D1,
 1114          in(rn(AX,_RN2,_,_),modal(MS2),_,X2,hyp(HYPS2),ab(_D2))) :- 
 1115	not(not(X1 = X2)),
 1116	testEqualHypotheses(HYPS1,HYPS2),
 1117	equalWorlds(MS1,MS2), !.
 1118sameAxiom(AX,_RN1,MS1,c(X1,_,_),HYPS1,_D1,
 1119          eq(rn(AX,_RN2,_,_),modal(MS2),X2,_,hyp(HYPS2))) :- 
 1120	not(not(X1 = X2)),
 1121	testEqualHypotheses(HYPS1,HYPS2),
 1122	equalWorlds(MS1,MS2), !.
 1123sameAxiom(AX,_RN1,MS1,c(X1,_,_),HYPS1,_D1,
 1124          eq(rn(AX,_RN2,_,_),modal(MS2),X2,_,hyp(HYPS2))) :- 
 1125	not(not(X1 = X2)),
 1126	testEqualHypotheses(HYPS1,HYPS2),
 1127	equalWorlds(MS1,MS2), !.
 1128sameAxiom(AX,_RN1,MS1,c(X1,_,_),HYPS1,_D1,
 1129          eq(rn(AX,_RN2,_,_),modal(MS2),_,app(_,X2),hyp(HYPS2))) :- 
 1130	not(not(X1 = X2)),
 1131	testEqualHypotheses(HYPS1,HYPS2),
 1132	equalWorlds(MS1,MS2), !.
 1133sameAxiom(AX,_RN1,MS1,c(X1,R1,Rel1),HYPS1,_D1,
 1134          constraint(rn(AX,_RN2,_,_),MS2,(card,app(_F:R2,X2),Rel2,_N),hyp(HYPS2))) :- 
 1135	not(not(X1 = X2)),
 1136	not(not(R1 = R2)),
 1137	not(not(Rel1 = Rel2)),
 1138	testEqualHypotheses(HYPS1,HYPS2),
 1139	equalWorlds(MS1,MS2), !.
 1140sameAxiom(AX,_RN1,MS1,c(X1,R1,Rel1),HYPS1,_D1,
 1141          solveConstraint(rn(AX,_RN2,_,_),MS2,(card,app(_F:R2,X2),Rel2,_N),hyp(HYPS2))) :- 
 1142	not(not(X1 = X2)),
 1143	not(not(R1 = R2)),
 1144	not(not(Rel1 = Rel2)),
 1145	testEqualHypotheses(HYPS1,HYPS2),
 1146	equalWorlds(MS1,MS2), !.
 1147sameAxiom(AX,_RN1,MS1,sc(X1,_,_),HYPS1,_D1,
 1148          in(rn(AX,_RN2,_,_),modal(MS2),_,X2,hyp(HYPS2),ab(_D2))) :- 
 1149	not(not(X1 = X2)),
 1150	testEqualHypotheses(HYPS1,HYPS2),
 1151	equalWorlds(MS1,MS2), !.
 1152sameAxiom(AX,_RN1,MS1,sc(X1,_,_),HYPS1,_D1,
 1153          eq(rn(AX,_RN2,_,_),modal(MS2),X2,_,hyp(HYPS2))) :- 
 1154	not(not(X1 = X2)),
 1155	testEqualHypotheses(HYPS1,HYPS2),
 1156	equalWorlds(MS1,MS2), !.
 1157sameAxiom(AX,_RN1,MS1,sc(X1,_,_),HYPS1,_D1,
 1158          eq(rn(AX,_RN2,_,_),modal(MS2),X2,_,hyp(HYPS2))) :- 
 1159	not(not(X1 = X2)),
 1160	testEqualHypotheses(HYPS1,HYPS2),
 1161	equalWorlds(MS1,MS2), !.
 1162sameAxiom(AX,_RN1,MS1,sc(X1,_,_),HYPS1,_D1,
 1163          eq(rn(AX,_RN2,_,_),modal(MS2),_,app(_,X2),hyp(HYPS2))) :- 
 1164	not(not(X1 = X2)),
 1165	testEqualHypotheses(HYPS1,HYPS2),
 1166	equalWorlds(MS1,MS2), !.
 1167sameAxiom(AX,_RN1,MS1,sc(X1,R1,Rel1),HYPS1,_D1,
 1168          constraint(rn(AX,_RN2,_,_),MS2,(card,app(_F:R2,X2),Rel2,_N),hyp(HYPS2))) :- 
 1169	not(not(X1 = X2)),
 1170	not(not(R1 = R2)),
 1171	not(not(Rel1 = Rel2)),
 1172	testEqualHypotheses(HYPS1,HYPS2),
 1173	equalWorlds(MS1,MS2), !.
 1174sameAxiom(AX,_RN1,MS1,sc(X1,R1,Rel1),HYPS1,_D1,
 1175          solveConstraint(rn(AX,_RN2,_,_),MS2,(card,app(_F:R2,X2),Rel2,_N),hyp(HYPS2))) :- 
 1176	not(not(X1 = X2)),
 1177	not(not(R1 = R2)),
 1178	not(not(Rel1 = Rel2)),
 1179	testEqualHypotheses(HYPS1,HYPS2),
 1180	equalWorlds(MS1,MS2), !.
 1181sameAxiom(_AX,_,_,_,_,_,_) :- !, fail.
 1182
 1183equalWorlds(W1,W2) :-
 1184	var(W1),
 1185	var(W2),
 1186	!.
 1187equalWorlds(W1,W2) :-
 1188	var(W1),
 1189	nonvar(W2),
 1190	!,
 1191	fail.
 1192equalWorlds(W1,W2) :-
 1193	var(W2),
 1194	nonvar(W1),
 1195	!,
 1196	fail.
 1197equalWorlds([],[]) :-
 1198	!.
 1199equalWorlds(app(_F:m(_MOp,_A),_W1),[]) :-
 1200	!,
 1201	fail.
 1202equalWorlds([],app(_F:m(_MOp,_A),_W2)) :-
 1203	!,
 1204	fail.
 1205equalWorlds(app(F1:m(MOp,A1),W1),app(F2:m(MOp,A2),W2)) :-
 1206	A1 == A2,
 1207%	var(F1),
 1208%	var(F2),
 1209%       not(not(F1 = F2)),
 1210	!,
 1211	equalWorlds(W1,W2).
 1212equalWorlds(app(F1:m(MOp,A1),_W1),app(F2:m(MOp,A2),_W2)) :-
 1213	A1 == A2,
 1214	nonvar(F1),
 1215	nonvar(F2),
 1216	not(not(F1 = F2)),
 1217	!.
 1218equalWorlds(_W1,_W2) :-
 1219	!,
 1220	fail.
 1221	
 1222
 1223noAxiom(true,_) :- !.
 1224noAxiom(_,[]) :- !.
 1225noAxiom(in(rn(AX,RN,_,_),modal(MS),C,X,hyp(HYPS),ab(D)),[C1|CL]) :-
 1226	not(sameAxiom(AX,RN,MS,in(C,X),HYPS,D,C1)),
 1227	noAxiom(in(rn(AX,RN,_,_),modal(MS),C,X,hyp(HYPS),ab(D)),CL).
 1228noAxiom(eq(rn(AX,RN,_,_),modal(MS),X,Y,hyp(HYPS)),[C1|CL]) :-
 1229	not(sameAxiom(AX,RN,MS,eq(X,Y),HYPS,_D,C1)),
 1230	noAxiom(eq(rn(AX,RN,_,_),modal(MS),X,Y,hyp(HYPS)),CL).
 1231noAxiom(constraint(rn(AX,RN,_,_),MS,(card,app(_F:R,X),Rel,N),hyp(HYPS)),[C1|CL]) :-
 1232	not(sameAxiom(AX,RN,MS,c(X,R,Rel),HYPS,_D,C1)),
 1233	noAxiom(constraint(rn(AX,RN,_,_),MS,(card,app(_,X),Rel,N),hyp(HYPS)),CL).
 1234noAxiom(solveConstraint(rn(AX,RN,_,_),MS,(card,app(_F:R,X),Rel,N),hyp(HYPS)),[C1|CL]) :-
 1235	not(sameAxiom(AX,RN,MS,sc(X,R,Rel),HYPS,_D,C1)),
 1236	noAxiom(solveConstraint(rn(AX,RN,_,_),MS,(card,app(_,X),Rel,N),hyp(HYPS)),CL).
 1237
 1238noDouble([in(rn(AX,RN,_,_),modal(MS),not(C),X,hyp(_HYPS1),ab(D))|IL]) :-
 1239	!,
 1240	not(member(in(rn(AX1,RN1,_,_),modal(MS),not(C),X,hyp(_HYPS2),ab(D)),IL)),
 1241	not(member(in(rn(AX2,RN2,_,_),modal(MS),C,X,hyps(_HYPS3),ab(noAb)),IL)),
 1242	!,
 1243	noDouble(IL).
 1244noDouble([in(rn(AX,RN,_,_),modal(MS),C,X,hyp(_HYPS1),ab(D))|IL]) :-
 1245	!,
 1246	not(member(in(rn(AX1,RN1,_,_),modal(MS),C,X,hyp(_HYPS2),ab(D)),IL)),
 1247	not(member(in(rn(AX2,RN2,_,_),modal(MS),not(C),X,hyps(_HYPS3),ab(noAb)),IL)),
 1248	!,
 1249	noDouble(IL).
 1250noDouble([eq(rn(AX,RN,_,_),modal(MS),X,Y,hyp(_HYPS1))|IL]) :-
 1251	!,
 1252	not(member(eq(rn(AX1,RN1,_,_),modal(MS),X,Y,hyp(_HYPS2)),IL)),
 1253	!,
 1254	noDouble(IL).
 1255noDouble([constraint(rn(AX,RN,_,_),MS,(card,app(_F:R,X),Rel,N),hyp(_HYPS1))|IL]) :-
 1256	!,
 1257	not(member(constraint(rn(AX1,RN1,_,_),MS,(card,app(_F:R,X),Rel,N),hyp(_HYPS2)),IL)),
 1258	!,
 1259	noDouble(IL).
 1260noDouble([solveConstraint(rn(AX,RN,_,_),MS,(card,app(_F:R,X),Rel,N),hyp(_HYPS1))|IL]) :-
 1261	!,
 1262	not(member(solveConstraint(rn(AX1,RN1,_,_),MS,(card,app(_F:R,X),Rel,N),hyp(_HYPS2)),IL)),
 1263	!,
 1264	noDouble(IL).
 1265noDouble([I1|IL]) :-
 1266	not(member(I1,IL)),
 1267	!,
 1268	noDouble(I1,IL).
 1269noDouble([]) :-
 1270	!.
 1271
 1272printAxiom(solveConstraint(MS,(card,app((_FF:R),X),Rel,N),hyp(HYPS))) :-
 1273	print('axiom???'),
 1274	print('   '),
 1275	print(solveConstraint(MS,(app(R,X),Rel,N),hyp(HYPS))),
 1276	!.
 1277printAxiom(eq(rn(AX,RN,_,_),modal(MS),Y,app((_FF:R),X),hyp(HYPS))) :-
 1278	print(rn(AX,RN)),
 1279	print('   '),
 1280	print(eq(MS,Y,app(R,X),hyp(HYPS))),
 1281	!.
 1282printAxiom(in(rn(AX,RN,_,_),modal(_MS),CN,CON,hyp(HYP))) :-
 1283	print(rn(AX,RN)),
 1284	print('   '),
 1285	print(in(CN,CON,hyp(HYP))),
 1286	!.
 1287printAxiom(constraint(rn(AX,RN,_,_),MS,(card,app((_FF:R),X),Rel,N),hyp(HYPS))) :-
 1288	print(rn(AX,RN)),
 1289	print('   '),
 1290	print(constraint(MS,(app(R,X),Rel,N),hyp(HYPS))),
 1291	!.
 1292printAxiom(true) :-
 1293	!.
 1294
 1295/**********************************************************************
 1296 *
 1297 * clashCS(+CL)
 1298 * succeeds if CL is a clash, i.e. it obeys one of the following 
 1299 * conditions:
 1300 * - it contains in('bot',X) for some X.
 1301 * - it contains both in(A,X) and in(not(A),X) for some A and some X.
 1302 *
 1303 */
 1304
 1305last([],[],_) :-
 1306	!,
 1307	fail.
 1308last([L1],[],L1) :-
 1309	!.
 1310last([L1|LL1],[L1|LL2],Last) :-
 1311	last(LL1,LL2,Last),
 1312	!.
 1313
 1314generateClashGoal(CS1,Goal) :-
 1315	last(CS1,CS2,in(rn(AX,RN,S,O),modal(W1),C,X,hyp(HYPS))),
 1316	getCurrentEnvironment(EnvName),
 1317	environment(EnvName,Env,_),
 1318	constructMLHead(Env,rn(_AX1,_RN1,user,_O1),W1,C1,X,CS1,noAb,[],_,Goal),
 1319	!.
 1320
 1321		
 1322clashCS(CL) :-
 1323	retract(clashTest(possible)),
 1324	assertz_logged(clashTest(impossible)),
 1325	generateClashGoal(CL,Goal),
 1326	!,
 1327	doClashTest(Goal).
 1328clashCS(_CL) :-
 1329	!,
 1330	fail.
 1331	
 1332doClashTest(InHead1) :-
 1333	call(InHead1),
 1334	InHead1 = in(Env,_,modal(W1),C1,X,hyp(HYP),ab(_),call(_CALL),_),
 1335	atomic(X),
 1336	normalizeNot(not(C1),C2),
 1337	constructMLHead(Env,rn(_AX2,_RN2,_S2,_O2),W1,C2,X,HYP,noAb,[],_,InHead2),
 1338	call(InHead2),
 1339	print('Clash test succeeded for'), nl,
 1340	print(HYP), nl,
 1341	print('and'), nl,
 1342	print(InHead1), nl,
 1343	nl,
 1344	retract(clashTest(impossible)),
 1345	assertz_logged(clashTest(possible)),
 1346	!.
 1347doClashTest(Goal) :-
 1348	% the clash goal has failed, so there is no clash
 1349	print('Clash test succeeded for'), nl,
 1350	print(HYP), nl,
 1351	nl,
 1352	retract(clashTest(impossible)),
 1353	assertz_logged(clashTest(possible)),
 1354	!,
 1355	fail.
 1356
 1357
 1358
 1359
 1360% clashCS(CL) :-
 1361% 	clashTest(possible),
 1362% 	member(in(rn(_,_,_,_),modal(_MS),'bot',_X,hyp(_HYPS1)),CL),
 1363% 	!.
 1364% clashCS(CL) :-
 1365% 	clashTest(possible),
 1366% 	member(in(rn(_,_,_,_),modal(MS),not(A),X,hyp(_HYPS1)),CL),
 1367% 	member(in(rn(_,_,_,_),modal(MS),A,X,hyp(_HYPS2)),CL),
 1368% 	!.
 1369% clashCS(CL) :-
 1370% 	clashTest(possible),
 1371% 	member(constraint(rn(_,_,_,_),MS,
 1372% 			  (card,app(_F1:R,X),'>=',N1),hyp(_HYPS1)),CL),
 1373% 	member(constraint(rn(_,_,_,_),MS,
 1374% 			  (card,app(_F2:R,X),'=<',N2),hyp(_HYPS2)),CL),
 1375% 	number(N1),
 1376% 	number(N2),
 1377% 	N1 > N2,
 1378% 	!.
 1379% clashCS(CL) :-
 1380% 	member(constraint(rn(_,_,_,_),MS,
 1381% 			  (card,app(_F1:R,X),'=<',N1),hyp(_HYPS1)),CL),
 1382% 	number(N1),
 1383% 	countAllRoleFillersInCS(MS,R,X,CL,N2),
 1384% 	N2 > N1,
 1385% 	!.
 1386% 		
 1387% 		 
 1388% countAllRoleFillersInCS(MS,R,X,CL,N) :-
 1389% 	getAllRoleFillersInCS(MS,R,X,CL,[],RF),
 1390% 	length(RF,N).
 1391% 
 1392% getAllRoleFillersInCS(_MS,_R,_X,[],RF,RF) :-
 1393% 	!.
 1394% getAllRoleFillersInCS(MS,R,X,
 1395%    [eq(rn(_,_,_,_),modal(MS),Y,app(_F:R,X),hyp(_HYPS))|CL],RF1,RF2) :-
 1396% 	nonvar(Y),
 1397% 	nonvar(X),
 1398% 	atomic(Y),
 1399% 	not(member((X,Y),RF1)),
 1400% 	!,
 1401% 	getAllRoleFillersInCS(MS,R,CL,[(X,Y)|RF1],RF2).
 1402% getAllRoleFillersInCS(MS,R,X,[_|CL],RF1,RF2) :-
 1403% 	getAllRoleFillersInCS(MS,R,X,CL,RF1,RF2),
 1404% 	!.
 1405
 1406/**********************************************************************
 1407 *
 1408 * @(#) clash.pl 1.2@(#)
 1409 *
 1410 */
 1411
 1412clashInHyp(CL) :-
 1413	member(in(_,modal(_MS),'bot',_X,hyp(_HYPS1),ab(_)),CL),
 1414	!.
 1415clashInHyp(CL) :-
 1416	member(in(_N2,modal(MS2),A,X,hyp(_HYPS2),ab(_D2)),CL),
 1417	atomic(A),
 1418	member(in(_N1,modal(MS1),not(A),X,hyp(_HYPS1),ab(_D1)),CL),
 1419	not(not(MS1 = MS2)),
 1420	!.
 1421% clashInHyp(CL) :-
 1422% 	member(constraint(rn(_,_,_,_),MS,
 1423% 			  (card,app(_F1:R,X),'>=',N1),hyp(_HYPS1)),CL),
 1424% 	member(constraint(rn(_,_,_,_),MS,
 1425% 			  (card,app(_F2:R,X),'=<',N2),hyp(_HYPS2)),CL),
 1426% 	number(N1),
 1427% 	number(N2),
 1428% 	N1 > N2,
 1429% 	!.
 1430% clashInHyp(CL) :-
 1431% 	member(constraint(rn(_,_,_,_),MS,
 1432% 			  (card,app(_F1:R,X),'=<',N1),hyp(_HYPS1)),CL),
 1433% 	number(N1),
 1434% 	countAllRoleFillersInCS(MS,R,X,CL,N2),
 1435% 	N2 > N1,
 1436% 	!.
 1437% 		
 1438% 		 
 1439% countAllRoleFillersInCS(MS,R,X,CL,N) :-
 1440% 	getAllRoleFillersInCS(MS,R,X,CL,[],RF),
 1441% 	length(RF,N).
 1442% 
 1443% getAllRoleFillersInCS(_MS,_R,_X,[],RF,RF) :-
 1444% 	!.
 1445% getAllRoleFillersInCS(MS,R,X,
 1446%    [eq(rn(_,_,_,_),modal(MS),Y,app(_F:R,X),hyp(_HYPS))|CL],RF1,RF2) :-
 1447% 	nonvar(Y),
 1448% 	nonvar(X),
 1449% 	atomic(Y),
 1450% 	not(member((X,Y),RF1)),
 1451% 	!,
 1452% 	getAllRoleFillersInCS(MS,R,CL,[(X,Y)|RF1],RF2).
 1453% getAllRoleFillersInCS(MS,R,X,[_|CL],RF1,RF2) :-
 1454% 	getAllRoleFillersInCS(MS,R,X,CL,RF1,RF2),
 1455% 	!.
 1456% 
 1457/**********************************************************************
 1458 *
 1459 * @(#) classifier.pl 1.12@(#)
 1460 *
 1461 */
 1462
 1463/***********************************************************************
 1464 *
 1465 * subsumes(+Name1,+Name2)
 1466 * Parameter: Name1     concept or role name
 1467 *            Name2     concept or role name
 1468 * true iff Name1 subsumes Name2 in modal context []
 1469 * (so Name1 and Name2 must both be concept names or role names).
 1470 *
 1471 */
 1472
 1473subsumes(N1,N2) :-
 1474	getCurrentEnvironment(EnvName),
 1475	subsumes(EnvName,[],N1,N2).
 1476
 1477/***********************************************************************
 1478 *
 1479 * subsumes(+MS,+Name1,+Name2)
 1480 * Parameter: MS        modal context
 1481 *            Name1     concept or role name
 1482 *            Name2     concept or role name
 1483 * true iff Name1 subsumes Name2 (so Name1 and Name2 must both be
 1484 * concept names or role names).
 1485 *
 1486 */
 1487
 1488subsumes(MS,N1,N2) :-
 1489	nonvar(MS),
 1490	(MS = [] ; MS = [_|_]),
 1491	currentEnvironment(Env),
 1492	clause(conceptName(Env,_MS1,_W1,N1),_),
 1493	clause(conceptName(Env,_MS2,_W2,N2),_),
 1494	!,
 1495	subsumes(concepts,Env,MS,N1,N2).
 1496subsumes(MS,N1,N2) :-
 1497	nonvar(MS),
 1498	(MS = [] ; MS = [_|_]),
 1499	currentEnvironment(Env),
 1500	clause(roleName(Env,_MS1,_W1,N1),_),
 1501	clause(roleName(Env,_MS2,_W2,N2),_),
 1502	subsumes(roles,Env,MS,N1,N2).
 1503
 1504subsumes(EnvName,MS,N1,N2) :-
 1505	environment(EnvName,Env,_),
 1506	nonvar(MS),
 1507	(MS = [] ; MS = [_|_]),
 1508	clause(conceptName(Env,_MS1,_W1,N1),_),
 1509	clause(conceptName(Env,_MS2,_W2,N2),_),
 1510	!,
 1511	subsumes(concepts,Env,MS,N1,N2).
 1512subsumes(EnvName,MS,N1,N2) :-
 1513	environment(EnvName,Env,_),
 1514	nonvar(MS),
 1515	(MS = [] ; MS = [_|_]),
 1516	currentEnvironment(Env),
 1517	clause(roleName(Env,_MS1,_W1,N1),_),
 1518	clause(roleName(Env,_MS2,_W2,N2),_),
 1519	subsumes(roles,Env,MS,N1,N2).
 1520
 1521subsumes(concepts,Env,MS,C,D) :-
 1522	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 1523	constructMLHead(Env,_RN1,W1,D,aaa,_HYPS,noAb,_CALLS,abox,InHeadD),
 1524	asserta_logged((InHeadD :- call(G1))),
 1525 	getQuery(Env,W1,C,aaa,Exp,InHeadC),
 1526%	convertToGoal(Env,_RN2,MS,C,aaa,[or([]),rl([]),fl(_DML1)],noAb,[],
 1527%		      _PT2,InHeadC),
 1528	call((call(G1), InHeadC)),
 1529	retract((InHeadD :- _Body)).
 1530subsumes(concepts,Env,MS,_C,D) :-
 1531	convertMS(positive,Env,[[],true],MS,[],[W1,_G1],_),
 1532	constructMLHead(Env,_RN1,W1,D,aaa,_HYPS,noAb,_CALLS,abox,InHeadD),
 1533	retract((InHeadD :- _Body)),
 1534	!,
 1535	fail.
 1536subsumes(roles,Env,MS,R,S) :-
 1537	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 1538	gensym(mskolem,SF),
 1539	constructEqHead(Env,_RN1,W1,bbb,SF,S,aaa,_HYPS,noAb,_CALLS,abox,InHeadS),
 1540	asserta_logged((InHeadS :- call(G1))),
 1541	constructEqHead(Env,_RN2,W1,bbb,_FF,R,aaa,[or([]),rl([]),fl(_DML1)],
 1542			noAb,[],_PT2,InHeadR),
 1543	call((G1, InHeadR)),
 1544	retract((InHeadS :- _Body)).
 1545subsumes(roles,Env,MS,_R,S) :-
 1546	convertMS(positive,Env,[[],true],MS,[],[W1,_G1],_),
 1547	constructEqHead(Env,_RN2,W1,bbb,_FF,S,aaa,_HYPS,noAb,_CALLS,_,InHeadS),
 1548	retract((InHeadS :- _Body)),
 1549	!,
 1550	fail.
 1551
 1552/***********************************************************************
 1553 *
 1554 * classified(+MS,+Name)
 1555 * succeeds iff Name is already in the appropriate subsumption hierarchy 
 1556 * in modal context MS.
 1557 *
 1558 */
 1559
 1560classified(Env,MS,Concept) :-
 1561	clause(conceptName(Env,_MS1,_W1,Concept),_),
 1562	!,
 1563	conceptHierarchy(Env,MS,Tree),
 1564	search(Concept,Tree).
 1565classified(Env,MS,Role) :-
 1566	clause(roleName(Env,_MS1,_W1,Role),_),
 1567	roleHierarchy(Env,MS,Tree),
 1568	search(Role,Tree).
 1569
 1570
 1571search(Concept,node(CL,_NL)) :-
 1572	member(Concept,CL),
 1573	!.
 1574search(Concept,node(_CL,NL)) :-
 1575	searchSubtrees(Concept,NL),
 1576	!.
 1577
 1578searchSubtrees(_Concept,[]) :-
 1579	!,
 1580	fail.
 1581searchSubtrees(Concept,[N1|_]) :-
 1582	search(Concept,N1),
 1583	!.
 1584searchSubtrees(Concept,[_|NL]) :-
 1585	searchSubtrees(Concept,NL).
 1586
 1587
 1588search(Concept,node(CL,NL),[node(CL,NL)]) :-
 1589	member(Concept,CL),
 1590	!.
 1591search(Concept,node(_CL,NL),T1) :-
 1592	searchSubtrees(Concept,NL,T1),
 1593	!.
 1594
 1595searchSubtrees(_Concept,[],[]) :-
 1596	!.
 1597searchSubtrees(Concept,[N1|NL],T2) :-
 1598	search(Concept,N1,T1),
 1599	searchSubtrees(Concept,NL,TL),
 1600	append(T1,TL,T2),
 1601	!.
 1602
 1603/***********************************************************************
 1604 *
 1605 * classify
 1606 * compute the subsumption hierarchy 
 1607 * side effects: 
 1608 * asserts a clause
 1609 *               conceptHierarchy(MS,Tree)
 1610 * where Tree is a tree representation of the subsumption hierarchy.
 1611 * This is now done using the new classification algorithm written
 1612 * by Patrick Brandmeier.
 1613 *
 1614 */
 1615
 1616classify :-
 1617	newClassify.
 1618classify(Arg1) :-
 1619	newClassify(Arg1).
 1620classify(EnvName,MS) :-
 1621	newClassify(EnvName,MS).
 1622
 1623/***********************************************************************
 1624 *
 1625 * classify(+NewConcept)
 1626 * adds concept NewConcept to the subsumption hierarchy in the modal
 1627 * context [].
 1628 * side effects: 
 1629 * asserts a clause
 1630 *               conceptHierarchy([],Tree)
 1631 * or            roleHierachy([],Tree)
 1632 * where Tree is a tree representation of the subsumption hierarchy.
 1633 *
 1634 */
 1635
 1636classify(EnvName,NewConcept) :-
 1637	environment(EnvName,Env,_),
 1638	atomic(NewConcept),
 1639	clause(conceptName(Env,_MS1,_W2,NewConcept),_), % _MS1 might be [] ?
 1640	classify(concepts,[],NewConcept).
 1641classify(EnvName,NewRole) :-
 1642	environment(EnvName,Env,_),
 1643	atomic(NewRole),
 1644	clause(roleName(Env,_MS1,_W1,NewRole),_), % _MS1 might be [] ?
 1645	classify(roles,[],NewRole).
 1646
 1647
 1648/***********************************************************************
 1649 *
 1650 * oldClassify
 1651 * compute the subsumption hierarchy in the modal context MS
 1652 * side effects: 
 1653 * asserts a clause
 1654 *               conceptHierarchy(MS,Tree)
 1655 * where Tree is a tree representation of the subsumption hierarchy.
 1656 * This is the original classification algorithm written by 
 1657 * Ullrich Hustadt.
 1658 *
 1659 */
 1660
 1661oldClassify :-
 1662	getCurrentEnvironment(EnvName),
 1663	oldClassify(EnvName,[]).
 1664
 1665oldClassify(EnvName) :-
 1666	environment(EnvName,_Env,_),
 1667	!,
 1668	oldClassify(EnvName,[]).
 1669oldClassify(MS) :-
 1670	(MS = [] ; MS = [_|_]),
 1671	getCurrentEnvironment(EnvName),
 1672	oldClassify(EnvName,MS).
 1673
 1674oldClassify(EnvName,MS) :-
 1675	environment(EnvName,Env,_),
 1676	clause(conceptName(Env,MS,_,Concept),_),
 1677	once(classify(concepts,Env,MS,Concept)),
 1678	fail.
 1679oldClassify(EnvName,MS) :-
 1680	environment(EnvName,Env,_),
 1681	clause(roleName(Env,MS,_,Role),_),
 1682	once(classify(roles,Env,MS,Role)),
 1683	fail.
 1684oldClassify(_,_) :-
 1685	!.
 1686	
 1687
 1688/***********************************************************************
 1689 *
 1690 * classify(+MS,+NewConcept)
 1691 * adds concept NewConcept to the subsumption hierarchy in the modal
 1692 * context MS.
 1693 * side effects: 
 1694 * asserts a clause
 1695 *               conceptHierarchy(MS,Tree)
 1696 * or            roleHierarchy(MS,Tree)
 1697 * where Tree is a tree representation of the subsumption hierarchy.
 1698 *
 1699 */
 1700 
 1701classify(EnvName,MS,NewConcept) :-
 1702	environment(EnvName,Env,_),
 1703	clause(conceptName(Env,_MS1,_W1,NewConcept),_), % _MS1 might be MS ?
 1704	!,
 1705	classify(concepts,Env,MS,NewConcept).
 1706classify(EnvName,MS,NewRole) :-
 1707	environment(EnvName,Env,_),
 1708	clause(roleName(Env,_MS1,_W1,NewRole),_), % _MS1 might be MS ?
 1709	!,
 1710	classify(roles,Env,MS,NewRole).
 1711
 1712classify(concepts,Env,MS,NewConcept) :-
 1713	classified(Env,MS,NewConcept),
 1714	!.
 1715classify(roles,Env,MS,NewRole) :-
 1716	classified(Env,MS,NewRole),
 1717	!.
 1718classify(concepts,Env,MS,NewConcept) :-
 1719	retract(conceptHierarchy(Env,MS,OldTree)),
 1720	classify(concepts,Env,MS,NewConcept,OldTree,NewTree),
 1721	assertz_logged(conceptHierarchy(Env,MS,NewTree)).
 1722classify(roles,Env,MS,NewRole) :-
 1723	retract(roleHierarchy(Env,MS,OldTree)),
 1724	classify(roles,Env,MS,NewRole,OldTree,NewTree),
 1725	assertz_logged(roleHierarchy(Env,MS,NewTree)).
 1726
 1727classify(Type,Env,MS,NewConcept,OldTree,NewTree) :-
 1728	testForSubsumption(Type,Env,MS,NewConcept,OldTree,NewTree,_Judgement),
 1729	!.
 1730
 1731
 1732/***********************************************************************
 1733 *
 1734 * testForSubsumption(+Type,+MS,+NewConcept,+OldTree
 1735 *                    -NewTree,-Judgement)
 1736 * builds a tree representation NewTree of the subsumption hierarchy 
 1737 * Judgement has the following meaning:
 1738 * below  : NewConcept is below  the 'top' concept of OldTree
 1739 *          in this case NewTree is instantiated with the tree which
 1740 *          has NewConcept inserted in OldTree
 1741 * beside : NewConcept is beside the 'top' concept of OldTree
 1742 *          in this case NewTree is instantiated with the tree which
 1743 *          has NewConcept as 'top' concept and all concepts of OldTree
 1744 *          which are subsumed by NewConcept below it
 1745 * above  : NewConcept is above  the 'top' concept of OldTree
 1746 *          in this case NewTree is not instantiated
 1747 * in     : NewConcept is equivalent to the 'top' concept of OldTree
 1748 *          in this case NewTree is instantiated with the tree which
 1749 *          has NewConcept inserted in OldTree
 1750 *
 1751 */
 1752
 1753testForSubsumption(Type,Env,MS,NewConcept,node([ClassifiedConcept|CL],AL),NewTree,Judgement) :-
 1754	once(subsume2(Type,Env,MS,NewConcept,ClassifiedConcept)), 
 1755	testForEquivalence(Type,Env,MS,NewConcept,node([ClassifiedConcept|CL],AL),NewTree,Judgement),
 1756	!.
 1757testForSubsumption(Type,Env,MS,NewConcept,node([ClassifiedConcept|CL],AL),NewTree,below) :-
 1758	% to get here the subsumption test in the first clause
 1759        % must have failed
 1760	once(subsume2(Type,Env,MS,ClassifiedConcept,NewConcept)),
 1761	% so only x \in NewConcept        => x \in ClassifiedConcept
 1762	% but not x \in ClassifiedConcept => x \in NewConcept
 1763	tfsList1(Type,Env,MS,NewConcept,[ClassifiedConcept|CL],[],AL,
 1764                below([]),beside([]),above([]),NewTree),
 1765	!.
 1766testForSubsumption(Type,Env,MS,NewConcept,node([ClassifiedConcept|CL],AL),NewTree,beside) :-
 1767	% neither x \in NewConcept        => x \in ClassifiedConcept
 1768	% nor     x \in ClassifiedConcept => x \in NewConcept
 1769	tfsList2(Type,Env,MS,NewConcept,[ClassifiedConcept|CL],[],AL,
 1770                below([]),beside([]),above([]),NewTree),
 1771	!.
 1772
 1773tfsList1(_Type,_,_MS,NewConcept,N,_NL1,[],
 1774        below(NL3),beside(NL4),above(NL5),Tree) :-
 1775	buildTree1(NewConcept,N,below(NL3),beside(NL4),above(NL5),Tree),
 1776	!.
 1777tfsList1(Type,Env,MS,NewConcept,N,NL1,[Node1|NL2],
 1778        below(NL3),beside(NL4),above(NL5),NewTree) :-
 1779	testForSubsumption(Type,Env,MS,NewConcept,Node1,Tree,Judgement),
 1780	continue1(Type,Env,MS,NewConcept,N,NL1,[Node1|NL2],
 1781	         below(NL3),beside(NL4),above(NL5),Tree,Judgement,NewTree).
 1782
 1783buildTree1(NewConcept,N,below([]),beside(NL2),above(NL3),
 1784	node(N,[node([NewConcept],NL3)|NL2])) :- 
 1785	!.
 1786buildTree1(_NewConcept,N,below(NL1),beside(NL2),above(_),
 1787	node(N,NL)) :-
 1788	motel_union(NL1,NL2,NL),
 1789	!.
 1790buildTree1(_NewConcept,_N,_,_,_,_) :-
 1791	!,
 1792	fail.
 1793
 1794	
 1795continue1(Type,Env,MS,NewConcept,N,NL1,[Node1|NL2],
 1796         below(NL3),beside(NL4),above(NL5),Tree,below,NewTree) :-
 1797	% NL4 can be non-empty
 1798	% NL5 should be the empty list !
 1799	tfsList1(Type,Env,MS,NewConcept,N,[Node1|NL1],NL2,
 1800                below([Tree|NL3]),beside(NL4),above(NL5),NewTree),
 1801	!.
 1802continue1(Type,Env,MS,NewConcept,N,NL1,[Node1|NL2],
 1803         below(NL3),beside(NL4),above(NL5),
 1804         node([NewConcept],[]),beside,NewTree) :-
 1805	tfsList1(Type,Env,MS,NewConcept,N,[Node1|NL1],NL2,
 1806                below(NL3),beside([Node1|NL4]),above(NL5),NewTree),
 1807	!.
 1808continue1(Type,Env,MS,NewConcept,N,NL1,[Node1|NL2],
 1809         below(NL3),beside(NL4),above(NL5),
 1810         node([NewConcept],[N1|NL]),beside,NewTree) :-
 1811	motel_union(NL5,[N1|NL],NL6),
 1812	tfsList1(Type,Env,MS,NewConcept,N,[Node1|NL1],NL2,
 1813                below(NL3),beside([Node1|NL4]),above(NL6),NewTree),
 1814	!.
 1815continue1(Type,Env,MS,NewConcept,N,NL1,[Node1|NL2],
 1816         below(NL3),beside(NL4),above(NL5),_Tree,above,NewTree) :-
 1817	tfsList1(Type,Env,MS,NewConcept,N,[Node1|NL1],NL2,
 1818	        below(NL3),beside(NL4),above([Node1|NL5]),NewTree),
 1819	!.
 1820continue1(_Type,_,_MS,_NewConcept,N,NL1,[_Node1|NL2],
 1821         below(_NL3),beside(_NL4),above(_NL5),
 1822         Tree,in,node(N,NL)) :-
 1823        % NL3, NL4 and NL5 can be non-empty
 1824	reverseList(NL1,NL6),
 1825	motel_union(NL6,[Tree|NL2],NL),
 1826	!.
 1827
 1828tfsList2(_Type,_,_MS,NewConcept,N,_NL1,[],
 1829        below(NL3),beside(NL4),above(NL5),Tree) :-
 1830	buildTree2(NewConcept,N,below(NL3),beside(NL4),above(NL5),Tree),
 1831	!.
 1832tfsList2(Type,Env,MS,NewConcept,N,NL1,[Node1|NL2],
 1833        below(NL3),beside(NL4),above(NL5),NewTree) :-
 1834	testForSubsumption(Type,Env,MS,NewConcept,Node1,Tree,Judgement),
 1835	continue2(Type,Env,MS,NewConcept,N,NL1,[Node1|NL2],
 1836	         below(NL3),beside(NL4),above(NL5),Tree,Judgement,NewTree).
 1837
 1838buildTree2(NewConcept,_N,below([]),beside(_NL2),above([]),
 1839	node([NewConcept],[])) :-
 1840	!.
 1841buildTree2(NewConcept,_N,below([]),beside(_NL2),above(NL3),
 1842	node([NewConcept],NL3)) :- 
 1843	!.
 1844buildTree2(_NewConcept,_N,_,_,_,_) :-
 1845	!,
 1846	fail.
 1847
 1848	
 1849continue2(_Type,_,_MS,_NewConcept,_N,_NL1,[_Node1|_NL2],
 1850         below(_NL3),beside(_NL4),above(_NL5),_Tree,below,_NewTree) :-
 1851	!,
 1852	fail.
 1853continue2(Type,Env,MS,NewConcept,N,NL1,[Node1|NL2],
 1854         below(NL3),beside(NL4),above(NL5),_Tree,beside,NewTree) :-
 1855	tfsList2(Type,Env,MS,NewConcept,N,[Node1|NL1],NL2,
 1856                below(NL3),beside([Node1|NL4]),above(NL5),NewTree),
 1857	!.
 1858continue2(Type,Env,MS,NewConcept,N,NL1,[Node1|NL2],
 1859         below(NL3),beside(NL4),above(NL5),_Tree,above,NewTree) :-
 1860	tfsList2(Type,Env,MS,NewConcept,N,[Node1|NL1],NL2,
 1861	        below(NL3),beside(NL4),above([Node1|NL5]),NewTree),
 1862	!.
 1863continue2(_Type,_,_MS,_NewConcept,_N,_NL1,[_Node1|_NL2],
 1864         below(_NL3),beside(_NL4),above(_NL5),
 1865         _Tree,in,node(_N,_NL)) :-
 1866	!,
 1867	fail.
 1868
 1869testForEquivalence(Type,Env,MS,NewConcept,node([ClassifiedConcept|CL],AL),
 1870	           node([NewConcept,ClassifiedConcept|CL],AL),in) :-
 1871	once(subsume2(Type,Env,MS,ClassifiedConcept,NewConcept)),
 1872	% so NewConcept = ClassifiedConcept
 1873	!.
 1874testForEquivalence(_Type,_,_MS,_NewConcept,node([_ClassifiedConcept|_CL],_AL),
 1875	           _,above) :-
 1876	% so only x \in ClassifiedConcept => x \in NewConcept
 1877        % but not x \in NewConcept        => x \in ClassifiedConcept
 1878	!.
 1879
 1880subsume2(Type,Env,MS,X,Y) :- var(X),!,fail.
 1881subsume2(Type,Env,MS,X,Y) :- var(Y),!,fail.
 1882subsume2(Type,Env,MS,X,'top') :- !,fail.
 1883subsume2(Type,Env,MS,'bot',X) :- !,fail.
 1884subsume2(Type,Env,MS,X,'bot') :- !.
 1885subsume2(Type,Env,MS,'top',X) :- !.
 1886subsume2(Type,Env,MS,X,Y) :- 
 1887	sub3(X,Y),
 1888	!.
 1889subsume2(Type,Env,MS,X,Y) :- 
 1890	nsub3(X,Y),
 1891	!,fail. 
 1892subsume2(Type,Env,MS,X,Y) :- 
 1893	X \== Y,
 1894	subsumes(Type,Env,MS,X,Y), 
 1895  	cont4(X,Y),
 1896	!.
 1897subsume2(Type,Env,MS,X,Y) :- 
 1898	X \== Y,
 1899	cont5a(X,Y),
 1900	!,
 1901	fail.
 1902cont4('top',Y).
 1903cont4(X,Y) :- 
 1904	assert1(sub3(X,Y)),
 1905	succ3(Z,X),
 1906	cont4(Z,Y),!.
 1907cont4(X,Y). 
 1908cont5a('bot',X) :- !.
 1909cont5a(X,'bot') :- !,fail.
 1910cont5a(X,Y) :-
 1911	assert1(nsub3(X,Y)),
 1912	succ3(Y,Z),
 1913	cont5a(X,Z),!.
 1914
 1915assert2(G) :- not(G),assert_logged(G),!.
 1916assert2(_G) :-!.
 1917
 1918retract2(G) :- retract(G),!.
 1919retract2(_G) :- !.
 1920
 1921succ2(X,Y) :- succ3(X,Y),!.
 1922succ2(_X,'bot') :- !.
 1923
 1924
 1925/***********************************************************************
 1926 *
 1927 * showHierarchy(+Type)
 1928 * Parameter: Type     'concepts' or 'roles'
 1929 * display subsumption hierarchy in the modal context [].
 1930 *
 1931 */
 1932showHierarchy:- ignore(((member(Type,['concepts', 'roles']),showHierarchy(Type),fail))).
 1933showHierarchy(Type) :-
 1934	getCurrentEnvironment(EnvName),
 1935	showHierarchy(EnvName,[],Type).
 1936
 1937/***********************************************************************
 1938 *
 1939 * showHierarchy(+EnvName,+MS,+Type)
 1940 * Parameter: EnvName   environment name
 1941 *            MS        modal context
 1942 *            Type      'concepts' or 'roles'
 1943 * display subsumption hierarchy in the modal context MS.
 1944 *
 1945 */
 1946
 1947showHierarchy(EnvName,MS,concepts) :-
 1948	environment(EnvName,Env,_),
 1949	conceptHierarchy(Env,MS,Tree),
 1950	showDag([],Tree).
 1951showHierarchy(EnvName,MS,roles) :-
 1952	environment(EnvName,Env,_),
 1953	roleHierarchy(Env,MS,Tree),
 1954	showDag([],Tree).
 1955
 1956showHierarchy(EnvName,Type) :-
 1957	environment(EnvName,_,_),
 1958	!,
 1959	showHierarchy(EnvName,[],Type).
 1960showHierarchy(MS,Type) :-
 1961	(MS = [] ; MS = [_|_]),
 1962	!,
 1963	getCurrentEnvironment(EnvName),
 1964	showHierarchy(EnvName,MS,Type).
 1965
 1966/***********************************************************************
 1967 *
 1968 * getHierarchy(+Type,-H)
 1969 * Parameter: Type     'concepts' or 'roles'
 1970 * instantiates H with the internal representation of the subsumption 
 1971 * hierarchy of Type in the current environment and modal context [].
 1972 *
 1973 */
 1974
 1975getHierarchy(Type,H) :-
 1976	getCurrentEnvironment(EnvName),
 1977	getHierarchy(EnvName,[],Type,H).
 1978
 1979/***********************************************************************
 1980 *
 1981 * getHierarchy(+EnvName,+MS,+Type,-H)
 1982 * Parameter: EnvName   environment name
 1983 *            MS        modal context
 1984 *            Type      'concepts' or 'roles'
 1985 * instantiates H with the internal representation of the subsumption 
 1986 * hierarchy of Type in environment EnvName and modal context MS.
 1987 *
 1988 */
 1989
 1990getHierarchy(EnvName,MS,concepts,Tree) :-
 1991	environment(EnvName,Env,_),
 1992	conceptHierarchy(Env,MS,Tree).
 1993getHierarchy(EnvName,MS,roles,Tree) :-
 1994	environment(EnvName,Env,_),
 1995	roleHierarchy(Env,MS,Tree).
 1996
 1997getHierarchy(EnvName,Type,Tree) :-
 1998	environment(EnvName,_,_),
 1999	!,
 2000	getHierarchy(EnvName,[],Type,Tree).
 2001getHierarchy(MS,Type,Tree) :-
 2002	(MS = [] ; MS = [_|_]),
 2003	!,
 2004	getCurrentEnvironment(EnvName),
 2005	getHierarchy(EnvName,MS,Type,Tree).
 2006
 2007/***********************************************************************
 2008 *
 2009 * showDag(+Depth,+Tree)
 2010 * display subtree of the tree representation of the subsumption 
 2011 * hierarchy which is located at depth D, where D is the lenght of
 2012 * the list Depth of minus signs, in the hierarchy.
 2013 *
 2014 */
 2015
 2016showDag(Depth,node(CL,AL)) :-
 2017	writes(Depth),
 2018	writes(" "),
 2019	printClass(CL),
 2020	printArgs([45|Depth],AL).
 2021
 2022printClass([C1]) :-
 2023	print(C1),
 2024	nl,
 2025	!.
 2026printClass([C1,C2|CL]) :-
 2027	print(C1),
 2028	writes(" ("),
 2029	printRest([C2|CL]),
 2030	writes(")"),
 2031	nl.
 2032printRest([]) :- !.
 2033printRest([C1]) :-
 2034	print(C1).
 2035printRest([C1,C2|CL]) :-
 2036	print(C1),
 2037	print(", "),
 2038	printRest([C2|CL]).
 2039
 2040printArgs(_Depth,[]) :- !.
 2041printArgs(Depth,[N1|NL]) :-
 2042	showDag(Depth,N1),
 2043	printArgs(Depth,NL).
 2044
 2045
 2046
 2047
 2048
 2049
 2050/**********************************************************************
 2051 *
 2052 * @(#) classifier2.pl 1.35@(#)
 2053 *
 2054 */
 2055
 2056init_new_daten :- 
 2057	currentEnvironment(Env),
 2058	init_new_daten(Env).
 2059
 2060init_new_daten(Env) :-
 2061        init_succ(_),
 2062	init_sub(_),
 2063	init_nsub(_),
 2064	assert_logged(conceptName1(Env,_,'top')),
 2065	assert_logged(roleName1(Env,_,'top')),
 2066       	assertz_logged(succ(concepts,Env,_,'top','bot')),
 2067	assertz_logged(sub(concepts,Env,_,'top',_)),
 2068	assertz_logged(nsub(concepts,Env,_,X,X)),	
 2069	assertz_logged(succ(roles,Env,_,'top','bot')),
 2070	assertz_logged(sub(roles,Env,_,'top',_)),
 2071	assertz_logged(nsub(roles,Env,_,X,X)),
 2072	assertz_logged(sub(roles,Env,_,_,'bot')),
 2073	assertz_logged(sub(concepts,Env,_,_,'bot')).
 2074
 2075init_new_daten1 :-
 2076	currentEnvironment(Env),
 2077	!,
 2078	init_new_daten1(Env).	
 2079init_new_daten1(Env) :-
 2080	conceptName(Env,MS,W1,NewConcept),
 2081	not(name(NewConcept,[99,111,110,99,101,112,116|_])),
 2082	assert1(conceptName1(Env,MS,W1,NewConcept)),
 2083	fail.
 2084init_new_daten1(Env) :-
 2085	roleName(Env,MS,W1,NewRole),
 2086	not(name(NewRole,[114,111,108,101|_])),
 2087	assert1(roleName1(Env,MS,W1,NewRole)),
 2088	fail.
 2089init_new_daten1(Env).
 2090
 2091init_succ(MS) :- 
 2092 	currentEnvironment(Env),
 2093        init_succ(Env,MS),
 2094	!.
 2095init_succ(MS).
 2096init_succ(Env,MS) :- 
 2097	retractall_head(succ(_,Env,MS,_,_)),
 2098	!.
 2099init_sub(MS) :-
 2100	currentEnvironment(Env),
 2101	init_sub(Env,MS).
 2102init_sub(MS).
 2103init_sub(Env,MS) :- 
 2104	retractall_head(sub(_,Env,MS,_,_)),
 2105	!.
 2106
 2107init_nsub(MS) :-
 2108 	currentEnvironment(Env),
 2109	init_nsub(Env,MS).
 2110init_nsub(MS).
 2111init_nsub(Env,MS) :-
 2112	retractall_head(nsub(_,Env,MS,_,_)),
 2113	!.
 2114
 2115/********************************************************************/
 2116% Test-functions 
 2117
 2118neu1 :- newClassify,
 2119	show_dag([]),printStat. 
 2120show :- getCurrentEnvironment(EnvName),
 2121	environment(EnvName,Env,_),
 2122	showDefconcept(Env),
 2123	showDefprimconcept(Env),
 2124	showDefrole(Env),
 2125        showDefprimrole(Env).
 2126test1(Concept):- 
 2127	environment(EnvName,Env,_),	
 2128	conceptEqualSets(Env,user,MS,Concept,CT,_),
 2129	find_concept2(concepts,Env,MS,Concept,CT).
 2130
 2131test2 :- 
 2132	environment(EnvName,Env,_),	
 2133	conceptEqualSets(Env,user,MS,Concept,CT,_),
 2134	conceptEqualSets(Env,user,MS,Concept1,Concept,_),
 2135	print(Concept),
 2136	print(Concept1),nl,
 2137	fail.
 2138
 2139test3(MS,_) :- 
 2140	environment(EnvName,Env,_),	
 2141	conceptEqualSets(Env,user,MS,Concept,CT,_),
 2142	clause(conceptName(Env,_MS1,_W1,Concept),_),
 2143	conceptEqualSets(Env,user,MS,CT,Concept1,_),
 2144%	conceptName(Env,_MS2,_W2,Concept1),
 2145	print(Concept),print(" "),
 2146	print(CT),print(" "),
 2147	print(Concept1),
 2148	nl,
 2149	fail.
 2150test3(MS,_) :- 
 2151	environment(EnvName,Env,_),	
 2152	conceptSubsets(Env,user,MS,Concept,CT,_),
 2153	clause(conceptName(Env,_MS1,_W1,Concept),_),
 2154	conceptSubsets(Env,user,MS,CT,Concept1,_),
 2155%	not(conceptName(Env,_MS2,_W2,Concept)),
 2156	print(Concept),print(" "),
 2157	print(CT),print(" "),
 2158	print(Concept1),
 2159	nl,
 2160	fail.
 2161test3(MS) :- 
 2162	environment(EnvName,Env,_),	
 2163	roleEqualSets(Env,user,MS,Concept,CT,_),
 2164	clause(roleName(Env,_MS1,_W1,Concept),_),
 2165	roleEqualSets(Env,user,MS,CT,Concept1,_),
 2166%	roleName(Env,MS,Concept1),
 2167	print(Concept),print(" "),
 2168	print(CT),print(" "),
 2169	print(Concept1),
 2170	nl,
 2171	fail.
 2172
 2173test3(MS) :- 
 2174	environment(EnvName,Env,_),	
 2175	roleSubsets(Env,user,MS,Concept,CT,_),
 2176	clause(roleName(Env,_MS1,_W1,Concept),_),
 2177	roleSubsets(Env,user,MS,CT,Concept1,_),
 2178%	not(roleName(Env,_,MS,Concept)),
 2179	print(Concept),print(" "),
 2180	print(CT),print(" "),
 2181	print(Concept1),
 2182	nl,
 2183	fail.
 2184	
 2185% just for fun and test
 2186
 2187neu1(MS) :- newClassify(MS).
 2188
 2189newClassify :-
 2190	getCurrentEnvironment(EnvName),
 2191	newClassify(EnvName,[]).
 2192
 2193newClassify(EnvName) :-
 2194	environment(EnvName,_Env,_),
 2195	!,
 2196	newClassify(EnvName,[]).
 2197newClassify(MS) :-
 2198	(MS = [] ; MS = [_|_]),
 2199	getCurrentEnvironment(EnvName),
 2200	newClassify(EnvName,MS).
 2201
 2202newClassify(EnvName,MS) :-
 2203	environment(EnvName,Env,_),
 2204	testa(Env,MS).
 2205	
 2206testa(Env,MS) :-
 2207	init_new_daten(Env),
 2208	initStat,
 2209	testb(Env,MS),
 2210	buildOrdering(Env,MS,CTree,RTree),
 2211	retractall_head(conceptHierarchy(Env,MS,_)),
 2212	retractall_head(roleHierarchy(Env,MS,_)),
 2213	assert_logged(conceptHierarchy(Env,MS,CTree)),
 2214	assert_logged(roleHierarchy(Env,MS,RTree)),
 2215	ifOption(testOutput,yes,printStat),
 2216%	ifOption(testOutput,yes,show_dag(MS)),
 2217	!.	
 2218testb(Env,MS) :-
 2219        not(find_concept(concepts,Env,MS)),
 2220        not(find_role(roles,Env,MS)).
 2221	
 2222find_concept(concepts,Env,MS) :-
 2223	getConceptName(Env,MS,Concept),
 2224	not(name(Concept,[99,111,110,99,101,112,116|_])),
 2225	ifOption(testOutput,yes,(print(Concept), nl)),
 2226	addCounter(conceptsClassified,1),
 2227	find_concept1(concepts,Env,MS,Concept).
 2228find_role(roles,Env,MS) :-
 2229	getRoleName(Env,MS,Role),
 2230	not(name(Role,[114,111,108,101|_])),
 2231	addCounter(rolesClassified,1),
 2232	find_role1(roles,Env,MS,Role).
 2233
 2234find_role1(roles,Env,MS,Role) :-
 2235	roleEqualSets(Env,user,MS,Role,CT,_),
 2236	find_role2(roles,Env,MS,Role,CT),
 2237	!,
 2238 	fail.
 2239find_role1(roles,Env,MS,Role) :-
 2240	roleSubsets(Env,user,MS,Role,CT,_),
 2241	find_prole2(roles,Env,MS,Role,CT),
 2242 	!,
 2243	fail.
 2244/*
 2245find_role1(roles,Env,MS,Role) :-
 2246	roleEqualSets(Env,user,MS,Role,CT,_),
 2247	find_role11(roles,Env,MS,Role,CT),
 2248	!,
 2249	fail.
 2250find_role1(roles,Env,MS,Role) :-
 2251	roleSubsets(Env,user,MS,Role,CT,_),
 2252	find_role11(roles,Env,MS,Role,CT),
 2253	!,
 2254	fail.
 2255find_role11(roles,Env,MS,Role,CT) :-
 2256	roleEqualSets(Env,user,MS,CT,CT1,_),
 2257	find_prole2(roles,Env,MS,Role,CT1),
 2258	find_role11(roles,Env,MS,Role,CT).
 2259find_role11(roles,Env,MS,Role,CT) :-
 2260	roleSubsets(Env,user,MS,CT,CT1,_),
 2261	find_prole2(roles,Env,MS,Role,CT1),
 2262	find_role11(roles,Env,MS,Role,CT).
 2263*/
 2264
 2265find_role1(roles,Env,MS,Role) :-
 2266	make_succ2(roles,Env,MS,Role),
 2267	!,
 2268	fail.
 2269find_concept1(concepts,Env,MS,Concept) :-
 2270	conceptEqualSets(Env,user,MS,Concept,CT,_),
 2271	find_concept2(concepts,Env,MS,Concept,CT),
 2272	!,
 2273 	fail.
 2274find_concept1(concepts,Env,MS,Concept) :-
 2275	conceptSubsets(Env,user,MS,Concept,CT,_),
 2276	find_pconcept2(concepts,Env,MS,Concept,CT),
 2277 	!,
 2278	fail.
 2279/*
 2280find_concept1(concepts,Env,MS,Concept) :-
 2281	conceptEqualSets(Env,user,MS,Concept,CT,_),
 2282	find_concept11(concepts,Env,MS,Concept,CT),
 2283	!,
 2284	fail.
 2285find_concept1(concepts,Env,MS,Concept) :-
 2286	conceptSubsets(Env,user,MS,Concept,CT,_),
 2287	find_concept11(concepts,Env,MS,Concept,CT),
 2288	!,
 2289	fail.
 2290find_concept11(concepts,Env,MS,Concept,CT) :-
 2291	conceptEqualSets(Env,user,MS,CT,CT1,_),
 2292	find_pconcept2(concepts,Env,MS,Concept,CT1),
 2293	find_concept11(concepts,Env,MS,Concept,CT).
 2294find_concept11(concepts,Env,MS,Concept,CT) :-
 2295	conceptSubsets(Env,user,MS,CT,CT1,_),
 2296	find_pconcept2(concepts,Env,MS,Concept,CT1),
 2297	find_concept11(concepts,Env,MS,Concept,CT).
 2298*/
 2299find_concept1(concepts,Env,MS,Concept) :-
 2300	make_succ2(concepts,Env,MS,Concept),
 2301	!,
 2302	fail.
 2303/***** Entwicklungsecke....
 2304
 2305test fuer den trans.abschluss von roleEqualSets,roleSubsets,concept...
 2306find_role1(roles,Env,MS,Role) :-
 2307	roleEqualSets(Env,user,MS,Role,CT,_),
 2308	find_role11(roles,Env,MS,Role,CT),
 2309	!,
 2310	fail.
 2311find_role11(roles,Env,MS,Role,CT) :-
 2312	find_role2(roles,Env,MS,Role,CT),
 2313	!,
 2314	roleEqualSets(Env,user,MS,CT,CT1,_),
 2315	find_role11(roles,Env,MS,Role,CT1),
 2316	!.
 2317find_role11(roles,Env,MS,Role,CT) :-
 2318	!.
 2319
 2320find_role1(roles,Env,MS,Role) :-
 2321	roleSubsets(Env,user,MS,Role,CT,_),
 2322	find_prole11(roles,Env,MS,Role,CT),
 2323 	!,
 2324	fail.
 2325find_prole11(roles,Env,MS,Role,CT) :-
 2326	find_prole2(roles,Env,MS,Role,CT),
 2327	!,
 2328	roleSubsets(Env,user,MS,CT,CT1,_),
 2329	find_prole11(roles,Env,MS,Role,CT1),
 2330	!.
 2331find_prole11(roles,Env,MS,Role,CT) :-
 2332	!.
 2333*/
 2334
 2335/*******************************************/
 2336
 2337find_concept2(concepts,Env,MS,Concept,CT) :-
 2338	getConceptName(Env,MS,CT),
 2339	(succ(concepts,Env,MS,Topconcept,Concept),
 2340	subsume1(concepts,Env,MS,Topconcept,CT)),
 2341	assert1(nsub(concepts,Env,MS,Concept,CT)),
 2342	assert1(nsub(concepts,Env,MS,CT,Concept)),
 2343	assert_succ(concepts,Env,MS,Topconcept,CT),
 2344	!.	
 2345find_concept2(concepts,Env,MS,Concept,CT) :-
 2346%	getConceptName(Env,_MS1,W1,CT),
 2347	getConceptName(Env,MS,CT),
 2348	(succ(concepts,Env,MS,Topconcept,CT),
 2349	subsume1(concepts,Env,MS,Topconcept,Concept)),
 2350	assert1(nsub(concepts,Env,MS,Concept,CT)),
 2351	assert1(nsub(concepts,Env,MS,CT,Concept)),
 2352	assert_succ(concepts,Env,MS,Topconcept,Concept),
 2353	!.
 2354find_concept2(concepts,Env,MS,Concept,CT) :-
 2355	getConceptName(Env,MS,CT),
 2356	assert1(nsub(concepts,Env,MS,Concept,CT)),
 2357	assert1(nsub(concepts,Env,MS,CT,Concept)),
 2358	assert1(succ(concepts,Env,MS,'top',Concept)),
 2359	assert1(succ(concepts,Env,MS,'top',CT)),
 2360	!.
 2361find_concept2(concepts,Env,MS,Concept,CT) :-
 2362	CT = and([X|[R]]),
 2363	conceptEqualSets(Env,user,MS,Concept1,R,_),
 2364	assert_succ(concepts,Env,MS,X,Concept),
 2365	assert_succ(concepts,Env,MS,Concept1,Concept),		
 2366	!. 
 2367find_concept2(concepts,Env,MS,Concept,CT) :-
 2368	CT = and(L),
 2369	find_concept21(concepts,Env,MS,Concept,L),
 2370	!.
 2371
 2372find_concept2(concepts,Env,MS,Concept,CT) :-
 2373	CT = and([X|[R]]),
 2374	R = some(Role,Concept1),
 2375	getRoleName(Env,MS,Role),
 2376	X == Concept1,
 2377	assert_succ(concepts,Env,MS,X,Concept),	
 2378	!.
 2379
 2380find_concept2(concepts,Env,MS,Concept,CT) :-
 2381	CT = some(Role,Concept1),
 2382	find_concept25(Env,MS,Concept,Concept1),
 2383	!. 
 2384
 2385find_concept2(concepts,Env,MS,Concept,CT) :-
 2386	CT = or([and(L)]),
 2387	find_concept26(concepts,Env,MS,Concept,L),
 2388	!.
 2389	
 2390find_concept2(concepts,Env,MS,Concept,CT) :-
 2391	CT = or([L|R]),
 2392	L = and(L1),
 2393	find_concept3(Env,MS,Concept,R,L1,Z),
 2394	find_concept31(Env,MS,Concept,Z),	
 2395	!.
 2396find_concept2(concepts,Env,MS,Concept,CT) :-
 2397	CT = or(L),
 2398	find_concept26(concepts,Env,MS,Concept,L),
 2399	!.
 2400
 2401find_concept21(concepts,Env,MS,Concept,[]) :-
 2402	!.
 2403find_concept21(concepts,Env,MS,Concept,[X|R]) :-
 2404	getConceptName(Env,MS,X),
 2405	assert_succ(concepts,Env,MS,X,Concept),
 2406	find_concept21(concepts,Env,MS,Concept,R),
 2407	!.
 2408find_concept21(concepts,Env,MS,Concept,[X|R]) :-    
 2409	X = not(R1),
 2410	getConceptName(Env,MS,R1),
 2411	setofOrNil(K,find_concept22(concepts,Env,MS,Concept,R1,K),L),
 2412	find_concept23(concepts,Env,MS,Concept,L),
 2413	find_concept21(concepts,Env,MS,Concept,R),
 2414	!.
 2415find_concept21(concepts,Env,MS,Concept,[X|R]) :-
 2416	find_concept21(concepts,Env,MS,Concept,R),
 2417	!,
 2418	fail.
 2419
 2420find_concept22(concepts,Env,MS,Concept,R1,K) :-
 2421	succ(concepts,Env,MS,K,R1),
 2422	not(succ(concepts,Env,MS,K,Concept)).
 2423
 2424find_concept23(concepts,Env,MS,Concept,[]) :-
 2425	!.	
 2426find_concept23(concepts,Env,MS,Concept,[L1|R1]) :-
 2427	find_concept24(concepts,Env,MS,Concept,L1),
 2428	find_concept23(concepts,Env,MS,Concept,R1),
 2429	!.
 2430find_concept24(concepts,Env,MS,Concept,L1) :-
 2431	succ(concepts,Env,MS,Top,L1),
 2432	succ(concepts,Env,MS,Top,K),
 2433	subsume1(concepts,Env,MS,K,Concept),
 2434	setofOrNil(Nf,succ(concepts,Env,MS,K,Nf),Lnf),
 2435	make_succ1(concepts,Env,MS,K,Lnf,Concept),
 2436	!. 
 2437		
 2438find_concept25(Env,MS,Concept,Concept1) :-
 2439	succ(concepts,Env,MS,K,Concept1),
 2440	setofOrNil(Nf,succ(concepts,Env,MS,Concept1,Nf),Lnf),
 2441	make_succ1(concepts,Env,MS,K,Lnf,Concept),
 2442	fail.
 2443find_concept25(Env,MS,Concept,Concept1).
 2444
 2445find_concept26(concepts,Env,MS,Concept,[C1|R]) :-
 2446	getConceptName(Env,MS,C1),
 2447	subsume1(concepts,Env,MS,C1,Concept),
 2448	assert_succ(concepts,Env,MS,C1,Concept),
 2449	find_concept26(concepts,Env,MS,Concept,R).
 2450
 2451find_concept3(Env,MS,Concept,[],Z,Z) :- 
 2452	!.
 2453find_concept3(Env,MS,Concept,[L|R],Z,K) :-
 2454	L = and(L1),
 2455	intersection_motel(Z,L1,Z1),
 2456	find_concept3(Env,MS,Concept,R,Z1,K),
 2457	!.
 2458
 2459find_concept31(Env,MS,Concept,[]) :-
 2460	!.
 2461find_concept31(Env,MS,Concept,[L1|R1]) :-
 2462	assert_succ(concepts,Env,MS,L1,Concept),
 2463	find_concept31(Env,MS,Concept,R1),
 2464	!.
 2465
 2466% das hier ist die entwicklungsecke....	
 2467/*
 2468% weiss nicht ob das stimmt
 2469find_concept21(concepts,Env,MS,Concept,[X|R]) :-    
 2470	X = all(R1,X1),
 2471	getRoleName(Env,MS,R1),
 2472	getConceptName(Env,MS,X1),
 2473	assert_succ(concepts,Env,MS,X1,Concept),
 2474	find_concept21(concepts,Env,MS,Concept,R),
 2475	!.
 2476*/
 2477% ********************** Primconcepte **************************
 2478% es fehlt noch defprimconcept(_,_,some(_,_..))
 2479%                   "         (_,not(),...)
 2480%                   "         (_,...(),...)
 2481					
 2482find_pconcept2(concepts,Env,MS,PrimConcept,CT) :-
 2483	CT = not(X),
 2484	getConceptName(Env,MS,X),
 2485	cont1a(concepts,Env,MS,[],X,PrimConcept),
 2486%	succ(concepts,Env,MS,Topconcept,X),
 2487	find_pconcept23(Env,MS,X,PrimConcept,Top),
 2488	assert_succ(concepts,Env,MS,Top,PrimConcept),
 2489	assert_succ(concepts,Env,MS,Top,X),
 2490	!.
 2491find_pconcept2(concepts,Env,MS,Primconcept,CT) :-
 2492	CT = and(L),
 2493	find_pconcept24(Env,MS,Primconcept,L),
 2494	!.
 2495
 2496find_pconcept2(concepts,Env,MS,PrimConcept,CT) :-
 2497	getConceptName(Env,MS,CT),
 2498	assert1(sub(concepts,Env,MS,CT,PrimConcept)),
 2499	direct_succ(concepts,Env,MS,[],CT,PrimConcept,Z,L1),
 2500        contb(concepts,Env,MS,Z,L1,PrimConcept),
 2501	!.
 2502
 2503find_pconcept2(concepts,Env,MS,PrimConcept,CT) :-
 2504	CT = some(X,Y),
 2505	find_pconcept21(Env,MS,PrimConcept,X,Y),
 2506	!.
 2507
 2508find_pconcept2(concepts,Env,MS,Primconcept,CT) :-
 2509	CT = and([X|[R]]),
 2510%	getConceptName(Env,MS,W1,X),
 2511	getConceptName(Env,MS,X),
 2512	R = not(Y),
 2513	getConceptName(Env,MS,Y),
 2514	find_pconcept23(Env,MS,X,Y,Top),
 2515	assert_succ(concepts,Env,MS,Top,PrimConcept),	
 2516%	assert1(sub(concepts,Env,MS,PrimConcept)),
 2517	!.
 2518
 2519
 2520
 2521find_pconcept2(concepts,Env,MS,Primconcept,CT) :-
 2522	CT = or([and(L)]),
 2523	find_pconcept26(concepts,Env,MS,Primconcept,L),
 2524	!.
 2525	
 2526find_pconcept2(concepts,Env,MS,Primconcept,CT) :-
 2527	CT = or([L|R]),
 2528	L = and(L1),
 2529	find_pconcept3(Env,MS,Primconcept,R,L1,Z),
 2530	find_pconcept31(Env,MS,Primconcept,Z),	
 2531	!.
 2532find_pconcept2(concepts,Env,MS,Primconcept,CT) :-
 2533	CT = or(L),
 2534	find_pconcept26(concepts,Env,MS,Primconcept,L),
 2535	!.
 2536
 2537find_pconcept26(concepts,Env,MS,Primconcept,[C1|R]) :-
 2538%	getConceptName(Env,_MS,W1,C1),
 2539	getConceptName(Env,MS,X),
 2540	subsume1(concepts,Env,MS,C1,Primconcept),
 2541	find_pconcept27(concepts,Env,MS,Primconcept,C1),
 2542  	find_pconcept26(concepts,Env,MS,Primconcept,R).
 2543
 2544find_pconcept27(concepts,Env,MS,Primconcept,C1):-
 2545	assert1(sub(concepts,Env,MS,C1,Primconcept)),
 2546	direct_succ(concepts,Env,MS,[],C1,PrimConcept,Z,L1),
 2547        contb(concepts,Env,MS,Z,L1,PrimConcept),
 2548	!.
 2549
 2550find_pconcept3(Env,MS,Primconcept,[],Z,Z) :- 
 2551	!.
 2552find_pconcept3(Env,MS,Primconcept,[L|R],Z,K) :-
 2553	L = and(L1),
 2554	intersection_motel(Z,L1,Z1),
 2555	find_pconcept3(Env,MS,Primconcept,R,Z1,K),
 2556	!.
 2557
 2558find_pconcept31(Env,MS,Primconcept,[]) :-
 2559	!.
 2560find_pconcept31(Env,MS,Primconcept,[L1|R1]) :-
 2561	find_pconcept27(concepts,Env,MS,Primconcept,L1),
 2562	find_pconcept31(Env,MS,Primconcept,R1),
 2563	!.
 2564
 2565
 2566find_pconcept21(Env,MS,PrimConcept,X,Y) :-
 2567	Y = or([Y1|[Y2]]),
 2568	conceptEqualSets(Env,user,MS,Concept,some(X,Y1),_),
 2569	conceptEqualSets(Env,user,MS,Concept1,some(X,Y2),_),
 2570	find_pconcept23(Env,MS,Concept1,Concept,Top),
 2571	assert_succ(concepts,Env,MS,Top,PrimConcept),
 2572	!.
 2573
 2574find_pconcept23(Env,MS,X,Y,X) :-
 2575	sub(concepts,Env,MS,X,Y),
 2576	!.
 2577find_pconcept23(Env,MS,X,Y,Y) :-
 2578	sub(concepts,Env,MS,Y,X),
 2579	!.
 2580find_pconcept23(Env,MS,X,Y,Top) :-
 2581	sub(concepts,Env,MS,Top,X),sub(concepts,Env,MS,Top,Y).
 2582
 2583find_pconcept24(Env,MS,Primconcept,[X|R]) :-
 2584	getConceptName(Env,MS,X),
 2585	assert1(sub(concepts,Env,MS,X,PrimConcept)),
 2586	direct_succ(concepts,Env,MS,[],X,PrimConcept,Z,L1),
 2587        contb(concepts,Env,MS,Z,L1,PrimConcept),
 2588	find_pconcept24(Env,MS,Primconcept,R),
 2589	!.
 2590
 2591/*	
 2592find_pconcept2(concepts,Env,MS,Primconcept,CT) :-
 2593	CT = and([X|[R]]),
 2594	getConceptName(Env,_MS1,W1,X),
 2595	getConceptName(Env,_MS2,W2,R),
 2596	direct_succ(concepts,Env,MS,[],X,PrimConcept,Z,L1),
 2597        contb(concepts,Env,MS,Z,L1,PrimConcept),
 2598	!.
 2599
 2600find_pconcept2(concepts,Env,MS,PrimConcept,CT) :-
 2601	conceptName1(Env,MS1,CT),
 2602	find_pconcept23(Env,MS,CT,PrimConcept,Top),
 2603	assert_succ(concepts,Env,MS,Top,PrimConcept),
 2604	assert_succ(concepts,Env,MS,Top,CT),
 2605	!.
 2606*/
 2607
 2608/*************************************************************************
 2609*                    jetzt mit rollen
 2610*/
 2611find_role2(roles,Env,MS,Role,CT) :-
 2612	getRoleName(Env,MS,CT),
 2613	succ(roles,Env,MS,Toprole,Role),
 2614	assert1(nsub(roles,Env,MS,Role,Ct)),
 2615	assert1(nsub(roles,Env,MS,Ct,Role)),
 2616	assert_succ(roles,Env,MS,Toprole,CT),
 2617	!.	
 2618find_role2(roles,Env,MS,Role,CT) :-
 2619	getRoleName(Env,MS,CT),
 2620	succ(roles,Env,MS,Toprole,CT),
 2621	assert1(nsub(roles,Env,MS,Role,Ct)),
 2622	assert1(nsub(roles,Env,MS,Ct,Role)),
 2623	assert_succ(roles,Env,MS,Toprole,Role),
 2624	!.
 2625find_role2(roles,Env,MS,Role,CT) :-	
 2626	getRoleName(Env,MS,CT),
 2627	assert1(nsub(roles,Env,MS,Role,Ct)),
 2628	assert1(nsub(roles,Env,MS,Ct,Role)),
 2629	assert_succ(roles,Env,MS,'top',Role),
 2630	assert_succ(roles,Env,MS,'top',CT),
 2631	!.	
 2632find_role2(roles,Env,MS,Role,CT) :-	
 2633	CT = and([X|[R]]),
 2634	roleEqualSets(Env,user,MS,Role1,R,_),
 2635	assert_succ(roles,Env,MS,X,Role),
 2636	assert_succ(roles,Env,MS,Role1,Role),	
 2637	!. 
 2638find_role2(roles,Env,MS,Role,CT) :-	
 2639	CT = and([X|[R]]),
 2640	getRoleName(Env,MS,X),
 2641	getRoleName(Env,MS,R),
 2642	assert_succ(roles,Env,MS,X,Role),
 2643	assert_succ(roles,Env,MS,R,Roles),
 2644	!.
 2645find_role2(roles,Env,MS,Role,CT) :-	
 2646	CT = and([X|[R]]),
 2647	R = some(_,Role1),
 2648	X == Role1,
 2649	assert_succ(Roles,Env,MS,X,Role),	
 2650	!.
 2651find_role2(roles,Env,MS,Role,CT) :-	
 2652	CT = or([X|[R]]),
 2653	find_role3(Env,MS,Role,X,R).
 2654find_role2(roles,Env,MS,Role,CT) :-
 2655	CT = and(L),
 2656	find_role21(roles,Env,MS,Role,L),
 2657	!.
 2658find_role2(roles,Env,MS,Role,CT) :-
 2659	CT = restr(Role1,Concept),
 2660	assert_succ(roles,Env,MS,Role1,Role),
 2661	!.
 2662
 2663
 2664find_role2(roles,Env,MS,Role,CT) :-
 2665	CT = or([and(L)]),
 2666	find_role26(roles,Env,MS,Role,L),
 2667	!.
 2668	
 2669find_role2(roles,Env,MS,Role,CT) :-
 2670	CT = or([L|R]),
 2671	L = and(L1),
 2672	find_role30(Env,MS,Role,R,L1,Z),
 2673	find_role31(Env,MS,Role,Z),	
 2674	!.
 2675find_role2(roles,Env,MS,Role,CT) :-
 2676	CT = or(L),
 2677	find_role26(roles,Env,MS,Role,L),
 2678	!.
 2679
 2680find_role26(roles,Env,MS,Role,[C1|R]) :-
 2681	getRoleName(Env,MS,C1),
 2682	subsume1(roles,Env,MS,C1,Role),
 2683	assert_succ(roles,Env,MS,C1,Role),
 2684	find_role26(roles,Env,MS,Role,R).
 2685
 2686find_role30(Env,MS,Role,[],Z,Z) :- 
 2687	!.
 2688find_role30(Env,MS,Role,[L|R],Z,K) :-
 2689	L = and(L1),
 2690	intersection_motel(Z,L1,Z1),
 2691	find_role30(Env,MS,Role,R,Z1,K),
 2692	!.
 2693
 2694find_role31(Env,MS,Role,[]) :-
 2695	!.
 2696find_role31(Env,MS,Role,[L1|R1]) :-
 2697	assert_succ(roles,Env,MS,L1,Role),
 2698	find_role31(Env,MS,Role,R1),
 2699	!.
 2700
 2701
 2702find_role3(Env,MS,Role,X,R) :-
 2703	X = and([X1|[R1]]),
 2704	subsume1(roles,Env,MS,Role,R1),
 2705	find_role2(roles,Env,MS,Role,X).
 2706find_role3(Env,MS,Role,X,R) :-
 2707	R = and([X1|[R1]]),
 2708	subsume1(roles,Env,MS,Role,R1),
 2709	find_role2(roles,Env,MS,Role,X).
 2710
 2711find_role21(roles,Env,MS,Role,[]) :-
 2712	!.
 2713find_role21(roles,Env,MS,Role,[X|R]) :-
 2714	getRoleName(Env,MS,X),
 2715	assert_succ(roles,Env,MS,X,Role),
 2716	find_role21(roles,Env,MS,Role,R),
 2717	!.
 2718find_role21(roles,Env,MS,Role,[X|R]) :-
 2719	X = not(R1),
 2720	getRoleName(Env,MS,R1),
 2721	setofOrNil(K,find_role22(roles,Env,MS,Role,R1,K),L),
 2722	find_role23(roles,Env,MS,Role,L),
 2723	!.
 2724find_role22(roles,Env,MS,Role,R1,K) :-
 2725	succ(roles,Env,MS,K,R1),
 2726	not(succ(roles,Env,MS,K,Role)).
 2727
 2728find_role23(roles,Env,MS,Role,[]) :-
 2729	!.	
 2730find_role23(roles,Env,MS,Role,[L1|R1]) :-
 2731	find_role24(roles,Env,MS,Role,L1),
 2732	find_role23(roles,Env,MS,Role,R1),
 2733	!.
 2734find_role24(roles,Env,MS,Role,L1) :-
 2735	succ(roles,Env,MS,Top,L1),
 2736	succ(roles,Env,MS,Top,K),
 2737	subsume1(roles,Env,MS,K,Role),
 2738	setofOrNil(Nf,succ(roles,Env,MS,K,Nf),Lnf),
 2739	make_succ1(roles,Env,MS,K,Lnf,Role),
 2740	!.
 2741
 2742/******** PrimRollen ************************************************/
 2743
 2744find_prole2(roles,Env,MS,PrimRole,CT) :-
 2745	CT = and(L),
 2746	find_prole24(Env,MS,PrimRole,L),
 2747	!.
 2748
 2749find_prole2(roles,Env,MS,PrimRole,CT) :-
 2750	CT = not(X),
 2751	getRoleName(Env,MS,X),
 2752	cont1a(roles,Env,MS,X,PrimRole),
 2753	find_prole23(Env,MS,X,PrimRole,Top),
 2754	assert_succ(roles,Env,MS,Top,PrimRole),
 2755	assert_succ(roles,Env,MS,Top,X),
 2756	!.
 2757
 2758find_prole2(roles,Env,MS,PrimRole,CT) :-
 2759	convertMS(Env,[[],true],MS,[],[W1,G1],_),
 2760	call(G1),
 2761	getRoleName(Env,MS,CT),
 2762	assert1(sub(roles,Env,MS,CT,PrimRole)),
 2763	direct_succ(roles,Env,MS,[],CT,PrimRole,Z,L1),
 2764              contb(roles,Env,MS,Z,L1,PrimRole),
 2765	!.
 2766/*
 2767find_prole2(roles,Env,MS,PrimRole,CT) :-
 2768	CT = and([X|[R]]),
 2769	getRoleName(Env,MS,X),
 2770	getRoleName(Env,MS,R),	
 2771	find_prole23(Env,MS,X,R,Top),
 2772	assert_succ(roles,Env,MS,Top,PrimRole),
 2773	!.
 2774
 2775find_prole2(roles,Env,MS,PrimRole,CT) :-
 2776	getRoleName(Env,MS,CT),
 2777	find_prole23(Env,MS,PrimRole,CT,Top),
 2778	assert_succ(roles,Env,MS,Top,PrimRole),
 2779	assert_succ(roles,Env,MS,Top,CT),
 2780	!.
 2781*/
 2782find_prole2(roles,Env,MS,PrimRole,CT) :-
 2783	CT = some(X,Y),
 2784	find_prole21(Env,MS,PrimRole,X,Y),
 2785	!.
 2786find_prole2(roles,Env,MS,PrimRole,CT) :-
 2787	CT = and([X|[R]]),
 2788	getRoleName(Env,MS,X),
 2789	R = not(Y),
 2790	getRoleName(Env,MS,Y),
 2791	find_prole23(Env,MS,X,Y,Top),
 2792	assert_succ(roles,Env,MS,Top,PrimRole),	
 2793	!.
 2794	
 2795find_prole2(roles,Env,MS,Primrole,CT) :-
 2796	CT = or([L|R]),
 2797	L = and(L1),
 2798	find_prole3(Env,MS,Primrole,R,L1,Z),
 2799	find_prole31(Env,MS,Primrole,Z),	
 2800	!.
 2801find_prole2(roles,Env,MS,Primrole,CT) :-
 2802	CT = or(L),
 2803	find_prole26(roles,Env,MS,Primrole,L),
 2804	!.
 2805
 2806find_prole21(Env,MS,PrimRole,X,Y) :-
 2807	Y = or([Y1|[Y2]]),
 2808	roleEqualSets(Env,user,MS,Role,some(X,Y1),_),
 2809	roleEqualSets(Env,user,MS,Role1,some(X,Y2),_),
 2810	find_prole23(Env,MS,Role,Role1,Top),
 2811	assert_succ(roles,Env,MS,Top,PrimRole),
 2812	!.
 2813
 2814find_prole23(Env,MS,X,Y,X) :-
 2815	sub(roles,Env,MS,X,Y),
 2816	!.
 2817find_prole23(Env,MS,X,Y,Y) :-
 2818	sub(roles,Env,MS,Y,X),
 2819	!.
 2820find_prole23(Env,MS,X,Y,Top) :-
 2821	sub(roles,Env,MS,Top,X),sub(roles,Env,MS,Top,Y).
 2822
 2823find_prole24(Env,MS,PrimRole,[]).
 2824find_prole24(Env,MS,PrimRole,[X|R]) :-
 2825	getRoleName(Env,MS,X),
 2826	assert1(sub(roles,Env,MS,X,PrimRole)),
 2827	direct_succ(roles,Env,MS,[],X,PrimRole,Z,L1),
 2828              contb(roles,Env,MS,Z,L1,PrimRole),
 2829	find_prole24(Env,MS,PrimRole,R),
 2830	!.
 2831
 2832find_prole26(roles,Env,MS,Primrole,[C1|R]) :-
 2833	convertMS(Env,[[],true],MS,[],[W1,G1],_),
 2834	call(G1),
 2835	getRoleName(Env,MS,C1),
 2836	subsume1(roles,Env,MS,C1,Primrole),
 2837	find_prole27(roles,Env,MS,Primrole,C1),
 2838  	find_prole26(roles,Env,MS,Primrole,R).
 2839
 2840find_prole27(roles,Env,MS,Primrole,C1):-
 2841	assert1(sub(roles,Env,MS,C1,Primrole)),
 2842	direct_succ(roles,Env,MS,[],C1,PrimRole,Z,L1),
 2843              contb(roles,Env,MS,Z,L1,Primrole),
 2844	!.
 2845
 2846find_prole3(Env,MS,Primrole,[],Z,Z) :- 
 2847	!.
 2848find_prole3(Env,MS,Primrole,[L|R],Z,K) :-
 2849	L = and(L1),
 2850	intersection_motel(Z,L1,Z1),
 2851	find_prole3(Env,MS,Primrole,R,Z1,K),
 2852	!.
 2853
 2854find_prole31(Env,MS,Primrole,[]) :-
 2855	!.
 2856find_prole31(Env,MS,Primrole,[L1|R1]) :-
 2857	find_prole27(roles,Env,MS,Primrole,L1),
 2858	find_prole31(Env,MS,Primrole,R1),
 2859	!.
 2860
 2861/****************************************************************/
 2862
 2863make_succ(MS) :-           
 2864	currentEnvironment(Env),            
 2865              not(make_succ(concepts,Env,MS)),
 2866	not(make_succ(roles,Env,MS)),!.
 2867make_succ(concepts,Env,MS) :-        
 2868	getConceptName(Env,MS,NewConcept),
 2869	ifOption(testOutput,yes,(print(NewConcept),nl)),
 2870	make_succ2(concepts,Env,MS,NewConcept),
 2871	fail.
 2872make_succ(roles,Env,MS) :-
 2873	getRoleName(Env,MS,NewRole),
 2874	ifOption(testOutput,yes,(print(NewRole),nl)),
 2875	make_succ2(roles,Env,MS,NewRole),
 2876       	fail.
 2877make_succ2(Type,Env,MS,NewConcept) :- 
 2878              NewConcept \== 'top',!,
 2879              NewConcept \== 'bot',!,
 2880              direct_succ(Type,Env,MS,[],'top',NewConcept,X,L),
 2881              contb(Type,Env,MS,X,L,NewConcept),
 2882              !.
 2883
 2884contb(Type,Env,MS,[],L,NewConcept) :- 
 2885        !.
 2886
 2887contb(Type,Env,MS,[X|R],L,NewConcept) :-
 2888        setofOrNil(Y,contc(Type,Env,MS,X,Y,L),L1),
 2889	list_to_set(L1,L2),
 2890        make_succ1(Type,Env,MS,X,L2,NewConcept),
 2891        !,
 2892        contb(Type,Env,MS,R,L,NewConcept).
 2893contb(Type,Env,MS,X,L,NewConcept) :-
 2894	list_to_set(L,L1),	
 2895        make_succ1(Type,Env,MS,X,L1,NewConcept),
 2896        !.
 2897
 2898contc(Type,Env,MS,X,Y,L) :-
 2899        sub(Type,Env,MS,X,Y),member(Y,L).
 2900
 2901direct_succ(Type,Env,MS,_Done,'bot',X,_,[]) :- fail.
 2902direct_succ(Type,Env,MS,Done,X,NewConcept,Z,L1) :-
 2903	subsume1(Type,Env,MS,X,NewConcept),
 2904	setofOrNil(Y,(succ1(Type,Env,MS,X,Y), not(member(Y,[X|Done]))),L),
 2905	!,
 2906	check(Type,Env,MS,[X|Done],L,_,X,NewConcept,Z,L1),
 2907	!.
 2908direct_succ(Type,Env,MS,_Done,X,NewConcept,X,[]) :- 
 2909	!.
 2910
 2911check(Type,Env,MS,Done,[Y|L],_L1,X,NewConcept,Z,L1) :-
 2912        subsume1(Type,Env,MS,Y,NewConcept),
 2913	!,
 2914        direct_succ(Type,Env,MS,Done,Y,NewConcept,Z1,L10),
 2915	!,        	 				 	 	
 2916	conta(Type,Env,MS,[Y|Done],L,L2,X,NewConcept,Z1,L10,Z,L1),
 2917        !.
 2918check(Type,Env,MS,Done,[Y|L],L2,X,NewConcept,Z,L1) :-
 2919	!,
 2920	check(Type,Env,MS,[Y|Done],L,[Y|L2],X,NewConcept,Z,L1).
 2921check(Type,Env,MS,Done,[],L2,X,NewConcept,X,L1) :-
 2922	check1(Type,Env,MS,Done,L2,NewConcept,L1),
 2923	!.
 2924
 2925conta(Type,Env,MS,_Done,[],L2,X,NewConcept,Z1,L10,Z1,L10) :-
 2926        !.
 2927conta(Type,Env,MS,Done,L,L2,X,NewConcept,Z1,L10,Z,L1) :-
 2928        check(Type,Env,MS,Done,L,L2,X,NewConcept,Z2,L11),
 2929 	union1(Z1,Z2,Za),delete1(Za,'top',Z),
 2930	union1(L10,L11,L1),
 2931        !.
 2932check1(_,_,_,_,[],_,[]) :- !.
 2933check1(Type,Env,MS,Done,[Y|L],NewConcept,[Y|L1]) :-
 2934	subsume1(Type,Env,MS,NewConcept,Y),
 2935	!,
 2936	check1(Type,Env,MS,[Y|Done],L,NewConcept,L1).
 2937check1(Type,Env,MS,Done,[Y|L],NewConcept,L1) :-
 2938	not(member(Y,Done)),
 2939	setofOrNil(Z,succ1(Type,Env,MS,Y,Z),L2),
 2940	check1(Type,Env,MS,[Y|Done],L2,NewConcept,L3),
 2941	check1(Type,Env,MS,[Y|Done],L,NewConcept,L4),
 2942	motel_union(L3,L4,L5),
 2943	deleteInList(L5,'top',L1),
 2944	!.
 2945check1(Type,Env,MS,Done,[Y|L],NewConcept,L1) :-
 2946	check1(Type,Env,MS,[Y|Done],L,NewConcept,L1),
 2947	!.
 2948
 2949make_succ1(Type,Env,MS,X,[Y|L],NewConcept) :- 
 2950	not(succ(Type,Env,MS,NewConcept,Y)),
 2951        retract1(succ(Type,Env,MS,X,Y)),
 2952%	assert1(succ(Type,Env,MS,NewConcept,Y)),
 2953        assert_succ(Type,Env,MS,NewConcept,Y),
 2954	!,
 2955	make_succ1(Type,Env,MS,X,L,NewConcept). 
 2956make_succ1(Type,Env,MS,X,[Y|L],NewConcept) :- 
 2957% 	assert1(succ(Type,Env,MS,x,NewConcept)),
 2958        assert_succ(Type,Env,MS,X,NewConcept),
 2959	!,
 2960	make_succ1(Type,Env,MS,X,L,NewConcept).
 2961make_succ1(Type,Env,MS,X,[],NewConcept) :- 
 2962%	assert1(succ(Type,Env,MS,X,NewConcept)),
 2963        assert_succ(Type,Env,MS,X,NewConcept),
 2964	!.
 2965
 2966
 2967/****************  practical funktions ******************************/	
 2968
 2969subsume1(Type,Env,MS,X,Y) :- var(X),!,fail.
 2970subsume1(Type,Env,MS,X,Y) :- var(Y),!,fail.
 2971subsume1(Type,Env,MS,X,'top') :- !,fail.
 2972subsume1(Type,Env,MS,'bot',X) :- !,fail.
 2973subsume1(Type,Env,MS,X,[]) :- !.
 2974subsume1(Type,Env,MS,X,'bot') :- !.
 2975subsume1(Type,Env,MS,'top',X) :- !.
 2976subsume1(Type,Env,MS,X,Y) :- 
 2977	sub(Type,Env,MS,X,Y),
 2978	!.
 2979subsume1(Type,Env,MS,X,Y) :- 
 2980	nsub(Type,Env,MS,X,Y),
 2981	!,
 2982	fail. 
 2983subsume1(Type,Env,MS,X,Y) :- 
 2984	X \== Y,
 2985	addCounter(Type,1),
 2986	subsumes(Type,Env,MS,X,Y), 
 2987	cont(Type,Env,MS,[],X,Y),
 2988	!.
 2989subsume1(Type,Env,MS,X,Y) :- 
 2990	X \== Y,
 2991	cont1a(Type,Env,MS,[],X,Y),
 2992	!,
 2993	fail.
 2994
 2995cont(Type,Env,MS,_,'top',Y).
 2996cont(Type,Env,MS,Done,X,Y) :- 
 2997	assert1(sub(Type,Env,MS,X,Y)),
 2998	succ1(Type,Env,MS,Z,X),
 2999	not(member(Z,Done)),
 3000	cont(Type,Env,MS,[Z|Done],Z,Y),!.
 3001cont(Type,Env,MS,_,X,Y). 
 3002cont1a(Type,Env,MS,_,'bot',X) :- 
 3003	!.
 3004cont1a(Type,Env,MS,_,X,'bot') :- 
 3005	!,fail.
 3006cont1a(Type,Env,MS,Done,X,Y) :-
 3007       member(X,Done), 
 3008       !.
 3009cont1a(Type,Env,MS,Done,X,Y) :-
 3010	assert1(nsub(Type,Env,MS,X,Y)),
 3011	succ1(Type,Env,MS,X,Z),
 3012	cont1a(Type,Env,MS,[X|Done],Z,Y),
 3013	!.
 3014
 3015delete1([X|R],'top',Z) :-
 3016	deleteInList([X|R],'top',Z),
 3017	!.
 3018delete1(X,'top',Z) :-
 3019	!.
 3020
 3021union1([],[],[]).
 3022union1([X|R],[Y|R1],Z):-
 3023	motel_union([X|R],[Y|R1],Z),
 3024	!.
 3025union1([X|R],Y,Z) :-
 3026	motel_union([X|R],[Y],Z),
 3027	!.
 3028union1([X],Y,Z) :-
 3029	motel_union([X],[Y],Z),
 3030	!.
 3031union1(X,[Y],Z) :-
 3032	motel_union([X],[Y],Z),
 3033	!.
 3034union1(X,[Y|R],Z) :-
 3035	motel_union([X],[Y|R],Z),
 3036	!.
 3037union1(X,Y,Z) :-
 3038	motel_union([X],[Y],Z),
 3039	!.
 3040
 3041assert1(G) :- 
 3042	\+ (G),
 3043	assert_logged(G),
 3044	!.
 3045assert1(G) :-
 3046	!.
 3047% assert_succ wurde wegen eines Fehlers in direct_succ(bzw conta) veraendert
 3048% duerfte an der Laufzeit nicht sehr viel ausmachen,ging auch einfacher
 3049% als den Fehler zu finden...
 3050
 3051assert_succ(Type,Env,MS,X,X) :-
 3052	!.
 3053/*
 3054assert_succ(Type,Env,MS,X,RorC) :-
 3055	assert1(succ(Type,Env,MS,X,RorC)),
 3056	cont(Type,Env,MS,[],X,RorC),
 3057	!.
 3058*/
 3059assert_succ(Type,Env,MS,X,RorC) :-
 3060	cont(Type,Env,MS,[],X,RorC),
 3061	not((sub(Type,Env,MS,X,Y),not(var(Y)),sub(Type,Env,MS,Y,RorC),Y \== RorC)),
 3062	assert1(succ(Type,Env,MS,X,RorC)),
 3063	!.
 3064assert_succ(Type,Env,MS,X,RorC).
 3065
 3066
 3067retract1(G) :- 
 3068	retract(G),
 3069	!.
 3070retract1(G) :- 
 3071	!.
 3072
 3073succ1(Type,Env,MS,X,Y) :- 
 3074	succ(Type,Env,MS,X,Y).
 3075%	!.
 3076succ1(Type,Env,MS,X,'bot').
 3077% 	:-  !.
 3078
 3079/*****************************************************************************/
 3080/***************** print and statistic - functions ***************************/
 3081newShowHierarchy :-
 3082	show_dag.
 3083
 3084show_dag :-
 3085	currentEnvironment(Env),
 3086	show_dag(Env,[]).
 3087show_dag(MS) :-
 3088	currentEnvironment(Env),
 3089	show_dag(Env,MS).
 3090show_dag(Env,MS) :-
 3091	!,
 3092	print('Concepts'),nl,
 3093        not(show_dag(concepts,Env,MS,'top',[])),nl,nl,
 3094	print('Roles'),nl,
 3095	not(show_dag(roles,Env,MS,'top',[])).
 3096show_dag(Type,Env,MS,'bot',_) :- !,fail.
 3097show_dag(Type,Env,MS,Node,L) :-
 3098	writes(L),
 3099	print(Node),nl,
 3100	succ(Type,Env,MS,Node,N),
 3101  	show_dag(Type,Env,MS,N,[45|L]),
 3102	fail.
 3103
 3104initStat :-
 3105	!,
 3106	setCounter(subsumptionTests,0),
 3107	setCounter(concepts,0),
 3108	setCounter(roles,0),
 3109	setCounter(conceptsClassified,0),
 3110	setCounter(rolesClassified,0),
 3111	getRuntime(T0),
 3112	setCounter(runtime,T0),
 3113	!.
 3114getStat(CN,CST,RN,RST,T) :-
 3115	!,
 3116	getRuntime(T1),
 3117	getCounter(subsumptionTests,ST),
 3118	getCounter(concepts,CST),
 3119	getCounter(conceptsClassified,CN),
 3120	getCounter(roles,RST),
 3121	getCounter(rolesClassified,RN),
 3122	getCounter(runtime,T0),
 3123	T is T1 - T0,
 3124	!.
 3125printStat :-
 3126	!,
 3127	getStat(CN,CST,RN,RST,T),
 3128	format('Concepts classified:         ~d~n',CN),
 3129	format('Subsumption tests performed: ~d~n',CST),
 3130	format('Roles    classified:         ~d~n',RN),
 3131	format('Subsumption tests performed: ~d~n',RST),
 3132	format('Total runtime:               ~3d sec.~2n',T),
 3133	!.
 3134
 3135buildOrdering(Env,MS,CTree,RTree) :- 
 3136	buildOrdering(concepts,Env,MS,'top',[],CTree),
 3137	buildOrdering(roles,Env,MS,'top',[],RTree),
 3138	!.
 3139
 3140
 3141buildOrdering(Type,Env,MS,'bot',Done,node(['bot'|EquivClass],[])) :-
 3142	!,
 3143	setofOrNil(Z2,(succ(Type,Env,MS,'bot',Z2),succ(Type,Env,MS,Z2,'bot')),EquivClass),
 3144	!.
 3145buildOrdering(Type,Env,MS,Concept1,Done,node([Concept1|EquivClass],SubtreeList)) :-
 3146	setofOrNil(Z1,succ(Type,Env,MS,Concept1,Z1),S1),
 3147	setofOrNil(Z2,(succ(Type,Env,MS,Concept1,Z2),succ(Type,Env,MS,Z2,Concept1)),EquivClass),
 3148	successorSet(S1,EquivClass,Succ),
 3149	append(Done,[Concept1|EquivClass],Done1),
 3150	buildOrderingList(Type,Env,MS,Succ,Done1,SubtreeList).
 3151
 3152buildOrderingList(_Type,_Env,_MS,[],_Done,[]) :-
 3153	!.
 3154buildOrderingList(Type,Env,MS,[C1|CL],Done,SubtreeList) :-
 3155	member(C1,Done),
 3156	!,
 3157	buildOrderingList(Type,Env,MS,CL,Done,SubtreeList).
 3158buildOrderingList(Type,Env,MS,[C1|CL],Done,[Subtree|SubtreeList]) :-
 3159	buildOrdering(Type,Env,MS,C1,Done,Subtree),
 3160	buildOrderingList(Type,Env,MS,CL,Done,SubtreeList),
 3161	!.
 3162
 3163successorSet(S1,EquivClass,S2) :-
 3164	successor_set(S1,EquivClass,S3),
 3165	((S3 \== [], S2 = S3) ; (S2 = ['bot'])),
 3166	!.
 3167
 3168successor_set([],_,[]) :-
 3169	!.
 3170successor_set([C1|CL],EquivClass,S2) :-
 3171	member(C1,EquivClass),
 3172	!,
 3173	successor_set(CL,EquivClass,S2).
 3174successor_set(['bot'|CL],EquivClass,S2) :-
 3175	!,
 3176	successor_set(CL,EquivClass,S2).
 3177successor_set([C1|CL],EquivClass,[C1|S2]) :-
 3178	successor_set(CL,EquivClass,S2).
 3179/**********************************************************************
 3180 *
 3181 * @(#) compileEnv.pl 1.9@(#)
 3182 *
 3183 */
 3184
 3185/**********************************************************************
 3186 *
 3187 * compileEnvironment(FileName)
 3188 * 
 3189 */
 3190
 3191compileEnvironment(FileName) :-
 3192	see(FileName),
 3193	read(environment(EnvName,_Env,_Comment)),
 3194	seen,
 3195	compileEnvironment(FileName,EnvName),
 3196	!.
 3197compileEnvironment(FileName) :-
 3198	% Some file handling error has occured
 3199	seen,
 3200	!, 
 3201	fail.
 3202
 3203compileEnvironment(FileName,EnvName) :-
 3204	see(FileName),
 3205	read(environment(_EnvName,Env,Comment)),
 3206	(removeEnvironment(EnvName) ; true),
 3207	termExpansion(on,Env,CPList),
 3208	tell('/tmp/compile.tmp'),
 3209	write((:- dynamic(constraint/8))), write('.'), nl,
 3210	write((:- dynamic(numb/1))), write('.'), nl,
 3211%	write((:- dynamic(in/9))), write('.'), nl,
 3212%	write((:- dynamic(kb_in/10))), write('.'), nl,
 3213	write((:- dynamic(falsum/2))), write('.'), nl,
 3214%	write((:- dynamic(conceptName/4))), write('.'), nl,
 3215%	write((:- dynamic(roleName/4))), write('.'), nl,
 3216%	write((:- dynamic(conceptEqualSets/6))), write('.'), nl,
 3217	write((:- dynamic(conceptSubsets/6))), write('.'), nl,
 3218%	write((:- dynamic(eq/9))), write('.'), nl,
 3219	write((:- dynamic(inconsistencyCheck/3))), write('.'), nl,
 3220	write((:- dynamic(roleEqualSets/6))), write('.'), nl,
 3221	write((:- dynamic(roleSubsets/6))), write('.'), nl,
 3222	write((:- dynamic(conceptElement/7))), write('.'), nl,
 3223	write((:- dynamic(roleElement/8))), write('.'), nl,
 3224	write((:- dynamic(closed/5))), write('.'), nl,
 3225	write((:- dynamic(sub/5))), write('.'), nl,
 3226	write((:- dynamic(succ/5))), write('.'), nl,
 3227	write((:- dynamic(nsub/5))), write('.'), nl,
 3228	write((:- dynamic(sub3/2))), write('.'), nl,
 3229	write((:- dynamic(succ3/2))), write('.'), nl,
 3230	write((:- dynamic(nsub3/2))), write('.'), nl,
 3231	write((:- dynamic(abductiveDerivation/3))), write('.'), nl,
 3232	write((:- dynamic(consistencyDerivation/3))), write('.'), nl,
 3233	write((:- dynamic(hypothesis/1))), write('.'), nl,
 3234	write((:- dynamic(roleDomain/4))), write('.'), nl,
 3235	write((:- dynamic(roleRange/4))), write('.'), nl,
 3236	write((:- dynamic(roleDefault/4))), write('.'), nl,
 3237	write((:- dynamic(roleNr/4))), write('.'), nl,
 3238	write((:- dynamic(roleDefNr/4))), write('.'), nl,
 3239	write((:- dynamic(roleAttributes/5))), write('.'), nl,
 3240%	write((:- dynamic(given_inflLink/4))), write('.'), nl,
 3241%	write((:- dynamic(given_change/4))), write('.'), nl,
 3242%       write((:- dynamic(value/2))), write('.'), nl,
 3243	write((:- dynamic(motel_option/2))), write('.'), nl,
 3244%	write((:- dynamic(environment/3))), write('.'), nl,
 3245%	write((:- dynamic(conceptHierarchy/3))), write('.'), nl,
 3246%	write((:- dynamic(roleHierarchy/3))), write('.'), nl,
 3247	write((:- dynamic(modalAxiom/6))), write('.'), nl,
 3248%	write((:- dynamic(rel/5))), write('.'), nl,
 3249	write((:- dynamic(compiledPredicate/2))), write('.'), nl,
 3250	writeq((:- asserta_logged(environment(EnvName,Env,Comment)))), write('.'), nl,
 3251	writeq((:- retractall_head(currentEnvironment(_)))), write('.'), nl,
 3252	writeq((:- asserta_logged(currentEnvironment(Env)))), write('.'), nl,
 3253	writeCompiledPredicateFactsToFile(Env,CPList),
 3254	expand_term((in(Env,Name,modal(MS),CN,CON,hyp(HYP),
 3255                        ab(D),call(CALL),PT) :-
 3256	                   kb_in(Env,pr(5),Name,modal(MS),CN,CON,hyp(HYP),
 3257                                 ab(D),call(CALL),PT)),
 3258		    InClause1),
 3259	writeq(InClause1), write('.'), nl,
 3260	expand_term((in(Env,Name,modal(MS),CN,CON,
 3261                        hyp([or(H1),rl(H2),fl(H3)]),ab(noAb),call(CALL),PT) :-
 3262		           clashInHyp(H2), !, fail),
 3263		    InClause2),
 3264	writeq(InClause2), write('.'), nl,
 3265	expand_term(in(Env,X2,X3,X4,X5,X6,X7,X8,X9), Head3),
 3266	writeq((Head3 :- kb_in(Env,pr(3),X2,X3,X4,X5,X6,X7,X8,X9))),
 3267	write('.'), nl,
 3268	expand_term((in(Env,Name,modal(MS),CN,CON,hyp(HYP),
 3269                        ab(D),call(CALL),PT) :-
 3270		          (CN \== 'top', CN \== 'bot', CN \== not('top'), 
 3271                           CN \== not('bot'),
 3272	                   kb_in(Env,pr(3),Name,modal(MS),CN,CON,hyp(HYP),
 3273                                 ab(D),call(CALL),PT))),
 3274		    InClause4),
 3275	writeq(InClause4), write('.'), nl,
 3276	expand_term((in(Env,Name,modal(MS),CN,CON,hyp(HYP),
 3277                        ab(D),call(CALL),PT) :-
 3278		          (CN \== 'top',CN \== 'bot', CN \== not('top'), 
 3279                           CN \== not('bot'),
 3280			   kb_in(Env,pr(1),Name,modal(MS),CN,CON,hyp(HYP),
 3281				 ab(D),call(CALL),PT))),
 3282		    InClause5),
 3283	writeq(InClause5), write('.'), nl,
 3284	repeat,
 3285	read(Clause),
 3286	treatClause(Clause),
 3287	seen,
 3288	told,
 3289	assertConnectionClauses(Env),
 3290	termExpansion(off,Env),
 3291	compile('/tmp/compile.tmp'),
 3292	!.
 3293compileEnvironment(FileName,EnvName) :-
 3294	% Some file handling error has occured
 3295	seen,
 3296	told,
 3297	!,
 3298	fail.
 3299
 3300treatClause('end_of_file') :-
 3301	!.
 3302treatClause((:-dynamic Pred/Arity)) :-
 3303%	write((:-dynamic Pred/Arity)), write('.'), nl,
 3304	!,
 3305	fail.
 3306treatClause((in(_X1,_X2,_X3,_X4,_X5,_X6,_X7,_X8,_X9) :- _Body)) :-
 3307	!,
 3308	fail.
 3309treatClause(X) :-
 3310	expand_term(X,Y),
 3311	writeq(Y), write('.'), nl,
 3312	!,
 3313	fail.
 3314
 3315writeCompiledPredicateFactsToFile(Env,[]) :-
 3316	!.
 3317writeCompiledPredicateFactsToFile(Env,[Pred/Arity|List]) :-
 3318	writeq((compiledPredicate(Env,Pred/Arity))),
 3319	write('.'), nl,
 3320	writeCompiledPredicateFactsToFile(Env,List).
 3321
 3322assertConnectionClauses(Env) :-
 3323	expand_term(constraint(Env,X2,X3,X4,X5,X6,X7,X8),CompConAtom),
 3324	assertz_logged((constraint(Env,X2,X3,X4,X5,X6,X7,X8) :-
 3325		 CompConAtom)),
 3326	expand_term(eq(Env,X2,X3,X4,X5,X6,X7,X8,X9),CompEqAtom),
 3327	assertz_logged((eq(Env,X2,X3,X4,X5,X6,X7,X8,X9) :-
 3328		 CompEqAtom)),
 3329	expand_term(in(Env,X2,X3,X4,X5,X6,X7,X8,X9),CompInAtom),
 3330	assertz_logged((in(Env,X2,X3,X4,X5,X6,X7,X8,X9) :-
 3331		 CompInAtom)),
 3332%	assertz_logged((kb_in(Env,X2,X3,X4,X5,X6,X7,X8,X9,X10) :-
 3333%		 comp_kb_in(Env,X2,X3,X4,X5,X6,X7,X8,X9,X10))),
 3334	expand_term(rel(Env,X2,X3,X4,X5),CompRelAtom),
 3335	assertz_logged((rel(Env,X2,X3,X4,X5) :-
 3336		 CompRelAtom)),
 3337	!.
 3338
 3339termExpansion(on,env(Id),
 3340              [CompCon/8,CompEq/9,CompIn/9,CompKb_in/10,CompRel/6]) :-
 3341	% Generate the names for the compiled in, kb_in, constraint, and rel
 3342	% predicates in environment Id.
 3343	name(Id,IdChars),
 3344	name(in,InChars),
 3345	append(InChars,[95,99,95|IdChars],CompInChars),
 3346	name(CompIn,CompInChars),
 3347	name(constraint,ConChars),
 3348	append(ConChars,[95,99,95|IdChars],CompConChars),
 3349	name(CompCon,CompConChars),
 3350	name(eq,EqChars),
 3351	append(EqChars,[95,99,95|IdChars],CompEqChars),
 3352	name(CompEq,CompEqChars),
 3353	name('kb_in',Kb_inChars),
 3354	append(Kb_inChars,[95,99,95|IdChars],CompKb_inChars),
 3355	name(CompKb_in,CompKb_inChars),
 3356	name('rel',RelChars),
 3357	append(RelChars,[95,99,95|IdChars],CompRelChars),
 3358	name(CompRel,CompRelChars),
 3359	% Abolish any previously asserted clauses for the 
 3360	% compiled predicades
 3361	abolish(CompCon/8),
 3362	abolish(CompEq/9),
 3363	abolish(CompIn/9),
 3364	abolish(CompKb_in/10),
 3365	abolish(CompRel/6),
 3366	% Generate the atoms for these predicates 
 3367	CompConAtom =.. [CompCon|[X4,X1,X2,X3,X5,X6,X7,X8]],
 3368	CompEqAtom =.. [CompEq|[X4-X5,X1,X2,X3,X6,X7,X8,X9]],
 3369	CompInAtom =.. [CompIn|[X4-X5,X1,X2,X3,X6,X7,X8,X9]],
 3370	CompKb_inAtom =.. [CompKb_in|[X5-X6,X1,X2,X3,X4,X7,X8,X9,X10]],
 3371	CompRelAtom =.. [CompRel|[X1,X2,X3,X4,X5,X6]],
 3372	% Assert the term_expansion rules needed to translate the
 3373	% interpreted clauses into compiled clauses.
 3374	abolish(term_expansion/2),
 3375	assertz_logged((term_expansion((Head :- Body),(Head1 :- Body1)) :-
 3376	term_expansion(Head,Head1),
 3377	term_expansion(Body,Body1))),
 3378	assertz_logged((term_expansion((L, Body), (L1,Body1)) :-
 3379	term_expansion(L,L1),
 3380	term_expansion(Body,Body1))),
 3381	assertz_logged((term_expansion((L; Body), (L1,Body1)) :-
 3382	term_expansion(L,L1),
 3383	term_expansion(Body,Body1))),
 3384	assertz_logged((term_expansion(\+Atom,\+Atom1) :-
 3385	term_expansion(Atom,Atom1))),
 3386	assertz_logged((term_expansion(constraint(X1,X2,X3,X4,X5,X6,X7,X8),
 3387				CompConAtom))),
 3388	assertz_logged((term_expansion(eq(X1,X2,X3,X4,X5,X6,X7,X8,X9),
 3389				CompEqAtom))),
 3390	assertz_logged((term_expansion(in(X1,X2,X3,X4,X5,X6,X7,X8,X9),
 3391				CompInAtom))),
 3392	assertz_logged((term_expansion(kb_in(X1,X2,X3,X4,X5,X6,X7,X8,X9,X10),
 3393				CompKb_inAtom))),
 3394	assertz_logged((term_expansion(rel(X1,X2,X3,X4,X5),
 3395				CompRelAtom))),
 3396	assertz_logged((term_expansion(once(Body1),once(Body2)) :-
 3397		term_expansion(Body1,Body2))),
 3398	assertz_logged((term_expansion(call(Body1),call(Body2)) :-
 3399		 term_expansion(Body1,Body2))),
 3400	assertz_logged(term_expansion(X,X)),
 3401	!.
 3402termExpansion(off,_) :-
 3403	abolish(term_expansion/2),
 3404	!.
 3405
 3406/**********************************************************************
 3407 *
 3408 * @(#) cnf.pl 1.2@(#)
 3409 *
 3410 */
 3411
 3412/***********************************************************************
 3413 *
 3414 * negate(+C1,-C2)
 3415 * C2 is just the term not(C1).
 3416 *
 3417 */
 3418
 3419negate(not(C1),C1) :- !.
 3420negate(C1,not(C1)) :- !.
 3421
 3422/***********************************************************************
 3423 *
 3424 * invert(+R1,-R2)
 3425 * R2 is just the term inverse(R1).
 3426 *
 3427 */
 3428
 3429invert(inverse(R),R) :- !.
 3430invert(R,inverse(R)) :- !.
 3431
 3432/***********************************************************************
 3433 *
 3434 * normalizeNot(+C1,-C2)
 3435 * applies the laws
 3436 *      not(and([A,B]))   -> and([not(A),not(B)])
 3437 *      not(or([A,B]))    -> or([not(A),not(B)])
 3438 *      not(not(A))       -> A
 3439 *      not(all(R,C))     -> some(R,not(C))
 3440 *      not(some(R,C))    -> all(R,not(C))
 3441 *      not(atleast(N,R)) -> atmost(N-1,R)
 3442 *      not(atmost(N,R))  -> atleast(N+1,R)
 3443 *      not(b(O,P,C))     -> d(O,P,not(C))
 3444 *      not(d(O,P,C))     -> b(O,P,not(C))
 3445 * to C1 as long as possible to get C2.
 3446 *
 3447 */
 3448
 3449normalizeNot(not(and([C1,C2|L1])),or(L3)) :-
 3450	!,
 3451	hop_map(negate,[C1,C2|L1],L2),
 3452	hop_map(normalizeNot,L2,L3).
 3453normalizeNot(not(and([C1])),C2) :-
 3454	negate(C1,C2).
 3455normalizeNot(not(and([])),'bot') :-
 3456	!.
 3457normalizeNot(not(set(L)),not(set(L))) :-
 3458	!.
 3459normalizeNot(not(or([C1,C2|L1])),and(L3)) :-
 3460	!,
 3461	hop_map(negate,[C1,C2|L1],L2),
 3462	hop_map(normalizeNot,L2,L3).
 3463normalizeNot(not(or([C1])),C2) :-
 3464	negate(C1,C2).
 3465normalizeNot(not(or([])),'top') :-
 3466	!.
 3467normalizeNot(not(all(R,C1)),some(R,C2)) :-
 3468	normalizeNot(not(C1),C2).
 3469normalizeNot(not(some(R,C1)),all(R,C2)) :-
 3470	normalizeNot(not(C1),C2).
 3471normalizeNot(not(atleast(N,R)),atmost(M,R)) :-
 3472	M is N-1.
 3473normalizeNot(not(atmost(N,R)),atleast(M,R)) :-
 3474	M is N+1.
 3475normalizeNot(not(b(O,P,C1)),d(O,P,C2)) :-
 3476	normalizeNot(not(C1),C2).
 3477normalizeNot(not(d(O,P,C1)),b(O,P,C2)) :-
 3478	normalizeNot(not(C1),C2).
 3479normalizeNot(not(bc(O,P,C1)),dc(O,P,C2)) :-
 3480	normalizeNot(not(C1),C2).
 3481normalizeNot(not(dc(O,P,C1)),bc(O,P,C2)) :-
 3482	normalizeNot(not(C1),C2).
 3483normalizeNot(not(b(O,P)),d(O,P)) :-
 3484	!.
 3485normalizeNot(not(d(O,P)),b(O,P)) :-
 3486	!.
 3487normalizeNot(not(bc(O,P)),dc(O,P)) :-
 3488	!.
 3489normalizeNot(not(dc(O,P)),bc(O,P)) :-
 3490	!.
 3491normalizeNot(not(not(C1)),C3) :-
 3492	normalizeNot(C1,C3).
 3493normalizeNot(not(set([])),'top') :- !.
 3494normalizeNot(C1,C1).
 3495
 3496/***********************************************************************
 3497 *
 3498 * normalizeInverse(+R1,-R2)
 3499 * applies the laws
 3500 *      inverse(and([R,S])) -> and([inverse(R),inverse(S)])
 3501 *      inverse(inverse(R)) -> R
 3502 * to R1 as long as possible to get R2.
 3503 *
 3504 */
 3505
 3506normalizeInverse(inverse(and(RL1)),and(RL3)) :-
 3507	hop_map(invert,RL1,RL2),
 3508	hop_map(normalizeInverse,RL2,RL3),
 3509	!.
 3510normalizeInverse(inverse(inverse(R1)),R3) :-
 3511	normalizeInverse(R1,R3).
 3512normalizeInverse(R1,R1).
 3513
 3514/***********************************************************************
 3515 *
 3516 * motel_flatten(+C1,-C2)
 3517 * deletes unnecessary occurrences of `and' and `or' in C1 to get C2.
 3518 *
 3519 */
 3520
 3521motel_flatten(and(L1),and(L2)) :-
 3522	!,
 3523	hop_map(motel_flatten,L1,L3),
 3524	flattenAnd([],L3,L2).
 3525motel_flatten(or(L1),or(L2)) :-
 3526	!,
 3527	hop_map(motel_flatten,L1,L3),
 3528	flattenOr([],L3,L2).
 3529motel_flatten(set(L1),set(L1)) :-
 3530	!.
 3531motel_flatten(all(R1,C1),all(R2,C2)) :-
 3532	motel_flatten(R1,R2),
 3533	motel_flatten(C1,C2).
 3534motel_flatten(some(R1,C1),some(R2,C2)) :-
 3535	motel_flatten(R1,R2),
 3536	motel_flatten(C1,C2).
 3537motel_flatten(atleast(N,R1),atleast(N,R2)) :-
 3538	motel_flatten(R1,R2).
 3539motel_flatten(atmost(N,R1),atmost(N,R2)) :-
 3540	motel_flatten(R1,R2).
 3541motel_flatten(b(O,P,C1),b(O,P,C2)) :-
 3542	motel_flatten(C1,C2).
 3543motel_flatten(d(O,P,C1),d(O,P,C2)) :-
 3544	motel_flatten(C1,C2).
 3545motel_flatten(bc(O,P,C1),bc(O,P1,C2)) :-
 3546	motel_flatten(P,P1),
 3547	motel_flatten(C1,C2).
 3548motel_flatten(dc(O,P,C1),dc(O,P1,C2)) :-
 3549	motel_flatten(P,P1),
 3550	motel_flatten(C1,C2).
 3551motel_flatten(not(C1),not(C2)) :-
 3552	!,
 3553	motel_flatten(C1,C2).
 3554motel_flatten(inverse(R1),inverse(R2)) :-
 3555	motel_flatten(R1,R2).
 3556motel_flatten(C1,C1).
 3557
 3558
 3559/***********************************************************************
 3560 *
 3561 * flattenAnd(+L1,+L2,-L3)
 3562 * eliminates occurrences of `and' in L2 to get L2'. L3 is the result
 3563 * of appending L2' to L1.
 3564 *
 3565 */
 3566
 3567flattenAnd(L1,[and(L2)|L3],L4) :-
 3568	!,
 3569%	flattenAnd([],L2,L5),
 3570	L5 = L2,
 3571	append(L1,L5,L6),
 3572	flattenAnd(L6,L3,L4).
 3573flattenAnd(L1,[C1|L3],L4) :-
 3574	append(L1,[C1],L6),
 3575	flattenAnd(L6,L3,L4).
 3576flattenAnd(L1,[],L1).
 3577
 3578/***********************************************************************
 3579 *
 3580 * flattenOr(+L1,+L2,-L3)
 3581 * eliminates occurrences of `or' in L2 to get L2'. L3 is the result
 3582 * of appending L2' to L1.
 3583 *
 3584 */
 3585 
 3586flattenOr(L1,[or(L2)|L3],L4) :-
 3587	!,
 3588%	flattenOr([],L2,L5),
 3589	L5 = L2,
 3590	append(L1,L5,L6),
 3591	flattenOr(L6,L3,L4).
 3592flattenOr(L1,[C1|L3],L4) :-
 3593	append(L1,[C1],L6),
 3594	flattenOr(L6,L3,L4).
 3595flattenOr(L1,[],L1).
 3596
 3597/***********************************************************************
 3598 *
 3599 * distributeAnd(and(+L1),or(+L2),or(-L3))
 3600 * here or(L3) has the form
 3601 *     or([C_1,...,C_n])
 3602 * where C_i is the result of applying de Morgan's laws to
 3603 * and(L1|[A_i]) 
 3604 * where A_i is the ith element of L2.
 3605 *
 3606 */
 3607
 3608distributeAnd(and(L1),or([C2|L2]),or([C3|L4])) :-
 3609	% L3 := L1 and C2
 3610	append(L1,[C2],L3),
 3611	% C3 := deMorganAnd(L3)
 3612	deMorgan(and(L3),C3),
 3613	% build other disjuncts
 3614	distributeAnd(and(L1),or(L2),or(L4)).
 3615distributeAnd(and(_L1),or([]),or([])).
 3616
 3617/***********************************************************************
 3618 *
 3619 * distributeOr(or(+L1),and(+L2),and(-L3))
 3620 * here and(L3) has the form
 3621 *     and([C_1,...,C_n])
 3622 * where C_i is the result of applying de Morgan's laws to
 3623 * or(L1|[A_i]) 
 3624 * where A_i is the ith element of L2.
 3625 *
 3626 */
 3627	
 3628distributeOr(or(L1),and([C2|L2]),and([C3|L4])) :-
 3629	% L3 := L1 or C2
 3630	append(L1,[C2],L3),
 3631	% C3 := deMorgan(L3)
 3632	deMorgan(or(L3),C3),
 3633	% build other conjuncts
 3634	distributeOr(or(L1),and(L2),and(L4)).
 3635distributeOr(or(_L1),and([]),and([])).
 3636	
 3637/***********************************************************************
 3638 *
 3639 * deMorganAnd(+L1,+L2,-C1)
 3640 * applies de Morgan's law
 3641 *      and([A,or([B,C])]) -> or([and([A,B]),and([A,C])])
 3642 * to and(L1|L2) as long as possible to get C1.
 3643 *
 3644 */
 3645
 3646deMorganAnd(L1,[or(L2)|L3],L4) :-
 3647	append(L1,L3,L5),
 3648	distributeAnd(and(L5),or(L2),L4).
 3649deMorganAnd(L1,[C1|L3],L4) :-
 3650	append(L1,[C1],L5),
 3651	deMorganAnd(L5,L3,L4).
 3652deMorganAnd(L1,[],and(L1)).
 3653
 3654/***********************************************************************
 3655 *
 3656 * deMorganOr(+L1,+L2,-C1)
 3657 * applies de Morgan's law
 3658 *      or([A,or([B,C])]) -> and([or([A,B]),or([A,C])])
 3659 * to or(L1|L2) as long as possible to get C1.
 3660 *
 3661 */
 3662
 3663deMorganOr(L1,[and(L2)|L3],L4) :-
 3664	append(L1,L3,L5),
 3665	distributeOr(or(L5),and(L2),L4).
 3666deMorganOr(L1,[C1|L3],L4) :-
 3667	append(L1,[C1],L5),
 3668	deMorganOr(L5,L3,L4).
 3669deMorganOr(L1,[],or(L1)).
 3670
 3671/***********************************************************************
 3672 *
 3673 * deMorgan(+C1,-C2)
 3674 * applies de Morgan's laws to C1
 3675 *      and([A,or([B,C])]) -> or([and([A,B]),and([A,C])])
 3676 *      or([A,and([B,C])]) -> and([or([A,B]),or([A,C])])
 3677 * as long as possible to get C2.
 3678 *
 3679 */
 3680
 3681deMorgan(and(L1),C1) :-
 3682	deMorganAnd([],L1,C1).
 3683deMorgan(or(L1),C1) :-
 3684	deMorganOr([],L1,C1).
 3685deMorgan(C1,C1) :-
 3686	!.
 3687
 3688/***********************************************************************
 3689 *
 3690 * cnf(+C1,-C2)
 3691 * C2 is the conjunctive normalform of C1.
 3692 *
 3693 */
 3694
 3695cnf(C1,C6) :-
 3696	normalizeNot(C1,C2),
 3697	motel_flatten(C2,C3),
 3698	normalizeInverse(C3,C4),
 3699	deMorgan(C4,C5),
 3700	motel_flatten(C5,C6).
 3701
 3702/**********************************************************************
 3703 *
 3704 * @(#) conceptFunctions.pl 1.5@(#)
 3705 *
 3706 */
 3707
 3708/***********************************************************************
 3709 *
 3710 * memberConcept(+Concept,+Dag)
 3711 * Arguments: Concept     concept name
 3712 *            Dag         subsumption hierarchy
 3713 * checks wether or not Concept occurs in the subsumption hierarchy.
 3714 *
 3715 */
 3716
 3717memberConcept(Concept,Dag) :-
 3718	memberElement(Concept,Dag).
 3719
 3720memberConceptSubtrees(Concept,List) :-
 3721	memberElementSubtrees(Concept,List).
 3722
 3723/***********************************************************************
 3724 *
 3725 * memberDirectSubConcepts(+Concept,+Dag)
 3726 * Arguments: Concept     concept name
 3727 *            Dag         subsumption hierarchy
 3728 * checks wether or not Concept occurs in the direct subconcepts of
 3729 * the 'top' concept of Dag.
 3730 *
 3731 */
 3732
 3733memberDirectSubConcepts(Concept,node(_CL,NL)) :-
 3734	!,
 3735	memberDirectSubElements(Concept,NL).
 3736
 3737memberDirectSubConcepts(Concept,List) :-
 3738	memberDirectSubElements(Concept,List).
 3739
 3740/***********************************************************************
 3741 *
 3742 * getDirectSuperConcepts(+EnvName,+MS,+Concept,-CL)
 3743 * Arguments: EnvName     environment identifier
 3744 *            MS          modal context
 3745 *            Concept     concept name
 3746 *            CL          list of concept names
 3747 * CL is the list of all concept names which are direct super concepts
 3748 * of Concept.
 3749 *
 3750 */
 3751
 3752getDirectSuperConcepts(EnvName,MS,Concept,CL) :-
 3753	environment(EnvName,Env,_),
 3754	conceptHierarchy(Env,MS,Dag),
 3755	getDirectSuperElements(Concept,CL,Dag).
 3756
 3757
 3758/***********************************************************************
 3759 *
 3760 * getAllSuperConcepts(+EnvName,+MS,+Concept,-CL)
 3761 * Arguments: EnvName     environment identifier
 3762 *            MS          modal context
 3763 *            Concept     concept name
 3764 *            CL          list of concept names
 3765 * CL is the list of all concept names which are super concepts of
 3766 * Concept.
 3767 *
 3768 */
 3769
 3770getAllSuperConcepts(EnvName,MS,Concept,CL) :-
 3771	environment(EnvName,Env,_),
 3772	conceptHierarchy(Env,MS,Dag),
 3773	getAllSuperElements(Concept,CL,Dag).
 3774
 3775/***********************************************************************
 3776 *
 3777 * getDirectSubConcepts(+EnvName,+MS,+Concept,-CL)
 3778 * Arguments: EnvName     environment identifier
 3779 *            MS          modal context
 3780 *            Concept     concept name
 3781 *            CL          list of concept names
 3782 * CL is the list of all concept names which are direct super concepts
 3783 * of Concept.
 3784 *
 3785 */
 3786
 3787getDirectSubConcepts(EnvName,MS,Concept,CL) :-
 3788	environment(EnvName,Env,_),
 3789	conceptHierarchy(Env,MS,Dag),
 3790	getDirectSubElements(Concept,CL,Dag).
 3791
 3792/***********************************************************************
 3793 *
 3794 * getAllSubConcepts(+EnvName,+MS,+Concept,-CL)
 3795 * Arguments: EnvName     environment identifier
 3796 *            MS          modal context
 3797 *            Concept     concept name
 3798 *            CL          list of concept names
 3799 * CL is the list of all concept names which are super concepts of 
 3800 * Concept.
 3801 *
 3802 */
 3803
 3804getAllSubConcepts(EnvName,MS,Concept,CL) :-
 3805	environment(EnvName,Env,_),
 3806	conceptHierarchy(Env,MS,Dag),
 3807	getAllSubElements(Concept,CL,Dag).
 3808
 3809/***********************************************************************
 3810 *
 3811 * getConcepts(+MS,-CL)
 3812 * Arguments: EnvName     environment identifier
 3813 *            MS          modal context
 3814 *            CL          list of concept names
 3815 * CL is the list of all concept names in the subsumption hierarchy.
 3816 *
 3817 */
 3818
 3819getConcepts(EnvName,MS,['top'|CL]) :-
 3820	getAllSubConcepts(EnvName,MS,'top',CL).
 3821
 3822/***********************************************************************
 3823 *
 3824 * testDirectSuperConcept(+EnvName,+MS,+Concept1,+Concept2,-Concept)
 3825 * Arguments: EnvName        environment identifier
 3826 *            MS             modal context
 3827 *            Concept1       concept name
 3828 *            Concept2       concept name
 3829 *            Concept        concept name
 3830 * Concept is Concept1 iff Concept1 is a direct superconcept of Concept2
 3831 * or
 3832 * Concept is Concept2 iff Concept2 is a direct superconcept of Concept1
 3833 * otherwise
 3834 * the predicate fails.
 3835 *
 3836 */
 3837
 3838testDirectSuperConcept(EnvName,MS,Concept1,Concept2,Concept) :-
 3839	environment(EnvName,Env,_),
 3840	conceptHierarchy(Env,MS,Dag),
 3841	testDirectSuperElement(Concept1,Concept2,Concept,Dag).
 3842
 3843/***********************************************************************
 3844 *
 3845 * testDirectSubConcept(+EnvName,+MS,+Concept1,+Concept2,-Concept)
 3846 * Arguments: EnvName        environment identifier
 3847 *            MS             modal context
 3848 *            Concept1       concept name
 3849 *            Concept2       concept name
 3850 *            Concept        concept name
 3851 * Concept is Concept1 iff Concept1 is a direct subconcept of Concept2
 3852 * or
 3853 * Concept is Concept2 iff Concept2 is a direct subconcept of Concept1
 3854 * otherwise
 3855 * the predicate fails.
 3856 *
 3857 */
 3858
 3859testDirectSubConcept(EnvName,MS,Concept1,Concept2,Concept) :-
 3860	environment(EnvName,Env,_),
 3861	conceptHierarchy(Env,MS,Dag),
 3862	testDirectSubElement(Concept1,Concept2,Concept,Dag).
 3863
 3864/***********************************************************************
 3865 *
 3866 * testSuperConcept(+EnvName,+MS,+Concept1,+Concept2,-Concept)
 3867 * Arguments: EnvName        environment identifier
 3868 *            MS             modal context
 3869 *            Concept1       concept name
 3870 *            Concept2       concept name
 3871 *            Concept        concept name
 3872 * Concept is Concept1 iff Concept1 is a direct superconcept of Concept2
 3873 * or
 3874 * Concept is Concept2 iff Concept2 is a direct superconcept of Concept1
 3875 * otherwise
 3876 * the predicate fails.
 3877 *
 3878 */
 3879
 3880testSuperConcept(EnvName,MS,Concept1,Concept2,Concept) :-
 3881	environment(EnvName,Env,_),
 3882	conceptHierarchy(Env,MS,Dag),
 3883	testSuperElement(Concept1,Concept2,Concept,Dag).
 3884
 3885/***********************************************************************
 3886 *
 3887 * testSubConcept(+EnvName,+MS,+Concept1,+Concept2,-Concept)
 3888 * Arguments: EnvName        environment identifier
 3889 *            MS             modal context
 3890 *            Concept1       concept name
 3891 *            Concept2       concept name
 3892 *            Concept        concept name
 3893 * Concept is Concept1 iff Concept1 is a direct superconcept of Concept2
 3894 * or
 3895 * Concept is Concept2 iff Concept2 is a direct superconcept of Concept1
 3896 * otherwise
 3897 * the predicate fails.
 3898 *
 3899 */
 3900
 3901testSubConcept(EnvName,MS,Concept1,Concept2,Concept) :-
 3902	environment(EnvName,Env,_),
 3903	conceptHierarchy(Env,MS,Dag),
 3904	testSubElement(Concept1,Concept2,Concept,Dag).
 3905
 3906/***********************************************************************
 3907 *
 3908 * getCommonSuperConcepts(+EnvName,+MS,+CL1,-CL2)
 3909 * Arguments: EnvName  environment identifier
 3910 *            MS       modal context
 3911 *            CL1      list of concept names
 3912 *            CL2      list of concept names
 3913 * CL2 is the list of all concept names subsuming all concepts in CL1.
 3914 *
 3915 */
 3916
 3917getCommonSuperConcepts(EnvName,MS,CL1,CL2) :-
 3918	hop_map(getAllSuperConcepts,[EnvName,MS],CL1,CLL1),
 3919	intersection_motel(CLL1,CL2).
 3920
 3921/***********************************************************************
 3922 *
 3923 * getCommonSubConcepts(+EnvName,+MS,+CL1,-CL2)
 3924 * Arguments: EnvName  environment identifier
 3925 *            MS       modal context
 3926 *            CL1      list of concept names
 3927 *            CL2      list of concept names
 3928 * CL2 is the list of all concept names which are subsumed by all
 3929 * concepts in CL1.
 3930 *
 3931 */
 3932
 3933getCommonSubConcepts(EnvName,MS,CL1,CL2) :-
 3934	hop_map(getAllSubConcepts,[EnvName,MS],CL1,CLL1),
 3935	intersection_motel(CLL1,CL2).
 3936
 3937/***********************************************************************
 3938 *
 3939 * getAllObjects(+EnvName,+MS,+O)
 3940 *
 3941 */
 3942
 3943getAllObjects(EnvName,MS,O13) :-
 3944	!,
 3945	environment(EnvName,Env,_),
 3946	setofOrNil(X1,[C1,AX1]^(conceptElement(Env,MS,_,user,X1,C1,AX1)),O1),
 3947	setofOrNil(X2,[R2,Y2,AX2]^roleElement(Env,MS,_,user,X2,Y2,R2,AX2),O2),
 3948	setofOrNil(Y3,[R3,X3,AX3]^roleElement(Env,MS,_,user,X3,Y3,R3,AX3),O3),
 3949	motel_union( O1,O2,O12),
 3950	motel_union(O12,O3,O13),
 3951	!.
 3952/**********************************************************************
 3953 *
 3954 * @(#) constraints.pl 1.2@(#)
 3955 *
 3956 */
 3957
 3958/**********************************************************************
 3959 *
 3960 * solveConstraint(MS,(card,app((FF:R),X),Rel,N),hyp(HYPS),call(CALLS))
 3961 * if Rel is '>=', 
 3962 *    the predicate succeeds if the cardinality of 
 3963 *    app((FF:R),X) in modal context MS is greater than N.
 3964 *    If N is a variable, it will be instantiated with the greatest
 3965 *    number M such that the cardinality of  app((FF:R),X) in modal 
 3966 *    context MS is provably greater than M.
 3967 * if Rel is '=<', 
 3968 *    the predicate succeeds if the cardinality of 
 3969 *    app((FF:R),X) in modal context MS is smaller than N.
 3970 *    If N is a variable, it will be instantiated with the greatest
 3971 *    number M such that the cardinality of  app((FF:R),X) in modal 
 3972 *    context MS is provably smaller than M.
 3973 *
 3974 */
 3975
 3976solveConstraint(Env,MS,(card,app((FF:R),X),Rel,N),(M,S),hyp(HYPS),ab(D),call(CALLS),PTO) :-
 3977%	SolveHead = solveConstraint(MS,(card,app((FF:R),X),Rel,N),hyp(HYPS)),
 3978%	cCS(CALLS,SolveHead),
 3979%	CALLS1 = [SolveHead|CALLS],
 3980	length(CALLS,XXX),
 3981%	format('trying ~d  solve(~w(~w)) ~w ~w~n',[XXX,R,X,Rel,N]),
 3982	collectAllFillers(Env,MS,R,X,HYPS,D,CALLS,S),
 3983	computeNumber(S,Rel,(M,PTAbox)),
 3984	continueSolve(Env,MS,(card,app((FF:R),X),Rel,N),hyp(HYPS),ab(D),call(CALLS),(M,PTAbox),PT),
 3985	PTO = proved(card(R,X,Rel,N),hyp(HYPS),basedOn(PT)).
 3986	
 3987
 3988computeNumber([],'=<',(noRestriction,basedOn(noAboxEntries))) :- !.
 3989computeNumber([],'>=',(noRestriction,basedOn(noAboxEntries))) :- !.
 3990computeNumber(S,_Rel,(M,and(PL))) :-
 3991	reduceToSolutionSet(S,EL,PL),
 3992	length(EL,M).
 3993
 3994reduceToSolutionSet([],[],[]) :- !.
 3995reduceToSolutionSet([(E1,PT1,_)|L],L2,L3) :-
 3996	member((E1,_PT2,_R2),L),
 3997	!,
 3998	reduceToSolutionSet(L,L2,L3).
 3999reduceToSolutionSet([(E1,PT1,_)|L],[E1|L2],[PT1|L3]) :-
 4000	reduceToSolutionSet(L,L2,L3).
 4001
 4002continueSolve(_,_,(card,_,'=<',N),hyp(_),ab(_),call(_),(M,_PTAbox),_) :-
 4003	number(M),
 4004	nonvar(N),
 4005	M >= N,
 4006	!,
 4007	fail.
 4008continueSolve(Env,MS,(card,app((FF:R),X),Rel,N),hyp(HYPS),ab(D),call(CALLS),(M1,PTAbox),PT3) :-
 4009	collectAllConstraints(Env,MS,FF,R,X,Rel,HYPS,D,CALLS,S),
 4010	findNumberRestriction(Rel,(M1,PTAbox),S,(M3,PT3)),
 4011	!,
 4012	comparison(Rel,M3,N).
 4013
 4014collectAllFillers(Env,MS,R,X,HYPS,D,CALLS,S) :-
 4015	EqLiteral = eqGenerator(Env,AX,RN,S,O,MS,Y,app((FF:R),X),HYPS,D,CALLS,PT),
 4016	bagof((Y,PT,[Env,MS,R,X,HYPS,D,CALLS]),AX^RN^S^O^FF^EqLiteral,S),
 4017	!.
 4018collectAllFillers(_,_,_,_,_,_,_,[]) :-
 4019	!.
 4020
 4021
 4022collectAllConstraints(Env,MS,FF,R,X,Rel,HYPS,D,CALLS,S) :-
 4023%	constructConHead(Env,rn(AX,RN,S,O),MS,FF,R,X,Rel,M2,HYPS,D,CALLS,PT,C1),
 4024	C1 = constraint(Env,rn(AX,RN,S,O),MS,(card,app((FF:R),X),Rel,M2),
 4025                       hyp(HYPS),ab(D),call(CALLS),PT),
 4026	bagof((M2,PT,[Env,MS,FF,R,X,Rel,HYPS,D,CALLS]),AX^RN^S^O^FF^C1,S),
 4027	!.
 4028collectAllConstraints(_,_MS,_FF,_R,_X,_Rel,_HYPS,_D,_CALLS,[]) :-
 4029	!.
 4030
 4031
 4032/**********************************************************************
 4033 * 
 4034 * comparison(+Rel,+M,?N)
 4035 * if N is a variable then N is instantiated with M and the predicate
 4036 * succeeds.
 4037 * if N is a number, the predicates succeeds if then goal Rel(M,N)
 4038 * succeeds.
 4039 *
 4040 */
 4041
 4042comparison(_Rel,M3,N) :-
 4043	var(N),
 4044	!,
 4045	N = M3.
 4046comparison(Rel,M3,N) :-
 4047	number(M3), number(N),
 4048	Goal =.. [Rel,M3,N],
 4049	call(Goal).
 4050	
 4051/**********************************************************************
 4052 *
 4053 * findNumberRestristriction(+Rel,+L,-N)
 4054 * if Rel is '=<' then N will be instantiated with the smallest number
 4055 * in the list of numbers L.
 4056 * if Rel is '>=' then N will be instantiated with the greatest number
 4057 * in the list of numbers L.
 4058 *
 4059 */
 4060
 4061findNumberRestriction('=<',(noRestriction,PT1),[],(1000000,PT1)) :- !.
 4062findNumberRestriction('>=',(noRestriction,PT1),[],(0,PT1)) :- !.
 4063findNumberRestriction('>=',(N,PT1),[],(N,PT1)) :- !.
 4064findNumberRestriction('=<',(N,PT1),[],(1000000,noConstraintsFound)) :- !.
 4065findNumberRestriction(_,(noRestriction,_),[(N1,PT2,_)],(N1,PT2)) :- !.
 4066findNumberRestriction('=<',(M,_PT1),[(N1,PT2,_)],(N1,PT2)) :-
 4067	N1 =< M,
 4068	!.
 4069findNumberRestriction('=<',(M,PT1),[(_N1,_,_)],(M,PT1)) :-
 4070	!.
 4071findNumberRestriction('>=',(M,PT1),[(N1,_,_)],(M,PT1)) :-
 4072	M >= N1,
 4073	!.
 4074findNumberRestriction('>=',(_M,_),[(N1,PT2,_)],(N1,PT2)) :-
 4075	!.
 4076findNumberRestriction('>=',(K,PT1),[(N1,_,_)|NL],(N2,PT3)) :-
 4077	findNumberRestriction('>=',(K,PT1,_),NL,(N2,PT3)),
 4078	N2 >= N1,
 4079	!.
 4080findNumberRestriction('=<',(K,PT1),[(N1,_,_)|NL],(N2,PT3)) :-
 4081	findNumberRestriction('=<',(K,PT1,_),NL,(N2,PT3)),
 4082	N2 =< N1,
 4083	!.
 4084findNumberRestriction(_,_,[(N1,PT1,_)|_NL],(N1,PT1)) :-
 4085	!.
 4086
 4087
 4088
 4089
 4090
 4091
 4092
 4093
 4094/**********************************************************************
 4095 *
 4096 * @(#) construct.pl 1.11@(#)
 4097 *
 4098 */
 4099inProofTerm(MS,rn(AX,Rule,_,_),D,X,HYPS,PT1,PT) :-
 4100	nonvar(AX),
 4101	conceptSubsets(_Env,_user,MS1,C1,C2,AX),
 4102	PT = proved(in(MS1,D,X),usingAxiom(defprimconcept(C1,C2)),basedOn(PT1)),
 4103	!.
 4104inProofTerm(MS,rn(AX,Rule,_,_),D,X,HYPS,PT1,PT) :-
 4105	nonvar(AX),
 4106	conceptEqualSets(_Env,_user,MS1,C1,C2,AX),
 4107	PT = proved(in(MS1,D,X),usingAxiom(defconcept(C1,C2)),basedOn(PT1)),
 4108	!.
 4109inProofTerm(MS,rn(AX,Rule,_,_),D,X,HYPS,PT1,PT) :-
 4110	nonvar(AX),
 4111	conceptElement(_Env,MS1,_,user,C1,C2,AX),
 4112	PT = proved(in(MS1,D,X),usingAxiom(assert_ind(C1,C2)),basedOn(PT1)),
 4113	!.
 4114inProofTerm(MS,rn(AX,Rule,_,_),D,X,HYPS,PT1,PT) :-
 4115	!.
 4116
 4117inProofTerm(MS,Name,D,X,HYPS,PT1,PT) :-
 4118	PT = proved(in(MS,Name,D,X),basedOn(PT1)),
 4119	!.
 4120inProofTerm(MS,D,X,HYPS,PT1,PT) :-
 4121	PT = proved(in(MS,D,X),basedOn(PT1)),
 4122	!.
 4123
 4124eqProofTerm(MS,Y,_FF,R,X,HYPS,PT1,PT) :-
 4125	nonvar(R),
 4126	atomic(R),
 4127	!,
 4128	Rel =.. [R,MS,X,Y],
 4129	PT = proved(Rel,basedOn(PT1)),
 4130	!.
 4131eqProofTerm(MS,Y,_FF,R,X,HYPS,PT1,PT) :-
 4132	Rel = rel(R,MS,X,Y),
 4133	PT = proved(Rel,basedOn(PT1)),
 4134	!.
 4135conProofTerm(MS,R,X,Rel,N,HYPS,PT1,PT) :-
 4136	PT = proved(card(R,MS,X,Rel,N),basedOn(PT1)),
 4137	!.
 4138/***********************************************************************
 4139 *
 4140 * makeTerm(+TermPieces,-Term)
 4141 *
 4142 */
 4143
 4144makeTerm(Term,Term) :-
 4145	var(Term),
 4146	!.
 4147makeTerm(Term,Term) :-
 4148	atomic(Term),
 4149	!.
 4150makeTerm([Functor|ArgList],Term) :-
 4151	hop_map(makeTerm,ArgListTerms,ArgList),
 4152	Term =.. [Functor|ArgListTerms].
 4153makeTerm(Term,Term).
 4154
 4155%element(X) :-
 4156%	atomic(X),
 4157%	!.
 4158%element(X) :-
 4159%	var(X),
 4160%	!.
 4161element(_) :- !.
 4162
 4163relation(R,RN,X1,Y1) :-
 4164%	nonvar(R),
 4165%	R =.. [RN,X1,Y1].
 4166	T =.. [RN,X1,Y1],
 4167	R = T.
 4168
 4169eqGenerator(Env,AX,RN,S,O,MS,X,Y,HYPS,D,CALLS,PT) :-
 4170	eq(Env,rn(AX,RN,S,O),modal(MS),X,Y,hyp(HYPS),ab(D),call(CALLS),PT),
 4171	nonvar(X),
 4172	nonvar(Y),
 4173	atomic(X).
 4174
 4175gensymbol(Symbol,_L,NewSymbol) :-
 4176	gensym(Symbol,NewSymbol),
 4177	!.
 4178
 4179
 4180/**********************************************************************
 4181 *
 4182 * ruleName(+AxiomName,+RuleName,+Orientation)
 4183 *
 4184 */
 4185
 4186ruleName(AxiomName,RuleName,Origin,Orientation,
 4187	 rn(AxiomName,RuleName,Origin,Orientation)) :- 
 4188	!.
 4189
 4190reverseOrientation(lInR,rInL) :- !.
 4191reverseOrientation(rInL,lInR) :- !.
 4192
 4193
 4194typeOfDefinition(_,_,C,system) :-
 4195	var(C),
 4196	!.
 4197typeOfDefinition(Env,MS,C,user) :-
 4198	getConceptName(Env,MS,C),
 4199	!.
 4200typeOfDefinition(_,_,C,system) :-
 4201	atomic(C),
 4202	name(C,[99,111,110,99,101,112,116|_]),
 4203	!.
 4204typeOfDefinition(Env,MS,R,user) :-
 4205	getRoleName(Env,MS,R),
 4206	!.
 4207typeOfDefinition(_,_,R,system) :-
 4208	atomic(R),
 4209	name(R,[114,111,108,101|_]),
 4210	!.
 4211typeOfDefinition(Env,MS,not(C),Type) :-
 4212	!,
 4213	typeOfDefinition(Env,MS,C,Type).
 4214typeOfDefinition(_,_,normal(C),system) :-
 4215	!.
 4216typeOfDefinition(_,_,not(normal(C)),system) :-
 4217	!.
 4218typeOfDefinition(_,_,_,user) :-
 4219	!.
 4220
 4221% someInterpretation([]).
 4222% someInterpretation([I1|IL]) :-
 4223% 	call(I1),
 4224% 	someInterpretation(IL).
 4225% 
 4226% allInterpretation([]) :-
 4227% 	fail.
 4228% allInterpretation([I1|IL]) :-
 4229% 	(call(I1) ; allInterpretation(IL)).
 4230% 
 4231% roleConjunction(X,IL) :-
 4232% 	var(X),
 4233% 	someInterpretation(IL).
 4234% roleConjunction(X,IL) :-
 4235% 	nonvar(X),
 4236% 	name(X,[115,107,111,108,101,109|_]),
 4237% 	allInterpretation(IL).
 4238% roleConjunction(X,IL) :-
 4239% 	nonvar(X),
 4240% 	not(name(X,[115,107,111,108,101,109|_])),
 4241% 	someInterpretation(IL).
 4242
 4243
 4244/***********************************************************************
 4245 *
 4246 * convertMS(Env,+MS1,+ModalOperator,WVL1,-MS2,WVL2)
 4247 * Arguments: MS1                modal context
 4248 *            ModalOperator      modal operator
 4249 *            WVL1               list of free world variables already
 4250 *                               generated during the conversion
 4251 *            MS2                modal context
 4252 *            WVL2               list of all free world variables 
 4253 *                               generated during the conversion
 4254 * MS2 is the translation of ModalOperator appended to MS1.
 4255 *
 4256 */
 4257
 4258genagent(X,_,X) :-
 4259	var(X),
 4260	!.
 4261genagent(all,free,_A) :-
 4262	!.
 4263genagent(all,skolemize,A) :-
 4264	gensym(agent,A),
 4265	!.
 4266genagent(A,_,A) :-
 4267	!.
 4268
 4269convertMS(positive,Env,Start,MS,WVL1,End,WVL2) :-
 4270	!,
 4271	convertMS(Env,Start,MS,WVL1,End,WVL2).
 4272convertMS(negative,Env,Start,MS1,WVL1,End,WVL2) :-
 4273	!,
 4274	hop_map(negate,MS1,MS2),
 4275	hop_map(normalizeNot,MS2,MS3),
 4276	convertMS(Env,Start,MS3,WVL1,End,WVL2).
 4277	
 4278
 4279convertMS(_Env,_,MS,WVL,[_W1,true],WVL) :-
 4280	var(MS),
 4281	!.
 4282convertMS(_Env,[MS1,Lits1],[],WVL,[MS1,Lits1],WVL) :-
 4283	!.
 4284convertMS(Env,[MS1,Lits1],[d(MOp,A)|L],WVL,[MS3,Lits3],WVL3) :-
 4285	gensym(wp,WP),
 4286	WPTerm = [WP,WVL],
 4287	genagent(A,skolemize,Agent),
 4288	MS2 = app(WPTerm:m(MOp,Agent),MS1),
 4289	convertMS(Env,[MS2,Lits1],L,WVL,[MS3,Lits3],WVL3),
 4290	!.
 4291convertMS(Env,[MS1,Lits1],[b(MOp,A)|L],WVL,[MS3,Lits3],WVL3) :-
 4292	genagent(A,free,Agent),
 4293	Lit = rel(Env,_,m(MOp,Agent),MS1,MS2),
 4294	convertMS(Env,[MS2,(Lit,Lits1)],L,[MS2|WVL],[MS3,Lits3],WVL3),
 4295	!.
 4296convertMS(Env,[MS1,Lits1],[dc(MOp,C)|L],WVL,[MS3,Lits3],WVL3) :-
 4297	gensym(wp,WP),
 4298	WPTerm = [WP,WVL],
 4299	genagent(all,skolemize,Agent),
 4300	MS2 = app(WPTerm:m(MOp,Agent),MS1),
 4301	getQuery(Env,MS1,C,Agent,_Exp,Body),
 4302	convertMS(Env,[MS2,(once(Body),Lits1)],L,WVL,[MS3,Lits3],WVL3),
 4303	!.
 4304convertMS(Env,[MS1,Lits1],[bc(MOp,C)|L],WVL,[MS3,Lits3],WVL3) :-
 4305	genagent(all,free,Agent),
 4306	Lit = rel(Env,_,m(MOp,Agent),MS1,MS2),
 4307	getQuery(Env,MS1,C,Agent,_Exp,Body),
 4308	convertMS(Env,[MS2,((once(Body),Lit),Lits1)],L,[MS2|WVL],[MS3,Lits3],WVL3),
 4309	!.
 4310
 4311
 4312/***********************************************************************
 4313 *
 4314 * THE STRUCTURE OF THE IN-CLAUSES
 4315 * 
 4316 * 1) THE HEAD
 4317 *    in(Env,RN,modal(W),A1,X,hyp(C1),ab(D),call(H1),Exp)
 4318 *    Env is a internal environment name
 4319 *    RN  is a rule name
 4320 *    W   is a world
 4321 *    A1  is a concept name or the negation of a concept name
 4322 *    X   is a free variable
 4323 *    C1  is a list of clauses --- the hypotheses that can be used
 4324 *    D   is a name identifying a specific abductive derivation
 4325 *    H1  is a list of calls   --- the calls to in that have already
 4326 *                                 been used
 4327 *    Exp is a explanation term
 4328 * 2) THE BODY
 4329 *
 4330 */
 4331
 4332/***********************************************************************
 4333 *
 4334 * constructMLHead(+ModalSequence,
 4335 *                 +ConceptName,+Constraint,
 4336 *                 +Hypotheses,+CallStack,-Inhead)
 4337 * 
 4338 */
 4339
 4340constructInHead(Env,Name,MS,CN,CON,HYP,D,CALL,PT1,InHead) :-
 4341	inProofTerm(MS,Name,CN,CON,HYP,PT1,PT),
 4342	InHead = in(Env,Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT),
 4343	!.
 4344
 4345constructKBHead(Env,Priority,Name,MS,CN,CON,HYP,D,CALL,PT1,InHead) :-
 4346	inProofTerm(MS,Name,CN,CON,HYP,PT1,PT),
 4347	InHead = kb_in(Env,Priority,Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT),
 4348	!.
 4349
 4350constructMLHead(Env,Name,MS,CN,CON,HYP,D,CALL,PT1,InHead) :-
 4351	inProofTerm(MS,Name,CN,CON,HYP,PT1,PT),
 4352	InHead = kb_in(Env,pr(3),Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT),
 4353	!.
 4354
 4355getEnvironment(kb_in(Env,pr(_),_,modal(_MS),_CN,_CON,hyp(_HYP),ab(_),call(_CALL),_),Env).
 4356getEnvironment(in(Env,_,modal(_MS),_CN,_CON,hyp(_HYP),ab(_),call(_CALL),_),Env).
 4357getModalSequence(kb_in(_,pr(_),_,modal(MS),_CN,_CON,hyp(_HYP),ab(_),call(_CALL),_),MS).
 4358getModalSequence(in(_,_,modal(MS),_CN,_CON,hyp(_HYP),ab(_),call(_CALL),_),MS).
 4359getConceptName(kb_in(_,pr(_),_,modal(_MS),CN,_CON,hyp(_HYP),ab(_),call(_CALL),_),CN).
 4360getConceptName(in(_,_,modal(_MS),CN,_CON,hyp(_HYP),ab(_),call(_CALL),_),CN).
 4361getConstraint(kb_in(_,pr(_),_,modal(_MS),_CN,CON,hyp(_HYP),ab(_),call(_CALL),_),CON).
 4362getConstraint(in(_,_,modal(_MS),_CN,CON,hyp(_HYP),ab(_),call(_CALL),_),CON).
 4363getHypotheses(kb_in(_,pr(_),_,modal(_MS),_CN,_CON,hyp(HYP),ab(_),call(_CALL),_),HYP).
 4364getHypotheses(in(_,_,modal(_MS),_CN,_CON,hyp(HYP),ab(_),call(_CALL),_),HYP).
 4365getCallStack(kb_in(_,pr(_),_,modal(_MS),_CN,_CON,hyp(_HYP),ab(_),call(CALL),_),CALL).
 4366getCallStack(in(_,_,modal(_MS),_CN,_CON,hyp(_HYP),ab(_),call(CALL),_),CALL).
 4367getExplanation(kb_in(_,pr(_),_,modal(_MS),_CN,_CON,hyp(_HYP),ab(_),call(_CALL),E),E).
 4368getExplanation(in(_,_,modal(_MS),_CN,_CON,hyp(_HYP),ab(_),call(_CALL),E),E).
 4369getInExplanation(kb_in(_,pr(_),_,modal(_MS),_CN,_CON,hyp(_HYP),ab(_),call(_CALL),
 4370	         proved(I,_)),I).
 4371getInExplanation(in(_,_,modal(_MS),_CN,_CON,hyp(_HYP),ab(_),call(_CALL),
 4372	         proved(I,_)),I).
 4373
 4374/**********************************************************************
 4375 *
 4376 * constructEqHead(Env,+MS,+Y,+F,+R,+X,+HYPS,+CALLS,-L)
 4377 *
 4378 */
 4379
 4380constructEqHead(Env,Name,MS,Y,F,R,X,HYPS,D,CALLS,PT1,L) :-
 4381	eqProofTerm(MS,Y,F,R,X,HYPS,PT1,PT),
 4382	L = eq(Env,Name,modal(MS),Y,app((F:R),X),hyp(HYPS),ab(D),call(CALLS),PT),
 4383	!.
 4384
 4385/**********************************************************************
 4386 *
 4387 * constructEqMark(+MS,+Y,+F,+R,+X,+HYPS,+CALLS,+AN,-L)
 4388 *
 4389 */
 4390
 4391constructEqMark(Name,MS,Y,F,R,X,HYPS,_D,_CALLS,L) :-
 4392	HYPS = [or(H1),rl(H2),fl(H3)],
 4393	L = eq(Name,modal(MS),Y,app((F:R),X),hyp(H1)),
 4394	!.
 4395
 4396/**********************************************************************
 4397 *
 4398 * constructEqCall(Env,+MS,+Y,+F,+R,+X,+HYPS,+CALLS,+AN,-L)
 4399 *
 4400 */
 4401
 4402constructEqCall(Env,rn(AX,RN,_Source,Orientation),bodyMC(MS1),headMC(MS2),
 4403	        Y,F,R,X,HYPS,D,CALLS,PT,L) :-
 4404	constructEqMark(rn(AX,RN,_S1,Orientation),MS2,Y,F,R,X,HYPS,D,CALLS,C1),
 4405	L = eq(Env,rn(_AX2,_RN2,_S2,_O2),modal(MS1),Y,app((F:R),X),
 4406               hyp(HYPS),ab(D),call([C1|CALLS]),PT),
 4407	!.
 4408
 4409/***********************************************************************
 4410 *
 4411 * constructMLMark(+ModalSequence,+ConceptName,+Constraint,
 4412 *                      +AxiomName,-LoopCheck)
 4413 *
 4414 */
 4415
 4416constructMLMark(Name,MS,CN,CON,HYPS,D,LoopCheck) :-
 4417	HYPS = [or(H1),rl(H2),fl(H3)],
 4418	LoopCheck = in(Name,modal(MS),CN,CON,hyp(H1),ab(D)),
 4419	!.
 4420
 4421constructMLMark(kb_in(_,Pr,Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(_),_),LoopCheck) :-
 4422	HYPS = [or(H1),rl(H2),fl(H3)],
 4423	LoopCheck = in(Name,modal(MS),CN,CON,hyp(H1),ab(D)),
 4424	!.
 4425constructMLMark(in(_,Pr,Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(_),_),LoopCheck) :-
 4426	HYPS = [or(H1),rl(H2),fl(H3)],
 4427	LoopCheck = in(Name,modal(MS),CN,CON,hyp(H1),ab(D)),
 4428	!.
 4429constructMLMark(in(_,Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(_),_),LoopCheck) :-
 4430	HYPS = [or(H1),rl(H2),fl(H3)],
 4431	LoopCheck = in(Name,modal(MS),CN,CON,hyp(H1),ab(D)),
 4432	!.
 4433
 4434/***********************************************************************
 4435 *
 4436 * constructMLCall(+Env,rn(+AX1,+RN1,+S1,+O1),bodyMC(MS1),headMC(MS2), 
 4437 *                 +ConceptName,+Variable,
 4438 *                 +Hypotheses,+DerivationName,+CallStack,+Proofterm,
 4439 *                 -InTerm)
 4440 *
 4441 * The information in rn(AX1,RN1,S1,O1)  is used in the following way:
 4442 * AX1, RN1, and O1 is used in the construction of the MLMark
 4443 * which is added to the call stack. If AX1 is `no' then the MLMark is
 4444 * not added to the call stack at all.
 4445 * S1 is used in the construction of InHead. If S1 is a variable, any 
 4446 * rule can be used to prove the call. If S1 is `user' then only user
 4447 * provided rules may be used. If S1 is `system' then only system provided
 4448 * rules may be used.
 4449 *
 4450 */
 4451
 4452constructMLCall(Env,rn(AX1,RN1,S1,O1),bodyMC(MS1),headMC(MS2),
 4453                CN,CON,HYPS,D,CALLS,PT1,InHead) :-
 4454	HYPS = [or(H1),rl(H2),fl(H3)],
 4455	constructMLMark(rn(AX1,RN1,_S2,_O2),MS2,CN,CON,HYPS,D,Mark),
 4456	convert_loop(AX1,CALLS,Mark,CALLS1),
 4457	getNegatedConcept(CN,C2),
 4458	InHeadH = in(_NameH,modal(MS1),C2,CON,hyp(_H),ab(_D)),
 4459	getSource(S1,Source),
 4460	InHead = in(Env,rn(_AX3,_RN3,Source,_O3),modal(MS1),CN,CON,
 4461                    hyp([or(H1),rl([InHeadH|H2]),fl(H3)]),
 4462                    ab(D),call(CALLS1),PT1),
 4463	!.
 4464
 4465
 4466getSource(V1,_V2) :-
 4467	var(V1),
 4468	!.
 4469getSource(any,_V2) :-
 4470	!.
 4471getSource(V1,V1) :-
 4472	!.
 4473
 4474getNegatedConcept(CN,not(CN)) :-
 4475	var(CN),
 4476	!.
 4477getNegatedConcept(CN,C2) :-
 4478	normalizeNot(not(CN),C2),
 4479	!.
 4480
 4481/***********************************************************************
 4482 *
 4483 * constructConHead(Env,+Name,+MS,+F,+R,+X,+Rel,+N,
 4484 *                  +HYPS,+CALLS,-Literal)
 4485 *
 4486 */
 4487
 4488constructConHead(Env,Name,MS,F,R,X,Rel,N,HYPS,D,CALLS,PT1,L) :-
 4489	conProofTerm(MS,R,X,Rel,N,HYPS,PT1,PT),
 4490	L = constraint(Env,Name,MS,(card,app((F:R),X),Rel,N),
 4491                       hyp(HYPS),ab(D),call(CALLS),PT),
 4492	!.
 4493
 4494/***********************************************************************
 4495 *
 4496 * constructConMark(+MS,+F,+R,+X,+Rel,+N,+HYPS,+CALLS,+AN,-Literal)
 4497 *
 4498 */
 4499
 4500constructConMark(Name,MS,F,R,X,Rel,N,HYPS,_D,_CALLS,L) :-
 4501	HYPS = [or(H1),rl(H2),fl(H3)],
 4502	L = constraint(Name,MS,(card,app((F:R),X),Rel,N),hyp(H1)),
 4503	!.
 4504
 4505constructConMark(constraint(_,Name,MS,(card,A,Rel,N),hyp(HYPS),ab(_D),call(_CALLS),_PT),L) :-
 4506	HYPS = [or(H1),rl(H2),fl(H3)],
 4507	L = constraint(Name,MS,(card,A,Rel,N),hyp(H1)),
 4508	!.
 4509
 4510/***********************************************************************
 4511 *
 4512 * constructSolveConMark(+MS,+F,+R,+X,+Rel,+N,+HYPS,+CALLS,+AN,-Literal)
 4513 *
 4514 */
 4515
 4516constructSolveConMark(Name,MS,F,R,X,Rel,N,HYPS,_D,_CALLS,L) :-
 4517	HYPS = [or(H1),rl(H2),fl(H3)],
 4518	L = solveConstraint(Name,MS,(card,app((F:R),X),Rel,N),hyp(H1)),
 4519	!.
 4520
 4521constructSolveConMark(constraint(_,Name,MS,(card,A,Rel,N),hyp(HYPS),ab(_D),call(_CALLS),_PT),L) :-
 4522	HYPS = [or(H1),rl(H2),fl(H3)],
 4523	L = solveConstraint(Name,MS,(card,A,Rel,N),hyp(H1)),
 4524	!.
 4525
 4526/***********************************************************************
 4527 *
 4528 * constructConCall(Env,+MS,+F,+R,+X,+Rel,+N,+HYPS,+CALLS,+AN,-Literal)
 4529 *
 4530 */
 4531
 4532constructConCall(Env,bodyMC(MS1),headMC(MS2),F,R,X,Rel,N,HYPS,D,CALLS,AN,PT1,L) :-
 4533	constructConMark(MS2,F,R,X,Rel,N,HYPS,D,CALLS,AN,Mark),
 4534        L = constraint(Env,_Name,MS1,(card,app((F:R),X),Rel,N),
 4535                       hyp(HYPS),ab(D),call([Mark|CALLS]),PT1),
 4536	!.
 4537
 4538
 4539addDefaultML(I1,L1) :-
 4540	var(L1),
 4541	!,
 4542	L1 = [I1|_L2],
 4543	!.
 4544addDefaultML(I1,[_|L1]) :-
 4545	addDefaultML(I1,L1),
 4546	!.
 4547
 4548memberDML(I1,L) :-
 4549	nonvar(L),
 4550	L = [I1|L2],
 4551	!.
 4552memberDML(I1,L) :-
 4553	nonvar(L),
 4554	L = [_|L2],
 4555	memberDML(I1,L2).
 4556
 4557
 4558/**********************************************************************
 4559 *
 4560 * getAxiom(+Env,+MS,AX)
 4561 * succeeds if AX is an axiom in environment Env and modal context
 4562 * MS.
 4563 *
 4564 */
 4565	
 4566getAxiom(Env,MS,Ax) :-
 4567	axiom(Env,MS,Ax).
 4568
 4569/**********************************************************************
 4570 *
 4571 * getConceptName(+Env,+MS,CN)
 4572 * succeeds if CN is a concept name in environment Env and modal context
 4573 * MS.
 4574 *
 4575 */
 4576
 4577getConceptName(Env,MS1,CN) :-
 4578	convertMS(negative,Env,[[],true],MS1,[],[W1,G1],_),
 4579	clause(conceptName(Env,_,W1,CN),_),
 4580	once((call(G1),conceptName(Env,_,W1,CN))).
 4581
 4582/**********************************************************************
 4583 *
 4584 * getRoleName(+Env,+MS,CN)
 4585 * succeeds if CN is a role name in environment Env and modal context
 4586 * MS.
 4587 *
 4588 */
 4589
 4590getRoleName(Env,MS1,CN) :-
 4591	convertMS(negative,Env,[[],true],MS3,[],[W1,G1],_),
 4592	clause(roleName(Env,_,W1,CN),_),
 4593	once((call(G1),roleName(Env,_,W1,CN))).
 4594
 4595
 4596/**********************************************************************
 4597 *
 4598 * @(#) dag.pl 1.3@(#)
 4599 *
 4600 */
 4601
 4602/***********************************************************************
 4603 *
 4604 * memberElement(+Element,+Dag)
 4605 * Parameter: Element     element name
 4606 *            Dag         subsumption hierarchy
 4607 * checks wether or not Element occurs in the subsumption hierarchy.
 4608 *
 4609 */
 4610
 4611memberElement(Element,node(CL,_NL)) :-
 4612	member(Element,CL),
 4613	!.
 4614memberElement(Element,node(_CL,NL)) :-
 4615	memberElementSubtrees(Element,NL),
 4616	!.
 4617
 4618memberElementSubtrees(_Element,[]) :-
 4619	!,
 4620	fail.
 4621memberElementSubtrees(Element,[N1|_NL]) :-
 4622	memberElement(Element,N1),
 4623	!.
 4624memberElementSubtrees(Element,[_N1|NL]) :-
 4625	memberElementSubtrees(Element,NL).
 4626
 4627/***********************************************************************
 4628 *
 4629 * memberDirectSubElements(+Element,+Dag)
 4630 * Parameter: Element     element name
 4631 *            Dag         subsumption hierarchy
 4632 * checks wether or not Element occurs in the direct subelements of
 4633 * the 'top' element of Dag.
 4634 *
 4635 */
 4636
 4637memberDirectSubElements(Element,node(_CL,NL)) :-
 4638	!,
 4639	memberDirectSubElements(Element,NL).
 4640
 4641memberDirectSubElements(_Element,[]) :-
 4642	!,
 4643	fail.
 4644memberDirectSubElements(Element,[node(CL,_NL1)|_NL]) :-
 4645	member(Element,CL),
 4646	!.
 4647memberDirectSubElements(Element,[_N1|NL]) :-
 4648	memberDirectSubElements(Element,NL).
 4649
 4650/***********************************************************************
 4651 *
 4652 * getDirectSuperElements(+Element,-CL,+Dag)
 4653 * Parameter: Dag         subsumption hierarchy
 4654 *            Element     element name
 4655 *            CL          list of element names
 4656 * CL is the list of all element names which are direct super elements
 4657 * of Element.
 4658 *
 4659 */
 4660
 4661getDirectSuperElements(Element,CL,node(CL,NL)) :-
 4662	memberDirectSubElements(Element,node(CL,NL)),
 4663	!.
 4664getDirectSuperElements(Element,CL,node(_,NL)) :-
 4665	getDirectSuperElements(Element,CL,NL).
 4666
 4667getDirectSuperElements(_Element,[],[]) :-
 4668	!.
 4669getDirectSuperElements(Element,CL,[N1|NL]) :-
 4670	getDirectSuperElements(Element,CL1,N1),
 4671	getDirectSuperElements(Element,CL2,NL),
 4672	motel_union(CL1,CL2,CL).
 4673
 4674/***********************************************************************
 4675 *
 4676 * getAllSuperElements(+Element,-CL,+Dag)
 4677 * Parameter: Element     element name
 4678 *            CL          list of element names
 4679 *            Dag         subsumption hierarchy
 4680 * CL is the list of all element names which are super elements of
 4681 * Element.
 4682 *
 4683 */
 4684
 4685getAllSuperElements(Element,CL,Dag) :-
 4686	getAllSuperElements(Element,CL,[],Dag).
 4687
 4688getAllSuperElements(Element,CL1,CL1,node(CL,_NL)) :-
 4689	member(Element,CL),
 4690	!.
 4691getAllSuperElements(Element,CL3,CL1,node(CL,NL)) :-
 4692	motel_union(CL,CL1,CL2),
 4693	getAllSuperElements(Element,CL3,CL2,NL).
 4694
 4695getAllSuperElements(_Element,[],_CL1,[]) :-
 4696	!.
 4697getAllSuperElements(Element,CL2,CL1,[N1|NL]) :-
 4698	getAllSuperElements(Element,CL3,CL1,N1),
 4699	getAllSuperElements(Element,CL4,CL1,NL),
 4700	motel_union(CL3,CL4,CL2).
 4701
 4702
 4703/***********************************************************************
 4704 *
 4705 * getDirectSubElements(+Element,-CL,+Dag)
 4706 * Parameter: Element     element name
 4707 *            CL          list of element names
 4708 *            Dag         subsumption hierarchy
 4709 * CL is the list of all element names which are direct sub elements
 4710 * of Element.
 4711 *
 4712 */
 4713
 4714getDirectSubElements(Element,CL1,node(CL,NL)) :-
 4715	member(Element,CL),
 4716	!,
 4717	getSubElements(CL1,NL).
 4718getDirectSubElements(Element,CL1,node(_CL,NL)) :-
 4719	getDirectSubElements(Element,CL1,NL).
 4720
 4721getDirectSubElements(_Element,[],[]) :-
 4722	!.
 4723getDirectSubElements(Element,CL,[N1|NL]) :-
 4724	getDirectSubElements(Element,CL1,N1),
 4725	getDirectSubElements(Element,CL2,NL),
 4726	motel_union(CL1,CL2,CL).
 4727
 4728getSubElements([],[]) :-
 4729	!.
 4730getSubElements(CL,[node(CL1,_)|NL]) :-
 4731	getSubElements(CL2,NL),
 4732	motel_union(CL1,CL2,CL).
 4733
 4734
 4735/***********************************************************************
 4736 *
 4737 * getAllSubElements(+Dag,+Element,-CL,+Dag)
 4738 * Parameter: Element     element name
 4739 *            CL          list of element names
 4740 *            Dag         subsumption hierarchy
 4741 * CL is the list of all element names which are sub elements of 
 4742 * Element
 4743 *
 4744 */
 4745
 4746getAllSubElements(Element,CL1,node(CL,NL)) :-
 4747	member(Element,CL),
 4748	!,
 4749	getElements(CL1,NL).
 4750getAllSubElements(Element,CL1,node(_CL,NL)) :-
 4751	getAllSubElements(Element,CL1,NL),
 4752	!.
 4753
 4754getAllSubElements(_Element,[],[]) :-
 4755	!.
 4756getAllSubElements(Element,CL,[N1|NL1]) :-
 4757	getAllSubElements(Element,CL2,N1),
 4758	getAllSubElements(Element,CL3,NL1),
 4759	motel_union(CL2,CL3,CL).
 4760
 4761/***********************************************************************
 4762 *
 4763 * getElements(-CL,+Dag)
 4764 * Parameter: CL     list of element names
 4765 *            Dag    subsumption hierarchy
 4766 * CL is the list of all element names in the subsumption hierarchy.
 4767 *
 4768 */
 4769
 4770getElements(CL,node(CL1,NL)) :-
 4771	getElements(CL2,NL),
 4772	motel_union(CL1,CL2,CL).
 4773getElements([],[]) :-
 4774	!.
 4775getElements(CL,[N1|NL]) :-
 4776	getElements(CL1,N1),
 4777	getElements(CL2,NL),
 4778	motel_union(CL1,CL2,CL).
 4779
 4780
 4781/***********************************************************************
 4782 *
 4783 * testDirectSuperElement(+Element1,+Element2,-Element,+Dag)
 4784 * Parameter: Element1       element name
 4785 *            Element2       element name
 4786 *            Element        element name
 4787 *            Dag            subsumption hierarchy
 4788 * Element is Element1 iff Element1 is a direct superelement of Element2
 4789 * or
 4790 * Element is Element2 iff Element2 is a direct superelement of Element1
 4791 * otherwise
 4792 * the predicate fails
 4793 *
 4794 */
 4795
 4796testDirectSuperElement(Element1,Element2,Element1,node(CL,NL)) :-
 4797	member(Element1,CL),
 4798	!,
 4799	memberDirectSubElements(Element2,node(CL,NL)).
 4800testDirectSuperElement(Element1,Element2,Element2,node(CL,NL)) :-
 4801	member(Element2,CL),
 4802	!,
 4803	memberDirectSubElements(Element1,node(CL,NL)).
 4804
 4805/***********************************************************************
 4806 *
 4807 * testDirectSubElement(+Element1,+Element2,-Element,+Dag)
 4808 * Parameter: Element1       element name
 4809 *            Element2       element name
 4810 *            Element        element name
 4811 *            Dag            subsumption hierarchy
 4812 * Element is Element1 iff Element1 is a direct subelement of Element2
 4813 * or
 4814 * Element is Element2 iff Element2 is a direct subelement of Element1
 4815 * otherwise
 4816 * the predicate fails
 4817 *
 4818 */
 4819
 4820testDirectSubElement(Element1,Element2,Element2,node(CL,NL)) :-
 4821	member(Element1,CL),
 4822	!,
 4823	memberDirectSubElements(Element2,node(CL,NL)).
 4824testDirectSubElement(Element1,Element2,Element1,node(CL,NL)) :-
 4825	member(Element2,CL),
 4826	!,
 4827	memberDirectSubElements(Element1,node(CL,NL)).
 4828
 4829
 4830/***********************************************************************
 4831 *
 4832 * testSuperElement(+Element1,+Element2,-Element,+Dag)
 4833 * Parameter: Element1       element name
 4834 *            Element2       element name
 4835 *            Element        element name
 4836 *            Dag            subsumption hierarchy
 4837 * Element is Element1 iff Element1 is a direct superelement of Element2
 4838 * or
 4839 * Element is Element2 iff Element2 is a direct superelement of Element1
 4840 * otherwise
 4841 * the predicate fails
 4842 *
 4843 */
 4844
 4845testSuperElement(Element1,Element2,Element1,node(CL,NL)) :-
 4846	member(Element1,CL),
 4847	!,
 4848	memberElementSubtrees(Element2,NL).
 4849testSuperElement(Element1,Element2,Element2,node(CL,NL)) :-
 4850	member(Element2,CL),
 4851	!,
 4852	memberElementSubtrees(Element1,NL).
 4853
 4854
 4855/***********************************************************************
 4856 *
 4857 * testSubElement(+Element1,+Element2,-Element,+Dag)
 4858 * Parameter: Element1       element name
 4859 *            Element2       element name
 4860 *            Element        element name
 4861 *            Dag            subsumption hierarchy
 4862 * Element is Element1 iff Element1 is a direct superelement of Element2
 4863 * or
 4864 * Element is Element2 iff Element2 is a direct superelement of Element1
 4865 * otherwise
 4866 * the predicate fails
 4867 *
 4868 */
 4869
 4870testSubElement(Element1,Element2,Element1,node(CL,NL)) :-
 4871	member(Element2,CL),
 4872	!,
 4873	memberElementSubtrees(Element1,NL).
 4874testSubElement(Element1,Element2,Element2,node(CL,NL)) :-
 4875	member(Element1,CL),
 4876	!,
 4877	memberElementSubtrees(Element2,NL).
 4878
 4879
 4880/***********************************************************************
 4881 *
 4882 * getCommonSuperElements(+CL1,-CL2,+Dag)
 4883 * Parameter: CL1      list of element names
 4884 *            CL2      list of element names
 4885 *            Dag      subsumption hierarchy
 4886 * CL2 is the list of all element names subsuming all elements in CL1.
 4887 *
 4888 */
 4889
 4890getCommonSuperElements(CL1,CL2,Dag) :-
 4891	hop_map(getAllSuperElements,[Dag],CL1,CLL1),
 4892	intersection_motel(CLL1,CL2).
 4893
 4894/***********************************************************************
 4895 *
 4896 * getCommonSubElements(+CL1,-CL2,Dag)
 4897 * Parameter: CL1      list of element names
 4898 *            CL2      list of element names
 4899 *            Dag      subsumption hierarchy
 4900 * CL2 is the list of all element names which are subsumed by all
 4901 * elements in CL1.
 4902 *
 4903 */
 4904
 4905getCommonSubElements(CL1,CL2,Dag) :-
 4906	hop_map(getAllSubElements,[Dag],CL1,CLL1),
 4907	intersection_motel(CLL1,CL2).
 4908
 4909
 4910
 4911
 4912
 4913
 4914
 4915
 4916/**********************************************************************
 4917 *
 4918 * @(#) env.pl 1.21@(#)
 4919 *
 4920 */
 4921
 4922
 4923/**********************************************************************
 4924 *
 4925 * getCurrentEnvironment(EnvName)
 4926 * gets the name of the current environment
 4927 *
 4928 */
 4929
 4930getCurrentEnvironment(EnvName) :-
 4931	currentEnvironment(Env),
 4932	environment(EnvName,Env,_),
 4933	!.
 4934
 4935/**********************************************************************
 4936 *
 4937 * makeEnvironment(+Name,+Comment)
 4938 * creates new environement with name Name. Comment can be any string
 4939 * Name will become the current environment.
 4940 *
 4941 */
 4942
 4943makeEnvironment(Name,Comment) :-
 4944	getTwoRandomNumbers(RT,CT),
 4945	FirstChar is 97 + (CT mod 26),
 4946	Runtime   is (RT mod 10000),
 4947	name(Runtime,RTChars),
 4948	name(EnvIdentifier,[FirstChar|RTChars]),
 4949	asserta_logged(environment(Name,env(EnvIdentifier),Comment)),
 4950	retractall_head(currentEnvironment(_)),
 4951	asserta_logged(currentEnvironment(env(EnvIdentifier))),
 4952	!.
 4953
 4954/**********************************************************************
 4955 *
 4956 * showEnvironment(+Name)
 4957 * 
 4958 */
 4959
 4960showEnvironment :- 
 4961	getCurrentEnvironment(Name),
 4962	showEnvironment(Name),
 4963	!.
 4964
 4965showEnvironment(EnvName) :-
 4966	environment(EnvName,Name,Comment),
 4967	write('Knowledge base '), 
 4968	write(EnvName), 
 4969	nl,
 4970	write('('),
 4971	write(Comment),
 4972	write(')'),
 4973	nl,
 4974	showModalAxioms(Name),
 4975	showDefprimconcept(Name),
 4976	showDefconcept(Name),
 4977	showDefprimrole(Name),
 4978	showDefrole(Name),
 4979	showDefclosed(Name),
 4980	showAssertConcept(Name),
 4981	showAssertRole(Name),
 4982	showFDW(Name),
 4983	!.
 4984
 4985showModalAxioms(Name) :-
 4986	modalAxioms(Name,user,K,C,MOp,A),
 4987	(nonvar(A) ; (A = C)),
 4988	write('        '), write('     modalAxioms('), write(K), write(','),
 4989	write(MOp), write(','), write(A), write(')'), nl,
 4990	fail.
 4991showModalAxioms(_) :-
 4992	!.
 4993showAssertConcept(Name) :-
 4994	clause(conceptElement(Name,MS,_,user,A,C,Ax),_),
 4995	write(Ax), write(':     assert_ind('), write(MS), write(','),
 4996	write(A), write(','), write(C), write(')'), nl,
 4997	fail.
 4998showAssertConcept(_) :-
 4999	!.
 5000showAssertRole(Name) :-
 5001	clause(roleElement(Name,MS,_,user,A,B,R,Ax),_),
 5002	write(Ax), write(':     assert_ind('), write(MS), write(','),
 5003	write(A), write(','), write(B), write(','), write(R), write(')'), nl,
 5004	fail.
 5005showAssertRole(_) :-
 5006	!.
 5007showDefconcept(Name) :-
 5008	conceptEqualSets(Name,user,MS,CN,CT,Ax),
 5009	write(Ax), write(':     defconcept('), write(MS), write(','),
 5010	write(CN), write(','), write(CT), write(')'), nl,
 5011	fail.
 5012showDefconcept(_Name) :-
 5013	!.
 5014showDefprimconcept(Name) :-
 5015	conceptSubsets(Name,user,MS,CN,CT,Ax),
 5016	write(Ax), write(': defprimconcept('), write(MS), write(','),
 5017	write(CN), write(','), write(CT), write(')'), nl,
 5018	fail.
 5019showDefprimconcept(_Name) :-
 5020	!.
 5021showDefrole(Name) :-
 5022	roleEqualSets(Name,user,MS,CN,CT,Ax),
 5023	write(Ax), write(':        defrole('), write(MS), write(','),
 5024	write(CN), write(','), write(CT), write(')'), nl,
 5025	fail.
 5026showDefrole(_Name) :-
 5027	!.
 5028showDefprimrole(Name) :-
 5029	roleSubsets(Name,user,MS,CN,CT,Ax),
 5030	write(Ax), write(':    defprimrole('), write(MS), write(','),
 5031	write(CN), write(','), write(CT), write(')'), nl,
 5032	fail.
 5033showDefprimrole(_Name) :-
 5034	!.
 5035showDefclosed(Name) :-
 5036	closed(Name,MS,X,Y,R),
 5037	write('axiom   '), write(':     defclosed('), write(MS), write(','),
 5038	write(X), write(','), write(Y), write(','), write(R), write(')'),
 5039	nl,
 5040	fail.
 5041showDefclosed(_Name) :-
 5042	!.
 5043
 5044
 5045/**********************************************************************
 5046 *
 5047 * removeEnvironment(+Name)
 5048 *
 5049 */
 5050
 5051removeEnvironment :-
 5052	getCurrentEnvironment(EnvName),
 5053	!,
 5054	removeEnvironment(EnvName).
 5055
 5056
 5057removeEnvironment(Name) :-
 5058	clearEnvironment(Name),
 5059	retractall_head(environment(Name,_,_)),
 5060	retract(currentEnvironment(Name)),
 5061	asserta_logged(currentEnvironment(env(e0))),
 5062	!.
 5063removeEnvironment(_Name) :-
 5064	% if we get here, Name was not the current environemt
 5065	!.
 5066
 5067/***********************************************************************
 5068 *
 5069 * clearEnvironment(Name)
 5070 *
 5071 */
 5072
 5073clearEnvironment :- 
 5074	getCurrentEnvironment(EnvName),
 5075	clearEnvironment(EnvName),
 5076	!.
 5077
 5078clearEnvironment(EnvName) :-
 5079	environment(EnvName,Env,_),
 5080	retractCompiledPredicates(Env),
 5081	retractallEnv(Env,in/9),
 5082	retractallEnv(Env,kb_in/10),
 5083	retractallEnv(Env,eq/9),
 5084	retractallEnv(Env,constraint/8),
 5085	retractallEnv(Env,rel/5),
 5086	retractallEnv(Env,closed/5),
 5087	retractallEnv(Env,compiledPredicate/2),
 5088	retractallEnv(Env,conceptElement/7),
 5089	retractallEnv(Env,conceptEqualSets/6),
 5090	retractallEnv(Env,conceptHierarchy/3),
 5091	retractallEnv(Env,conceptName/4),
 5092	retractallEnv(Env,conceptSubsets/6),
 5093	retractallEnv(Env,environment/3),
 5094	retractallEnv(Env,given_change/4),
 5095	retractallEnv(Env,given_inflLink/4),
 5096	retractallEnv(Env,modalAxioms/6),
 5097	retractallEnv(Env,roleAttributes/5),
 5098	retractallEnv(Env,roleDefault/4),
 5099	retractallEnv(Env,roleDefNr/4),
 5100	retractallEnv(Env,roleDomain/4),
 5101	retractallEnv(Env,roleElement/8),
 5102	retractallEnv(Env,roleEqualSets/6),
 5103	retractallEnv(Env,roleHierarchy/3),
 5104	retractallEnv(Env,roleName/4),
 5105	retractallEnv(Env,roleNr/5),
 5106	retractallEnv(Env,roleRange/4),
 5107	retractallEnv(Env,roleSubsets/6),
 5108	retractallEnv(Env,sub/4),
 5109	retractallEnv(Env,succ/4),
 5110	retractallEnv(Env,abductiveDerivation/3),
 5111	retractallEnv(Env,consistencyDerivation/3),
 5112	retractallEnv(Env,hypothesis/1),
 5113	retractallEnv(Env,inconsistencyCheck/3),
 5114	retractallEnv(Env,motel_option/2),
 5115	retractallEnv(Env,nsub/4),
 5116	retractallEnv(Env,nsub3/2),
 5117	retractallEnv(Env,sub3/2),
 5118	retractallEnv(Env,succ3/2),
 5119	!.
 5120
 5121/**********************************************************************
 5122 *
 5123 * retractCompiledPredicates(+Env)
 5124 * if the environment Env contains compiled predicates, then for each
 5125 * compiled predicate Pred there is a fact 
 5126 *                    compilePredicate(Env,Pred/Arity).
 5127 * So when the environment is to be removed, we just abolish the 
 5128 * compiled predicates.
 5129 *
 5130 */
 5131
 5132retractCompiledPredicates(Env) :-
 5133	compiledPredicate(Env,Pred/Arity),
 5134	abolish(Pred/Arity),
 5135	fail.
 5136retractCompiledPredicates(_) :-
 5137	!.
 5138
 5139
 5140/**********************************************************************
 5141 *
 5142 * initEnvironment(Name)
 5143 *
 5144 */
 5145
 5146initEnvironment :- 
 5147	getCurrentEnvironment(EnvName),
 5148	initEnvironment(EnvName),
 5149	!.
 5150
 5151initEnvironment(EnvName) :-
 5152	clearEnvironment(EnvName),
 5153	environment(EnvName,Env,_),
 5154	assert_logged(theory(Env,
 5155	[
 5156        (in([],P,pair(X,Y)) <== equal(X,Z), in([],P,pair(Z,Y))),
 5157	(in([],P,pair(X,Y)) <== equal(Y,Z), in([],P,pair(X,Z))),
 5158	(in([],C,X) <== equal(X,Y), in([],C,Y)),
 5159	(equal(X,Y) <== equal(Y,X)),
 5160	(equal(X,X) <== true),
 5161	(in(MS,'top',X) <== true)])),
 5162	assertInRules(Env),
 5163	% Assert equality axioms
 5164	assertEqRule(Env,1),
 5165	% Assert 'top' role
 5166skipped	assertEqRule(Env,2),
 5167	assertEqRule(Env,3),
 5168	% Proof by hypothesis for roles (Test 14.07.92)
 5169	assertEqRule(Env,4),
 5170	% Assert 'top' concept
 5171	assertInRule(Env,1),
 5172	% Assert 'bot' concept
 5173	assertInRule(Env,2),
 5174	% Proof by hypothesis for concepts
 5175	assertInRule(Env,3),
 5176	% Assert X in some(r,c) => X in atleast(1,r)
 5177skipped	gensym(axiom,AN11),
 5178skipped	assertInRule(Env,3,AN11),
 5179	% Assert X in atleast(1,r) => X in some(r,'top')
 5180skipped	assertInRule(Env,4,AN11),
 5181	% Assert X in atmost(0,r) => X in all(r,c)
 5182skipped	gensym(axiom,AN10),
 5183skipped	assertInRule(Env,1,AN10),
 5184	% Assert X in all(r,'bot') => X in atmost(0,r)
 5185skipped	assertInRule(Env,2,AN10),
 5186	% Assert not('top') law
 5187	% necessary for inconsistent knowledge bases?
 5188	% bad influence on runtime!
 5189skipped	assertInRule(Env,4),
 5190	% Assert double negation laws
 5191	gensym(axiom,AN6),
 5192skipped	assertInRule(Env,5,AN6),
 5193skipped	assertInRule(Env,6,AN6),
 5194	% Concrete domains
 5195	gensym(axiom,AN7),
 5196skipped	assertInRule(Env,7,AN7),
 5197skipped	assertInRule(Env,8,AN7),
 5198skipped	assertInRule(Env,9,AN7),
 5200	% Proof by abductive hypothesis
 5201	assertAbductionRule(Env,1),
 5202	% Proof by abduction
 5203	assertAbductionRule(Env,2),
 5204	% Meta Reasoning
 5205skipped	metaReasoning,
 5206	% Assert concept hierarchy
 5207	assertz_logged(conceptHierarchy(Env,[],node(['top'],[]))),
 5208	assertz_logged(conceptName(Env,[],[],'top')),
 5209	assertz_logged(conceptName(Env,[],[],'bot')),
 5210	% Assert role hierarchy
 5211	assertz_logged(roleHierarchy(Env,[],node(['top'],[]))),
 5212	assertz_logged(roleName(Env,[],[],'top')),
 5213	initFuncdep,
 5214	!
 5214.
 5215
 5216/**********************************************************************
 5217 *
 5218 * assertInRules(+Env)
 5219 * asserts the clauses for the in predicate which is used to 
 5220 * construct goals in the user interface. In general the in clauses
 5221 * just call corresponding kb_in clauses. The kb_in clauses result
 5222 * from the translation of terminological and assertional axioms.
 5223 *
 5224 * !! Remember: Changes to this clauses have to be reflected in the
 5225 *              definition of the compileEnv predicate.
 5226 *
 5227 */
 5228
 5229assertInRules(Env) :-
 5230	assertz_logged((in(Env,Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT) :-
 5231		 ifOption(traceOutput,yes,(length(CALL,Depth), format('trying ~d  in(~w,~w)~n',[Depth,CN,CON]))),
 5232	kb_in(Env,pr(5),Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT),
 5233		 ifOption(traceOutput,yes,(length(CALL,Depth), format('succeeded ~d  in(~w,~w)~n',[Depth,CN,CON]))))),
 5234% There are no kb_in clauses with priority 4 at the moment (07.10.92)
 5235skipped	assertz_logged((in(Env,Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT) :-
 5236skipped	kb_in(Env,pr(4),Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT))),
 5237	assertz_logged((in(Env,Name,modal(MS),CN,CON,hyp([or(H1),rl(H2),fl(H3)]),ab(noAb),call(CALL),PT) :-
 5238		 clashInHyp(H2), !, fail)),
 5239	assertz_logged((in(Env,Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT) :-
 5240		 (CN \== 'top', CN \== 'bot', CN \== not('top'), CN \== not('bot'),
 5241	kb_in(Env,pr(3),Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT)))),
 5242% There are no kb_in clauses with priority 2 at the moment (07.10.92)
 5243skipped	assertz_logged((in(Env,Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT) :-
 5244skipped	kb_in(Env,pr(2),Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT))),
 5245	assertz_logged((in(Env,Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT) :-
 5246		 (CN \== 'top',CN \== 'bot', CN \== not('top'), CN \== not('bot'),
 5247	kb_in(Env,pr(1),Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT)))),
 5248% Experimental code (07.10.92 uh)
 5249% It might be useful to have global information about the failure of
 5250% derivations. With the code below such a failure is used to assert_logged to
 5251% hypothesis that the negation of the goal is true.
 5252%	assertz_logged((in(Env,Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT) :-
 5253%		 (nonvar(CON), nonvar(CN), 
 5254%		  \+ hypothesis(in(Env,modal(MS),CN,CON,ab(D),PT)),
 5255%		  getNegatedConcept(CN,C1),
 5256%		  assertz_logged(hypothesis(in(Env,modal(MS),C1,CON,ab(D),assume))),
 5257%		  fail))),
 5258% There are no kb_in clauses with priority 0 at the moment (07.10.92)
 5259skipped	assertz_logged((in(Env,Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT) :-
 5260skipped	kb_in(Env,pr(0),Name,modal(MS),CN,CON,hyp(HYP),ab(D),call(CALL),PT))),
 5261	!.
 5262
 5263assertEqRule(Env,1) :-
 5264	gensym(axiom,AN1),
 5265	gensym(rule,RN1),
 5266	constructEqHead(Env,rn(AN1,RN1,user,lInR),W1,app((F:R),X),F,R,X,HYPS,AB,CALLS,PT3,EqHead1),
 5267	constructMLCall(Env,rn(AX,_RN4,_S4,_O4),bodyMC(W1),headMC(W1),normal(R),X,HYPS,AB,CALLS,PT3,InHead2),
 5268	assertz_logged((EqHead1 :- cCS(CALLS,true),  simple_term(X))),
 5269%       The following would be correct
 5270%	assertz_logged((EqHead1 :- cCS(CALLS,true),  InHead2)),
 5271%       old code (uh 20.08.92)
 5272%	assertz_logged((eq(Env,rn(AN1,RN1,user,lInR),modal(MS),X,X,hyp(HYPS),
 5273%                   ab(_AB),call(CALLS),proved(eq(MS,X,X,hyp(HYPS),
 5274%                   basedOn(true)))) :- 
 5275%                (cCS(CALLS,true)))),
 5276	!.
 5277assertEqRule(Env,2) :-
 5278	Role1 =.. ['top',_X,_Y],
 5279	gensym(axiom,AN3),
 5280	gensym(rule,RN3),
 5281	constructMLHead(Env,rn(AN3,RN3,user,lInR),_MS,_,Role1,
 5282			_HYPS,_D,_CALLS,tbox,InHeadR),
 5283	assertz_logged((InHeadR)),
 5284	!.
 5285assertEqRule(Env,3) :-
 5286	gensym(axiom,AN20),
 5287	gensym(rule,RN20),
 5288	constructEqHead(Env,rn(AN20,RN20,user,lInR),_W1,_Y,_F,'top',_X,_HYPS,_D,_CALLS,tbox,EqHead20),
 5289	assertz_logged(EqHead20),
 5290	!.
 5291assertEqRule(Env,4) :-
 5292	gensym(axiom,AN21),
 5293	gensym(rule,RN21),
 5294	HYPS = [or(H1),rl(H2),fl(H3)],
 5295	constructEqHead(Env,rn(AN21,RN21,user,lInR),W1,Y,F,R,X,HYPS,D,_CALLS,tbox,EqHead20),
 5296	constructEqMark(rn(_AN21,_RN21,_,_),W1,Y,F,R,X,_HYPS2,D,_CALLS2,EqMark20),
 5297	assertz_logged((EqHead20 :- append(H1,H2,H), member(EqMark20,H))),
 5298	!.
 5299
 5300
 5301assertInRule(Env,1) :-
 5302	% For all X: X in 'top'
 5303	% Priority 5 (high priority)
 5304	gensym(axiom,AN2),
 5305	gensym(rule,RN2),
 5306	constructKBHead(Env,pr(5),rn(AN2,RN2,user,lInR),_W1,'top',_X,
 5307			_HYPS,_D,_CALLS,tbox,InHead),
 5308	assertz_logged(InHead),
 5309	!.
 5310assertInRule(Env,2) :-
 5311	% For all X: X in not('bot') 
 5312	% What is actually needed is the equivalence of 'top' and not('bot').
 5313	% So we need
 5314	% For all X: X in 'top' if X in not('bot')
 5315	% is subsumed by assertInRule(Env,1).
 5316	% For all X: X in not('top') if X in 'bot'
 5317	% This rule will not be asserted.
 5318	% For all X: X in 'bot' if X in not('top')
 5319	% is subsumed by assertInRule(Env,4).
 5320	% For all X: X in not('bot') if X in 'top'.
 5321	% is subsumed by assertInRule(Env,2), i.e. the rule we will
 5322	% assert_logged now.
 5323	% Priority 5 (high priority)
 5324	gensym(axiom,AN4),
 5325	gensym(rule,RN4),
 5326	constructKBHead(Env,pr(5),rn(AN4,RN4,user,lInR),_W1,not('bot'),X,
 5327	                _HYPS,_D,_CALLS,tbox,InHead1),
 5328	assertz_logged(InHead1),
 5329	!.
 5330assertInRule(Env,3) :-
 5331	% For all X: X in C if (X in C) is a hypothesis
 5332	% Priority 5 (high priority)
 5333	gensym(axiom,AN4),
 5334	gensym(rule,RN4),
 5335	HYPS = [or(H1),rl(H2),fl(H3)],
 5336	constructInHead(Env,rn(_AN5,_RN5,_S5,_O5),MS,C,X,_HYPS,_D1,_CALLS1,_,InHead1),
 5337	constructKBHead(Env,pr(5),rn(AN4,RN4,system,lInR),MS,C,X,
 5338	                HYPS,_D,_CALLS2,usingHyp(InHead1),InHead2),
 5339	assertz_logged((InHead2 :- append(H1,H2,H), member(InHead1,H))),
 5340	constructMLMark(InHead1,Mark1),
 5341	assertz_logged((InHead2 :- (append(H1,H2,H), member(Mark1,H)) ; memberDML(Mark1,H3))),
 5342	!.
 5343assertInRule(Env,4) :-
 5344	% For all X: X in not('top') => X in C 
 5345	% Priority 1 (low priority)
 5346	% necessary for inconsistent knowledge bases ?
 5347	gensym(axiom,AN7),
 5348	gensym(rule,RN7),
 5349	constructKBHead(Env,pr(1),rn(AN7,RN7,system,lInR),MS,_C,X,
 5350	                HYPS,D,CALLS,PT3,InHead3),
 5351	constructMLCall(Env,rn(AN7,_RN7,_S7,_O7),bodyMC(MS),headMC(MS),
 5352                        not('top'),X,HYPS,D,CALLS,PT3,L3),
 5353 	constructMLMark(InHead3,Mark3),
 5354	assertz_logged((InHead3 :- cCS(CALLS,Mark3), L3)),
 5355	!.
 5356
 5357assertInRule(Env,1,AN10) :- 
 5358	% Assert x in atmost(0,r) => x in all(r,c)
 5359	% Priority 1 (low priority)
 5360	gensym(rule,Rule),
 5361	ruleName(AN10,Rule,system,lInR,RN),
 5362	convertInConsequence(Env,pr(1),RN,_MS,W,all(R,C),X,
 5363	                     HYPS,AB,CALLS,PT,(EqLiteral,InHead)),
 5364	constructMLMark(InHead,Mark),
 5365	convertInAntecedent(Env,rn(AN10,system,lInR),bodyMC(W),headMC(W),
 5366			    atmost(0,R),X,HYPS,AB,CALLS,PT,Body),
 5367	asserta_logged((InHead :- (nonvar(C), (cCS(CALLS,Mark), once((EqLiteral, Body)))))),
 5368	!.
 5369assertInRule(Env,2,AxiomName) :-
 5370	% Assert x in all(r,'bot') => x in atmost(0,r)
 5371	% Priority 1 (low priority)
 5372	convertInAntecedent(Env,rn(AxiomName,_O,_Orientation),
 5373                            bodyMC(W1),headMC(W1),all(R,'bot'),X,
 5374			    HYPS,AB,CALLS,PT1,Body),
 5375	gensym(rule,RuleName),
 5376	ruleName(AxiomName,RuleName,system,lInR,RN1),
 5377	convertInConsequence(Env,pr(1),RN1,_MS,W1,
 5378			     atmost(0,R),X,HYPS,AB,CALLS,PT1,InHead1),
 5379	constructConMark(InHead1,Mark1),
 5380	asserta_logged((InHead1 :- (nonvar(R),(cCS(CALLS,Mark1), once(Body))))),
 5381	!.
 5382assertInRule(Env,3,AxiomName) :-
 5383	% Assert x in some(R,'top') => x in atleast(1,R)
 5384	% Priority 1 (low priority)
 5385	convertInAntecedent(Env,rn(AxiomName,system,lInR),
 5386			    bodyMC(W1),headMC(W1),
 5387			    some(R,'top'),X,HYPS,AB,CALLS,PT1,Body),
 5388	gensym(rule,RuleName),
 5389	ruleName(AxiomName,RuleName,system,lInR,RN1),
 5390	convertInConsequence(Env,pr(1),RN1,_MS,W1,
 5391			     atleast(1,R),X,HYPS,AB,CALLS,PT1,InHead1),
 5392	constructConMark(InHead1,Mark1),
 5393	asserta_logged((InHead1 :- (nonvar(R), cCS(CALLS,Mark1), once(Body)))),
 5394	!.
 5395assertInRule(Env,4,AxiomName) :-
 5396	% Assert x in atleast(1,R) => x in some(R,'top')
 5397	% Priority 1 (low priority)
 5398	gensym(rule,RuleName),
 5399	ruleName(AxiomName,RuleName,system,lInR,RN1),
 5400	convertInConsequence(Env,pr(1),RN1,_MS,W1,some(R,'top'),X,
 5401	                     HYPS,AB,CALLS,PT1,(EqLiteral, InHead1)),
 5402	constructMLMark(InHead1,Mark1),
 5403	convertInAntecedent(Env,rn(AxiomName,system,lInR),
 5404	                    bodyMC(W1),headMC(W1),
 5405			    atleast(1,R),X,HYPS,AB,CALLS,PT1,Body),
 5406	asserta_logged((InHead1 :- (cCS(CALLS,Mark1), once((EqLiteral, Body))))),
 5407	!.
 5408assertInRule(Env,5,AN6) :-
 5409	% For all X: X in C => X in not(not(C))
 5410	% Priority 1 (low priority)
 5411	gensym(rule,RN6),
 5412	constructKBHead(Env,pr(1),rn(AN6,RN6,system,lInR),MS,not(not(C)),X,
 5413	                HYPS,D,CALLS,PT4,Consequence3),
 5414	constructMLCall(Env,rn(AN6,_RN9,_S9,_O9),bodyMC(MS),headMC(MS),C,X,
 5415	                HYPS,D,CALLS,PT4,Antecedent4),
 5416	constructMLMark(Consequence3,AxiomHead3),
 5417	assertz_logged((Consequence3 :- cCS(CALLS,AxiomHead3), Antecedent4)),
 5418	!.
 5419assertInRule(Env,6,AN6) :-
 5420	% For all X: X in not(not(C)) => X in C 
 5421	% Priority 1 (low priority)
 5422	gensym(rule,RN8),
 5423	constructKBHead(Env,pr(1),rn(AN6,RN8,system,lInR),MS,C,X,
 5424	                HYPS,D,CALLS,PT3,Consequence4),
 5425	constructMLCall(Env,rn(AN6,_RN7,_S7,_O7),bodyMC(MS),headMC(MS),
 5426			not(not(C)),X,HYPS,D,CALLS,PT3,Antecedent3),
 5427	constructMLMark(Consequence4,AxiomHead4),
 5428	assertz_logged((Consequence4 :- cCS(CALLS,AxiomHead4), Antecedent3)),
 5429	!.
 5430assertInRule(Env,7,AN7) :-
 5431	% For all X: X in set(S2) and motel_subset(S2,S1) => X in S1
 5432	% Priority 1 (low priority)
 5433	gensym(rule,RN8),
 5434	constructKBHead(Env,pr(1),rn(AN7,RN8,system,lInR),MS,set(S1),X,
 5435	                HYPS,D,CALLS,PT2,Consequence1),
 5436	constructMLCall(Env,rn(AN7,_RN2,_S2,_O2),bodyMC(MS),headMC(MS),
 5437			set(S2),X,HYPS,D,CALLS,PT2,Antecedent2),
 5438	L1 = motel_subset(S2,S1),
 5439	constructMLMark(Consequence1,AxiomHead1),
 5440	assertz_logged((Consequence1 :- cCS(CALLS,AxiomHead1), (Antecedent2, L1))),
 5441	!.
 5442assertInRule(Env,8,AN7) :-
 5443	% For all X: X in set(S2) and X in set(S3) and 
 5444	%            intersection_motel(S2,S3,S1) => X in S1
 5445	% Priority 1 (low priority)
 5446	gensym(rule,RN8),
 5447	constructKBHead(Env,pr(1),rn(AN7,RN8,system,lInR),MS,set(S1),X,
 5448	                HYPS,D,CALLS,and([PT2,PT3]),Consequence1),
 5449	constructMLCall(Env,rn(AN7,_RN2,_S2,_O2),bodyMC(MS),headMC(MS),
 5450			set(S2),X,HYPS,D,CALLS,PT2,Antecedent2),
 5451	constructMLCall(Env,rn(AN7,_RN3,_S3,_O3),bodyMC(MS),headMC(MS),
 5452			set(S3),X,HYPS,D,CALLS,PT3,Antecedent3),
 5453	L1 = intersection_motel([S2,S3],S1),
 5454	constructMLMark(Consequence1,AxiomHead1),
 5455	assertz_logged((Consequence1 :- cCS(CALLS,AxiomHead1), (Antecedent3, (Antecedent2, L1)))),
 5456	!.
 5457assertInRule(Env,9,AN7) :-
 5458	% For all X: X in set(S2) and X in set(S3) and 
 5459	%            intersection_motel(S2,S3,S1) => X in S1
 5460	% Priority 1 (low priority)
 5461	gensym(rule,RN8),
 5462	constructKBHead(Env,pr(1),rn(AN7,RN8,system,lInR),MS,not(set(S1)),X,
 5463	                HYPS,D,CALLS,and([PT2,PT3]),Consequence1),
 5464	constructMLCall(Env,rn(AN7,_RN2,_S2,_O2),bodyMC(MS),headMC(MS),
 5465			set(S2),X,HYPS,D,CALLS,PT2,Antecedent2),
 5466	constructMLCall(Env,rn(AN7,_RN3,_S3,_O3),bodyMC(MS),headMC(MS),
 5467			set(S3),X,HYPS,D,CALLS,PT3,Antecedent3),
 5468	L1 = motel_subtract(S2,S3,S1),
 5469	constructMLMark(Consequence1,AxiomHead1),
 5470	assertz_logged((Consequence1 :- cCS(CALLS,AxiomHead1), (Antecedent3, (Antecedent2, L1)))),
 5471	!.
 5472
 5473
 5474assertAbductionRule(Env,1) :-
 5475	% Proof by abductive hypothesis
 5476	gensym(axiom,AN1),
 5477	gensym(rule,RN1),
 5478	constructInHead(Env,rn(_AN2,_RN2,_S2,_O2),MS,C,X,
 5479	                _HYPS1,_D,_CALLS1,_,InHead1),
 5480	constructMLHead(Env,rn(AN1,RN1,system,lInR),MS,C,X,_HYPS2,D1,_CALLS2,usingAbHyp(in(MS,C,X)),InHead2),
 5481	assertz_logged((InHead2 :- memberDML(InHead1,D1))),
 5482	!.
 5483assertAbductionRule(Env,2) :-
 5484	% Proof by abduction
 5485	gensym(axiom,AN1),
 5486	gensym(rule,RN1),
 5487	constructInHead(Env,rn(_AN2,_RN2,_S2,_O2),MS,C,X,_HYPS,_D0,_CALLS3,_,InHead2),
 5488	constructMLHead(Env,rn(AN1,RN1,system,lInR),MS,C1,X,
 5489	                HYPS,D1,CALLS,usingAbHyp(in(MS,C,X)),InHead1),
 5490	L1 = normalizeNot(C1,C),
 5491	L2 = not(memberDML(InHead2,D1)),
 5492	L3 = addDefaultML(InHead2,D1),
 5493	assertz_logged((InHead1 :- L1, L2, L3)),
 5494	!.
 5495
 5496
 5497	
 5498
 5499/**********************************************************************
 5500 *
 5501 * switchToEnvironment(Name)
 5502 *
 5503 */
 5504
 5505switchToEnvironment(Name) :-
 5506	environment(Name,Env,_),
 5507	retractall_head(currentEnvironment(_)),
 5508	asserta_logged(currentEnvironment(Env)),
 5509	!.
 5510
 5511/**********************************************************************
 5512 *
 5513 * saveEnvironment(FileName)
 5514 * 
 5515 */
 5516
 5517saveEnvironment(FileName) :-
 5518	getCurrentEnvironment(EnvName),
 5519	saveEnvironment(EnvName,FileName).
 5520
 5521saveEnvironment(EnvName,FileName) :-
 5522	tell(FileName),
 5523	writeEnvironment(EnvName),
 5524	told,
 5525	% to be implemented
 5526	!.
 5527
 5528writeEnvironment(EnvName) :-
 5529	environment(EnvName,Env,C),
 5530	writeq(environment(EnvName,Env,C)), write('.'), nl,
 5531skipped	write(':- dynamic(constraint/8).'), nl,
 5532skipped	write(':- dynamic(in/9).'), nl,
 5533skipped	write(':- dynamic(kb_in/10).'), nl,
 5534skipped	write(':- dynamic(rel/5).'), nl,
 5535skipped	write(':- dynamic(eq/9).'), nl,
 5536skipped	write(':- dynamic(conceptHierarchy/3).'), nl,
 5537skipped	write(':- dynamic(roleHierarchy/3).'), nl,
 5538skipped	write(':- dynamic(conceptEqualSets/6).'), nl,
 5539skipped	write(':- dynamic(conceptSubsets/6).'), nl,
 5540skipped	write(':- dynamic(roleEqualSets/6).'), nl,
 5541skipped	write(':- dynamic(roleSubsets/6).'), nl,
 5542skipped	write(':- dynamic(conceptName/4).'), nl,
 5543skipped	write(':- dynamic(roleName/4).'), nl,
 5544skipped	write(':- dynamic(falsum/2).'), nl,
 5545skipped	write(':- dynamic(inconsistencyCheck/3).'), nl,
 5546skipped	write(':- dynamic(conceptElement/6).'), nl,
 5547skipped	write(':- dynamic(roleElement/7).'), nl,
 5548skipped	write(':- dynamic(modalAxioms/6).'), nl,
 5549	writeall(in(Env,_A0,_B0,_C0,_D0,_E0,_F0,_G0,_H0)),
 5550	writeall(kb_in(Env,_A1,_B1,_C1,_D1,_E1,_F1,_G1,_H1,_I1)),
 5551	writeall(eq(Env,_A11,_B11,_C11,_D11,_E11,_F11,_G11,_H11)),
 5552	writeall(constraint(Env,_A12,_B12,_C12,_D12,_E12,_F12,_G12)),
 5553	writeall(rel(Env,_A17,_B17,_C17,_D17)),
 5554	writeall(closed(Env,_A18,_B18,_C18,_D18)),
 5555	writeall(compiledPredicate(Env,_A19)),
 5556	writeall(conceptElement(Env,_A14,_B14,_C14,_D14,_E14,_F14)),
 5557	writeall(conceptEqualSets(Env,_A4,_B4,_C4,_D4,_E4)),
 5558	writeall(conceptHierarchy(Env,_A2,_B2)),
 5559	writeall(conceptName(Env,_A8,_B8,_C8)),
 5560	writeall(conceptSubsets(Env,_A5,_B5,_C5,_D5,_E5)),
 5561	writeall(Env,given_change/4),
 5562	writeall(Env,given_inflLink/4),
 5563	writeall(modalAxioms(Env,_A16,_B16,_C16,_D16,_E16)),
 5564	writeall(Env,roleAttributes/5),
 5565	writeall(Env,roleDefault/4),
 5566	writeall(Env,roleDefNr/4),
 5567	writeall(Env,roleDomain/4),
 5568	writeall(Env,roleElement/8),
 5569	writeall(Env,roleEqualSets/6),
 5570	writeall(Env,roleHierarchy/3),
 5571	writeall(Env,roleName/4),
 5572	writeall(Env,roleNr/5),
 5573	writeall(Env,roleRange/4),
 5574	writeall(Env,roleSubsets/6),
 5575	writeall(succ(_A17,Env,_B17,_C17)),
 5576	writeall(sub(_A18,Env,_B18,_C18)),
 5577	!.
 5578
 5579writeall(Head) :-
 5580	clause(Head,Body),
 5581	writeq((Head :- Body)), write('.'), nl, 
 5582	fail.
 5583writeall(_) :- !.
 5584
 5585writeall(Env,Pred/Arity) :-
 5586	constructHead(Env,Pred/Arity,Head),
 5587	clause(Head,Body),
 5588	writeq((Head :- Body)), write('.'), nl, 
 5589	fail.
 5590writeall(_,_) :- !.
 5591
 5592
 5593constructHead(Env,Pred/Arity,Head) :-
 5594	constructArguments(Env,Arity,[],Arguments),
 5595	Head =.. [Pred|Arguments],
 5596	!.
 5597constructArguments(Env,1,Args,[Env|Args]) :-
 5598	!.
 5599constructArguments(Env,N,Args,Arguments) :-
 5600	M is (N - 1),
 5601	constructArguments(Env,M,[X|Args],Arguments),
 5602	!.
 5603
 5604/**********************************************************************
 5605 *
 5606 * loadEnvironment(FileName)
 5607 * 
 5608 */
 5609
 5610loadEnvironment(FileName) :-
 5611	see(FileName),
 5612	read(environment(EnvName,Env,C)),
 5613	(removeEnvironment(EnvName) ; true),
 5614	asserta_logged(environment(EnvName,Env,C)),
 5615	repeat,
 5616	read(Clause),
 5617	assertClause(Clause),
 5618	seen,
 5619	!.
 5620loadEnvironment(FileName) :-
 5621	seen,
 5622	!,
 5623	fail.
 5624loadEnvironment(FileName,EnvName) :-
 5625	see(FileName),
 5626	read(environment(_EnvName2,Env,C)),
 5627	(removeEnvironment(EnvName) ; true),
 5628	assertz_logged(environment(EnvName,Env,C)),
 5629	repeat,
 5630	read(Clause),
 5631	assertClause(Clause),
 5632	seen,
 5633	!.
 5634loadEnvironment(_FileName,_EnvName) :-
 5635	seen,
 5636	!, 
 5637	fail.
 5638
 5639assertClause('end_of_file') :-
 5640	!.
 5641assertClause(Clause) :-
 5642	assertz_logged(Clause),
 5643	fail.
 5644
 5645/**********************************************************************
 5646 *
 5647 * copyEnvironment(Name1,Name2)
 5648 * copies environment Name1 to environment Name2.
 5649 *
 5650 */
 5651
 5652copyEnvironment(Name2) :-
 5653	getCurrentEnvironment(Name1),
 5654	copyEnvironment(Name1,Name2).
 5655
 5656copyEnvironment(Name1,Name2) :-
 5657	environment(Name1,Env1,Comment),
 5658	makeEnvironment(Name2,Comment),
 5659	% !! This environment mustn't be initializes because
 5660	% the clauses asserted usually during initialization
 5661	% will also be copied from environment Name1.
 5662	environment(Name2,Env2,_),
 5663	term_expansion(copy,on,Env1,Env2),
 5664	!,
 5665	copyAll(Env1,Env2,in/9),
 5666	copyAll(Env1,Env2,kb_in/10),
 5667	copyAll(Env1,Env2,eq/9),
 5668	copyAll(Env1,Env2,constraint/8),
 5669	copyAll(Env1,Env2,rel/5),
 5670	copyAll(Env1,Env2,closed/5),
 5671	copyAll(Env1,Env2,compiledPredicate/2),
 5672	copyAll(Env1,Env2,conceptElement/7),
 5673	copyAll(Env1,Env2,conceptEqualSets/6),
 5674	copyAll(Env1,Env2,conceptHierarchy/3),
 5675	copyAll(Env1,Env2,conceptName/4),
 5676	copyAll(Env1,Env2,conceptSubsets/6),
 5677	copyAll(Env1,Env2,environment/3),
 5678	copyAll(Env1,Env2,given_change/4),
 5679	copyAll(Env1,Env2,given_inflLink/4),
 5680	copyAll(Env1,Env2,modalAxioms/6),
 5681	copyAll(Env1,Env2,roleAttributes/5),
 5682	copyAll(Env1,Env2,roleDefault/4),
 5683	copyAll(Env1,Env2,roleDefNr/4),
 5684	copyAll(Env1,Env2,roleDomain/4),
 5685	copyAll(Env1,Env2,roleElement/8),
 5686	copyAll(Env1,Env2,roleEqualSets/6),
 5687	copyAll(Env1,Env2,roleHierarchy/3),
 5688	copyAll(Env1,Env2,roleName/4),
 5689	copyAll(Env1,Env2,roleNr/5),
 5690	copyAll(Env1,Env2,roleRange/4),
 5691	copyAll(Env1,Env2,roleSubsets/6),
 5692skipped	copyAll(Env1,Env2,sub/4),
 5693skipped	copyAll(Env1,Env2,succ/4),
 5694skipped	copyAll(Env1,Env2,motel_option/2),
 5695skipped	copyAll(Env1,Env2,nsub/4),
 5696	term_expansion(copy,off,Env1,Env2),
 5697	!.
 5698
 5699term_expansion(copy,on,Env1,Env2) :-
 5700	abolish(term_expansion/2),
 5701	assertz_logged((term_expansion((Head :- Body),(Head1 :- Body1)) :-
 5702	term_expansion(Head,Head1),
 5703	term_expansion(Body,Body1))),
 5704	assertz_logged((term_expansion((L, Body), (L1,Body1)) :-
 5705	term_expansion(L,L1),
 5706	term_expansion(Body,Body1))),
 5707	assertz_logged((term_expansion((L; Body), (L1,Body1)) :-
 5708	term_expansion(L,L1),
 5709	term_expansion(Body,Body1))),
 5710	assertz_logged((term_expansion(\+Atom,\+Atom1) :-
 5711	term_expansion(Atom,Atom1))),
 5712	assertz_logged((term_expansion(once(Body1),once(Body2)) :-
 5713		term_expansion(Body1,Body2))),
 5714	assertz_logged((term_expansion(call(Body1),call(Body2)) :-
 5715		 term_expansion(Body1,Body2))),
 5716	assertTermExpansionClause(in/9,Env1,Env2),
 5717	assertTermExpansionClause(kb_in/10,Env1,Env2),
 5718	assertTermExpansionClause(eq/9,Env1,Env2),
 5719	assertTermExpansionClause(constraint/8,Env1,Env2),
 5720	assertTermExpansionClause(rel/5,Env1,Env2),
 5721	assertTermExpansionClause(closed/5,Env1,Env2),
 5722	assertTermExpansionClause(compiledPredicate/2,Env1,Env2),
 5723	assertTermExpansionClause(conceptElement/7,Env1,Env2),
 5724	assertTermExpansionClause(conceptEqualSets/6,Env1,Env2),
 5725	assertTermExpansionClause(conceptHierarchy/3,Env1,Env2),
 5726	assertTermExpansionClause(conceptName/4,Env1,Env2),
 5727	assertTermExpansionClause(conceptSubsets/6,Env1,Env2),
 5728	assertTermExpansionClause(environment/3,Env1,Env2),
 5729	assertTermExpansionClause(given_change/4,Env1,Env2),
 5730	assertTermExpansionClause(given_inflLink/4,Env1,Env2),
 5731	assertTermExpansionClause(modalAxioms/6,Env1,Env2),
 5732	assertTermExpansionClause(roleAttributes/5,Env1,Env2),
 5733	assertTermExpansionClause(roleDefault/4,Env1,Env2),
 5734	assertTermExpansionClause(roleDefNr/4,Env1,Env2),
 5735	assertTermExpansionClause(roleDomain/4,Env1,Env2),
 5736	assertTermExpansionClause(roleElement/8,Env1,Env2),
 5737	assertTermExpansionClause(roleEqualSets/6,Env1,Env2),
 5738	assertTermExpansionClause(roleHierarchy/3,Env1,Env2),
 5739	assertTermExpansionClause(roleName/4,Env1,Env2),
 5740	assertTermExpansionClause(roleNr/5,Env1,Env2),
 5741	assertTermExpansionClause(roleRange/4,Env1,Env2),
 5742	assertTermExpansionClause(roleSubsets/6,Env1,Env2),
 5743	assertTermExpansionClause(sub/4,Env1,Env2),
 5744	assertTermExpansionClause(succ/4,Env1,Env2),
 5745	assertz_logged((term_expansion(succ(X1,Env1,X3,X4),
 5746				succ(X1,Env2,X3,X4)))),
 5747	assertz_logged((term_expansion(sub(X1,Env1,X3,X4),
 5748				sub(X1,Env2,X3,X4)))),
 5749	assertz_logged(term_expansion(X,X)),
 5750	!.
 5751term_expansion(copy,off,_Env1,_Env2) :-
 5752	abolish(term_expansion/2),
 5753	!.
 5754
 5755
 5756assertTermExpansionClause(Pred/Arity,Env1,Env2) :-
 5757	constructArguments(Env,Arity,[],[Env1|Arguments]),
 5758	Head1 =.. [Pred|[Env1|Arguments]],
 5759	Head2 =.. [Pred|[Env2|Arguments]],
 5760	assertz_logged((term_expansion(Head1,Head2))),
 5761	!.
 5762
 5763expandTerm(A,B) :-
 5764	expand_term(A,B),
 5765	!.
 5766
 5767copyall(Env1,_Env2,Pred,Args) :-
 5768	Head1 =.. [Pred,Env1|Args],
 5769	clause(Head1,Body1),
 5770	expandTerm((Head1,Body1),(Head2,Body2)),
 5771	assertz_logged((Head2 :- Body2)),
 5772	fail.
 5773copyall(_,_,_,_) :- !.
 5774
 5775copyAll(Env1,_Env2,Pred/Arity) :-
 5776	constructHead(Env1,Pred/Arity,Head1),
 5777	clause(Head1,Body1),
 5778	expandTerm((Head1,Body1),(Head2,Body2)),
 5779	assertz_logged((Head2 :- Body2)),
 5780	fail.
 5781copyAll(_,_,_) :- !.
 5782
 5783/**********************************************************************
 5784 *
 5785 * renameEnvironment(Name1,Name2)
 5786 * renames environment Name1 to environment Name2.
 5787 *
 5788 */
 5789
 5790renameEnvironment(Name1,Name2) :-
 5791	retract(environment(Name1,Env,C)),
 5792	asserta_logged(environment(Name2,Env,C)),
 5793	% to be implemented
 5794	!.
 5795
 5796/**********************************************************************
 5797 *
 5798 * @(#) fdwAbduce.pl 1.1@(#)
 5799 *
 5800 */
 5801
 5802aux_abduce(Env,World,[],change(Y,_),Ws) :-
 5803	bagof(W,Z^changingInfl(Env,World,Z,Y,W),Ws),
 5804	!.
 5805
 5806aux_abduce(_,_,[],_,[]).
 5807
 5808aux_abduce(Env,World,[change(X,Wx)|Hs],change(Y,_),[Wy|Ws]) :-
 5809	infl(Env,World,X,Y,Wxy),
 5810	not(given_change(Env,World,X,_)),
 5811	weightOf_change(Wx,Wxy,Wy),
 5812	aux_abduce(Env,World,Hs,change(Y,_),Ws).
 5813
 5814aux2_abduce(_,_,_,[]).
 5815
 5816aux2_abduce(EnvName,MS,change(X,Wx),[Change|Changes]) :-
 5817	nonvar(Change),
 5818	abduce(EnvName,MS,change(X,Wx),Change,[]),
 5819	abduce(EnvName,MS,change(X,Wx),Changes,[]).
 5820
 5821/***********************************************************************
 5822 *
 5823 * default_change(+-Change,+-WeightedChange)
 5824 *
 5825 *	Change is of the form 
 5826 *		increase(+-X), noChange(+-X), decrease(+-X).
 5827 *
 5828 *	WeightedChange is of the form
 5829 *		change(+-X,+-W)
 5830 *	with W being the default weight associated with Change.
 5831 */
 5832
 5833default_change(increase(X),change(X,W)) :-
 5834	nonvar(W),
 5835	W > 0.0.
 5836
 5837default_change(decrease(X),change(X,W)) :-
 5838	nonvar(W),
 5839	W < 0.0.
 5840
 5841default_change(noChange(X),change(X,W)) :-
 5842	nonvar(W),
 5843	W = 0.0.
 5844
 5845default_change(increase(X),change(X,1.0)).
 5846
 5847default_change(decrease(X),change(X,-1.0)).
 5848
 5849default_change(noChange(X),change(X,0.0)).
 5850
 5851/***********************************************************************
 5852 *
 5853 * default_changes(+-Changes,+-WeightedChanges)
 5854 *
 5855 *	Changes is a list of
 5856 *		increase(+-X), noChange(+-X), decrease(+-X)
 5857 *	predicates.
 5858 *
 5859 *	WeightedChanges is a list of
 5860 *		change(+-X,+-W)
 5861 *	predicates where W being the default weight associated 
 5862 *	with the appropriate Changes predicates.
 5863 */
 5864
 5865default_changes([],[]).
 5866
 5867default_changes([Change|Changes],[WeightedChange|WeightedChanges]) :-
 5868	default_change(Change,WeightedChange),
 5869	default_changes(Changes,WeightedChanges).
 5870
 5871
 5872
 5873
 5874
 5875
 5876
 5877
 5878
 5879
 5880/**********************************************************************
 5881 *
 5882 * @(#) fdwUserInterface.pl 1.1@(#)
 5883 *
 5884 */
 5885
 5886
 5887/***********************************************************************
 5888 *
 5889 * initFuncdep
 5890 *
 5891 *	asserts default given_ clauses which prevent errors if the user
 5892 *	has not (implicitly) defined any given_ clauses.
 5893 */
 5894
 5895initFuncdep :-
 5896	assertz_logged((given_inflLink(_,_,_,_) :- !, fail)),
 5897	assertz_logged((given_change(_,_,_,_) :- !, fail)).
 5898	
 5899
 5900/***********************************************************************
 5901 *
 5902 * initializeMotel, initialiseMotel
 5903 *
 5904 *	Similar to initializeMotel in
 5905 *	~hustadt/pop/motel/motel-0.0.6/userInterface.pl
 5906 */
 5907
 5908% For those of us who prefer the alternative spelling
 5909initialiseMotel :-
 5910	initializeMotel.
 5911
 5912/***********************************************************************
 5913 *
 5914 * listFDW
 5915 *
 5916 *	lists the internal representation of the information defined by 
 5917 *	the user.
 5918 */
 5919
 5920listFDW :-
 5921	listing(given_inflLink),
 5922	listing(given_change).
 5923
 5924/***********************************************************************
 5925 *
 5926 * showFDW
 5927 *
 5928 *	displays the user defined information in the knowledge base.
 5929 *	(Similar to showEnvironment.)
 5930 */
 5931
 5932showFDW :-
 5933        getCurrentEnvironment(EnvName),
 5934	environment(EnvName,Env,Comment),
 5935        print('Functional Dependencies: '),
 5936        print(EnvName),
 5937        print(' ('),
 5938        print(Comment),
 5939        print(')'),
 5940        nl,
 5941	showFDW(Env).
 5942
 5943showFDW(Env) :-
 5944	showInfl(Env),
 5945	showChange(Env).
 5946
 5947showInfl(Env) :-
 5948	given_inflLink(Env,World,app(_,W,X),Y),
 5949	print(World),
 5950	print('infl('),
 5951	print(X), print(','), print(Y), print(','), print(W), print(').'),
 5952	nl,
 5953	fail.
 5954
 5955showInfl(_).
 5956
 5957showChange(Env) :-
 5958	given_change(Env,World,X,W),
 5959	print(World),
 5960	print('change('),
 5961	print(X), print(','), print(W), print(').'),
 5962	nl,
 5963	fail.
 5964
 5965showChange(_).
 5966
 5967showFD :-
 5968        getCurrentEnvironment(EnvName),
 5969	environment(EnvName,Env,Comment),
 5970        print('Functional Dependencies: '),
 5971        print(EnvName),
 5972        print(' ('),
 5973        print(Comment),
 5974        print(')'),
 5975        nl,
 5976	showFD(Env).
 5977
 5978showFD(Env) :-
 5979	showPosInfl(Env),
 5980	showNegInfl(Env),
 5981	showNoInfl(Env),
 5982	showIncrease(Env),
 5983	showDecrease(Env),
 5984	showNoChange(Env).
 5985
 5986showPosInfl(Env) :-
 5987	given_inflLink(Env,World,app(_,W,X),Y),
 5988	W > 0.0,
 5989	print(World),
 5990	print('posInfl('),
 5991	print(X), print(','), print(Y), print(').'),
 5992	nl,
 5993	fail.
 5994
 5995showPosInfl(_).
 5996
 5997showNegInfl(Env) :-
 5998	given_inflLink(Env,World,app(_,W,X),Y),
 5999	W < 0.0,
 6000	print(World),
 6001	print('negInfl('),
 6002	print(X), print(','), print(Y), print(').'),
 6003	nl,
 6004	fail.
 6005
 6006showNegInfl(_).
 6007
 6008showNoInfl(Env) :-
 6009	given_inflLink(Env,World,app(_,0.0,X),Y),
 6010	print(World),
 6011	print('noInfl('),
 6012	print(X), print(','), print(Y), print(').'),
 6013	nl,
 6014	fail.
 6015
 6016showNoInfl(_).
 6017
 6018showIncrease(Env) :-
 6019	given_change(Env,World,X,W),
 6020	W > 0.0,
 6021	print(World),
 6022	print('increase('),
 6023	print(X), print(').'),
 6024	nl,
 6025	fail.
 6026
 6027showIncrease(_).
 6028
 6029showDecrease(Env) :-
 6030	given_change(Env,World,X,W),
 6031	W < 0.0,
 6032	print(World),
 6033	print('decrease('),
 6034	print(X), print(').'),
 6035	nl,
 6036	fail.
 6037
 6038showDecrease(_).
 6039
 6040showNoChange(Env) :-
 6041	given_change(Env,World,X,0.0),
 6042	print(World),
 6043	print('noChange('),
 6044	print(X), print(').'),
 6045	nl,
 6046	fail.
 6047
 6048showNoChange(_).
 6049
 6050/***********************************************************************
 6051 *
 6052 * def(+EnvName,+MS,:+Fact)
 6053 *
 6054 *	Fact is one of 
 6055 *		infl(+X,+Y,+W),
 6056 *		posInfl(+X,+Y), negInfl(+X,+Y), noInfl(+X,+Y),
 6057 *		change(+X,+W),
 6058 *		increase(+X), decrease(+X).
 6059 *
 6060 *	This predicate is used to update the knowledge base of
 6061 *	information about the functional dependencies. The definition
 6062 *	of multiple influences between attributes and multiple changes 
 6063 *	on an attribute are prevented. 
 6064 *
 6065 *	Note that, X and Y denote roles/attributes (of cars) and W
 6066 *	denotes the weight of X influencing Y or W denotes the weight
 6067 *	of change of an attribute. 
 6068 *
 6069 *	posInfl is assigned the weight 1.0, negInfl the weight -1.0 and 
 6070 *	noInfl the weight 0.0. The weights for increase, decrease and
 6071 *	noChange are 1.0, -1.0 and 0.0, respectively.
 6072 */
 6073
 6074def(EnvName,MS,infl(X,Y,W)) :-
 6075	get_Env_World(EnvName,MS,Env,World),
 6076	atomic(X),
 6077	assertNames(Env,World,X,role),
 6078	atomic(Y),
 6079	assertNames(Env,World,Y,role),
 6080	wellDefined_InflWeight(W),
 6081	not(given_inflLink(Env,World,app(_,_,X),Y)),
 6082	gensym(sk,F),
 6083	asserta_logged(given_inflLink(Env,World,app(F,W,X),Y)).
 6084
 6085def(EnvName,MS,change(X,W)) :-
 6086	get_Env_World(EnvName,MS,Env,World),
 6087	atomic(X),
 6088	assertNames(Env,World,X,role),
 6089	wellDefined_ChangeWeight(W),
 6090	not(given_change(Env,World,X,_)),
 6091	asserta_logged(given_change(Env,World,X,W)).
 6092
 6093def(EnvName,MS,posInfl(X,Y)) :-
 6094	def(EnvName,MS,infl(X,Y,1.0)).
 6095
 6096def(EnvName,MS,negInfl(X,Y)) :-
 6097	def(EnvName,MS,infl(X,Y,-1.0)).
 6098
 6099def(EnvName,MS,noInfl(X,Y)) :-
 6100	def(EnvName,MS,infl(X,Y,0.0)).
 6101
 6102def(EnvName,MS,increase(X)) :-
 6103	def(EnvName,MS,change(X,1.0)).
 6104
 6105def(EnvName,MS,decrease(X)) :-
 6106	def(EnvName,MS,change(X,-1.0)).
 6107
 6108def(EnvName,MS,noChange(X)) :-
 6109	def(EnvName,MS,change(X,0.0)).
 6110
 6111/***********************************************************************
 6112 *
 6113 * def(:+Fact)
 6114 *
 6115 *	calls def(+EnvName,+MS,:+Fact) with default environment EnvName 
 6116 *	and empty modal sequence.
 6117 */
 6118
 6119def(Fact) :-
 6120        getCurrentEnvironment(EnvName),
 6121	def(EnvName,[],Fact).
 6122
 6123/***********************************************************************
 6124 *
 6125 * def(+EnvName,:+Fact)
 6126 *
 6127 *	calls def(+EnvName,+MS,:+Fact) with default environment EnvName 
 6128 *	and empty modal sequence.
 6129 */
 6130
 6131def(EnvName,Fact) :-
 6132        environment(EnvName,_,_),
 6133	def(EnvName,[],Fact).
 6134
 6135/***********************************************************************
 6136 *
 6137 * def(+MS,:+Fact)
 6138 *
 6139 *	calls def(+EnvName,+MS,:+Fact) with default environment EnvName 
 6140 *	and empty modal sequence.
 6141 */
 6142
 6143def(MS,Fact) :-
 6144        nonvar(MS),
 6145        (MS = [] ; MS = [_|_]),
 6146        !,
 6147        getCurrentEnvironment(EnvName),
 6148	def(EnvName,[],Fact).
 6149
 6150/***********************************************************************
 6151 *
 6152 * undef(+EnvName,+MS,:+-Fact)
 6153 *
 6154 *	retracts all facts matching Fact.
 6155 */
 6156
 6157undef(EnvName,MS,infl(X,Y,W)) :-
 6158        environment(EnvName,Env,_),
 6159	retract(given_inflLink(Env,MS,app(_,W,X),Y)),
 6160	fail.
 6161undef(EnvName,MS,change(X,W)) :-
 6162        environment(EnvName,Env,_),
 6163	retract(given_change(EnvName,MS,X,W)),
 6164	fail.
 6165undef(EnvName,MS,posInfl(X,Y)) :-
 6166        environment(EnvName,Env,_),
 6167	retract(given_inflLink(Env,MS,app(_,1.0,X),Y)),
 6168	fail.
 6169undef(EnvName,MS,negInfl(X,Y)) :-
 6170        environment(EnvName,Env,_),
 6171	retract(given_inflLink(Env,MS,app(_,-1.0,X),Y)),
 6172	fail.
 6173undef(EnvName,MS,noInfl(X,Y)) :-
 6174        environment(EnvName,Env,_),
 6175	retract(given_inflLink(Env,MS,app(_,0.0,X),Y)),
 6176	fail.
 6177undef(EnvName,MS,increase(X)) :-
 6178        environment(EnvName,Env,_),
 6179	retract(given_change(Env,MS,X,1.0)),
 6180	fail.
 6181undef(EnvName,MS,decrease(X)) :-
 6182        environment(EnvName,Env,_),
 6183	retract(given_change(Env,MS,X,-1.0)),
 6184	fail.
 6185undef(EnvName,MS,noChange(X)) :-
 6186        environment(EnvName,Env,_),
 6187	retract(given_change(Env,MS,X,0.0)),
 6188	fail.
 6189undef(_,_,_).
 6190
 6191/***********************************************************************
 6192 *
 6193 * undef(:+-Fact)
 6194 *
 6195 *	retracts all facts matching Fact in default environment and
 6196 *	default modal context.
 6197 */
 6198
 6199undef(Fact) :-
 6200        getCurrentEnvironment(EnvName),
 6201	undef(EnvName,[],Fact).
 6202
 6203/***********************************************************************
 6204 *
 6205 * undef(+EnvName,:+-Fact)
 6206 *
 6207 *	retracts all facts matching Fact in default environment and
 6208 *	default modal context.
 6209 */
 6210
 6211undef(EnvName,Fact) :-
 6212        environment(EnvName,_,_),
 6213	!,
 6214	undef(EnvName,[],Fact).
 6215
 6216/***********************************************************************
 6217 *
 6218 * undef(+MS,:+-Fact)
 6219 *
 6220 *	retracts all facts matching Fact in default environment and
 6221 *	default modal context.
 6222 */
 6223
 6224undef(MS,Fact) :-
 6225        (MS = [] ; MS = [_|_]),
 6226        !,
 6227        getCurrentEnvironment(EnvName),
 6228	undef(EnvName,[],Fact).
 6229
 6230/***********************************************************************
 6231 *
 6232 * get_Env_World(+EnvName,+MS,-Env,-World),
 6233 *
 6234 */
 6235
 6236get_Env_World(EnvName,MS,Env,World) :-
 6237	environment(EnvName,Env,_),
 6238        convertMS(Env,[[],true],MS,[],[World,G1],_),
 6239        call(G1).
 6240
 6241/**********************************************************************
 6242 *
 6243 * %A%
 6244 *
 6245 */
 6246%------------------------------------------------------------------------------
 6247% Project:      MOTEL 1.0
 6248% Module:       folToClause.pl
 6249% Purpose:      Translation of first-order logic formulae to clauses
 6250% Last Change:  04.02.93
 6251% Language:     Prolog
 6252% Author:       Ullrich Hustadt
 6253% Address:      Max-Planck-Institut for Computer Science
 6254%               Im Stadtwald
 6255%               6600 Saarbr"ucken
 6256%               Germany
 6257% Email:        Ullrich.Hustadt@mpi-sb.mpg.de
 6258% Copyright:    (C) 1993 Ullrich Hustadt
 6259% Copying:      This software is provided under the GNU General Public License.
 6260% Warranty:     This is a research prototype. There is absolutely no warranty.
 6261%------------------------------------------------------------------------------
 6262%
 6263% Syntax of first-order logic formulae
 6264% ====================================
 6265% Atom    => in(X,ConceptName)
 6266%            eq(PrologTerm,PrologTerm)
 6267%            rel(RoleName,PrologTerm,PrologTerm)
 6268%         
 6269% Formula =>   Atom
 6270%            | and([Formula,Formula]) 
 6271%            | or([Formula,Formula])
 6272%            | not(Formula)
 6273%            | implies(Formula,Formula)
 6274%            | equivalent(Formula,Formula)
 6275%            | forall(PrologVar,Formula)
 6276%            | exists(PrologVar,Formula)
 6277% 
 6278% Syntax of clauses
 6279% =================
 6280% Clause  => cl(Head,Body)
 6281% Head    =>   []
 6282%            | [Atom|Head]
 6283% Body    =>   []
 6284%            | [Atom|Body]
 6285    
 6286%----------------------------------------------------------------------
 6287% translate(+F,-C)
 6288% Parameter: F   First-order formula
 6289%            C   Clause
 6290
 6291translate(X,Clauses) :-
 6292	implout(X,X1),
 6293	negin(X1,X2),
 6294	mskolem(X2,X3,[]),
 6295	univout(X3,X4),
 6296	conjn(X4,X5),
 6297	clausify(X5,Clauses,[]).
 6298
 6299
 6300%----------------------------------------------------------------------
 6301% implout(+F1,-F2)
 6302% Arguments: F1   First-order formula
 6303%            F2   First-order formula
 6304% removes implications and equivalences in F1 resulting in F2
 6305%
 6306% Author: Ullrich Hustadt
 6307
 6308implout(equivalent(P,Q),or([and([P1,Q1]),and([not(P1),not(Q1)])])) :-
 6309	!,
 6310	implout(P,P1),
 6311	implout(Q,Q1).
 6312implout(implies(P,Q),or([not(P1),Q1])) :-
 6313	!,
 6314	implout(P,P1),
 6315	implout(Q,Q1).
 6316implout(forall(X,P),forall(X,P1)) :-
 6317	!,
 6318	implout(P,P1).
 6319implout(exists(X,P),exists(X,P1)) :-
 6320	!,
 6321	implout(P,P1).
 6322implout(and(L),and(L1)) :-
 6323	!,
 6324	map(implout,L,L1).
 6325implout(or(L),or(L1)) :-
 6326	!,
 6327	map(implout,L,L1).
 6328implout(not(P),not(P1)) :-
 6329	!,
 6330	implout(P,P1).
 6331implout(P,P).
 6332
 6333%----------------------------------------------------------------------
 6334% negin(+F1,-F2)
 6335% Parameter: F1   First-order formula
 6336%            F2   First-order formula
 6337% computes the negation normal form of F1 
 6338%
 6339% Author: Ullrich Hustadt
 6340
 6341negin(not(P),P1) :-
 6342	!,
 6343	neg(P,P1).
 6344negin(forall(X,P),forall(X,P1)) :-
 6345	!,
 6346	negin(P,P1).
 6347negin(exists(X,P),exists(X,P1)) :-
 6348	!,
 6349	negin(P,P1).
 6350negin(and(L),and(L1)) :-
 6351	!,
 6352	map(negin,L,L1).
 6353negin(or(L),or(L1)) :-
 6354	!,
 6355	map(negin,L,L1).
 6356negin(P,P).
 6357
 6358%----------------------------------------------------------------------
 6359% neg(+F1,-F2)
 6360% Parameter: F1   First-order formula
 6361%            F2   First-order formula
 6362% negates formula F1 to get F2
 6363%
 6364% Author: Ullrich Hustadt
 6365
 6366neg(not(P),P1) :-
 6367	!,
 6368	negin(P,P1).
 6369neg(forall(X,P),exists(X,P1)) :-
 6370	!,
 6371	neg(P,P1).
 6372neg(exists(X,P),forall(X,P1)) :-
 6373	!,
 6374	neg(P,P1).
 6375neg(and(L),or(L1)) :-
 6376	!,
 6377	map(neg,L,L1).
 6378neg(or(L),and(L1)) :-
 6379	!,
 6380	map(neg,L,L1).
 6381neg(P,not(P)).
 6382
 6383%----------------------------------------------------------------------
 6384% mskolem(+F1,-F2,*Vars)
 6385% Parameter: F1     First-order formula
 6386%            F2     First-order formula
 6387%            Vars   List of variables
 6388% F2 is the skolemized form of F1.
 6389%
 6390% Author: Ullrich Hustadt
 6391
 6392mskolem(forall(X,P),forall(X,P1),Vars) :-
 6393	!,
 6394	mskolem(P,P1,[X|Vars]).
 6395mskolem(exists(X,P),P2,Vars) :-
 6396	!,
 6397	mskolem(P,P1,Vars),
 6398	gensym(f,F),
 6399	Sk =.. [F|Vars],
 6400	motel_subst(P1,P2,X,Sk).
 6401mskolem(and(L),and(L1),Vars) :-
 6402	!,
 6403	map(mskolem,[Vars],L,L1).
 6404mskolem(or(L),or(L1),Vars) :-
 6405	!,
 6406	map(mskolem,[Vars],L,L1).
 6407mskolem(P,P,_).
 6408
 6409
 6410%----------------------------------------------------------------------
 6411% motel_subst(+F1,-F2,+X,+Sk)
 6412% Parameter: F1     First-order formula
 6413%            F2     First-order formula
 6414%            X      Variable that will be substituted
 6415%            Sk     Skolem term 
 6416% substitutes Sk for X in formula F1.
 6417% 
 6418% Author: Ullrich Hustadt
 6419
 6420motel_subst(T1,T2,X,Sk) :-
 6421	(atomic(T1) ; var(T1)),
 6422	T1 == X,
 6423	!,
 6424	T2 = Sk.
 6425motel_subst(T1,T2,X,_Sk) :-
 6426	(atomic(T1) ; var(T1)),
 6427	not(T1 == X),
 6428	!,
 6429	T2 = T1.
 6430motel_subst(forall(Y,P),forall(Y,P),X,_Sk) :-
 6431	X == Y,
 6432	!.
 6433motel_subst(forall(Y,P),forall(Y,P1),X,Sk) :-
 6434	!,
 6435	motel_subst(P,P1,X,Sk).
 6436motel_subst(exists(Y,P),exists(Y,P),X,_Sk) :-
 6437	X == Y,
 6438	!.
 6439motel_subst(exists(Y,P),exists(Y,P1),X,Sk) :-
 6440	!,
 6441	motel_subst(P,P1,X,Sk).
 6442motel_subst(and(L),and(L1),X,Sk) :-
 6443	!,
 6444	map(motel_subst,[X,Sk],L,L1).
 6445motel_subst(or(L),or(L1),X,Sk) :-
 6446	!,
 6447	map(motel_subst,[X,Sk],L,L1).
 6448motel_subst(not(P),not(P1),X,Sk) :-
 6449	!,
 6450	motel_subst(P,P1,X,Sk).
 6451motel_subst(T1,T2,X,Sk) :-
 6452	!,
 6453	T1 =.. [F|Args],
 6454	map(motel_subst,[X,Sk],Args,Args1),
 6455	T2 =.. [F|Args1].
 6456
 6457%----------------------------------------------------------------------
 6458% univout(+F1,-F1)
 6459% Parameter: F1   First-order formula
 6460%            F2   First-order formula
 6461% removes quantifiers
 6462%
 6463% Author: Ullrich Hustadt
 6464
 6465univout(forall(_X,P),P1) :-
 6466	!,
 6467	univout(P,P1).
 6468univout(and(L),and(L1)) :-
 6469	!,
 6470	map(univout,L,L1).
 6471univout(or(L),or(L1)) :-
 6472	!,
 6473	map(univout,L,L1).
 6474univout(P,P).
 6475
 6476%----------------------------------------------------------------------
 6477% conjn(+F1,-F2)
 6478% Parameter: F1   First-order formula
 6479%            F2   First-order formula
 6480% computes the conjunctive normal form of F1
 6481%
 6482% Author: Ullrich Hustadt
 6483
 6484conjn(and(L),R) :-
 6485	!,
 6486	map(conjn,L,L1),
 6487	conjn1(and(L1),R).
 6488conjn(or(L),R) :-
 6489	!,
 6490	map(conjn,L,L1),
 6491	conjn1(or(L1),R).
 6492conjn(P,P).
 6493
 6494conjn1(or([and([P,Q]),R]),and([P1,Q1])) :-
 6495	!,
 6496	conjn(or([P,R]),P1),
 6497	conjn(or([Q,R]),Q1).
 6498conjn1(or([R,and([P,Q])]),and([P1,Q1])) :-
 6499	!,
 6500	conjn(or([P,R]),P1),
 6501	conjn(or([Q,R]),Q1).
 6502conjn1(P,P).
 6503
 6504clausify(and([P,Q]),C1,C2) :-
 6505	!,
 6506	clausify(P,C1,C3),
 6507	clausify(Q,C3,C2).
 6508clausify(P,[cl(A,B)|Cs],Cs) :-
 6509	inclause(P,A,[],B,[]),
 6510	!.
 6511clausify(_,C,C).
 6512
 6513inclause(or([P,Q]),A,A1,B,B1) :-
 6514	!,
 6515	inclause(P,A2,A1,B2,B1),
 6516	inclause(Q,A,A2,B,B2).
 6517inclause(not(P),A,A,B1,B) :-
 6518	!,
 6519	not(memq(P,A)),
 6520	motel_putin(P,B,B1).
 6521inclause(P,A1,A,B,B) :-
 6522	not(memq(P,B)),
 6523	motel_putin(P,A,A1).
 6524
 6525motel_putin(X,[],[X]) :-
 6526	!.
 6527motel_putin(X,[Y|L],L) :-
 6528	X == Y,
 6529	!.
 6530motel_putin(X,[Y|L],[Y|L1]) :-
 6531	motel_putin(X,L,L1).
 6532
 6533
 6534	
 6535memq(_X,[]) :-
 6536	!,
 6537	fail.
 6538memq(X,[Y|_L]) :-
 6539	X == Y,
 6540	!.
 6541memq(X,[_|L]) :-
 6542	memq(X,L).
 6543	
 6544	
 6545
 6546
 6547/**********************************************************************
 6548 *
 6549 * @(#) hop.pl 1.4@(#)
 6550 *
 6551 */
 6552
 6553/***********************************************************************
 6554 * 
 6555 * hop_map(+Pred,+InList,-OutList)
 6556 * calls Pred(X,Y) for all X in InList and collects all Y to get
 6557 * OutList.
 6558 *
 6559 */
 6560
 6561hop_map(_Pred,[],[]) :- !.
 6562hop_map(Pred,[H1|T1],[H2|T2]) :-
 6563	Clause =.. [Pred,H1,H2],
 6564	call(Clause),
 6565	hop_map(Pred,T1,T2).
 6566
 6567/***********************************************************************
 6568 * 
 6569 * hop_map(+Pred,+[A1,...,An],+InList,-OutList)
 6570 * calls Pred(A1,...,An,X,Y) for all X in InList and collects all Y to 
 6571 * get OutList.
 6572 *
 6573 */
 6574
 6575hop_map(_Pred,_Args,[],[]) :- !.
 6576hop_map(Pred,Args,[H1|T1],[H2|T2]) :-
 6577	Clause =.. [Pred|[H1,H2|Args]],
 6578	call(Clause),
 6579	hop_map(Pred,Args,T1,T2).
 6580
 6581mapGoal(_Goal,_X,[]) :-
 6582	!.
 6583mapGoal(Goal,X,[(Y,_PTY)|L1]) :-
 6584	not(not((X = Y, call(Goal)))),
 6585	mapGoal(Goal,X,L1).
 6586	
 6587/**********************************************************************
 6588 *
 6589 * try(+G)
 6590 * calls G and succeeds even if G fails.
 6591 *
 6592 */
 6593
 6594try(G)  :- call(G).
 6595try(_G) :- true.
 6596
 6597
 6598/**********************************************************************
 6599 *
 6600 * doboth(G1,G2)
 6601 * calls G1 and if G1 succeeds G2 is called.
 6602 *
 6603 */
 6604
 6605doboth(G1,G2) :-
 6606	call(G1),
 6607	call(G2).
 6608
 6609/**********************************************************************
 6610 *
 6611 * tell(GoalList)
 6612 * calls all the goals given in argument GoalList which is either a
 6613 * list of PROLOG goals or a single PROLOG goal.
 6614 *
 6615 */
 6616
 6617callList([]) :-
 6618	!.
 6619callList([G1|GL]) :-
 6620	!,
 6621	call(G1),
 6622	callList(GL).
 6623callList(G1) :-
 6624	call(G1).
 6625
 6626
 6627/**********************************************************************
 6628 *
 6629 * toList(+ListTerm,-List)
 6630 * converts a LISP-like list ListTerm into a PROLOG-like list List
 6631 *
 6632 */
 6633
 6634tolist(nil,[]) :- !.
 6635tolist(cons(A,L1),[A|L2]) :-
 6636	tolist(L1,L2).
 6637
 6638/**********************************************************************
 6639 *
 6640 * setofOrNil(A,B,C)
 6641 * succeeds with the same result as setof(A,B,C) if setof(A,B,C) 
 6642 * succeeds. Otherwise C will be instantiated with the empty list.
 6643 *
 6644 */
 6645
 6646setofOrNil(A,B,C) :-
 6647	setof(A,B,C),
 6648	!.
 6649setofOrNil(A,B,[]) :-
 6650	!.
 6651
 6652/**********************************************************************
 6653 *
 6654 * bagofOrNil(A,B,C)
 6655 * succeeds with the same result as bagof(A,B,C) if bagof(A,B,C) 
 6656 * succeeds. Otherwise C will be instantiated with the empty list.
 6657 *
 6658 */
 6659
 6660bagofOrNil(A,B,C) :-
 6661	setof(A,B,C),
 6662	!.
 6663bagofOrNil(A,B,[]) :-
 6664	!.
 6665
 6666/**********************************************************************
 6667 *
 6668 * @(#) infl.pl 1.4@(#)
 6669 *
 6670 */
 6671
 6672/***********************************************************************
 6673 *
 6674 * getInflDescription(+Env,+World,-DescriptY,+-Y)
 6675 *
 6676 *	Given a role Y this predicate returns its description in terms
 6677 *	of the least attribute on which Y depends as a possible chain 
 6678 *	of influence relationships. DescriptY is an attribute or it 
 6679 *	is of the form app(sk_,W,Z) where W denotes a weight.
 6680 *
 6681 *	This predicate is now superfluous. Its replacement is the one
 6682 *	with three arguments.
 6683 */
 6684
 6685getInflDescription(Env,World,app(F,W,DescriptZ),Y) :-
 6686	given_inflLink(Env,World,app(F,W,Z),Y),
 6687	getInflDescription(Env,World,DescriptZ,Z).
 6688
 6689getInflDescription(Env,World,X,X) :-
 6690	atom(X),
 6691	!,
 6692	not(given_inflLink(Env,World,_,X)).
 6693
 6694getInflDescription(Env,World,X,X) :-
 6695	var(X),
 6696	!.
 6697
 6698/***********************************************************************
 6699 *
 6700 * getInflDescription(+Env,+World,-DescriptY,+-X,+-Y)
 6701 *
 6702 *	Given a role Y this predicate returns its description in terms
 6703 *	of the attribute X on which Y depends as a chain of influence 
 6704 *	relationships. DescriptY is an attribute or it is of the form 
 6705 *	app(sk_,W,Z) where W denotes a weight.
 6706 */
 6707
 6708getInflDescription(Env,World,app(F,W,DescriptZ),X,Y) :-
 6709	given_inflLink(Env,World,app(F,W,Z),Y),
 6710	getInflDescription(Env,World,DescriptZ,X,Z).
 6711
 6712getInflDescription(Env,World,X,X,X) :-
 6713	!.
 6714
 6715/***********************************************************************
 6716 *
 6717 * test_inflLink(+-X,+-Y,+-W)
 6718 *
 6719 *	X is either an attribute/role or it is of the form
 6720 *	app(sk_,_,_).
 6721 *
 6722 *	Y is of the form app(+-F,1.0,+Z) or app(+-F,-1.0,+Z).
 6723 *
 6724 *	This predicate tests for influence of weight W between X and Y.
 6725 */
 6726
 6727test_inflLink(X,Y,W) :-
 6728	nonvar(X),
 6729	Y = app(F,W,X),
 6730	!,
 6731	nonvar(F).
 6732
 6733test_inflLink(X,Y,W) :-
 6734	var(X),
 6735	nonvar(Y),
 6736	Y = app(F,W,X),
 6737	atom(X),
 6738	!.
 6739
 6740test_inflLink(X,Y,W) :-
 6741	var(X),
 6742	var(Y),
 6743	!,
 6744	fail.
 6745
 6746test_inflLink(X,app(F,W1,Y),W) :-
 6747	test_inflLink(X,Y,W2),
 6748	weightOf_ChainedInfl(W1,W2,W).
 6749
 6750/***********************************************************************
 6751 *
 6752 * inflLink(+Env,+World,+-X,+-Y,+-W)
 6753 *
 6754 *	tests if attribute X influences attribute Y with weight W.
 6755 *
 6756 *	Note: the output for uninstantiated X and/or Y depends on the
 6757 *	order of the literals in the body of infl.
 6758 */
 6759
 6760inflLink(Env,World,X,Y,W) :-
 6761	wellDefined_attribute(Env,World,X),
 6762	wellDefined_attribute(Env,World,Y),
 6763	(var(W);
 6764	wellDefined_InflWeight(W)),
 6765	getInflDescription(Env,World,Z2,X,Y),
 6766	test_inflLink(X,Z2,W).
 6767
 6768inflLink(Env,World,X,Y,W) :-
 6769	var(X),
 6770	wellDefined_attribute(Env,World,Y),
 6771	(var(W);
 6772	wellDefined_InflWeight(W)),
 6773	getInflDescription(Env,World,Z2,X,Y),
 6774	test_inflLink(X,Z2,W).
 6775
 6776inflLink(Env,World,X,Y,W) :-
 6777	wellDefined_attribute(Env,World,X),
 6778	var(Y),
 6779	(var(W);
 6780	wellDefined_InflWeight(W)),
 6781	getInflDescription(Env,World,Z2,X,Y),
 6782	test_inflLink(X,Z2,W).
 6783
 6784inflLink(Env,World,X,Y,W) :-
 6785	var(X),
 6786	var(Y),
 6787	(var(W);
 6788	wellDefined_InflWeight(W)),
 6789	getInflDescription(Env,World,Z2,X,Y),
 6790	test_inflLink(X,Z2,W).
 6791
 6792/***********************************************************************
 6793 *
 6794 * leastInfl(+Env,+World,+-X,+-Y)
 6795 *
 6796 *	succeeds if X is a least attribute influencing Y.
 6797 */
 6798
 6799leastInfl(Env,World,X,Y) :-
 6800	getInflDescription(Env,World,_,X,Y),
 6801	not(X = Y),
 6802	not(given_inflLink(Env,World,_,X)).
 6803
 6804/***********************************************************************
 6805 *
 6806 * leastInfls(+Env,+World,+-Xs,+Y)
 6807 *
 6808 *	collects the least attributes influencing Y in Xs.
 6809 */
 6810
 6811leastInfls(Env,World,Xs,Y) :-
 6812	setof(X,leastInfl(Env,World,X,Y),Xs).
 6813
 6814/***********************************************************************
 6815 *
 6816 * greatestInfl(+Env,+World,+-X,+-Y)
 6817 *
 6818 *	succeeds if Y is a greatest attribute influenced by X.
 6819 */
 6820
 6821greatestInfl(Env,World,X,Y) :-
 6822	getInflDescription(Env,World,_,X,Y),
 6823	not(X = Y),
 6824	not(given_inflLink(Env,World,app(_,_,Y),_)).
 6825
 6826/***********************************************************************
 6827 *
 6828 * greatestInfls(+Env,+World,+X,+-Ys)
 6829 *
 6830 *	collects the greatest attributes influenced by X in Ys.
 6831 */
 6832
 6833greatestInfls(Env,World,X,Ys) :-
 6834	setof(Y,greatestInfl(Env,World,X,Y),Ys).
 6835
 6836/***********************************************************************
 6837 *
 6838 * infl(+Env,+World,+-X,+-Y,+-W)
 6839 *
 6840 *	computes the cumulative weight W of all the influence links 
 6841 *	between the attributes X and Y.
 6842 */
 6843
 6844infl(Env,World,X,Y,W) :-
 6845	bagof(Weight,inflLink(Env,World,X,Y,Weight),Weights),
 6846	weightOf_TotalInfl(Weights,W).
 6847
 6848/***********************************************************************
 6849 *
 6850 * maxPosInfl(+Env,+World,+-X,+-Y,+-Wmax)
 6851 *
 6852 *	succeeds if Wmax is the greatest weight with which X influences 
 6853 *	Y positively.
 6854 */
 6855
 6856maxPosInfl(Env,World,X,Y,WMax) :-
 6857	var(X),
 6858	bagof(W,Z^posInfl(Env,World,Z,Y,W),Ws),
 6859	motel_max(Ws,WMax,wellDefined_InflWeight),
 6860	posInfl(Env,World,X,Y,WMax).
 6861
 6862maxPosInfl(Env,World,X,Y,WMax) :-
 6863	var(Y),
 6864	bagof(W,Z^posInfl(Env,World,X,Z,W),Ws),
 6865	motel_max(Ws,WMax,wellDefined_InflWeight),
 6866	posInfl(Env,World,X,Y,WMax).
 6867
 6868maxPosInfl(Env,World,X,Y,WMax) :-
 6869	nonvar(X),
 6870	nonvar(Y),
 6871	posInfl(Env,World,X,Y,WMax).
 6872
 6873/***********************************************************************
 6874 *
 6875 * maxNegInfl(+Env,+World,+-X,+-Y,+-WMin)
 6876 *
 6877 *	succeeds if WMin is the greatest weight with which X influences 
 6878 *	Y negatively.
 6879 */
 6880
 6881maxNegInfl(Env,World,X,Y,WMin) :-
 6882	var(X),
 6883	bagof(W,Z^negInfl(Env,World,Z,Y,W),Ws),
 6884	motel_min(Ws,WMin,wellDefined_InflWeight),
 6885	negInfl(Env,World,X,Y,WMin).
 6886
 6887maxNegInfl(Env,World,X,Y,WMin) :-
 6888	var(Y),
 6889	bagof(W,Z^negInfl(Env,World,X,Z,W),Ws),
 6890	motel_min(Ws,WMin,wellDefined_InflWeight),
 6891	negInfl(Env,World,X,Y,WMin).
 6892
 6893maxNegInfl(Env,World,X,Y,WMin) :-
 6894	nonvar(X),
 6895	nonvar(Y),
 6896	negInfl(Env,World,X,Y,WMin).
 6897
 6898/***********************************************************************
 6899 *
 6900 * posInfl(+Env,+World,+-X,+-Y)
 6901 *
 6902 *	succeeds if attribute X influences attribute Y positively.
 6903 */
 6904
 6905posInfl(Env,World,X,Y) :-
 6906	infl(Env,World,X,Y,W),
 6907	W > 0.0.
 6908
 6909/***********************************************************************
 6910 *
 6911 * posInfl(+Env,+World,+-X,+-Y,+-W)
 6912 *
 6913 *	succeeds if attribute X influences attribute Y positively with
 6914 *	weight W.
 6915 */
 6916
 6917posInfl(Env,World,X,Y,W) :-
 6918	infl(Env,World,X,Y,W),
 6919	W > 0.0.
 6920
 6921/***********************************************************************
 6922 *
 6923 * negInfl(+Env,+World,+-X,+-Y)
 6924 *
 6925 *	succeeds if attribute X influences attribute Y negatively.
 6926 */
 6927
 6928negInfl(Env,World,X,Y) :-
 6929	infl(Env,World,X,Y,W),
 6930	W < 0.0.
 6931
 6932/***********************************************************************
 6933 *
 6934 * negInfl(+Env,+World,+-X,+-Y,+-W)
 6935 *
 6936 *	succeeds if attribute X influences attribute Y negatively with
 6937 *	weight W.
 6938 */
 6939
 6940negInfl(Env,World,X,Y,W) :-
 6941	infl(Env,World,X,Y,W),
 6942	W < 0.0.
 6943
 6944/***********************************************************************
 6945 *
 6946 * noInfl(+Env,+World,+-X,+-Y)
 6947 *
 6948 *	succeeds if the cumulative influence between the attributes X and
 6949 *	Y is 0.0.
 6950 */
 6951
 6952noInfl(Env,World,X,Y) :-
 6953	infl(Env,World,X,Y,0.0).
 6954
 6955/***********************************************************************
 6956 *
 6957 * simultInfl(+Env,+World,+-Xs,+-Y,+-W)
 6958 *
 6959 *	checks if the list Xs is well-defined (that is, is Xs a SET of
 6960 *	independent attributes) and computes the total weight W of the 
 6961 *	attributes in the list Xs simultaneously influencing attribute Y.
 6962 */
 6963
 6964simultInfl(Env,World,Xs,Y,W) :-
 6965	nonvar(Xs),
 6966	wellDefined_setOfAttributes(Env,World,Xs),
 6967	aux_simultInfl(Env,World,Xs,Y,Ws),
 6968	weightOf_SimultInfl(Ws,W).
 6969
 6970simultInfl(Env,World,Xs,Y,W) :-
 6971	var(Xs),
 6972	!,
 6973	leastInfl(Env,World,Xs,Y),
 6974	aux_simultInfl(Env,World,Xs,Y,Ws),
 6975	wellDefined_setOfAttributes(Env,World,Xs),
 6976	weightOf_SimultInfl(Ws,W).
 6977
 6978aux_simultInfl(_,_,[],_,[]).
 6979
 6980aux_simultInfl(Env,World,[X|Xs],Y,[W|Ws]) :-
 6981	infl(Env,World,X,Y,W),
 6982	aux_simultInfl(Env,World,Xs,Y,Ws).
 6983
 6984aux_simultInfl(Env,World,[X|Xs],Y,Ws) :-
 6985	not(getInflDescription(Env,World,_,X,Y)),
 6986	aux_simultInfl(Env,World,Xs,Y,Ws).
 6987
 6988/***********************************************************************
 6989 *
 6990 * simultPosInfl(+Env,+World,+-Xs,+-Y)
 6991 *
 6992 *	succeeds if the simultaneous influence of the attributes in the
 6993 *	list Xs on the attribute Y is positive.
 6994 */
 6995
 6996simultPosInfl(Env,World,Xs,Y) :-
 6997	simultInfl(Env,World,Xs,Y,W),
 6998	W > 0.0.
 6999
 7000/***********************************************************************
 7001 *
 7002 * simultNegInfl(+Env,+World,+-Xs,+-Y)
 7003 *
 7004 *	succeeds if the simultaneous influence of the attributes in the
 7005 *	list Xs on the attribute Y is positive.
 7006 */
 7007
 7008simultNegInfl(Env,World,Xs,Y) :-
 7009	simultInfl(Env,World,Xs,Y,W),
 7010	W < 0.0.
 7011
 7012/***********************************************************************
 7013 *
 7014 * simultNoInfl(+Env,+World,+-Xs,+-Y)
 7015 *
 7016 *	succeeds if the simultaneous influence of the attributes in the
 7017 *	list Xs on the attribute Y is positive.
 7018 */
 7019
 7020simultNoInfl(Env,World,Xs,Y) :-
 7021	simultInfl(Env,World,Xs,Y,0.0).
 7022
 7023/***********************************************************************
 7024 *
 7025 * change(+Env,+World,+-Y,+-Wy)
 7026 *
 7027 *	determines the change in Y.
 7028 */
 7029
 7030change(Env,World,Y,Wy) :-
 7031	bagof(W,X^changingInfl(Env,World,X,Y,W),Ws),
 7032	weightOf_SimultChange(Ws,Wy).
 7033
 7034change(Env,World,Y,Wy) :-
 7035	given_change(Env,World,Y,Wy).
 7036
 7037/***********************************************************************
 7038 *
 7039 * changingInfl(+Env,+World,+-X,+-Y,+-Wy)
 7040 *
 7041 *	succeeds if the influencing attribute X of Y changes. Wy is the
 7042 *	weight of the resulting change in Y.
 7043 */
 7044
 7045changingInfl(Env,World,X,Y,Wy) :-
 7046	infl(Env,World,X,Y,Wxy),
 7047	given_change(Env,World,X,Wx),
 7048	weightOf_change(Wx,Wxy,Wy).
 7049
 7050/***********************************************************************
 7051 *
 7052 * increase(+Env,+World,+-Y)
 7053 *
 7054 *	succeeds if attribute Y increases.
 7055 */
 7056
 7057increase(Env,World,Y) :-
 7058	change(Env,World,Y,W),
 7059	W > 0.0.
 7060%	change(Env,World,Y,1.0).
 7061
 7062/***********************************************************************
 7063 *
 7064 * decrease(+Env,+World,+-Y)
 7065 *
 7066 *	succeeds if attribute Y decreases.
 7067 */
 7068
 7069decrease(Env,World,Y) :-
 7070	change(Env,World,Y,W),
 7071	W < 0.0.
 7072%	change(Env,World,Y,-1.0).
 7073
 7074/***********************************************************************
 7075 *
 7076 * noChange(+Env,+World,+-Y)
 7077 *
 7078 *	succeeds if attribute Y does not change (i.e. there is neither
 7079 *	an increase nor a decrease).
 7080 */
 7081
 7082noChange(Env,World,Y) :-
 7083	change(Env,World,Y,0.0).
 7084
 7085/***********************************************************************
 7086 *
 7087 * wellDefined_attribute(+EnvName,+World,+RoleName)
 7088 *
 7089 *	Is X an attribute?
 7090 *
 7091 *	Note: At the moment this clause succeeds if RoleName is an atom. We
 7092 *	may want to do more verifying here.
 7093 */
 7094
 7095wellDefined_attribute(Env,World,RoleName) :-
 7096	atom(RoleName),
 7097	roleName(Env,_MS,World,RoleName),
 7098	!.
 7099
 7100/***********************************************************************
 7101 *
 7102 * wellDefined_setOfAttributes(+Env,+World,+Xs)
 7103 *
 7104 *	Succeeds if Xs is a variable or if Xs is a SET of independent
 7105 *	(with respect to the influence relationship) atoms.
 7106 *
 7107 *	Note: In the current implementation the independence is NOT 
 7108 *	verified.
 7109 */
 7110
 7111wellDefined_setOfAttributes(Env,World,Xs) :-
 7112	isSet(Xs),
 7113	noInflLinks(Env,World,Xs).
 7114
 7115/***********************************************************************
 7116 *
 7117 * isSet(+L)
 7118 *
 7119 *	Succeeds if L is a SET.
 7120 */
 7121
 7122isSet([]) :-
 7123	!.
 7124isSet([E|L]) :-
 7125	not(member(E,L)),
 7126	isSet(L).
 7127
 7128
 7129/***********************************************************************
 7130 *
 7131 * noInflLinks(+Env,+World,+Xs)
 7132 *
 7133 *	Succeeds if there are no links among any of the attributes in Xs.
 7134 *
 7135 *	NOTE: The complexity is quadratic, namely (n-1)^2, 
 7136 *	where n is the length of Xs.
 7137 */
 7138
 7139noInflLinks(Env,World,[X|Xs]) :-
 7140	wellDefined_attribute(Env,World,X),
 7141	aux_noInflLinks(Env,World,X,Xs),
 7142	noInflLinks(Env,World,Xs).
 7143
 7144noInflLinks(_,_,[]).
 7145
 7146aux_noInflLinks(Env,World,X,[Y|Ys]) :-
 7147	not(getInflDescription(Env,World,_,X,Y)),
 7148	not(getInflDescription(Env,World,_,Y,X)),
 7149	aux_noInflLinks(Env,World,X,Ys).
 7150
 7151aux_noInflLinks(_,_,_,[]).
 7152
 7153/***********************************************************************
 7154 *
 7155 * wellDefined_SimultChanges(+Changes)
 7156 *
 7157 *	Succeeds if Changes is a list of changes of the form
 7158 *	change(X,W) in which no X occurs more than once.
 7159 */
 7160
 7161wellDefined_SimultChanges(Changes) :-
 7162	attributes(Changes,Xs),
 7163	isSet(Xs),
 7164	!.
 7165
 7166/***********************************************************************
 7167 *
 7168 * attributes(+Changes,+-Xs)
 7169 *
 7170 *	Generates a list Xs of attributes X from the list Changes.
 7171 *	The elements of Changes are of the form change(X,W).
 7172 */
 7173
 7174attributes([],[]).
 7175
 7176attributes([change(X,_)|Changes],[X|Xs]) :-
 7177	attributes(Changes,Xs).
 7178
 7179/***********************************************************************
 7180 *
 7181 * wellDefined_InflWeight(+W)
 7182 *
 7183 *	Is the given weight W of influence well-defined?
 7184 */
 7185
 7186wellDefined_InflWeight(W) :-
 7187	float(W),
 7188	!.
 7189wellDefined_InflWeight(W) :-
 7190	integer(W),
 7191	!.
 7192%	W >= -1.0,
 7193%	W =< 1.0.
 7194
 7195/***********************************************************************
 7196 *
 7197 * weightOf_ChainedInfl(+-W1,+-W2,+-W)
 7198 *
 7199 *	defines the weight W of chained influences with weights W1 and
 7200 *	W2.
 7201 *	W is given by f(W1,W2) where in this implementation f is
 7202 *	multiplication.
 7203 */
 7204
 7205weightOf_ChainedInfl(W1,W2,W) :-
 7206	product(W1,W2,W,wellDefined_ChangeWeight).
 7207
 7208/***********************************************************************
 7209 *
 7210 * weightOf_TotalInfl(+Ws,+-W)
 7211 *
 7212 *	computes the the total weight W from the Ws. 
 7213 *	Here, W is the motel_sum of the Ws.
 7214 *	We could have just as well chosen W to be the arithmetic
 7215 *	mean of the Ws.
 7216 *	Which is better remains open for the moment.
 7217 */
 7218
 7219weightOf_TotalInfl(Ws,W) :-
 7220	motel_sum(Ws,W,wellDefined_InflWeight).
 7221%	arithm_Mean(Ws,W,wellDefined_InflWeight).
 7222
 7223/***********************************************************************
 7224 *
 7225 * weightOf_SimultInfl(+-Ws,+-W)
 7226 *
 7227 *	computes the weight W of a list of simultaneous influences from
 7228 *	different attributes with weights specified in Ws.
 7229 *	Here, W is the motel_sum of the Ws.
 7230 *	We could have just as well chosen W to be the arithmetic
 7231 *	mean of the Ws.
 7232 *	Which is better remains open for the moment.
 7233 */
 7234
 7235weightOf_SimultInfl(Ws,W) :-
 7236	motel_sum(Ws,W,wellDefined_InflWeight).
 7237%	arithm_Mean(Ws,W,wellDefined_InflWeight).
 7238
 7239/***********************************************************************
 7240 *
 7241 * wellDefined_ChangeWeight(+W)
 7242 *
 7243 *	Is the given weight W of change well-defined?
 7244 */
 7245
 7246wellDefined_ChangeWeight(W) :-
 7247	float(W),
 7248	!.
 7249wellDefined_ChangeWeight(W) :-
 7250	integer(W),
 7251	!.
 7252%	W >= -1.0,
 7253%	W =< 1.0.
 7254
 7255/***********************************************************************
 7256 *
 7257 * weightOf_change(+-Wx,+-Wxy,-+Wy)
 7258 *
 7259 *	succeeds if Wy = f(Wx,Wxy) for a given f. 
 7260 *	I chose f to be multiplication.
 7261 */
 7262
 7263weightOf_change(Wx,Wxy,Wy) :-
 7264	product(Wx,Wxy,Wy,wellDefined_ChangeWeight).
 7265
 7266/***********************************************************************
 7267 *
 7268 * weightOf_SimultChange(+Ws,+-W)
 7269 *
 7270 *	computes the weight W of the change resulting from
 7271 *	simultaneous changes with weights Ws.
 7272 *	W is the motel_sum over the Ws.
 7273 */
 7274
 7275weightOf_SimultChange(Ws,W) :-
 7276	motel_sum(Ws,W,wellDefined_ChangeWeight).
 7277
 7278/***********************************************************************
 7279 *
 7280 * arithm_Mean([+-Value|+Values],+-Mean,+IsWellDefName)
 7281 *
 7282 *	Given a list of values (Values) and a predicate name for
 7283 *	checking whether each of the values is well-defined this clause
 7284 *	computes the arithmetical mean (Mean) over Values.
 7285 *	Provided Mean is given the first value may be a variable.
 7286 */
 7287
 7288arithm_Mean([],0.0,IsWellDefName) :-
 7289	!.
 7290
 7291arithm_Mean([Value|Values],Mean,IsWellDefName) :-
 7292	var(Value),
 7293	!,
 7294	length([Value|Values],N),
 7295	Sum is Mean * N,
 7296	motel_sum([Value|Values],Sum,IsWellDefName).
 7297
 7298arithm_Mean(Values,Mean,IsWellDefName) :-
 7299	motel_sum(Values,Sum,IsWellDefName),
 7300	length(Values,N),
 7301	Mean is Sum / N.
 7302
 7303/***********************************************************************
 7304 *
 7305 * motel_sum([+-Value|+Values],+-Sum,+IsWellDefName)
 7306 *
 7307 *	Given a list of values (Values) and a predicate name 
 7308 *	(IsWellDefName) for checking whether each of the values is 
 7309 *	well-defined this clause computes the motel_sum (Sum) of the values.
 7310 *	Provided Sum is given the first value may be a variable.
 7311 */
 7312
 7313motel_sum([Value|Values],Sum,IsWellDefName) :-
 7314	var(Value),
 7315	!,
 7316	IsWellDef =.. [IsWellDefName,Sum],
 7317	IsWellDef,
 7318	motel_sum(Values,VSum,IsWellDefName),
 7319	Value is Sum - VSum.
 7320
 7321motel_sum([Value|Values],Sum,IsWellDefName) :-
 7322	IsWellDef =.. [IsWellDefName,Value],
 7323	IsWellDef,
 7324	motel_sum(Values,VSum,IsWellDefName),
 7325	Sum is Value + VSum.
 7326
 7327motel_sum([],0.0,_).
 7328
 7329/***********************************************************************
 7330 *
 7331 * product(+Factor1,+Factor2,+-Product,+IsWellDefName)
 7332 * product(+-Factor1,+Factor2,+Product,+IsWellDefName)
 7333 * product(+Factor1,+-Factor2,+Product,+IsWellDefName)
 7334 *
 7335 *	Given two values (Factor1 and Factor2) and a predicate name 
 7336 *	(IsWellDefName) for checking whether each of the values is 
 7337 *	well-defined this clause computes the product (Product) of the 
 7338*	values.
 7339 */
 7340
 7341product(Factor1,Factor2,Product,IsWellDefName) :-
 7342	IsWellDef1 =.. [IsWellDefName,Factor1],
 7343	IsWellDef1,
 7344	IsWellDef2 =.. [IsWellDefName,Factor2],
 7345	IsWellDef2,
 7346	Product is Factor1 * Factor2,
 7347	!.
 7348
 7349product(Factor1,Factor2,Product,IsWellDefName) :-
 7350	IsWellDef1 =.. [IsWellDefName,Factor2],
 7351	IsWellDef1,
 7352	IsWellDef2 =.. [IsWellDefName,Product],
 7353	IsWellDef2,
 7354	Factor1 is Product / Factor2,
 7355	!.
 7356
 7357product(Factor1,Factor2,Product,IsWellDefName) :-
 7358	IsWellDef1 =.. [IsWellDefName,Factor1],
 7359	IsWellDef1,
 7360	IsWellDef2 =.. [IsWellDefName,Product],
 7361	IsWellDef2,
 7362	Factor2 is Product / Factor1,
 7363	!.
 7364
 7365/***********************************************************************
 7366 *
 7367 * max([+-Value|+Values],+-Max,+IsWellDefName)
 7368 *
 7369 *	Given a list of values (Values) and a predicate name 
 7370 *	(IsWellDefName) for checking whether each of the values is 
 7371 *	well-defined this clause determines the maximum (Max) of the 
 7372 *	values.
 7373 *
 7374 * max(+Value1,+Value2,+-Max)
 7375 *
 7376 *	returns the bigger value of Value1 and Value2 in Max.
 7377 */
 7378
 7379motel_max([Max],Max,_) :-
 7380	!.
 7381
 7382motel_max([Value|Values],Max,IsWellDefName) :-
 7383	IsWellDef =.. [IsWellDefName,Value],
 7384	IsWellDef,
 7385	motel_max(Values,VMax,IsWellDefName),
 7386	lub(Value,VMax,Max).
 7387
 7388lub(Value1,Value2,Value1) :-
 7389	Value1 > Value2,
 7390	!.
 7391
 7392lub(Value1,Value2,Value2).
 7393
 7394/***********************************************************************
 7395 *
 7396 * min([+-Value|+Values],+-Min,+IsWellDefName)
 7397 *
 7398 *	Given a list of values (Values) and a predicate name 
 7399 *	(IsWellDefName) for checking whether each of the values is 
 7400 *	well-defined this clause determines the minimum (Min) of the 
 7401 *	values.
 7402 *
 7403 * min(+Value1,+Value2,+-Min)
 7404 *
 7405 *	returns the smaller value of Value1 and Value2 in Min.
 7406 */
 7407
 7408motel_min([Min],Min,_) :-
 7409	!.
 7410
 7411motel_min([Value|Values],Min,IsWellDefName) :-
 7412	IsWellDef =.. [IsWellDefName,Value],
 7413	IsWellDef,
 7414	motel_min(Values,VMin,IsWellDefName),
 7415	glb(Value,VMin,Min).
 7416
 7417glb(Value1,Value2,Value1) :-
 7418	Value1 < Value2,
 7419	!.
 7420
 7421glb(Value1,Value2,Value2).
 7422
 7423/**********************************************************************
 7424 *
 7425 * %A%
 7426 *
 7427 */
 7428
 7429/***********************************************************************
 7430 *
 7431 * defprimconcept(+Environment,+Left)
 7432 * Parameter: ConceptName       concept name
 7433 * defines the concept ConceptName in modal context [].
 7434 *
 7435 */
 7436
 7437defprimconcept(Left) :-
 7438	getCurrentEnvironment(EnvName),
 7439	defprimconcept(EnvName,[],Left).
 7440
 7441
 7442/***********************************************************************
 7443 *
 7444 * defprimconcept(+Environment,+Left)
 7445 * Parameter: ConceptName       concept name
 7446 * defines the concept ConceptName in modal context [].
 7447 *
 7448 */
 7449
 7450defprimconcept(EnvName,Left) :-
 7451	environment(EnvName,_,_),
 7452	!,
 7453	defprimconcept(EnvName,[],Left).
 7454defprimconcept(MS,Left) :-
 7455	nonvar(MS),
 7456	(MS = [] ; MS = [_|_]),
 7457	!,
 7458	getCurrentEnvironment(EnvName),
 7459	defprimconcept(EnvName,MS,Left).
 7460defprimconcept(Left,Right) :-
 7461	getCurrentEnvironment(EnvName),
 7462	defprimconcept(EnvName,[],Left,Right).
 7463	
 7464
 7465/***********************************************************************
 7466 *
 7467 * defprimconcept(+Environment,+MS,+Left)
 7468 * Parameter: ModalSequence     modal context
 7469 *            ConceptName       concept name
 7470 * defines the concept ConceptName in modal context ModalSequence.
 7471 *
 7472 */
 7473
 7474defprimconcept(EnvName,MS,Left) :-
 7475	environment(EnvName,Env,_),
 7476	(MS = [] ; MS = [_|_]),
 7477	atomic(Left),
 7478	assertz_logged(conceptSubsets(Env,user,MS,Left,'top',noAxiom)),
 7479	assertz_logged(axiom(Env,MS,defprimconcept(MS,Left,'top'))),
 7480	assertNames(Env,MS,Left,concept),
 7481	!.
 7482
 7483defprimconcept(MS,Left,Right) :-
 7484	nonvar(MS),
 7485	(MS = [] ; MS = [_|_]),
 7486	!,
 7487	getCurrentEnvironment(EnvName),
 7488	defprimconcept(EnvName,MS,Left,Right).
 7489
 7490/***********************************************************************
 7491 *
 7492 * defprimconcept(+Environment,+Left,+Right)
 7493 * Parameter: ConceptName       concept name
 7494 *            ConceptTerm       concept term
 7495 * defines the concept ConceptName to be a motel_subset of the concept
 7496 * ConceptTerm in modal context [].
 7497 *
 7498 */
 7499
 7500defprimconcept(EnvName,Left,Right) :-
 7501	environment(EnvName,_,_),
 7502	defprimconcept(EnvName,[],Left,Right).
 7503
 7504/***********************************************************************
 7505 *
 7506 * defprimconcept(+Environment,+ModalSequence,+Left,+Right)
 7507 * Parameter: ModalSequence     modal context
 7508 *            ConceptName       concept name
 7509 *            ConceptTerm       concept term
 7510 * defines the concept ConceptName to be a motel_subset of the concept
 7511 * ConceptTerm in modal context ModalSequence.
 7512 *
 7513 */
 7514
 7515defprimconcept(EnvName,MS,L,R) :-
 7516	environment(EnvName,Env,_),
 7517%	nonvar(MS),
 7518	cnf(L,Left),
 7519	cnf(R,Right),
 7520	assertNames(Env,MS,Left,concept),
 7521	assertNames(Env,MS,Right,concept),
 7522	assertz_logged(axiom(Env,MS,defprimconcept(MS,L,R))),
 7523	unfold(Env,[(user,concept,Left,Right)],[(_Origin,_,L1,_,R1)|DL]),
 7524	gensym(axiom,AxiomName1),
 7525	assertz_logged(conceptSubsets(Env,user,MS,Left,Right,AxiomName1)),
 7526	typeOfDefinition(Env,MS,L1,Origin),
 7527	assertConceptLInR(Env,rn(AxiomName1,Origin,lInR),MS,L1,R1),
 7528	defList(Env,MS,DL,_),
 7529	negate(R1,NotRight1),
 7530	cnf(NotRight1,NotRight),
 7531	negate(L1,NotLeft1),
 7532	cnf(NotLeft1,NotLeft),
 7533	notClauseL(Env,MS,NotRight,NotLeft).
 7534
 7535
 7536notClauseL(Env,MS,Left,Right) :-
 7537	% assertz_logged that Left is included in Right
 7538	unfold(Env,[(user,concept,Left,Right)],[(_O,_,Concept1,C3,Concept2)|DL2]),
 7539	defPositiveList(Env,MS,DL2),
 7540	gensym(axiom,AxiomName2),
 7541	typeOfDefinition(Env,MS,Concept1,O),
 7542	assertz_logged(conceptSubsets(Env,user,MS,Concept1,C3,AxiomName2)),
 7543	assertConceptLInR(Env,rn(AxiomName2,O,lInR),MS,Concept1,Concept2).
 7544/* 
 7545notClauseL(Env,MS,Left,Right) :-
 7546	% assertz_logged that Left is included in Right
 7547	atomic(Left),
 7548	!,
 7549	unfold(Env,[(user,concept,Left,Right)],[(_O,_,Concept1,C3,Concept2)|DL2]),
 7550	defPositiveList(Env,MS,DL2),
 7551	gensym(axiom,AxiomName2),
 7552	typeOfDefinition(Env,MS,Concept1,O),
 7553	assertConceptLInR(Env,rn(AxiomName2,O,lInR),MS,Concept1,Concept2),
 7554	assertz_logged(conceptSubsets(Env,user,MS,Concept1,C3,AxiomName2)).
 7555notClauseL(Env,MS,Left,Right) :-
 7556	atomic(Right),
 7557	!,
 7558	gensym(concept,Concept1),
 7559	unfold(Env,[(system,concept,Concept1,Left)],DL2),
 7560	defPositiveList(Env,MS,DL2),
 7561	gensym(axiom,AxiomName2),
 7562	assertConceptLInR(Env,rn(AxiomName2,system,lInR)MS,Concept1,Right),
 7563	assertz_logged(conceptSubsets(Env,system,MS,Concept1,Concept2,AxiomName2)).
 7564notClauseL(Env,MS,Left,Right) :-
 7565	!,
 7566	gensym(concept,Concept1),
 7567	gensym(concept,Concept2),
 7568	unfold(Env,[(system,concept,Concept1,Left),(system,concept,Concept2,Right)],DL2),
 7569	defPositiveList(Env,MS,DL2),
 7570	gensym(axiom,AxiomName2),
 7571	assertConceptLInR(Env,rn(AxiomName,system,lInR),MS,Concept1,Concept2),
 7572	assertz_logged(conceptSubsets(Env,system,MS,Concept1,Concept2,AxiomName2)).
 7573*/
 7574
 7575notClausesLR(Env,MS,Left,Right,DL2) :-
 7576	unfold(Env,[(system,concept,Left,Right)],DL2),
 7577	defPositiveList(Env,MS,DL2).
 7578
 7579/***********************************************************************
 7580 *
 7581 * defconcept(+ConceptName,+ConceptTerm)
 7582 * Parameter: ConceptName       concept name
 7583 *            ConceptTerm       concept term
 7584 * defines the concept ConceptName to be equivalent to the concept
 7585 * ConceptTerm in modal context [].
 7586 *
 7587 */ 
 7588
 7589defconcept(ConceptName,ConceptTerm) :-
 7590	getCurrentEnvironment(EnvName),
 7591	defconcept(EnvName,[],ConceptName,ConceptTerm).
 7592
 7593defconcept(MS,CN,CT) :-
 7594	nonvar(MS),
 7595	(MS = [] ; MS = [_|_]),
 7596	!,
 7597	getCurrentEnvironment(EnvName),
 7598	defconcept(EnvName,MS,CN,CT),
 7599	!.
 7600defconcept(EnvName,ConceptName,ConceptTerm) :-
 7601	nonvar(EnvName),
 7602	environment(EnvName,_,_),
 7603	!,
 7604	defconcept(EnvName,[],ConceptName,ConceptTerm).
 7605
 7606/***********************************************************************
 7607 *
 7608 * defconcept(+ModalSequence,+ConceptName,+ConceptTerm)
 7609 * Parameter: ModalSequence     modal context
 7610 *            ConceptName       concept name
 7611 *            ConceptTerm       concept term
 7612 * defines the concept ConceptName to be equivalent to the concept
 7613 * ConceptTerm in modal context ModalSequence.
 7614 *
 7615 */ 
 7616
 7617defconcept(EnvName,MS,CT1,CT2) :-
 7618	nonvar(EnvName),
 7619	environment(EnvName,Env,_),
 7620	cnf(CT1,ConceptTerm1),
 7621	cnf(CT2,ConceptTerm2),
 7622	assertNames(Env,MS,ConceptTerm1,concept),
 7623	assertNames(Env,MS,ConceptTerm2,concept),
 7624	assertz_logged(axiom(Env,MS,defconcept(MS,CT1,CT2))),
 7625	unfold(Env,[(user,concept,ConceptTerm1,ConceptTerm2)],DL),
 7626	defList(Env,MS,DL,_).
 7627
 7628defPositiveList(_,_,[]) :- !.
 7629defPositiveList(Env,MS,[(Origin,concept,ConceptName,CTO,ConceptTerm)|DL]) :-
 7630	gensym(axiom,AxiomName),
 7631	assertz_logged(conceptEqualSets(Env,Origin,MS,ConceptName,CTO,AxiomName)),
 7632	assertConceptRInL(Env,rn(AxiomName,Origin,rInL),MS,ConceptName,ConceptTerm),
 7633	assertConceptLInR(Env,rn(AxiomName,Origin,lInR),MS,ConceptName,ConceptTerm),
 7634	defPositiveList(Env,MS,DL).
 7635defPositiveList(Env,MS,[(_Origin,role,RN,_RTO,RT)|RDL]) :-
 7636	gensym(axiom,AxiomName),
 7637	assertRoleLInR(Env,MS,RN,RT,AxiomName),
 7638	assertRoleRInL(Env,MS,RN,RT,AxiomName),
 7639	defPositiveList(Env,MS,RDL).
 7640
 7641defList(_,_,[],[]) :- !.
 7642defList(Env,MS,[(Origin,concept,ConceptName,CTO,ConceptTerm)|DL],
 7643        NeededDL3) :-
 7644	gensym(axiom,AxiomName),
 7645	assertz_logged(conceptEqualSets(Env,Origin,MS,ConceptName,CTO,AxiomName)),
 7646	assertConceptRInL(Env,rn(AxiomName,Origin,rInL),MS,ConceptName,ConceptTerm),
 7647	assertConceptLInR(Env,rn(AxiomName,Origin,lInR),MS,ConceptName,ConceptTerm),
 7648	negate(ConceptTerm,NotRight1),
 7649	cnf(NotRight1,NotRight),
 7650	negate(ConceptName,NotLeft1),
 7651	cnf(NotLeft1,NotLeft),
 7652	notClausesLR(Env,MS,NotRight,NotLeft,NeededDL1),
 7653	defList(Env,MS,DL,NeededDL2),
 7654	append(NeededDL1,NeededDL2,NeededDL3).
 7655defList(Env,MS,[(Origin,role,RN,RTO,RT)|RDL],NeededDL) :-
 7656	gensym(axiom,AxiomName),
 7657	assertz_logged(roleEqualSets(Env,Origin,MS,RN,RTO,AxiomName)),
 7658	assertRoleLInR(Env,MS,RN,RT,AxiomName),
 7659	assertRoleRInL(Env,MS,RN,RT,AxiomName),
 7660	defList(Env,MS,RDL,NeededDL).
 7661
 7662undefList(_,_,[]) :- !.
 7663undefList(EnvName,MS,[(Origin,concept,ConceptName,CTO,ConceptTerm)|DL]) :-
 7664	undefconcept(EnvName,MS,ConceptName,CTO),
 7665	undefList(Env,MS,DL).
 7666undefList(EnvName,MS,[(Origin,role,RN,RTO,RT)|RDL]) :-
 7667	undefrole(EnvName,MS,RN,RTO),
 7668	undefList(Env,MS,RDL).
 7669
 7670/***********************************************************************
 7671 *
 7672 * assert_ind(+ModalSequence,+ABoxElement,+ConceptTerm)
 7673 * Parameter: ModalSequence     modal context
 7674 *            ABoxElement       name of ABox element
 7675 *            ConceptTerm       concept term
 7676 * adds ABoxElement to Concept in modal context ModalSequence.
 7677 *
 7678 */
 7679
 7680assert_ind(X,CT) :-
 7681	getCurrentEnvironment(EnvName),
 7682	assert_ind(EnvName,[],X,CT).
 7683
 7684assert_ind(EnvName,X,CT) :-
 7685	environment(EnvName,_,_),
 7686	!,
 7687	assert_ind(EnvName,[],X,CT).
 7688assert_ind(MS,X,CT) :-
 7689	(MS = [] ; MS = [_|_]),
 7690	!,
 7691	getCurrentEnvironment(EnvName),
 7692	assert_ind(EnvName,MS,X,CT).
 7693
 7694assert_ind(X,Y,R) :-
 7695	getCurrentEnvironment(EnvName),
 7696	assert_ind(EnvName,X,Y,R).
 7697
 7698assert_ind(EnvName,MS,X,C) :-
 7699	environment(EnvName,Env,_),
 7700	nonvar(MS),
 7701	(MS = [] ; MS = [_|_]),
 7702	!,
 7703	atomic(X),
 7704	gensym(axiom,AxiomName),
 7705	gensym(rule,RuleName),
 7706	ruleName(AxiomName,RuleName,user,lInR,RN1),
 7707	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 7708	asserta_logged((conceptElement(Env,MS,W1,user,X,C,AxiomName) :- call(G1))),
 7709	assertz_logged(axiom(Env,MS,assert_ind(MS,X,C))),
 7710	constructMLHead(Env,RN1,W1,C,X,_HYPS,_D,_CALLS,abox,InHead),
 7711	asserta_logged((InHead :- call(G1))),
 7712	assertNames(Env,MS,C,concept).
 7713
 7714/***********************************************************************
 7715 *
 7716 * assert_ind(+ModalSequence,+ABoxElement1,+ABoxElement2,+Role)
 7717 * Parameter: ModalSequence     modal context
 7718 *            ABoxElement1      name of ABox element
 7719 *            ABoxElement2      name of ABox element
 7720 *            Role              role name
 7721 * adds the pair (ABoxElement1,ABoxElement2) to Role in modal context
 7722 * ModalSequence.
 7723 *
 7724 */
 7725
 7726assert_ind(EnvName,X,Y,R) :-
 7727	environment(EnvName,_,_),
 7728	!,
 7729	atomic(X),
 7730	atomic(Y),
 7731	!,
 7732	assert_ind(EnvName,[],X,Y,R).
 7733assert_ind(MS,X,Y,R) :-
 7734	(MS = [] ; MS = [_|_]),
 7735	!,
 7736	getCurrentEnvironment(EnvName),
 7737	!,
 7738	atomic(X),
 7739	atomic(Y),
 7740	!,
 7741	assert_ind(EnvName,MS,X,Y,R).
 7742
 7743assert_ind(EnvName,MS,X,Y,R) :-
 7744	environment(EnvName,Env,_),
 7745	atomic(X),
 7746	atomic(Y),
 7747	atomic(R),
 7748	Role1 =.. [R,X,Y],
 7749	asserta_logged(Role1),
 7750%	Role2 =.. [R,X,Y],
 7751	gensymbol(mskolem,[X,Y],SF),
 7752	gensym(axiom,AX),
 7753	gensym(rule,RN),
 7754	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 7755	constructEqHead(Env,rn(AX,RN,user,lInR),W1,Y,SF,R,X,_,_D,CALLS,abox,EqLiteral),
 7756	asserta_logged((EqLiteral :- (cCS(CALLS,true), call(G1)))),
 7757	assertNames(Env,MS,R,role),
 7758	assertz_logged(axiom(Env,MS,assert_ind(MS,X,Y,R))),
 7759	asserta_logged((roleElement(Env,MS,W1,user,X,Y,R,AX) :- call(G1))).
 7760
 7761
 7762/***********************************************************************
 7763 *
 7764 * defprimrole(+RN)
 7765 * Parameter: RN        role name
 7766 * defines the role RN in modal context [].
 7767 * 
 7768 */
 7769
 7770defprimrole(Role) :-
 7771	currentEnvironment(Env),
 7772	!,
 7773	assertNames(Env,[],Role,role),
 7774	asserta_logged(roleSubsets(Env,user,[],Role,'top',noAxiom)).
 7775
 7776defprimrole(EnvName,Role) :-
 7777	environment(EnvName,Env,_),
 7778	!,
 7779	assertNames(Env,[],Role,role),
 7780	asserta_logged(axiom(Env,[],defprimrole([],Role,'top'))),
 7781	asserta_logged(roleSubsets(Env,user,[],Role,'top',noAxiom)).
 7782
 7783/***********************************************************************
 7784 *
 7785 * defprimrole(+MS,+RN)
 7786 * Parameter: MS        modal context
 7787 *            RN        role name
 7788 * defines the role RN in modal context MS.
 7789 * 
 7790 */
 7791
 7792defprimrole(MS,Role) :-
 7793	nonvar(MS),
 7794	(MS = [] ; MS = [_|_]),
 7795	!,
 7796	currentEnvironment(Env),
 7797	assertNames(Env,MS,Role,role),
 7798	asserta_logged(axiom(Env,MS,defprimrole(MS,Role,'top'))),
 7799	asserta_logged(roleSubsets(Env,user,MS,Role,'top',noAxiom)).
 7800
 7801defprimrole(R1,R2) :-
 7802	getCurrentEnvironment(EnvName),
 7803	defprimrole(EnvName,[],R1,R2).
 7804
 7805/***********************************************************************
 7806 *
 7807 * defprimrole(+RN,+Role)
 7808 * Parameter: RN        role name
 7809 *            Role      role term
 7810 * defines the role RN to be a motel_subset of the role Role in modal
 7811 * context [].
 7812 * 
 7813 */
 7814
 7815defprimrole(EnvName,RN,Role) :-
 7816	environment(EnvName,_,_),
 7817	atomic(RN),
 7818	!,
 7819	defprimrole(EnvName,[],RN,Role).
 7820defprimrole(MS,RN,Role) :-
 7821	nonvar(MS),
 7822	(MS = [] ; MS = [_|_]),
 7823	getCurrentEnvironment(EnvName),
 7824	atomic(RN),
 7825	!,
 7826	defprimrole(EnvName,MS,RN,Role).
 7827
 7828/***********************************************************************
 7829 *
 7830 * defprimrole(+MS,+RN,+Role)
 7831 * Parameter: MS        modal context
 7832 *            RN        role name
 7833 *            Role      role term
 7834 * defines the role RN to be a motel_subset of the role Role in modal
 7835 * context MS.
 7836 *
 7837 */
 7838
 7839defprimrole(EnvName,MS,RN,Role) :-
 7840	environment(EnvName,Env,_),
 7841	atomic(RN),
 7842	assertNames(Env,MS,RN,role),
 7843	assertNames(Env,MS,Role,role),
 7844	unfold(Env,[(user,role,RN,Role)],[(user,role,RN,_,RT)|RDL]),
 7845	gensym(axiom,AxiomName),
 7846	asserta_logged(axiom(Env,MS,defprimrole(MS,RN,Role))),
 7847	asserta_logged(roleSubsets(Env,user,MS,RN,Role,AxiomName)),
 7848	assertRoleLInR(Env,MS,RN,RT,AxiomName),
 7849	defList(Env,MS,RDL,_).
 7850
 7851/***********************************************************************
 7852 *
 7853 * defrole(+RN,+Role)
 7854 * Parameter: RN        role name
 7855 *            Role      role term
 7856 * defines role RN to be equivalent to the role Role in modal context
 7857 * [].
 7858 *
 7859 */
 7860
 7861defrole(RN,Role) :-
 7862	getCurrentEnvironment(EnvName),
 7863	defrole(EnvName,[],RN,Role).
 7864
 7865defrole(MS,RN,Role) :-
 7866	nonvar(MS),
 7867	(MS = [] ; MS = [_|_]),
 7868	getCurrentEnvironment(EnvName),
 7869	defrole(EnvName,[],RN,Role).
 7870defrole(EnvName,RN,Role) :-
 7871	nonvar(EnvName),
 7872	environment(EnvName,_,_),
 7873	defrole(EnvName,[],RN,Role).
 7874
 7875/***********************************************************************
 7876 *
 7877 * defrole(+MS,+RN,+Role)
 7878 * Parameter: MS        modal context
 7879 *            RN        role name
 7880 *            Role      role term
 7881 * defines the role RN to be equivalent to the role Role in modal
 7882 * context MS.
 7883 *
 7884 */
 7885
 7886defrole(EnvName,MS,RN,Role) :-
 7887	environment(EnvName,Env,_),
 7888	atomic(RN),
 7889	unfold(Env,[(user,role,RN,Role)],RDL),
 7890	assertNames(Env,MS,RN,role),
 7891	assertNames(Env,MS,Role,role),
 7892	asserta_logged(axiom(Env,MS,defrole(MS,RN,Role))),
 7893	defList(Env,MS,RDL,_).
 7894
 7895/**********************************************************************
 7896 *
 7897 * defdisjoint(EnvName,MS,ConceptList)
 7898 * Parameter: EnvName         environment name
 7899 *            MS              modal context
 7900 *            ConceptList     list of concept names
 7901 *
 7902 */
 7903
 7904defdisjoint(CL) :-
 7905	getCurrentEnvironment(EnvName),
 7906	defdisjoint(EnvName,[],CL),
 7907	!.
 7908
 7909defdisjoint(EnvName,CL) :-
 7910	environment(EnvName,_,_),
 7911	defdisjoint(EnvName,[],CL),
 7912	!.
 7913defdisjoint(MS,CL) :-
 7914	nonvar(MS),
 7915	(MS = [] ; MS = [_|_]),
 7916	getCurrentEnvironment(EnvName),
 7917	defdisjoint(EnvName,MS,CL),
 7918	!.
 7919
 7920defdisjoint(_EnvName,_MS,[]) :-
 7921	!.
 7922defdisjoint(EnvName,MS,[C1|CL]) :-
 7923	defdisjoint(EnvName,MS,C1,CL),
 7924	defdisjoint(EnvName,MS,CL),
 7925	!.
 7926
 7927defdisjoint(_EnvName,_MS,_C1,[]) :-
 7928	!.
 7929defdisjoint(EnvName,MS,C1,[C2|CL]) :-
 7930	defprimconcept(EnvName,MS,C1,not(C2)),
 7931	defdisjoint(EnvName,MS,C1,CL).
 7932
 7933
 7934/**********************************************************************
 7935 *
 7936 * defclosed(EnvName,MS,X,Y,R)
 7937 *
 7938 */
 7939
 7940defclosed(X,Y,R) :-
 7941	getCurrentEnvironment(EnvName),
 7942	defclosed(EnvName,[],X,Y,R),
 7943	!.
 7944
 7945defclosed(EnvName,X,Y,R) :-
 7946	environment(EnvName,_,_),
 7947	defclosed(EnvName,[],X,Y,R),
 7948	!.
 7949defclosed(MS,X,Y,R) :-
 7950	nonvar(MS),
 7951	(MS = [] ; MS = [_|_]),
 7952	getCurrentEnvironment(EnvName),
 7953	defclosed(EnvName,MS,X,Y,R),
 7954	!.
 7955
 7956defclosed(EnvName,MS,X,Y,R) :-
 7957	environment(EnvName,Env,_),
 7958	assertz_logged(closed(Env,MS,X,Y,R)),
 7959	!.
 7960
 7961/***********************************************************************
 7962 *
 7963 * assertNames(+Type,+MS,+CT)
 7964 * Arguments: Type   'concept' or 'role'
 7965 *            MS     modal context
 7966 *            T      concept or role term
 7967 * asserts for each concept name CN in T a fact
 7968 *            conceptName(CN)
 7969 * and for each role name RN in T a fact
 7970 *            roleName(RN)
 7971 * These facts are used to distinguish concept and role names introduced 
 7972 * by the user from those introduced by the system.
 7973 *
 7974 */
 7975
 7976assertNames(Env,MS,CT,Type) :-
 7977	namesInTerm(CT,CNL1,Type),
 7978	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 7979	hop_map(assertName,[Env,MS,W1,G1],CNL1,_).
 7980
 7981assertName((concept,CN1),alreadyAsserted,Env,MS,W1,G1) :-
 7982% If the concept name is already asserted with identical modal sequence, 
 7983% then we do nothing.
 7984	clause(conceptName(Env,MS,_,CN1),_),
 7985	!.
 7986assertName((role,CN1),alreadyAsserted,Env,MS,W1,G1) :-
 7987% If the role name is already asserted with identical modal sequence, 
 7988% then we do nothing.
 7989	clause(roleName(Env,MS,_,CN1),_),
 7990	!.
 7991assertName((concept,CN1),newAsserted,Env,MS,W1,G1) :-
 7992% Otherwise we assert_logged the concept name
 7993% Remember: The fact that the concept name is not already asserted with
 7994% identical modal sequence does not mean that we are not already able to 
 7995% deduce that the concept name is present in the modal context corresponding
 7996% to the modal sequence.
 7997	assertz_logged((conceptName(Env,MS,W1,CN1) :- G1)),
 7998	!.
 7999assertName((role,CN1),newAsserted,Env,MS,W1,G1) :-
 8000% Otherwise we assert_logged the role name
 8001% Remember: The fact that the role name is not already asserted with
 8002% identical modal sequence does not mean that we are not already able to 
 8003% deduce that the role name is present in the modal context corresponding
 8004% to the modal sequence.
 8005	assertz_logged((roleName(Env,MS,W1,CN1) :- G1)),
 8006	!.
 8007	
 8008/***********************************************************************
 8009 *
 8010 * namesInTerm(+T1,-TL+Type) 
 8011 * Arguments: T1     concept or role term
 8012 *            TL     list of pairs (Type1,Name)
 8013 *            Type   'concept' or 'role'
 8014 * TL is the list of all concept and role names in T1.
 8015 *
 8016 */
 8017
 8018namesInTerm(and(CTL),CNL,Type) :-
 8019	hop_map(namesInTerm,[Type],CTL,CNLL),
 8020	motel_union(CNLL,CNL),
 8021	!.
 8022namesInTerm(or(CTL),CNL,Type) :-
 8023	hop_map(namesInTerm,[Type],CTL,CNLL),
 8024	motel_union(CNLL,CNL),
 8025	!.
 8026namesInTerm(some(R,C),L,_) :-
 8027	namesInTerm(R,L1,role),
 8028	namesInTerm(C,L2,concept),
 8029	append(L1,L2,L).
 8030namesInTerm(all(R,C),L,_) :-
 8031	namesInTerm(C,L1,concept),
 8032	namesInTerm(R,L2,role),
 8033	append(L1,L2,L).
 8034namesInTerm(restr(R,C),L,_) :-
 8035	namesInTerm(C,L1,concept),
 8036	namesInTerm(R,L2,role),
 8037	append(L1,L2,L).
 8038namesInTerm(atleast(_N,R),L,_) :-
 8039	namesInTerm(R,L,role).
 8040namesInTerm(atmost(_N,R),L,_) :-
 8041	namesInTerm(R,L,role).
 8042namesInTerm(inverse(R),L,Type) :-
 8043	namesInTerm(R,L,Type).
 8044namesInTerm(not(C),L,Type) :-
 8045	namesInTerm(C,L,Type).
 8046namesInTerm(naf(C),L,Type) :-
 8047	namesInTerm(C,L,Type).
 8048namesInTerm(b(_O,_P,C),L,Type) :-
 8049	namesInTerm(C,L,Type).
 8050namesInTerm(d(_O,_P,C),L,Type) :-
 8051	namesInTerm(C,L,Type).
 8052namesInTerm(bc(_O,D,C),L,Type) :-
 8053	namesInTerm(D,L1,Type),
 8054	namesInTerm(C,L2,Type),
 8055	append(L1,L2,L).
 8056namesInTerm(dc(_O,D,C),L,Type) :-
 8057	namesInTerm(D,L1,Type),
 8058	namesInTerm(C,L2,Type),
 8059	append(L1,L2,L).
 8060namesInTerm(set(_L),[],_Type) :-
 8061	!.
 8062namesInTerm(L,[(Type,L)],Type) :-
 8063	atomic(L),
 8064	!.
 8065
 8066
 8067
 8068/**********************************************************************
 8069 *
 8070 * %A%
 8071 *
 8072 */
 8073%------------------------------------------------------------------------------
 8074% Project:      MOTEL 1.0
 8075% Module:       malcToFOL.pl
 8076% Purpose:      Translation of modal KL-ONE terms to first-order logic 
 8077%               formulae to clauses
 8078% Last Change:  27.03.93
 8079% Language:     Prolog
 8080% Author:       Ullrich Hustadt
 8081% Address:      Max-Planck-Institut for Computer Science
 8082%               Im Stadtwald
 8083%               6600 Saarbr"ucken
 8084%               Germany
 8085% Email:        Ullrich.Hustadt@mpi-sb.mpg.de
 8086% Copyright:    (C) 1993 Ullrich Hustadt
 8087% Copying:      This software is provided under the GNU General Public License.
 8088% Warranty:     This is a research prototype. There is absolutely no warranty.
 8089%------------------------------------------------------------------------------
 8090
 8091axiomToClause(MC,VL,T1,Op,T2,C) :-
 8092	axiomToFOL(MC,VL,T1,Op,T2,F),
 8093	forallQuantify(VL,F,F1),
 8094	translate(F1,C1),
 8095	clausesToLOP(C1,C).
 8096
 8097forallQuantify([],F,F) :-
 8098	!.
 8099forallQuantify([X|VL],F1,forall(X,F2)) :-
 8100	forallQuantify(VL,F1,F2).
 8101
 8102%----------------------------------------------------------------------
 8103% axiomToFOL(+MODALCONTEXT,+VARLIST,+TERM1,+OPERATOR,+TERM2,-FORMULA)
 8104% If OPERATOR is 'equivalent', then FORMULA is the translation of the 
 8105% equivalence of TERM1 and TERM2 in MODALCONTEXT.
 8106% If OPERATOR is 'implies', then FORMULA is the translation of the
 8107% implication of TERM2 by TERM1 in MODALCONTEXT.
 8108
 8109axiomToFOL(MC,VL,_,in,C,F) :-
 8110	!,
 8111	malcToFOL(functional,U,VL,C,F1),
 8112	modalContextToFOL(MC,[],U,F1,F).
 8113axiomToFOL(MC,VL,C1,Op,C2,F) :-
 8114	malcToFOL(functional,U,VL,C1,F1),
 8115	malcToFOL(functional,U,VL,C2,F2),
 8116	F3 =.. [Op,F1,F2],
 8117	modalContextToFOL(MC,[],U,F3,F).
 8118
 8119modalContextToFOL([],V,V,F,F) :-
 8120	!.
 8121modalContextToFOL([b(O,A)|MC],U1,V,F3,
 8122	          forall(U2,implies(rel(X1,X2,U1,U2),F4))) :-
 8123	convertMS(e1,[U1,true],[b(O,A)],[],[U2,(rel(e1,X1,X2,U1,U2), true)],_),
 8124	modalContextToFOL(MC,U2,V,F3,F4).
 8125modalContextToFOL([d(O,A)|MC],U1,V,F3,F4) :-
 8126	convertMS(e1,[U1,true],[d(O,A)],[],
 8127                  [app(W1 : m(O,A), U1),true],_),
 8128	modalContextToFOL(MC,app(typed(W1,m(O,A)),U1),V,F3,F4).
 8129modalContextToFOL([bc(O,C)|MC],U1,V,F3,
 8130	          forall(A,forall(U2,implies(and([F1,rel(X1,m(O,A),U1,U2)]),F4)))) :-
 8131	convertMS(e1,[U1,true],[bc(O,C)],[],
 8132	          [U2,((once(_G),rel(e1,X1,m(O,A),U1,U2)),true)],_),
 8133	malcToFOL(functional,U1,[A],C,F1),
 8134	modalContextToFOL(MC,U2,V,F3,F4).
 8135modalContextToFOL([dc(O,C)|MC],U1,V,F3,
 8136	          forall(V,and([F1,F4]))) :-
 8137	convertMS(e1,[U1,true],[dc(O,C)],[],
 8138	          [app(W1 : m(O,A), U1),_G],_),
 8139	malcToFOL(functional,U1,[A],C,F1),
 8140	modalContextToFOL(MC,[app(typed(W1,m(O,A)), U1)],V,F3,F4).
 8141
 8142
 8143    
 8144%----------------------------------------------------------------------
 8145% malcToFOL(functional,+WORLD,+VARLIST,+TERM,-FORMULA)
 8146% translates TERM in WORLD for VARLIST into the first-order logic 
 8147% formula FORMULA.
 8148%
 8149% Author: Ullrich Hustadt
 8150
 8151malcToFOL(Trans,U,[X],and([C1]),F1) :-
 8152	malcToFOL(Trans,U,[X],C1,F1).
 8153malcToFOL(Trans,U,[X],and([C1|CL]),
 8154	   and([F1,F2])) :-
 8155	malcToFOL(Trans,U,[X],C1,F1),
 8156	malcToFOL(Trans,U,[X],and(CL),F2).
 8157malcToFOL(Trans,U,[X],or([C1]),F1) :-
 8158	malcToFOL(Trans,U,[X],C1,F1).
 8159malcToFOL(Trans,U,[X],or([C1|CL]),
 8160	   or([F1,F2])) :-
 8161	malcToFOL(Trans,U,[X],C1,F1),
 8162	malcToFOL(Trans,U,[X],or(CL),F2).
 8163malcToFOL(Trans,U,[X],not(C),not(F)) :-
 8164	malcToFOL(Trans,U,[X],C,F),
 8165	!.
 8166malcToFOL(Trans,U,[X],naf(C),F) :-
 8167	malcToFOL(Trans,U,[X],C,F),
 8168	!.
 8169malcToFOL(Trans,U,[X],all(R,C),
 8170	   forall(Y,implies(F1,F2))) :-
 8171	malcToFOL(Trans,U,[X,Y],R,F1),
 8172	malcToFOL(Trans,U,[Y],C,F2).
 8173malcToFOL(relational,U,[X],some(R,C),
 8174	   exists(Y,and([F1,F2]))) :-
 8175	malcToFOL(relational,U,[X,Y],R,F1),
 8176	malcToFOL(relational,U,[Y],C,F2).
 8177malcToFOL(functional,U,[X],some(R,C),
 8178	   exists(F,F2)) :-
 8179	malcToFOL(functional,U,[app(fun(F,R),X)],C,F2).
 8180malcToFOL(Trans,U,[X],atleast(N,R),F) :-
 8181	nrToFOL(U,[X],atleast(N,R),F).
 8182malcToFOL(Trans,U,[X],atmost(N,R),F) :-
 8183	nrToFOL(U,[X],atmost(N,R),F).
 8184malcToFOL(Trans,U,[X],b(O,A,C2),
 8185           forall(V,implies(rel(O,A,U,V),F))) :-
 8186	malcToFOL(Trans,V,[X],C2,F).    
 8187malcToFOL(Trans,U,[X],d(O,A,C2),
 8188	   exists(V,and([rel(O,A,U,V),F]))) :-
 8189	malcToFOL(Trans,V,[X],C2,F).
 8190malcToFOL(Trans,U,[X],bc(O,C1,C2),
 8191	   forall(A,forall(V,implies(and([F1,rel(O,A,U,V)]),F2)))) :-
 8192        malcToFOL(Trans,U,[A],C1,F1),
 8193	malcToFOL(Trans,V,[X],C2,F2).
 8194malcToFOL(Trans,U,[X],dc(O,C1,C2),
 8195	   forall(A,exists(V,and([and([F1,rel(O,A,U,V)]),F2])))) :-
 8196	malcToFOL(Trans,U,[A],C1,F1),
 8197	malcToFOL(Trans,V,[X],C2,F2).
 8198malcToFOL(Trans,U,[X],A,F) :-
 8199	atomic(A),
 8200	F =.. [in,U,A,X].
 8201malcToFOL(Trans,U,[X,Y],inverse(R),F) :-
 8202	malcToFOL(Trans,U,[Y,X],R,F).
 8203malcToFOL(Trans,U,[X,Y],and([R1]),F) :-
 8204	!,
 8205	malcToFOL(Trans,U,[X,Y],R1,F).
 8206malcToFOL(Trans,U,[X,Y],and([R1|RL]),
 8207	   and([F1,F2])) :-
 8208	malcToFOL(Trans,U,[X,Y],R1,F1),
 8209	malcToFOL(Trans,U,[X,Y],and(RL),F2).
 8210malcToFOL(Trans,U,[X,Y],restrict(R,C),
 8211	   and([F1,F2])) :-
 8212	malcToFOL(Trans,U,[X,Y],R,F1),
 8213	malcToFOL(Trans,U,[Y],C,F2).
 8214malcToFOL(Trans,U,[X,Y],restr(R,C),
 8215	   and([F1,F2])) :-
 8216	malcToFOL(Trans,U,[X,Y],R,F1),
 8217	malcToFOL(Trans,U,[Y],C,F2).
 8218malcToFOL(relational,U,[X,Y],P,F) :-
 8219	atomic(P),
 8220	F =.. [in,U,P,pair(X,Y)].
 8221malcToFOL(functional,U,[X,Y],P,equal(Y,app(fun(F,P),X))) :-
 8222	atomic(P),
 8223	atomic(X),
 8224	gensym(f,F),
 8225	!.
 8226malcToFOL(functional,U,[X,Y],P,equal(Y,app(fun(F,P),X))) :-
 8227	atomic(P),
 8228	var(X),
 8229	!.
 8230	
 8231	
 8232
 8233%----------------------------------------------------------------------
 8234% nrToFOL([+VAR],+NUMBERRESTRICTION,-FORMULA)
 8235% translates NUMBERRESTRICTION into FORMULA for variable VAR.
 8236%
 8237% Author: Ullrich Hustadt
 8238
 8239nrToFOL(U,[X],atmost(0,R),forall(Y,not(F1))) :-
 8240	!,
 8241	malcToFOL(functional,U,[X,Y],R,F1).
 8242nrToFOL(U,[X],atmost(M,R),F) :-
 8243	% M >= 1
 8244	N is M+1,
 8245	nVars(N,VarList),
 8246	relConjunction(U,X,VarList,R,F1),
 8247	eqDisjunction(VarList,F2),
 8248	quantify(forall,VarList,implies(F1,F2),F).
 8249nrToFOL(_U,[_X],atleast(0,_R),true) :-
 8250	!.
 8251nrToFOL(U,[X],atleast(1,R),exists(Y,F1)) :-
 8252	!,
 8253	malcToFOL(functional,U,[X,Y],R,F1).
 8254nrToFOL(U,[X],atleast(N,R),F) :-
 8255	nVars(N,VarList),
 8256	relConjunction(U,X,VarList,R,F1),
 8257	neqConjunction(VarList,F2),
 8258	quantify(exists,VarList,and([F1,F2]),F).
 8259
 8260%----------------------------------------------------------------------
 8261% quantify(+QUANTIFIER,+VARLIST,+MATRIX,-FORMULA)
 8262% FORMULA is a formula with a quantifier prefix consisting only of
 8263% quantifiers equal to QUANTIFIER and containing all variables in 
 8264% VARLIST and the matrix of FORMULA is MATRIX.
 8265%
 8266% Author: Ullrich Hustadt
 8267
 8268quantify(forall,[Y1],F,forall(Y1,F)) :-
 8269	!.
 8270quantify(forall,[Y1|YL],F,forall(Y1,F3)) :-
 8271	quantify(forall,YL,F,F3).
 8272quantify(exists,[Y1],F,exists(Y1,F)) :-
 8273	!.
 8274quantify(exists,[Y1|YL],F,exists(Y1,F3)) :-
 8275	quantify(exists,YL,F,F3).
 8276
 8277%----------------------------------------------------------------------
 8278% eqDisjunction(+VARLIST,-FORMULA)
 8279% FORMULA is a disjunction containing equalities for any pair
 8280% of variables that can be build using variables in VARLIST. VARLIST
 8281% must include at least 2 variables.
 8282%
 8283% Author: Ullrich Hustadt
 8284
 8285eqDisjunction([Y1,Y2],F1) :-
 8286	!,
 8287	eqDisjunction(Y1,[Y2],F1),
 8288	!.
 8289eqDisjunction([Y1|YL],or([F1,F2])) :-
 8290	eqDisjunction(Y1,YL,F1),
 8291	eqDisjunction(YL,F2).
 8292
 8293eqDisjunction(Y1,[Y2],equal(Y1,Y2)) :-
 8294	!.
 8295eqDisjunction(Y1,[Y2|YL],or([equal(Y1,Y2),F2])) :-
 8296	eqDisjunction(Y1,YL,F2).
 8297
 8298%----------------------------------------------------------------------
 8299% neqConjunction(+VARLIST,-FORMULA)
 8300% FORMULA is a conjunction containing inequalities for any pair
 8301% of variables that can be build using variables in VARLIST. VARLIST
 8302% must include at least 2 variables.
 8303%
 8304% Author: Ullrich Hustadt
 8305
 8306neqConjunction([Y1,Y2],F1) :-
 8307	!,
 8308	neqConjunction(Y1,[Y2],F1),
 8309	!.
 8310neqConjunction([Y1|YL],and([F1,F2])) :-
 8311	neqConjunction(Y1,YL,F1),
 8312	neqConjunction(YL,F2).
 8313
 8314neqConjunction(Y1,[Y2],not(equal(Y1,Y2))) :-
 8315	!.
 8316neqConjunction(Y1,[Y2|YL],and([not(equal(Y1,Y2)),F2])) :-
 8317	neqConjunction(Y1,YL,F2).
 8318
 8319%----------------------------------------------------------------------
 8320% relConjunction(+VAR,+VARLIST,+ROLETERM,-FORMULA)
 8321% FORMULA is a conjunction containing the translation of ROLETERM for
 8322% any pair that can be build taking VAR and an element of VARLIST.
 8323%
 8324% Author: Ullrich Hustadt
 8325
 8326relConjunction(_U,_X,[],_,true) :-
 8327	!.
 8328relConjunction(U,X,[Y1],R,F) :-
 8329	!,
 8330	malcToFOL(functional,U,[X,Y1],R,F).
 8331relConjunction(U,X,[Y1|YL],R,and([F1,F2])) :-
 8332	malcToFOL(functional,U,[X,Y1],R,F1),
 8333	relConjunction(U,X,YL,R,F2).
 8334
 8335%----------------------------------------------------------------------
 8336% nVars(+N,-VARLIST)
 8337% VARLIST is a list of N fresh Prolog variables.
 8338%
 8339% Author: Ullrich Hustadt
 8340
 8341nVars(0,[]) :-
 8342	!.
 8343nVars(N,[_Y1|VL]) :-
 8344	N >= 1,
 8345	!,
 8346	M is N-1,
 8347	nVars(M,VL).
 8348nVars(_,[]).
 8349
 8350%----------------------------------------------------------------------
 8351
 8352
 8353printNHProlog([(false <== T1)|CL]) :-
 8354	!,
 8355	write(<==),
 8356	print(T1),
 8357	write(' .'),
 8358	nl,
 8359	printNHProlog(CL).
 8360printNHProlog([(H1 <== true)|CL]) :-
 8361	!,
 8362	print(H1),
 8363	write(' '),
 8364	write(<==),
 8365	write(' .'),
 8366	nl,
 8367	printNHProlog(CL).
 8368printNHProlog([C1|CL]) :-
 8369	print(C1),
 8370	write(.),
 8371	nl,
 8372	printNHProlog(CL).
 8373printNHProlog([]).
 8374
 8375%----------------------------------------------------------------------
 8376% clausesToNHProlog(+CLAUSES,-NHCLAUSES)
 8377% translates CLAUSES which are given in abstract syntax to NHCLAUSES
 8378% which are in near-Horn Prolog syntax.
 8379%
 8380% Author: Ullrich Hustadt
 8381
 8382clausesToNHProlog([cl(HL,TL)|CL],[C2|CL2]) :-
 8383	literalsToNHProlog(HL,HL1),
 8384	literalsToNHProlog(TL,TL1),
 8385	implicationToNHProlog(HL1,TL1,C2),
 8386	clausesToNHProlog(CL,CL2).
 8387clausesToNHProlog([],[]).
 8388
 8389literalsToNHProlog([H1,H2|HL],(H1,HL2)) :-
 8390	literalsToNHProlog([H2|HL],HL2).
 8391literalsToNHProlog([H1],H1) :-
 8392	!.
 8393literalsToNHProlog([],true) :-
 8394	!.
 8395
 8396implicationToNHProlog(HL1,[],HL1) :-
 8397	!.
 8398implicationToNHProlog(HL1,TL1,(HL1 :- TL1)) :-
 8399	!.
 8400
 8401%----------------------------------------------------------------------
 8402% clausesToLOP(+CLAUSES,-NHCLAUSES)
 8403% translates CLAUSES which are given in abstract syntax to NHCLAUSES
 8404% which are in LOP syntax.
 8405%
 8406% Author: Ullrich Hustadt
 8407
 8408
 8409clausesToLOP([cl(HL0,TL0)|CL],[C2|CL2]) :-
 8410	clauseToSequent(cl(HL0,TL0),HL,TL),
 8411	literalsToLOP(succedent,HL,HL1),
 8412	literalsToLOP(antecedent,TL,TL1),
 8413	implicationToLOP(HL1,TL1,C2),
 8414	clausesToLOP(CL,CL2).
 8415clausesToLOP([],[]).
 8416
 8417clauseToSequent(cl([],TL),HL1,[]) :-
 8418	!,
 8419	map(negateLiterals,TL,HL1).
 8420clauseToSequent(cl(HL,TL),HL,TL) :-
 8421	!.
 8422
 8423negateLiterals( '~'(L),L) :-
 8424	!.
 8425negateLiterals(L, '~'(L)) :-
 8426	!.
 8427
 8428literalsToLOP(antecedent,[H1,H2|HL],(H1,HL2)) :-
 8429	literalsToLOP(antecedent,[H2|HL],HL2).
 8430literalsToLOP(succedent,[H1,H2|HL],(H1;HL2)) :-
 8431	literalsToLOP(succedent,[H2|HL],HL2).
 8432literalsToLOP(_,[H1],H1) :-
 8433	!.
 8434literalsToLOP(_,[],true) :-
 8435	!.
 8436
 8437implicationToLOP(HL1,true,(HL1 <== true)) :-
 8438	!.
 8439implicationToLOP(HL1,TL1,(HL1 <== TL1)) :-
 8440	!.
 8441
 8442
 8443%----------------------------------------------------------------------
 8444% envToFOL(+CLAUSES,-NHCLAUSES)
 8445% translates CLAUSES which are given in abstract syntax to NHCLAUSES
 8446% which are in LOP syntax.
 8447%
 8448% Author: Ullrich Hustadt
 8449
 8450envToFOL(Name,CL) :-
 8451	translateModalAxioms(Name,CL1),
 8452	translateAxioms(Name,CL2),
 8453	append(CL1,CL2,CL),
 8454	!.
 8455
 8456translateModalAxiom([],[]) :-
 8457	!.
 8458translateModalAxiom([[MS,KClass,MOp,concept(C)]|L1],CL) :-
 8459	!,
 8460	malcToFOL(functional,U,[A],C,F1),
 8461	translateClass(KClass,MOp,C,[A],F2),
 8462	modalContextToFOL(MS,[],U,forall(A,implies(F1,F2)),F3),
 8463	translate(F3,C1),
 8464	clausesToLOP(C1,CL1),
 8465	translateModalAxiom(L1,CL2),
 8466	append(CL1,CL2,CL).
 8467translateModalAxiom([[MS,KClass,MOp,all]|L1],CL) :-
 8468	!,
 8469	translateClass(KClass,MOp,all,[A],F2),
 8470	modalContextToFOL(MC,[],U,forall(A,F2),F3),
 8471	translate(F3,C1),
 8472	clausesToLOP(C1,CL1),
 8473	translateModalAxiom(L1,CL2),
 8474	append(CL1,CL2,CL).
 8475translateModalAxiom([[MS,KClass,MOp,A]|L1],CL) :-
 8476	!,
 8477	translateClass(KClass,MOp,C,[A],F2),
 8478	modalContextToFOL(MC,[],U,forall(A,F2),F3),
 8479	translate(F3,C1),
 8480	clausesToLOP(C1,CL1),
 8481	translateModalAxiom(L1,CL2),
 8482	append(CL1,CL2,CL).
 8483
 8484translateClass(kd45,MOp,C,[A],
 8485	rel(C,m(MOp,A),B,app(typed(F,m(MOp,A)),D))) :-
 8486	!.
 8487translateClass(k,   MOp,C,[A],
 8488        rel(C,m(MOp,A),B,app(typed(F,m(MOp,A)),B))) :-
 8489	!.
 8490translateClass(kd5, MOp,C,[A],
 8491        and([rel(C,m(MOp,A),app(typed(F1,m(MOp,A)),U),app(typed(F2,m(MOp,A)),V)),
 8492             rel(C,m(MOp,A),U,app(typed(F2,m(MOp,A)),U))])) :-
 8493	!.
 8494
 8495
 8496translateModalAxioms(Name,CL) :-
 8497	setofOrNil([MS,KClass,MOp,Concept],
 8498                   [X1,B1]^clause(modalAxioms(Name,MS,user,KClass,Concept,MOp,X1),B1),
 8499		   L1),
 8500	translateModalAxiom(L1,CL).
 8501
 8502translateAxiom([],[]) :-
 8503	!.
 8504translateAxiom([[A1,A2,A3,A4,A5]|L1],CL) :-
 8505	axiomToClause(A1,A2,A3,A4,A5,CL1),
 8506	translateAxiom(L1,CL2),
 8507	append(CL1,CL2,CL).
 8508
 8509translateAxioms(Name,CL) :-
 8510	theory(Name,CL0),
 8511	setofOrNil([MS,[A],_,in,C],
 8512	           [X1,Ax,B1]^clause(conceptElement(Name,MS,X1,user,A,C,Ax),B1),
 8513		   L1),
 8514	translateAxiom(L1,CL1),
 8515	setofOrNil([MS,[A,B],_,in,R],
 8516	           [X1,Ax,B1]^clause(roleElement(Name,MS,X1,user,A,B,R,Ax),B1),
 8517		   L2),
 8518	translateAxiom(L2,CL2),
 8519	setofOrNil([MS,[X],CT1,equivalent,CT2],
 8520	           [Ax,B1]^clause(conceptEqualSets(Name,user,MS,CT1,CT2,Ax),B1),
 8521		   L3),
 8522	translateAxiom(L3,CL3),
 8523	setofOrNil([MS,[X],CT1,implies,CT2],
 8524	           [Ax,B1]^clause(conceptSubsets(Name,user,MS,CT1,CT2,Ax),B1),
 8525		   L4),
 8526	translateAxiom(L4,CL4),
 8527	setofOrNil([MS,[X,Y],RN,equivalent,RT],
 8528	           [Ax,B1]^clause(roleEqualSets(Name,user,MS,RN,RT,Ax),B1),
 8529		   L5),
 8530	translateAxiom(L5,CL5),
 8531	setofOrNil([MS,[X,Y],RN,implies,RT],
 8532	           [Ax,B1]^clause(roleSubSets(Name,user,MS,RN,RT,Ax),B1),
 8533		   L6),
 8534	translateAxiom(L6,CL6),
 8535%	closed(Name,MS,X,Y,R),
 8536	append(CL1,CL2,CL12),
 8537	append(CL12,CL3,CL13),
 8538	append(CL13,CL4,CL14),
 8539	append(CL14,CL5,CL15),
 8540	append(CL15,CL6,CL16),
 8541	append(CL0,CL16,CL),
 8542	!.
 8543
 8544
 8545/**********************************************************************
 8546 *
 8547 * @(#) modal.pl 1.6@(#)
 8548 *
 8549 */
 8550
 8551modalAxioms(KName,MOp,A) :-
 8552	getCurrentEnvironment(EnvName),
 8553	modalAxioms(EnvName,[],KName,MOp,A).
 8554
 8555/**********************************************************************
 8556 *
 8557 * genclass(+Agent,-Class) 
 8558 * classifies Agent to distinguish axioms for a modal operator behaving
 8559 * equally for all agents from axioms for a mutual modal operator.
 8560 *
 8561 */
 8562
 8563genclass(_,_,A,A,every,true) :-
 8564	var(A),
 8565	!.
 8566genclass(_,_,all,_,all,true) :-
 8567	!.
 8568genclass(Env,[W1,G1],concept(C),Agent,C,(Body,G1)) :-
 8569	!,
 8570	getQuery(Env,W1,C,Agent,_Exp,Body),
 8571	!.
 8572genclass(_,_,A,A,some,true) :-
 8573	!.
 8574
 8575/**********************************************************************
 8576 * 
 8577 * assertMA(+Class,+Head,+WorldGoal,+Goal)
 8578 * asserts the appropriate clause for the given Class. If Class is all,
 8579 * i.e. we are dealing with a mutual modal operator, no world checks 
 8580 * have to be done. Otherwise WorldGoal has to be added to the body of
 8581 * the clause.
 8582 *
 8583 */
 8584
 8585 assertMA(Class,Head,Goal):- assertMA(Class,Head,_WorldGoal,Goal).
 8586
 8587
 8588assertMA(A1,rel(Env,every,m(MOp,A1),X,Y), WG, G) :-
 8589	var(A1),
 8590	asserta_logged((rel(Env,every,m(MOp,A1),X,Y) :- (WG, G))),
 8591	!.
 8592assertMA(all,rel(Env,all,m(MOp,A),X,Y), _WG, G) :-
 8593	asserta_logged((rel(Env,all,m(MOp,A),X,Y) :- G)),
 8594	!.
 8595assertMA(A,rel(Env,some,m(MOp,A),X,Y), WG, G) :-
 8596	asserta_logged((rel(Env,some,m(MOp,A),X,Y) :- (WG, G))),
 8597	!.
 8598assertMA(concept(_),rel(Env,D,m(MOp,A),X,Y), WG, G) :-
 8599	asserta_logged((rel(Env,D,m(MOp,A),X,Y) :- (WG, G))),
 8600	!.
 8601
 8602
 8603/**********************************************************************
 8604 *
 8605 * modalAxioms(+EnvName,+KripkeClass,+MOp,+Agent)
 8606 * asserts the modal axioms for the modal operator MOp and agent Agent
 8607 * in environment EnvName for KripkeClass.
 8608 *
 8609 */
 8610	
 8611modalAxioms(MS,KName,MOp,A1) :-
 8612	(MS = [] ; MS = [_|_]),
 8613	getCurrentEnvironment(EnvName),
 8614	modalAxioms(EnvName,MS,KName,MOp,A1).
 8615modalAxioms(EnvName,KName,MOp,A1) :-
 8616	environment(EnvName,_,_),
 8617	modalAxioms(EnvName,[],KName,MOp,A1).
 8618
 8619modalAxioms(EnvName,MS,k,MOp,A1) :-
 8620	environment(EnvName,Env,_),
 8621	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 8622	genclass(Env,[W1,G1],A1,A,C,Goal),
 8623	retractall_head(rel(Env,C,m(MOp,A),_,_)),
 8624	retractall_head(modalAxioms(Env,MS,user,_,A1,MOp,A)),
 8625	assertMA(A1,
 8626                 rel(Env,C,m(MOp,A),U,app(_FF:m(MOp,A),U)), 
 8627		 (not(not(world(Env,m(MOp,A),U,V)))), 
 8628		 (normal(Env,U), Goal)),
 8629	asserta_logged(modalAxioms(Env,MS,user,k,A1,MOp,A)),
 8630	!.
 8631modalAxioms(EnvName,MS,kd45,MOp,A1) :-
 8632	environment(EnvName,Env,_),
 8633	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 8634	genclass(Env,[W1,G1],A1,A,C,Goal),
 8635	retractall_head(rel(Env,C,m(MOp,A),_,_)),
 8636	retractall_head(modalAxioms(Env,MS,user,_,A1,MOp,A)),
 8637	assertMA(A1,
 8638	         rel(Env,C,m(MOp,A),U,app(_FF:m(MOp,A),V)), 
 8639		 (not(not(world(Env,m(MOp,A),U,V)))), 
 8640		 (normal(Env,U), Goal)),
 8641%	assertMA(A1,
 8642%                 rel(Env,C,m(MOp,A),U,app(_FF:m(MOp,A),U)), 
 8643%		 (not(not(world(Env,m(MOp,A),U,V)))), 
 8644%		 (normal(Env,U), Goal)),
 8645	asserta_logged(modalAxioms(Env,MS,user,kd45,A1,MOp,A)),
 8646	!.
 8647modalAxioms(EnvName,MS,kd4e,MOp,A) :-
 8648	modalAxioms(EnvName,kd45,MOp,A).
 8649modalAxioms(EnvName,MS,kd5,MOp,A1) :-
 8650	environment(EnvName,Env,_),
 8651	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 8652	genclass(Env,[W1,G1],A1,A,C,Goal),
 8653	retractall_head(rel(Env,C,m(MOp,A),_,_)),
 8654	retractall_head(modalAxioms(Env,MS,user,_,A1,MOp,A)),
 8655	assertMA(A1,
 8656	         rel(Env,C,m(MOp,A),app(_F1:m(MOp,A),U),app(_F2:m(MOp,A),V)), 
 8657		 ((world(Env,m(MOp,A),U,V), not(U == []))), 
 8658		 Goal),
 8659	assertMA(A1,
 8660	         rel(Env,C,m(MOp,A),U,app(_F2:m(MOp,A),U)), 
 8661		 true, 
 8662		 Goal),
 8663	asserta_logged(modalAxioms(Env,MS,user,kd5,A1,MOp,A)),
 8664	!.
 8665modalAxioms(EnvName,MS,kd4,MOp,A1) :-
 8666	environment(EnvName,Env,_),
 8667	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 8668	genclass(Env,[W1,G1],A1,A,C,Goal),
 8669	retractall_head(rel(Env,C,m(MOp,A),_,_)),
 8670	retractall_head(modalAxioms(Env,MS,user,_,A1,MOp,A)),
 8671	assertMA(A1, rel(Env,C,m(MOp,A),U,app(_F1:m(MOp,A),U)), Goal),
 8672	assertMA(A1,rel(Env,C,m(MOp,A),U,app(_F1:m(MOp,A),V)), (world(Env,m(MOp,A),U,V), (rel(Env,_,m(MOp,A),U,V), Goal))),
 8673	asserta_logged(modalAxioms(Env,MS,user,k4,A1,MOp,A)),
 8674	!.
 8675modalAxioms(EnvName,MS,kt,MOp,A1) :-
 8676	environment(EnvName,Env,_),
 8677	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 8678	genclass(Env,[W1,G1],A1,A,C,Goal),
 8679	retractall_head(rel(Env,C,m(MOp,A),_,_)),
 8680	retractall_head(modalAxioms(Env,MS,user,_,A1,MOp,A)),
 8681	assertMA(A1,rel(Env,C,m(MOp,A),U,app(_F1:m(MOp,A),U)), Goal),
 8682	assertMA(A1,rel(Env,C,m(MOp,A),U,U), Goal),
 8683	asserta_logged(modalAxioms(Env,MS,user,kt,A1,MOp,A)),
 8684	!.
 8685
 8686/**********************************************************************
 8687 *
 8688 * normal(+EnvName,+World)
 8689 * succeeds if World is normal, i.e. has a successor.
 8690 *
 8691 */
 8692
 8693normal(_,_).
 8694
 8695/**********************************************************************
 8696 *
 8697 * world(+EnvName,m(+MOp,+Agent),+WorldSequence) 
 8698 * checks wether or not WorldSequence is a sequence of worlds for
 8699 * modal operator MOp and agent Agent.
 8700 *
 8701 */
 8702
 8703world(_Env,m(_MOp,_A),U,U).
 8704world(Env,m(MOp,A),U,app(_FF:m(MOp,A),V)) :-
 8705	world(Env,m(MOp,A),U,V).
 8706
 8707/**********************************************************************
 8708 *
 8709 * @(#) roleFunctions.pl 1.2@(#)
 8710 *
 8711 */
 8712
 8713/***********************************************************************
 8714 *
 8715 * getDirectFatherRoles(+EnvName,+MS,+Role,-RL)
 8716 * Arguments: EnvName     environment identifier
 8717 *            MS          modal context
 8718 *            Role        role name
 8719 *            RL          list of role names
 8720 * RL is the list of all role names which are direct father roles
 8721 * of Role.
 8722 *
 8723 */
 8724
 8725getDirectFatherRoles(EnvName,MS,Role,RL) :-
 8726	environment(EnvName,Env,_),
 8727	roleHierarchy(Env,MS,Dag),
 8728	getDirectSuperElements(Role,RL,Dag).
 8729
 8730/***********************************************************************
 8731 *
 8732 * getAllFatherRoles(+EnvName,+MS,+Role,-RL)
 8733 * Arguments: EnvName     environment identifier
 8734 *            MS          modal context
 8735 *            Role        role name
 8736 *            RL          list of role names
 8737 * RL is the list of all role names which are father roles of
 8738 * Role
 8739 *
 8740 */
 8741
 8742getAllFatherRoles(EnvName,MS,Role,RL) :-
 8743	environment(EnvName,Env,_),
 8744	roleHierarchy(Env,MS,Dag),
 8745	getAllSuperElements(Role,RL,[],Dag).
 8746
 8747/***********************************************************************
 8748 *
 8749 * getDirectSonRoles(+EnvName,+MS,+Role,-RL)
 8750 * Arguments: EnvName     environment identifier
 8751 *            MS          modal context
 8752 *            Role        role name
 8753 *            RL          list of role names
 8754 * RL is the list of all role names which are direct father roles
 8755 * of Role
 8756 *
 8757 */
 8758
 8759getDirectSonRoles(EnvName,MS,Role,RL) :-
 8760	environment(EnvName,Env,_),
 8761	roleHierarchy(Env,MS,Dag),
 8762	getDirectSubElements(Role,RL,Dag).
 8763
 8764/***********************************************************************
 8765 *
 8766 * getAllSonRoles(+EnvName,+MS,+Role,-RL)
 8767 * Arguments: EnvName     environment identifier
 8768 *            MS          modal context
 8769 *            Role        role name
 8770 *            RL          list of role names
 8771 * RL is the list of all role names which are father roles of 
 8772 * Role
 8773 *
 8774 */
 8775
 8776getAllSonRoles(EnvName,MS,Role,RL) :-
 8777	environment(EnvName,Env,_),
 8778	roleHierarchy(Env,MS,Dag),
 8779	getAllSubElements(Role,RL,Dag).
 8780
 8781/***********************************************************************
 8782 *
 8783 * getRoles(+EnvName,+MS,-RL)
 8784 * Arguments: EnvName     environment identifier
 8785 *            MS     modal context
 8786 *            RL     list of role names
 8787 * RL is the list of all role names in the subsumption hierarchy.
 8788 *
 8789 */
 8790
 8791getRoles(EnvName,MS,['top'|RL]) :-
 8792	getAllSonRoles(EnvName,MS,'top',RL).
 8793
 8794/***********************************************************************
 8795 *
 8796 * testDirectFatherRole(+EnvName,+MS,+Role1,+Role2,-Role)
 8797 * Arguments: EnvName     environment identifier
 8798 *            MS          modal context
 8799 *            Role1       role name
 8800 *            Role2       role name
 8801 *            Role        role name
 8802 * Role is Role1 iff Role1 is a direct father role of Role2
 8803 * or
 8804 * Role is Role2 iff Role2 is a direct father role of Role1
 8805 * otherwise
 8806 * the predicate fails
 8807 *
 8808 */
 8809
 8810testDirectFatherRole(EnvName,MS,Role1,Role2,Role) :-
 8811	environment(EnvName,Env,_),
 8812	roleHierarchy(Env,MS,Dag),
 8813	testDirectSuperElement(Role1,Role2,Role,Dag).
 8814
 8815/***********************************************************************
 8816 *
 8817 * testDirectSonRole(+EnvName,+MS,+Role1,+Role2,-Role)
 8818 * Arguments: EnvName     environment identifier
 8819 *            MS          modal context
 8820 *            Role1       role name
 8821 *            Role2       role name
 8822 *            Role        role name
 8823 * Role is Role1 iff Role1 is a direct subrole of Role2
 8824 * or
 8825 * Role is Role2 iff Role2 is a direct subrole of Role1
 8826 * otherwise
 8827 * the predicate fails
 8828 *
 8829 */
 8830
 8831testDirectSonRole(EnvName,MS,Role1,Role2,Role) :-
 8832	environment(EnvName,Env,_),
 8833	roleHierarchy(Env,MS,Dag),
 8834	testDirectSuperElement(Role1,Role2,Role,Dag).
 8835
 8836/***********************************************************************
 8837 *
 8838 * testFatherRole(+EnvName,+MS,+Role1,+Role2,-Role)
 8839 * Arguments: EnvName     environment identifier
 8840 *            MS          modal context
 8841 *            Role1       role name
 8842 *            Role2       role name
 8843 *            Role        role name
 8844 * Role is Role1 iff Role1 is a direct father role of Role2
 8845 * or
 8846 * Role is Role2 iff Role2 is a direct father role of Role1
 8847 * otherwise
 8848 * the predicate fails
 8849 *
 8850 */
 8851
 8852testFatherRole(EnvName,MS,Role1,Role2,Role) :-
 8853	environment(EnvName,Env,_),
 8854	roleHierarchy(Env,MS,Dag),
 8855	testSuperElement(Role1,Role2,Role,Dag).
 8856
 8857/***********************************************************************
 8858 *
 8859 * testSonRole(+EnvName,+MS,+Role1,+Role2,-Role)
 8860 * Arguments: EnvName     environment identifier
 8861 *            MS          modal context
 8862 *            Role1       role name
 8863 *            Role2       role name
 8864 *            Role        role name
 8865 * Role is Role1 iff Role1 is a direct father role of Role2
 8866 * or
 8867 * Role is Role2 iff Role2 is a direct father role of Role1
 8868 * otherwise
 8869 * the predicate fails
 8870 *
 8871 */
 8872
 8873testSonRole(Env,MS,Role1,Role2,Role) :-
 8874	roleHierarchy(Env,MS,Dag),
 8875	testSubElement(Role1,Role2,Role,Dag).
 8876
 8877/***********************************************************************
 8878 *
 8879 * getCommonFatherRoles(+EnvName,+MS,RL1,RL2)
 8880 * Arguments: EnvName  environment identifier
 8881 *            MS       modal context
 8882 *            RL1      list of role names
 8883 *            RL2      list of role names
 8884 * RL2 is the list of all role names subsuming all roles in RL1.
 8885 *
 8886 */
 8887
 8888getCommonFatherRoles(EnvName,MS,RL1,RL2) :-
 8889	hop_map(getAllFatherRoles,[EnvName,MS],RL1,RLL1),
 8890	intersection_motel(RLL1,RL2).
 8891
 8892/***********************************************************************
 8893 *
 8894 * getCommonSonRoles(+EnvName,+MS,RL1,RL2)
 8895 * Arguments: EnvName  environment identifier
 8896 *            MS       modal context
 8897 *            RL1      list of role names
 8898 *            RL2      list of role names
 8899 * RL2 is the list of all role names which are subsumed by all
 8900 * roles in RL1.
 8901 *
 8902 */
 8903
 8904getCommonSonRoles(EnvName,MS,RL1,RL2) :-
 8905	hop_map(getAllSonRoles,[EnvName,MS],RL1,RLL1),
 8906	intersection_motel(RLL1,RL2).
 8907
 8908/**********************************************************************
 8909 *
 8910 * @(#) revision.pl 1.27@(#)
 8911 *
 8912 */
 8913
 8914/**********************************************************************
 8915 *
 8916 * undefconcept(+MS,+CN)
 8917 *
 8918 */
 8919
 8920
 8921undefconcept(CN) :-
 8922	getCurrentEnvironment(EnvName),
 8923	undefconcept(EnvName,[],CN).
 8924
 8925undefconcept(EnvName,CN) :-
 8926	environment(EnvName,_,_),
 8927	!,
 8928	undefconcept(EnvName,[],CN).
 8929undefconcept(MS,CN) :-
 8930	(MS = [] ; MS = [_|_]),
 8931	!,
 8932	getCurrentEnvironment(EnvName),
 8933	undefconcept(EnvName,MS,CN).
 8934undefconcept(CN,CT) :-
 8935	getCurrentEnvironment(EnvName),
 8936	undefconcept(EnvName,[],CN,CT).
 8937
 8938undefconcept(EnvName,MS,CN) :-
 8939	environment(EnvName,Env,_),
 8940	(MS = [] ; MS = [_|_]),
 8941	!,
 8942	undefConcept(Env,MS,CN).
 8943undefconcept(MS,CN,CT) :-
 8944	(MS = [] ; MS = [_|_]),
 8945	!,
 8946	getCurrentEnvironment(EnvName),
 8947	undefconcept(EnvName,MS,CN,CT).
 8948undefconcept(EnvName,CN,CT) :-
 8949	undefconcept(EnvName,[],CN,CT).
 8950
 8951undefconcept(EnvName,MS,CN,CT) :-
 8952	environment(EnvName,Env,_),
 8953	
 8954	conceptEqualSets(Env,_user,MS,CN,CT,AX),
 8955	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 8956%	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 8957%	retractall_head(kb_in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 8958 	motel_retract_all(kb_in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 8959 	motel_retract_all(in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 8960 	motel_retract_all(query(Env,MS,CN,_CT,_PT,_)),
 8961	retractall_head(eq(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 8962	retractall_head(eq(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 8963	retractall_head(constraint(Env,rn(AX,_,_,_),_,_,_,_,_)),
 8964	retractall_head(constraint(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 8965	change_classifier(EnvName,MS,CN,CT),
 8966	retract(conceptEqualSets(Env,_user,MS,CN,CT,AX)),
 8967	!.
 8968
 8969undefConcept(Env,MS,CN) :-
 8970	conceptEqualSets(Env,user,_,CN,_,Ax),
 8971	
 8972	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 8973%	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_,_)),
 8974%	retractall_head(kb_in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_,_)),
 8975 	motel_retract_all(kb_in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 8976 	motel_retract_all(in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 8977   	motel_retract_all(query(Env,MS,CN,_CT,_PT,_)),
 8978	retractall_head(eq(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 8979	retractall_head(eq(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 8980	retractall_head(constraint(Env,rn(AX,_,_,_),_,_,_,_,_)),
 8981	retractall_head(constraint(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 8982	change_classifier(EnvName,MS,CN,CT),
 8983	retractall_head(conceptEqualSets(Env,user,MS,CN,_CT,Ax)),
 8984	fail,
 8985	!.
 8986undefConcept(_Env,_MS,_CN) :-
 8987	!.
 8988
 8989motel_retract_all(kb_in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,_)) :-
 8990	clause(kb_in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_)),_),
 8991	member(rn(AX,_,_,_),[Name]),	
 8992	retractall_head(kb_in(Env,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 8993	fail.
 8994motel_retract_all(kb_in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,_)).
 8995
 8996motel_retract_all(in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,_)) :-
 8997	clause(in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_)),_),
 8998	member(rn(AX,_,_,_),[Name]),	
 8999	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 9000	fail.
 9001motel_retract_all(in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,_)) :-
 9002	retractall_head(in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 9003	retractall_head(in(_Name1,rn(AX,_,_,_),_,_,_,_,_,_,_)).
 9004
 9005motel_retract_all(query(Env,MS,CN,_CT,_PT,_PT1)) :-
 9006	query(Env,MS,CN1,CT,PT,PT1),
 9007	collect(PT,Liste),
 9008	member(CN,Liste),
 9009	retractall_head(query(Env,MS,CN1,CT,PT,PT1)),
 9010	fail.
 9011motel_retract_all(query(Env,MS,CN,_CT,_PT,_PT1)).
 9012
 9013/**********************************************************************
 9014 *
 9015 * undefrole(+MS,+CN)
 9016 *
 9017 */
 9018undefrole(RN) :-
 9019	getCurrentEnvironment(EnvName),
 9020	undefrole(EnvName,[],RN).
 9021
 9022undefrole(EnvName,RN) :-
 9023	environment(EnvName,_,_),
 9024	!,
 9025	undefrole(EnvName,[],RN).
 9026undefrole(MS,RN) :-
 9027	(MS = [] ; MS = [_|_]),
 9028	!,
 9029	getCurrentEnvironment(EnvName),
 9030	undefrole(EnvName,MS,RN).
 9031undefrole(RN,RT) :-
 9032	getCurrentEnvironment(EnvName),
 9033	undefrole(EnvName,[],RN,RT).
 9034
 9035undefrole(EnvName,MS,RN) :-
 9036	environment(EnvName,Env,_),
 9037	(MS = [] ; MS = [_|_]),
 9038	!,
 9039	undefRole(Env,MS,RN).
 9040undefrole(MS,RN,RT) :-
 9041	(MS = [] ; MS = [_|_]),
 9042	!,
 9043	getCurrentEnvironment(EnvName),
 9044	undefrole(EnvName,MS,RN,RT).
 9045undefrole(EnvName,RN,RT) :-
 9046	undefrole(EnvName,[],RN,RT).
 9047undefrole(EnvName,MS,RN,RT) :-
 9048	environment(EnvName,Env,_),
 9049
 9050	roleEqualSets(Env,_user,MS,RN,RT,AX),
 9051	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 9052
 9053%	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 9054%	retractall_head(kb_in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 9055 	motel_retract_all(kb_in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 9056 	motel_retract_all(in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 9057 	motel_retract_all(query(Env,MS,CN,_CT,_PT,_)),
 9058	retractall_head(eq(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 9059	retractall_head(eq(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 9060	retractall_head(constraint(Env,rn(AX,_,_,_),_,_,_,_,_)),
 9061	retractall_head(constraint(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 9062	change_classifier(EnvName,MS,RN,RT),
 9063	retract(roleEqualSets(Env,_user,MS,RN,RT,AX)),
 9064	!.
 9065undefRole(Env,MS,RN) :-
 9066	roleEqualSets(Env,user,MS,RN,_,Ax),
 9067
 9068	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 9069	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_,_)),
 9070%	retractall_head(kb_in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_,_)),
 9071% 	motel_retract_all(query(Env,MS,RN,_RT,_PT,_)),
 9072 	motel_retract_all(kb_in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 9073 	motel_retract_all(in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 9074	retractall_head(eq(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 9075	retractall_head(eq(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 9076	retractall_head(constraint(Env,rn(AX,_,_,_),_,_,_,_,_)),
 9077	retractall_head(constraint(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 9078	change_classifier(EnvName,MS,RN,_),
 9079	retractall_head(roleEqualSets(Env,user,MS,RN,_RT,Ax)),
 9080	fail,
 9081	!.
 9082undefRole(_Env,_MS,_RN) :-
 9083	!.
 9084
 9085/**********************************************************************
 9086 *
 9087 * undefprimconcept(+MS,+CN)
 9088 *
 9089 */
 9090
 9091undefprimconcept(CN) :-
 9092	getCurrentEnvironment(EnvName),
 9093	undefprimconcept(EnvName,CN,_).
 9094undefprimconcept(CN,CT) :-
 9095	getCurrentEnvironment(EnvName),
 9096	!,
 9097	undefprimconcept(EnvName,[],CN,CT).
 9098
 9099undefprimconcept(EnvName,CN,CT) :-
 9100	environment(EnvName,_,_),
 9101	!,
 9102	undefprimconcept(EnvName,[],CN,CT).
 9103undefprimconcept(MS,CN,CT) :-
 9104	(MS = [] ; MS = [_|_]),
 9105	!,
 9106	getCurrentEnvironment(EnvName),
 9107	undefprimconcept(EnvName,MS,CN,CT).
 9108
 9109undefprimconcept(EnvName,MS,CN,CT) :-	
 9110	environment(EnvName,Env,_),
 9111
 9112	conceptSubsets(Env,_user,MS,CN,CT,AX),
 9113	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 9114%	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 9115%	retractall_head(kb_in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 9116 	motel_retract_all(kb_in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 9117 	motel_retract_all(in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 9118 	motel_retract_all(query(Env,MS,CN,_CT,_PT,_)),
 9119	retractall_head(eq(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 9120	retractall_head(eq(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 9121	retractall_head(constraint(Env,rn(AX,_,_,lInR),_,_,_,_,_)),
 9122	retractall_head(constraint(Env,rn(AX,_,_,lInR),_,_,_,_,_,_)),
 9123	change_classifier(EnvName,MS,CN,CT),
 9124	retract(conceptSubsets(Env,_user,MS,CN,CT,AX)),
 9125	!.
 9126/**********************************************************************
 9127 *
 9128 * undefprimrole(+MS,+CN)
 9129 *
 9130 */
 9131undefprimrole(RN) :-
 9132	getCurrentEnvironment(EnvName),
 9133	undefprimrole(EnvName,[],RN).
 9134
 9135undefprimrole(EnvName,RN) :-
 9136	environment(EnvName,_,_),
 9137	!,
 9138	undefprimrole(EnvName,[],RN).
 9139undefprimrole(MS,RN) :-
 9140	(MS = [] ; MS = [_|_]),
 9141	!,
 9142	getCurrentEnvironment(EnvName),
 9143	undefprimrole(EnvName,MS,RN).
 9144undefprimrole(RN,RT) :-
 9145	getCurrentEnvironment(EnvName),
 9146	undefprimrole(EnvName,[],RN,RT).
 9147
 9148undefprimrole(EnvName,MS,RN) :-
 9149	environment(EnvName,Env,_),
 9150	(MS = [] ; MS = [_|_]),
 9151	!,
 9152	undefprimRole(Env,MS,RN).
 9153undefprimrole(MS,RN,RT) :-
 9154	(MS = [] ; MS = [_|_]),
 9155	!,
 9156	getCurrentEnvironment(EnvName),
 9157	undefprimrole(EnvName,MS,RN,RT).
 9158undefprimrole(EnvName,RN,RT) :-
 9159	undefprimrole(EnvName,[],RN,RT).
 9160undefprimrole(EnvName,MS,RN,RT) :-
 9161	environment(EnvName,Env,_),
 9162
 9163	roleSubsets(Env,_user,MS,RN,RT,AX),
 9164	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 9165%	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 9166%	retractall_head(kb_in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 9167 	motel_retract_all(kb_in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 9168 	motel_retract_all(in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 9169 	motel_retract_all(query(Env,MS,RN,_RT,_PT,_)),
 9170	retractall_head(eq(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 9171	retractall_head(eq(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 9172	retractall_head(constraint(Env,rn(AX,_,_,lInR),_,_,_,_,_)),
 9173	retractall_head(constraint(Env,rn(AX,_,_,lInR),_,_,_,_,_,_)),
 9174	change_classifier(EnvName,MS,RN,RT),
 9175	retract(roleSubsets(Env,_user,MS,RN,RT,AX)),
 9176	!.
 9177undefprimRole(Env,MS,RN) :-
 9178	roleSubsets(Env,user,MS,RN,_,Ax),
 9179
 9180	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 9181	retractall_head(in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_,_)),
 9182	retractall_head(kb_in(Env,rn(AX,_,_,_),_,_,_,_,_,_,_,_)),
 9183 	motel_retract_all(query(Env,MS,RN,_RT,_PT,_)),
 9184 	motel_retract_all(kb_in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 9185 	motel_retract_all(in(Env,_Name1,rn(AX,_,_,_),_,_,_,_,_,_,proved(in([],Name,_,_),_))),
 9186	retractall_head(eq(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 9187	retractall_head(eq(Env,rn(AX,_,_,_),_,_,_,_,_,_,_)),
 9188	retractall_head(constraint(Env,rn(AX,_,_,_),_,_,_,_,_)),
 9189	retractall_head(constraint(Env,rn(AX,_,_,_),_,_,_,_,_,_)),
 9190	change_classifier(EnvName,MS,RN,_),
 9191	retractall_head(roleSubsets(Env,user,MS,RN,_RT,Ax)),
 9192	fail,
 9193	!.
 9194
 9195/*  ist in arbeit    */
 9196
 9197
 9198delete_ind(X,C) :-
 9199	completeParameter([(X,C)],EnvName,MS,_,_),
 9200	delete_ind(EnvName,MS,X,C).
 9201delete_ind(EnvName,X,C) :-
 9202	environment(EnvName,_,_),
 9203	!,
 9204	delete_ind(EnvName,[],X,C).
 9205delete_ind(MS,X,C) :-
 9206	(MS = []; MS = [_|_]),
 9207	!,
 9208	getCurrentEnvironment(EnvName),
 9209	delete_ind(EnvName,MS,X,C).
 9210delete_ind(X,Y,R) :-
 9211	getCurrentEnvironment(EnvName),
 9212	delete_ind(EnvName,[],X,Y,R).
 9213delete_ind(EnvName,MS,X,C) :-
 9214	environment(EnvName,Env,_),
 9215	nonvar(MS),
 9216	(MS = [] ; MS = [_|_]),
 9217	!,
 9218	atomic(X),
 9219	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 9220	constructMLHead(Env,rn(_,_,user,lInR),W1,C,X,_HYPS,_D,_CALLS,abox,InHead),
 9221	not(not((retract((InHead :- call(user:G1))) ;
 9222 	 retract((InHead :- call(G1)))))),
 9223	not(not((retract((conceptElement(Env,_,W1,_,X,C,_) :- call(user:G1))) ;
 9224	 retract((conceptElement(Env,_,W1,_,X,C,_) :- call(G1)))))),
 9225	 retractall_head((InHead :- call(user:G1))),
 9226	 retractall_head((InHead :- call(G1))),
 9227	 retractall_head((conceptElement(Env,_,W1,_,X,C,_) :- call(user:G1))),
 9228	 retractall_head((conceptElement(Env,_,W1,_,X,C,_) :- call(G1))).
 9229delete_ind(P1,X,Y,R) :-
 9230	completeParameter([(X,Y,R)],EnvName,MS,_,_),
 9231	delete_ind(EnvName,MS,X,Y,R).
 9232delete_ind(EnvName,MS,X,Y,R) :-
 9233	environment(EnvName,Env,_),
 9234	atomic(X),
 9235	atomic(Y),
 9236	atomic(R),
 9237	Role1 =.. [R,X,Y],
 9238	retract(Role1),
 9239	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 9240	constructEqHead(Env,rn(_,_,user,lInR),W1,Y,_FF,R,X,_,_D,CALLS,abox,EqLiteral),
 9241	not(not((retract((EqLiteral :- (cCS(CALLS,true), call(user:G1))));
 9242	 retract((EqLiteral :- (cCS(CALLS,true), call(G1))))))),
 9243	not(not((retract((roleElement(Env,_,W1,X,Y,R,_) :- call(user:G1))) ;
 9244	 retract((roleElement(Env,_,W1,X,Y,R,_) :- call(G1)))))),
 9245	retractall_head((EqLiteral :- (cCS(CALLS,true), call(user:G1)))),
 9246	retractall_head((EqLiteral :- (cCS(CALLS,true), call(G1)))),
 9247	retractall_head((roleElement(Env,_,W1,X,Y,R,_) :- call(user:G1))),
 9248	retractall_head((roleElement(Env,_,W1,X,Y,R,_) :- call(G1))).
 9249
 9250	
 9251/***
 9252*    change_classifier veraendert die Sub.Hierarchie nach undefconcept      
 9253***/
 9254change_classifier(CN,CT) :-
 9255	change_classifier([],CN,CT),
 9256	!.
 9257change_classifier(MS,CN,CT) :-
 9258	getCurrentEnvironment(EnvName),
 9259	environment(EnvName,Env,_),
 9260	change_classifier(EnvName,MS,CN,CT),
 9261	!.
 9262
 9263change_classifier(EnvName,MS,CN,CT) :-
 9264	environment(EnvName,Env,_),
 9265	getConceptName(Env,[],CN),
 9266	getAllSubConcepts(EnvName,MS,CN,CL1),
 9267	find_definition(CN,CL2),
 9268 	union1(CL1,CL2,CL),print(CL),
 9269	change_classifier1(Env,MS,CN,CT,CL),
 9270	delete_hierarchy(concepts,Env,MS,CN),
 9271	change_classifier2(Env,MS,CT,CL),
 9272	!.
 9273change_classifier(EnvName,MS,RN,CT) :-
 9274	environment(EnvName,Env,_),
 9275	getRoleName(Env,[],RN),
 9276	getAllSonRoles(EnvName,MS,RN,CL1),
 9277	find_definition(RN,CL2),
 9278 	union1(CL1,CL2,CL),print(CL),
 9279	change_classifier1(Env,MS,RN,CT,CL),
 9280	delete_hierarchy(roles,Env,MS,RN),
 9281	change_classifier2(Env,MS,CT,CL),
 9282	!.
 9283change_classifier(EnvName,MS,CN,CT).
 9284
 9285change_classifier1(Env,MS,CR,CT,[]) :-
 9286	!.
 9287change_classifier1(Env,MS,CR,CT,[H|T]) :-
 9288	getConceptName(Env,MS,H),
 9289	(H \== 'top',H \== 'bot'),
 9290	delete_hierarchy(concepts,Env,MS,H),
 9291	change_classifier1(Env,MS,CR,CT,T),
 9292	!.
 9293change_classifier1(Env,MS,CR,CT,[H|T]) :-
 9294	getRoleName(Env,MS,H),
 9295	(H \== 'top',H \== 'bot'),
 9296	delete_hierarchy(roles,Env,MS,H),
 9297	change_classifier1(Env,MS,CR,CT,T),
 9298	!.
 9299change_classifier1(Env,MS,CR,CT,[H|T]) :-
 9300	change_classifier1(Env,MS,CR,CT,T),
 9301	!.
 9302
 9303change_classifier2(Env,MS,CT,[]) :-
 9304	!.
 9305change_classifier2(Env,MS,CT,[H|T]) :-
 9306	getConceptName(Env,MS,H),
 9307	not(make_succ2(concepts,Env,MS,H)),
 9308	change_classifier2(Env,MS,CT,T),
 9309	!.
 9310change_classifier2(Env,MS,CT,[H|T]) :-
 9311	getRoleName(Env,MS,H),
 9312	not(make_succ2(roles,Env,MS,H)),
 9313	change_classifier2(Env,MS,CT,T),
 9314	!.
 9315change_classifier2(Env,MS,CT,[H|T]) :-
 9316	change_classifier2(Env,MS,CT,T),
 9317	!.
 9318delete_hierarchy(Type,Env,MS,CR) :-
 9319        clause(succ(Type,Env,MS,CR,SC),_),
 9320	clause(succ(Type,Env,MS,PC,CR),_),
 9321	subsume1(Type,Env,MS,PC,SC),
 9322	assert_succ(Type,Env,MS,PC,SC),
 9323	fail.
 9324delete_hierarchy(Type,Env,MS,CR) :-
 9325	retractall_head(succ(Type,Env,MS,CR,_)),
 9326	retractall_head(succ(Type,Env,MS,_,CR)),
 9327	retractall_head(sub(Type,Env,MS,CR,_)),
 9328	retractall_head(sub(Type,Env,MS,_,CR)),
 9329	retractall_head(nsub(Type,Env,MS,CR,_)),
 9330	retractall_head(nsub(Type,Env,MS,_,CR)),
 9331	!.
 9332	
 9333/*****************************************************************************
 9334*      find_definition   sucht alle concepte die irgendwie mit dem 	     *
 9335* 			  zu loeschenden concept verbunden sind.             *
 9336*****************************************************************************/
 9337
 9338find_definition(Concept,Liste) :-
 9339	find_definition([],Concept,Liste).
 9340find_definition(MS,Concept,Liste) :-
 9341	getCurrentEnvironment(EnvName),
 9342	environment(EnvName,Env,_),
 9343	find_definition(Env,MS,Concept,Liste),
 9344	!.
 9345find_definition(Env,MS,Concept,Liste) :-
 9346	getConceptName(Env,MS,Concept),
 9347	find_conceptEqualSets(Env,_user,MS,Concept,CT1,_),
 9348	collect(CT1,List1),
 9349	find_conceptSubsets(Env,_user,MS,Concept,CT2,_),
 9350	collect(CT2,List2),
 9351	union1(List1,List2,List3),
 9352	find_Def_concept(MS,Concept,List_of_Concepts),
 9353	union1(List3,List_of_Concepts,Liste),
 9354	!.
 9355find_definition(Env,MS,Role,Liste) :-
 9356	getRoleName(Env,MS,Role),
 9357	find_roleEqualSets(Env,_user,MS,Role,CT1,_),
 9358	collect(CT1,List1),
 9359	find_roleSubsets(Env,_user,MS,Role,CT2,_),
 9360	collect(CT2,List2),
 9361	union1(List1,List2,List3),
 9362	find_Def_role(MS,Role,List_of_Roles),
 9363	union1(List3,List_of_Roles,Liste),
 9364	!.
 9365
 9366find_conceptEqualSets(Env,_user,MS,Concept,CT1,_) :-
 9367	conceptEqualSets(Env,_user,MS,Concept,CT1,_),
 9368	!.
 9369find_conceptEqualSets(Env,_user,MS,Concept,[],_).
 9370find_conceptSubsets(Env,_user,MS,Concept,CT2,_) :-
 9371	conceptSubsets(Env,_user,MS,Concept,CT2,_),
 9372	!.
 9373find_conceptSubsets(Env,_user,MS,Concept,[],_).
 9374
 9375find_roleEqualSets(Env,_user,MS,Role,CT1,_) :-
 9376	roleEqualSets(Env,_user,MS,Role,CT1,_),
 9377	!.
 9378find_roleEqualSets(Env,_user,MS,Role,[],_).
 9379find_roleSubsets(Env,_user,MS,Role,CT2,_) :-
 9380	roleSubsets(Env,_user,MS,Role,CT2,_),
 9381	!.
 9382find_roleSubsets(Env,_user,MS,Role,[],_).
 9383
 9384
 9385find_Def_concept(MS,Concept,List_of_Concepts) :-
 9386	getCurrentEnvironment(EnvName),
 9387	environment(EnvName,Env,_),
 9388	find_Def_concept(Env,MS,Concept,List_of_Concepts).
 9389% Removed redundant code 
 9390% 15.02.93 uh
 9391% find_Def_concept(MS,Concept,List_of_Concepts) :-
 9392%	getCurrentEnvironment(EnvName),
 9393%	environment(EnvName,Env,_),
 9394%	find_Def_concept(Env,MS,Concept,List_of_Concepts).
 9395find_Def_concept(Env,MS,Concept,List_of_Concepts) :-
 9396	setofOrNil(CN,find_Def_concept1(Env,MS,Concept,CN),List_of_Concepts),
 9397	!.
 9398
 9399find_Def_concept1(Env,MS,Concept,CN) :-
 9400	conceptEqualSets(Env,_user,MS,CN,CT,AX),
 9401	atom(CN),
 9402	not(name(CN,[99,111,110,99,101,112,116|_])),	
 9403	collect(CT,Liste),
 9404	member(Concept,Liste).
 9405find_Def_concept1(Env,MS,Concept,CN) :-
 9406	conceptSubsets(Env,_user,MS,CN,CT,AX),
 9407	atom(CN),
 9408	not(name(CN,[99,111,110,99,101,112,116|_])),
 9409	collect(CT,Liste),
 9410	member(Concept,Liste).
 9411find_Def_role(MS,Role,List_of_Roles) :-
 9412	getCurrentEnvironment(EnvName),
 9413	environment(EnvName,Env,_),
 9414	find_Def_role(Env,MS,Role,List_of_Roles).
 9415find_Def_role(Env,MS,Role,List_of_Roles) :-
 9416	setofOrNil(CN,find_Def_role1(Env,MS,Role,CN),List_of_Roles),
 9417	!.
 9418find_Def_role1(Env,MS,Role,CN) :-
 9419	roleEqualSets(Env,_user,MS,CN,CT,AX),
 9420	atom(CN),
 9421	not(name(CN,[99,111,110,99,101,112,116|_])),	
 9422	collect(CT,Liste),
 9423	member(Role,Liste).
 9424find_Def_role1(Env,MS,Role,CN) :-
 9425	roleSubsets(Env,_user,MS,CN,CT,AX),
 9426	atom(CN),
 9427	not(name(CN,[99,111,110,99,101,112,116|_])),
 9428	collect(CT,Liste),
 9429	member(Role,Liste).
 9430/****************************************************************************/
 9431/*       collect      sammelt aus einer bel.verschachtelten kette von      
 9432*/
 9433/*                    ausdruecken alle Concept'e oder Role's .             
 9434*/
 9435/***************************************************************************/	
 9436
 9437collect(CT,Liste) :-
 9438	collect1(CT,L),
 9439	collect2(L,Liste),
 9440	!.
 9441collect2([],[]).
 9442collect2([H|T],[H|L]) :-
 9443	currentEnvironment(Env),
 9444	nonvar(H),
 9445	(clause(conceptName(Env,_,_,H),_);clause(roleName(Env,_,_,H),_)),
 9446	collect2(T,L),
 9447	!.
 9448collect2([H|T],L) :-
 9449	collect2(T,L),
 9450	!.
 9451collect1([],L) :-
 9452	!.
 9453collect1([X|R],L) :-
 9454	(atom(X);number(X)),
 9455	collect1(R,L1),
 9456	union1(X,L1,L),
 9457	!.
 9458collect1([[X1|R1]|R],L) :-
 9459	(atom(X1);number(X1)),
 9460	collect1(R1,L1),
 9461	collect1(R,L2),
 9462	union1(L1,L2,L3),
 9463	union1(L3,X1,L),
 9464	!.
 9465collect1([[X1|R1]|R],L) :-
 9466	X1 =.. X2,
 9467	collect1(X2,L2),
 9468	collect1(R1,L1),
 9469	collect1(R,L3),
 9470	union1(L1,L2,L4),
 9471	union1(L4,L3,L),
 9472	!.
 9473collect1([X|R],L3) :-
 9474	X =.. X1,
 9475	collect1(X1,L1),
 9476	collect1(R,L2),
 9477	union1(L1,L2,L3),
 9478	!.
 9479collect1(X,[X|L]) :-
 9480	(atom(X);number(X)),
 9481	currentEnvironment(Env),
 9482	(clause(conceptName(Env,_,_,X),_);clause(roleName(Env,_,_,X),_)),
 9483	!.
 9484
 9485collect1(X,[L|L1]) :-
 9486	X =.. X1,
 9487	collect1(X1,L1),!.
 9488/**********************************************************************
 9489 *
 9490 * @(#) sb2.pl 1.20@(#)
 9491
 9492 *
 9493 */
 9494
 9495/*-------------------------------------------------------------------------------
 9496 * Der Konstruktor: sb_defenv(EName,Comment)
 9497 * generiert ein neues Environment und bindet dies ans uebergebene Symbol EName,
 9498 * EName wird also zu current environment.
 9499 * Comment kann irgendein string sein.
 9500 *-----------------------------------------*/
 9501
 9502  
 9503  sb_defenv(EName,Comment):- makeEnvironment(EName,Comment).
 9504
 9505
 9506/*-------------------------------------------------------------------------------
 9507 * sb_initenv
 9508 * loescht den Inhalt eines environment, und initialisiert den in sb_defenv schon
 9509 * definierten EName als Tbox-Environment.
 9510 *----------------------------------------*/
 9511
 9512 sb_initenv:- 
 9513	initEnvironment.
 9514 sb_initenv(EnvName) :- 
 9515	initEnvironment(EnvName).
 9516
 9517
 9518/*-------------------------------------------------------------------------------
 9519 * Der 4-stellige Hilfskonstruktor make__primconcept mit folgenden Parametern:
 9520 * (EnvName,MS,CName1,supers(Liste_von_Konzepte))  
 9521 * definiert ein Konzept CName1 als Subkonzept von den in der Liste auftretenden 
 9522 * Superkonzepten in environment EnvName und modal context MS.
 9523 *-------------------------------------------*/
 9524
 9525
 9526make_primconcept(EnvName,MS,CName1,supers([])).
 9527make_primconcept(EnvName,MS,CName1,supers([X|T])):-
 9528		  defprimconcept(EnvName,MS,CName1,X),
 9529		  make_primconcept(EnvName,MS,CName1,supers(T)).
 9530
 9531/*------------------------------------------------------------------------------
 9532 * make_primconcept(EnvName,MS,CName1,restr-inh(RName1,restricts(RName2,
 9533 *                                                     range(CName2,CNameDef))))
 9534 * 
 9535 * definiert ein Konzept CName1, an dem eine Rolle RName2  auf den Rollenfueller
 9536 * CName2 (range) restrigniert wird.
 9537 * Und zwar in evironment=EnvName und modal context MS. 
 9538 * Der neuen Rolle wird das Symbol RName1 zugewiesen,
 9539 * die Domain-Menge dieser neuen Rolle (CNameDom) ist gerade eine Teilmenge von 
 9540 * CName1.
 9541 *----------------------------------------*/
 9542make_primconcept(EnvName,MS,CName1,
 9543                'restr-inh'(RName1,restricts(RName2,range(CName2,CNameDef)))):-
 9544	make_primconcept(EnvName,MS,CName1,
 9545	                 restrict_inh(RName1, restricts(RName2 ,
 9546				 		        range(CName2,CNameDef)))).
 9547make_primconcept(EnvName,MS,CName1,
 9548                'restrict-inh'(RTerm1,restricts(RTerm2,range(CName2,CNameDef)))):-
 9549       make_primconcept(EnvName,MS,CName1,
 9550                        restrict_inh(RTerm1, restricts(RTerm2,
 9551						       range(CName2,CNameDef)))).
 9552
 9553make_primconcept(EnvName,MS,CName1,restrict_inh(RTerm1, restricts(RTerm2 ,
 9554						     range(CName2,CNameDef)))):-
 9555	expand_role(EnvName,MS,RTerm1,RName1,_,_,_),
 9556	expand_role(EnvName,MS,RTerm2,RName2,_,_,_),
 9557	environment(EnvName,Env,_),
 9558	defrole(EnvName,MS,RName1 , restr(RName2 , CName2)),
 9559	gensym(concept,CNameDom),
 9560	defconcept(EnvName,MS,CNameDom ,some(RName2 ,'top')),
 9561        defprimconcept(EnvName,MS,CNameDom ,CName1),
 9562%	defprimconcept(EnvName,MS,and([some(inverse(RName1),'top'),
 9563%				       naf(not(CNameDef))]),CNameDef),
 9564	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 9565	assertz_logged((roleDomain(Env,W1,RName1,CNameDom) :- G1)),
 9566	assertz_logged((roleRange(Env,W1,RName1,CName2) :- G1)),
 9567	assertz_logged((roleDefault(Env,W1,RName1,CNameDef) :- G1)),
 9568	assertz_logged((roleTripel(Env,W1,RName1,CNameDom,CName2,CNameDef))).
 9569   
 9570/*----------------------------------------------------------------------------
 9571 * make_primconcept(EnvName,MS,CName1, nr(RName1,MinNr,MaxNr,DefNr))
 9572 * definiert ein Konzept CName1, an dem eine "number restriction" stattfindet:
 9573 * die Rolle RName1 wird bzgl. ihrer Kardinalitaet restrigniert auf
 9574 * (MinNr,MaxNr,DefNr),
 9575 * und zwar in  environment=EnvName und modal context=MS,
 9576 * die Dom-Menge der Rolle RName1 ist dann ein CNameDom als Teilmenge von CName1.
 9577 *-----------------------------------------*/
 9578
 9579
 9580make_primconcept(EnvName,MS,CName1 , nr(RTerm1, MinNr,MaxNr,DefNr)):-
 9581	environment(EnvName,Env,_),
 9582	expand_role(EnvName,MS,RTerm1,RName1,CNameDomT,CName2T,CNameDefT),
 9583	gensym(concept,CNameDom),
 9584	defconcept(EnvName,MS,CNameDom, and([atleast(MinNr,RName1),atmost(MaxNr,RName1)])),
 9585	defconcept(EnvName,MS,CNameDom, some(RName1,'top')), 
 9586	defprimconcept(EnvName,MS,CNameDom , CName1),
 9587%	gensym(concept,CNameDef),
 9588%	defconcept(EnvName,MS,CNameDef, and([atleast(DefNr,RName1),atmost(DefNr,RName1)])),
 9589%	defprimconcept(EnvName,MS,and([some(inverse(RName1)),naf(not(CNameDef))]),CNameDef),
 9590	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 9591	assertz_logged((roleNr(Env,W1,RName1,MinNr,MaxNr) :- G1)),
 9592	assertz_logged((roleDefNr(Env,W1,RName1,DefNr) :- G1)),
 9593	assertz_logged((roleAll(Env,W1,Rname1,CNameDomT,CName2T,CNameDefT,MinNr,MaxNr,DefNr))).
 9594
 9595
 9596
 9597/*------------------------------------------------------------------------------
 9598 *sb_primconcept(CName)
 9599 * definiert ein generelles Konzept CName in modal context [].
 9600 *---------------------------------------*/
 9601
 9602
 9603 sb_primconcept(CName):- 
 9604	getCurrentEnvironment(EnvName),
 9605	sb_primconcept(EnvName,[],CName).
 9606
 9607
 9608/*-------------------------------------------------------------------------------
 9609 * Der 2-stellige Konstruktor sb_primconcept mit jeweils folgenden Parametern:
 9610 * (EnvName,CName1), oder (MS,CName1) definiert ein generelles Konzept CName1
 9611 * in invironment EnvName und modal context [], bzw. in current Environment und
 9612 * modal context MS.
 9613 *----------------------------------------*/
 9614  
 9615sb_primconcept(EnvName,Left) :-
 9616	environment(EnvName,_,_),
 9617	!,
 9618	sb_primconcept(EnvName,[],Left),
 9619	!.
 9620
 9621
 9622sb_primconcept(MS,Left) :-
 9623	nonvar(MS),
 9624	(MS = [] ; MS = [_|_]),
 9625	!,
 9626	getCurrentEnvironment(EnvName),
 9627	sb_primconcept(EnvName,MS,Left),
 9628	!.
 9629
 9630
 9631
 9632/*-----------------------------------------------------------------------------
 9633 * Der 2-stellige Konstruktor sb_primconcept(CName1,SpecListe) hat als Argumente ein
 9634 * CName1 und eine Liste von bis jetzt eingef"uhrten Pr"adikaten wie:
 9635 * [supers(Liste von concepts) , restr-inh(...) , nr(...)], es werden dann jeweils die
 9636 * entsprechenden Hilfskonstruktoren mit dem einzelnen Listenelement als aktueller Parameter 
 9637 * aktiviert bzw. aufgerufen und zwar in current environment und [] als modal context.
 9638 *-----------------------------------------*/
 9639
 9640sb_primconcept(CName1,[]) :-
 9641	!.
 9642sb_primconcept(CName1,[X|T]):-
 9643	getCurrentEnvironment(EnvName),
 9644	sb_primconcept(EnvName,[],CName1,[X|T]),
 9645	!.
 9646
 9647
 9648
 9649/*-----------------------------------------------------------------------------
 9650 * Der 3-stellige Konstruktor sb_primconcept(A,CName1,SpecListe) funktioniert analog
 9651 * nur in A= Environment und [] als modal context, bzw. in current environment und
 9652 * A=MS als modal context.
 9653 *-----------------------------------------*/
 9654
 9655sb_primconcept(A,CName1,[]) :-
 9656	!.
 9657sb_primconcept(A,CName1,[X|T]):-
 9658	environment(A,_,_),
 9659	!,
 9660	sb_primconcept(A,[],CName1,[X|T]),
 9661	!.
 9662sb_primconcept(A,CName1,[X|T]):-
 9663	(A = [] ; A = [_|_]),
 9664	getCurrentEnvironment(EnvName),
 9665	!,
 9666	sb_primconcept(EnvName,A,CName1,[X|T]).
 9667
 9668/*-------------------------------------------------------------------------------
 9669 * Der 3-stellige Konstruktor von  sb_primconcept mit folgenden Parametern:
 9670 * (Environment,MS,CName1)  
 9671 * definiert ein Konzept CName1 in environment EnvName und modal context MS. 
 9672 *-------------------------------------------*/
 9673
 9674sb_primconcept(EnvName,MS,CName):-
 9675	   environment(EnvName,Env,_),
 9676	   (MS = [] ; MS = [_|_]),
 9677	   defprimconcept(EnvName,MS,CName),
 9678	   !.
 9679
 9680/*-----------------------------------------------------------------------------
 9681 * Der 4-stellige Konstruktor sb_primconcept(EnvName,MS,CName1,SpecListe) wird aktiviert 
 9682 * mit expliziter Angabe von Environment=EnvName und modal context =MS.
 9683 *-----------------------------------------*/
 9684
 9685sb_primconcept(EnvName,MS,CName1,[]) :-
 9686	!.
 9687sb_primconcept(EnvName,MS,CName1,[X|T]):-
 9688	make_primconcept(EnvName,MS,CName1,X),
 9689        sb_primconcept(EnvName,MS,CName1,T),
 9690	!.
 9691
 9692/*------------------------------------------------------------------------------
 9693 * make_defconcept(EnvName,MS,CName1,restr-inh(RName1,restricts(RName2,
 9694 *                                          range(CName2,CNameDef))),CNameDom)
 9695 * 
 9696 * definiert ein Konzept CName1, an dem eine Rolle RName2  auf den Rollenfueller
 9697 * CName2 (range) restrigniert wird.
 9698 * Und zwar in evironment=EnvName und modal context MS. 
 9699 * Der neuen Rolle wird das Symbol RName1 zugewiesen,
 9700 * die Domain-Menge dieser neuen Rolle (CNameDom) ist gerade eine Teilmenge von 
 9701 * CName1.
 9702 *----------------------------------------*/
 9703
 9704make_defconcept(EnvName,MS,CName1,restrict_inh(RName1, restricts(RName2 ,
 9705				     range(CName2,CNameDef))),CNameDom):-
 9706	make_defconcept(EnvName,MS,CName1,'restr-inh'(RName1, restricts(RName2 ,
 9707                        range(CName2,CNameDef))),CNameDom).
 9708make_defconcept(EnvName,MS,CName1,'restrict-inh'(RName1, restricts(RName2 ,
 9709				     range(CName2,CNameDef))),CNameDom):-
 9710	make_defconcept(EnvName,MS,CName1,'restr-inh'(RName1, restricts(RName2 ,
 9711                        range(CName2,CNameDef))),CNameDom).
 9712
 9713make_defconcept(EnvName,MS,CName1,'restr-inh'(RName1, restricts(RName2 ,
 9714				     range(CName2,CNameDef))),CNameDom):-
 9715	environment(EnvName,Env,_),
 9716	defrole(EnvName,MS,RName1 , restr(RName2 , CName2)),
 9717	gensym(concept,CNameDom),
 9718	defconcept(EnvName,MS,CNameDom ,some(RName2 ,'top')),
 9719%	defprimconcept(EnvName,MS,and([some(inverse(RName1),'top'),
 9720%				       naf(not(CNameDef))]),CNameDef),
 9721	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 9722	assertz_logged((roleDomain(Env,MS,RName1,CNameDom) :- G1)),
 9723	assertz_logged((roleRange(Env,MS,RName1,CName2) :- G1)),
 9724	assertz_logged((roleDefault(Env,MS,RName1,CNameDef) :- G1)),
 9725	assertz_logged((roleTripel(Env,MS,RName1,CNameDom,CName2,CNameDef))).
 9726    
 9727/*----------------------------------------------------------------------------
 9728 * make_defconcept(EnvName,MS,CName1, nr(RName1,MinNr,MaxNr,DefNr),CNameDom)
 9729 * definiert ein Konzept CName1, an dem eine "number restriction" stattfindet:
 9730 * die Rolle RName1 wird bzgl. ihrer Kardinalitaet restrigniert auf
 9731 * (MinNr,MaxNr,DefNr),
 9732 * und zwar in  environment=EnvName und modal context=MS.
 9733 *-----------------------------------------*/
 9734
 9735
 9736make_defconcept(EnvName,MS,CName1 , nr(RTerm, MinNr,MaxNr,DefNr),CNameDom):-
 9737	environment(EnvName,Env,_),
 9738	expand_role(EnvName,MS,RTerm,RName1,CNameDomT,CNameT,CNameDefT),
 9739	gensym(concept,CNameDom),
 9740	defconcept(EnvName,MS,CNameDom, and([atleast(MinNr,RName1),atmost(MaxNr,RName1)])),
 9741%	defconcept(EnvName,MS,CNameDom, some(RName1,'top')), 
 9742%	gensym(concept,CNameDef),
 9743%	defconcept(EnvName,MS,CNameDef, and([atleast(DefNr,RName1),atmost(DefNr,RName1)])),
 9744%	defprimconcept(EnvName,MS,and([some(inverse(RName1)),naf(not(CNameDef))]),CNameDef),
 9745	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 9746	assertz_logged((roleNr(Env,W1,RName1,MinNr,MaxNr) :- G1)),
 9747	assertz_logged((roleDefNr(Env,W1,RName1,DefNr) :- G1)),
 9748	assertz_logged((roleAll(Env,W1,RName1,CNameDomT,CNameT,CNameDefT,MinNr,MaxNr,DefNr) :- G1)).
 9749
 9750make_defconcept(EnvName,MS,CName1 , necres(RTerm, nec),CNameDom):-
 9751	!,
 9752	environment(EnvName,Env,_),
 9753	expand_role(EnvName,MS,RTerm,RName1,_,_,_),
 9754	gensym(concept,CNameDom),
 9755	defconcept(EnvName,MS,CNameDom,atleast(1,RName1)),
 9756	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 9757	assertz_logged((roleDomain(Env,W1,RName1,CNameDom) :- G1)),
 9758	!.
 9759make_defconcept(EnvName,MS,CName1 , necres(RTerm, _),CNameDom):-
 9760	!,
 9761	environment(EnvName,Env,_),
 9762	expand_role(EnvName,MS,RTerm,RName1,_,_,_),
 9763	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 9764	assertz_logged((roleDomain(Env,W1,RName1,CNameDom) :- G1)),
 9765	!.
 9766
 9767
 9768expand_role(_,_,RTerm,RTerm,_,_,_) :-
 9769	atomic(RTerm),
 9770	!.
 9771expand_role(EnvName,MS1,RTerm,RName1,CName1,CName2,CNameDef) :-
 9772	RTerm = sb_primelemrole(EnvName,MS2,RName1, 'domain-range'(CName1,CName2,CNameDef)),
 9773	!,
 9774	append(MS1,MS2,MS3),
 9775	sb_primelemrole(EnvName,MS3,RName1, 'domain-range'(CName1,CName2,CNameDef)).
 9776expand_role(EnvName,MS1,RTerm,RName1,CName1,CName2,CNameDef) :-
 9777	RTerm = sb_primelemrole(MS2,RName1, 'domain-range'(CName1,CName2,CNameDef)),
 9778	!,
 9779	append(MS1,MS2,MS3),
 9780	sb_primelemrole(MS3,RName1, 'domain-range'(CName1,CName2,CNameDef)).
 9781expand_role(EnvName,MS1,RTerm,RName1,CName1,CName2,CNameDef) :-
 9782	RTerm = sb_primelemrole(RName1, 'domain-range'(CName1,CName2,CNameDef)),
 9783	!,
 9784	sb_primelemrole(MS1,RName1, 'domain-range'(CName1,CName2,CNameDef)).
 9785expand_role(_,_,RTerm,RTerm,_,_,_).
 9786
 9787
 9788/*-----------------------------------------------------------------------------
 9789 * elem(I,Liste,H) nimmt das I-te Element einer Liste und sieht so aus:
 9790 *-----------------------------------------*/
 9791
 9792
 9793elem(1,[H|T],H).
 9794elem(I,[H|T],X):- Hilf is I-1,
 9795		  elem(Hilf,T,X).
 9796
 9797
 9798/*-----------------------------------------------------------------------------
 9799 * Der 2-stellige Konstruktor sb_defconcept(CName1,SpecListe) hat als Argumente ein
 9800 * CName1 und eine Liste von bis jetzt eingef"uhrten Pr"adikaten wie:
 9801 * [supers(Liste von concepts) , restr-inh(...) , nr(...)], es werden dann jeweils die
 9802 * entsprechenden Hilfskonstruktoren mit dem einzelnen Listenelement als aktueller Parameter 
 9803 * aktiviert bzw. aufgerufen und zwar in current environment und [] als modal context.
 9804 *-----------------------------------------*/
 9805
 9806sb_defconcept(C1,SpecList):- 
 9807	getCurrentEnvironment(EnvName),
 9808	sb_defconcept(EnvName,[],C1,SpecList),
 9809	!.
 9810
 9811/*-----------------------------------------------------------------------------
 9812 * Der 3-stellige Konstruktor sb_defconcept(A,CName1,SpecListe) funktioniert analog
 9813 * nur in A= Environment und [] als modal context, bzw. in current environment und
 9814 * A=MS als modal context.
 9815 *-----------------------------------------*/
 9816
 9817
 9818sb_defconcept(A,C1,SpecList) :- 
 9819	environment(A,_,_),
 9820	!,
 9821	sb_defconcept(A,[],C1,SpecList),
 9822	!.
 9823sb_defconcept(A,C1,SpecList) :-
 9824	(A = [] ; A = [_|_]),
 9825	!,
 9826	getCurrentEnvironment(EnvName),
 9827	sb_defconcept(EnvName,A,C1,SpecList),
 9828	!.
 9829
 9830/*-----------------------------------------------------------------------------
 9831 * Der 4-stellige Konstruktor sb_defconcept(EnvName,MS,CName1,SpecListe) wird aktiviert 
 9832 * mit expliziter Angabe von Environment=EnvName und modal context =MS.
 9833 *-----------------------------------------*/
 9834
 9835
 9836sb_defconcept(EnvName,MS,C1,SpecList) :-
 9837	sb_defconcept(EnvName,MS,C1,SpecList,[]).
 9838
 9839sb_defconcept(EnvName,MS,C1,[],[]) :-
 9840	!.
 9841sb_defconcept(EnvName,MS,C1,[],CList) :-
 9842	!,
 9843	defconcept(EnvName,MS,C1,and(CList)),
 9844	!.
 9845sb_defconcept(EnvName,MS,C1,[supers(L)|SpecList],CList1) :-
 9846	!,
 9847	append(L,CList1,CList2),
 9848	sb_defconcept(EnvName,MS,C1,SpecList,CList2).
 9849sb_defconcept(EnvName,MS,C1,[Spec1|SpecList],CList1) :-
 9850	make_defconcept(EnvName,MS,C1,Spec1,Concept),
 9851	sb_defconcept(EnvName,MS,C1,SpecList,[Concept|CList1]).
 9852sb_defconcept(EnvName,MS,C1,supers(L),[]) :-
 9853	!,
 9854	defconcept(EnvName,MS,C1,and(L)).
 9855
 9856/*---------------------------------------------------------------------------
 9857 * sb_primelemrole(RName1,domain-range(CName1,CName2,CNameDef))
 9858 * definiert eine neue generelle Rolle RName1 mit CName1 als domain, CName2 
 9859 * als range und CNameDef als "default value restriction" in modal context [].
 9860 *------------------------------------------*/
 9861
 9862
 9863sb_primelemrole(RName1, 'domain-range'(CName1,CName2,CNameDef)):-
 9864	getCurrentEnvironment(EnvName),
 9865	sb_primelemrole(EnvName,[],RName1, 'domain-range'(CName1,CName2,CNameDef)).
 9866
 9867/*---------------------------------------------------------------------------
 9868 * sb_primelemrole(X,RName1,domain-range(CName1,CName2,CNameDef))
 9869 * definiert eine neue generelle Rolle RName1 mit CName1 als domain, CName2 
 9870 * als range und CNameDef als "default value restriction" in modal context []
 9871 * und X=environment bzw. in modal context X=MS und current environment.
 9872 *------------------------------------------*/
 9873
 9874
 9875sb_primelemrole(X,RName1, 'domain-range'(CName1,CName2,CNameDef)):-
 9876	environment(X,_,_),
 9877	sb_primelemrole(X,[],RName1, 'domain-range'(CName1,CName2,CNameDef)).
 9878sb_primelemrole(X,RName1, 'domain-range'(CName1,CName2,CNameDef)):-
 9879	(X = [] ; X = [_|_]),
 9880	getCurrentEnvironment(EnvName),
 9881	sb_primelemrole(EnvName,X,RName1, 'domain-range'(CName1,CName2,CNameDef)).
 9882
 9883/*---------------------------------------------------------------------------
 9884 * sb_primelemrole(EnvName,MS,RName1,domain-range(CName1,CName2,CNameDef))
 9885 * definiert eine neue generelle Rolle RName1 mit CName1 als domain, CName2 
 9886 * als range und CNameDef als "default value restriction" in modal context MS 
 9887 * und environment EnvName.
 9888 *------------------------------------------*/
 9889
 9890sb_primelemrole(EnvName,MS,RName1, 'domain-range'(CName1,CName2,CNameDef)):-
 9891	environment(EnvName,Env,_),
 9892	defprimconcept(EnvName,MS,CName1,some(RName1,'top')),
 9893	defprimconcept(EnvName,MS,some(inverse(RName1),'top'),CName2),
 9894%	defprimconcept(ENvName,MS,and([CName2,naf(not(CNameDef))]),CNameDef),
 9895	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 9896	assertz_logged((roleDomain(Env,W1,RName1,CName1) :- G1)),
 9897	assertz_logged((roleRange(Env,W1,RName1,CName2) :- G1)),
 9898	assertz_logged((roleDefault(Env,W1,RName1,CNameDef) :- G1)),
 9899	assertz_logged((roleTripel(Env,W1,RName1,CName1,CName2,CNameDef) :- G1)),
 9900	!.
 9901
 9902/*----------------------------------------------------------------------------
 9903 * sb_defelemrole(RName1,restricts(RName2,range(CName1,CNameDef)))
 9904 * restrigniert die Rolle RName2 bzgl. des Rollenfuellers, auf CName1,
 9905 * in modal context [] und current environment.
 9906 * (CNameDef gibt den Default-Rollenfueller an).
 9907 *------------------------------------------*/
 9908
 9909sb_defelemrole(RName1, restricts(RName2, range(CName1,CNameDef))):-
 9910	getCurrentEnvironment(EnvName),
 9911	sb_defelemrole(EnvName,[],RName1,restricts(RName2,range(CName1,CNameDef))),
 9912	!.
 9913
 9914/*----------------------------------------------------------------------------
 9915 * sb_defelemrole(X,RName1,restricts(RName2,range(CName1,CNameDef)))
 9916 * restrigniert die Rolle RName2 bzgl. des Rollenfuellers, auf CName1,
 9917 * in modal context [] und X=environment bzw. in modal context X=MS und 
 9918 * current environment.
 9919 *------------------------------------------*/
 9920
 9921
 9922sb_defelemrole(X,RName1, restricts(RName2, range(CName1,CNameDef))):-
 9923	environment(X,_,_),
 9924	!,
 9925	sb_defelemrole(X,[],RName1, restricts(RName2, range(CName1,CNameDef))).
 9926sb_defelemrole(X,RName1, restricts(RName2, range(CName1,CNameDef))):-
 9927	(X = [] ; X = [_|_]),
 9928	!,
 9929	getCurrentEnvironment(EnvName),
 9930	sb_defelemrole(EnvName,X,RName1, restricts(RName2, range(CName1,CNameDef))).
 9931
 9932/*----------------------------------------------------------------------------
 9933 * sb_defelemrole(EnvName,MS,RName1,restricts(RName2,range(CName1,CNameDef)))
 9934 * restrigniert die Rolle RName2 bzgl. des Rollenfuellers, auf CName1,
 9935 * in modal context MS und  environment EnvName. 
 9936 *------------------------------------------*/
 9937
 9938
 9939sb_defelemrole(EnvName,MS,RName1, restricts(RName2, range(CName1,CNameDef))):-
 9940	environment(EnvName,Env,_),
 9941	defrole(EnvName,MS,RName1,restr(RName2,CName1)),
 9942%	defprimconcept(EnvName,MS,and([some(inverse(RName1),'top'),
 9943%                                      naf(not(CNameDef))]),CNameDef),
 9944	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 9945	assertz_logged((roleRange(Env,MS,RName1,CName1) :- G1)),
 9946	assertz_logged((roleDefault(Env,MS,RName1,CNameDef) :- G1)),
 9947	!.
 9948
 9949
 9950
 9951/*----------------------------------------------------------------------------
 9952 * sb_disjoint(CName1,CName2)
 9953 * definiert generelle Konzepte CName1,CName2 als disjunkt.
 9954 *-------------------------------------------*/
 9955
 9956		     
 9957sb_disjoint(CName1,CName2):- 
 9958%	defprimconcept(CName1,not(CName2)),
 9959	defprimconcept(CName2,not(CName1)),
 9960	!.
 9961
 9962
 9963sb_disjoint(X,CName1,CName2):- 
 9964%	defprimconcept(X,CName1,not(CName2)),
 9965	defprimconcept(X,CName2,not(CName1)),
 9966	!.
 9967
 9968
 9969sb_disjoint(EnvName,MS,CName1,CName2):- 
 9970%	defprimconcept(EnvName,MS,CName1,not(CName2)),
 9971	defprimconcept(EnvName,MS,CName2,not(CName1)),
 9972	!.
 9973
 9974
 9975
 9976/*----------------------------------------------------------------------------
 9977 * sb_expand(CName1,CName2)
 9978 * erlaubt die Erweiterung der Definition eines bereits existierenden Konzeptes,
 9979 * der Konstruktor fuegt zu einem bestehenden Konzept ein weiteres "Vaterkonzept
 9980 * hinzu, die Moeglichkeit der Erweiterung ist also analog zur Definition der 
 9981 * Konzepte.
 9982 *------------------------------------------*/
 9983
 9984 /*? sb_expand(CName1,CName2):- defprimconcept(CName1,CName2)??*/
 9985
 9986
 9987
 9988
 9989
 9990
 9991make_irole(EnvName,MS,ICName1,irole(RName,iname(IRName),
 9992                                    nr(MinNr,MaxNr,DefNr))):-
 9993	environment(EnvName,Env,_),
 9994%       defprimrole(EnvName,MS,IRName,restr(RName,
 9995%	                                    and([atleast(MinNr,RName),
 9996%			  		    atmost(MaxNr,RName),
 9997%					    some(inverse(RName),'top')]))),
 9998	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
 9999	assertz_logged((roleNr(Env,W1,IRName,MinNr,MaxNr) :- G1)),
10000	assertz_logged((roleDefNr(Env,W1,IRName,DefNr) :- G1)).
10001                           
10002
10003
10004make_irole(EnvName,MS,ICName1,irole(RName,iname(IRName),vr(ICName2))) :-
10005        environment(EnvName,Env,_),
10006%	gensym(concept,CName2),
10007%       defprimrole(EnvName,MS,IRName, restr(RName,
10008%                 			     CName2)),
10009	getRoleDomain(Env,MS,RName,CDom),
10010	getRoleRange(Env,MS,RName,CRang),
10011	!,
10012	consistCheck(EnvName,MS,ICName1,CDom),
10013	!,
10014	consistCheck(EnvName,MS,ICName2,CRang),
10015	!,
10016%	consistCheck(EnvName,MS,ICName2,CName2),
10017	assert_ind(EnvName,MS,ICName1,ICName2,IRName).
10018
10019constructRestriction(RName,[],['top']) :-
10020	!.
10021constructRestriction(RName,[nr(MinNr,MaxNr,DefNr)|L1],
10022                     [atleast(MinNr,'top'), atmost(MaxNr,'top') | L2]) :-
10023	constructRestriction(RName,L1,L2),
10024	!.
10025constructRestriction(RName,[vr(ICName2)|L1],[ICName2|L2]) :-
10026	constructRestriction(RName,L1,L2),
10027	!.
10028     
10029 /*-------------------------------------------------------------------------------
10030  * make_defelem(ICName,isa(CName))
10031  * individualisiert ein Konzept CName mit dem Instanz-Namen ICName vom Typ string,
10032  * d.h. es wird das Abox-Element ICName zu Konzept hinzugefuegt und zwar in modal
10033  * context [] bzw MS.
10034  *-----------------------------------------*/
10035
10036
10037consistCheck(Env,MS,IC,Concept) :- 
10038% vor dem Test die Normalform von not(Concept) mittels
10039% normalizeNot(not(Concept),NotConcept)
10040% bilden und dann
10041% sb_ask(Env,MS,(isa(IC,NotConcept)))
10042% aufrufen
10043			 normalizeNot(not(Concept),NotConcept),
10044			 sb_ask(Env,MS,(isa(IC,NotConcept))),
10045			 nl,
10046			 write('--- impossible --- '),
10047			 !,
10048			 fail.
10049
10050			    
10051
10052
10053
10054consistCheck(Env,MS,IC,Concept) :-
10055			          !.
10056
10057
10058
10059make_defelem(EnvName,MS,ICName,isa(CName)):-
10060	  consistCheck(EnvName,MS,ICName,CName),
10061	  assert_ind(EnvName,MS,ICName,CName),
10062	  !.
10063
10064
10065
10066
10067
10068/*--------------------------------------------------------------------------------
10069 * make_defelem(EnvName,MS,ICName1,irole(RName,iname(IRName),[nr(MinNr,MaxNr,DefNr),vr(ICName2)]))
10070 * erzeugt eine Instanz ICName1 fuer ein Konzept, an dem auch die Rolle RName
10071 * individualisiert wird mit dem Instanznamen IRName,
10072 * der Kardinalitaet der indiv. Rolle nr(MinNr,MaxNr,DefNr),
10073 * dem Rollenfueller der indiv. Rolle vr(ICName2),
10074 * Und zwar in modal context MS und environment=EnvName.
10075 *------------------------------------------*/
10076 
10077
10078
10079
10080make_defelem(EnvName,MS,ICName1,irole(RName,iname(IRName),SpecList)) :-
10081	constructRestriction(IRName,SpecList,RestrList),
10082	defprimrole(EnvName,MS,IRName,restr(RName,and(RestrList))),
10083        make_defelem_list(EnvName,MS,ICName1,irole(RName,iname(IRName),SpecList)).
10084
10085make_defelem_list(EnvName,MS,ICName1,irole(RName,iname(IRName),[])).
10086make_defelem_list(EnvName,MS,ICName1,irole(RName,iname(IRName),[X|T])) :-
10087	make_irole(EnvName,MS,ICName1,irole(RName,iname(IRName),X)),
10088	make_defelem_list(EnvName,MS,ICName1,irole(RName,iname(IRName),T)).
10089
10090
10091/*--------------------------------------------------------------------------------
10092 * sb_defelem(ICName1,ISpecListe)
10093 * ISpecListe=[isa(...),irole(iname(...)nr(...)vr(...))]
10094 * erzeugt eine Instanz ICName1 fuer ein Konzept, an dem auch die Rolle RName
10095 * individualisiert werden kann mit dem Instanznamen IRName,
10096 * der Kardinalitaet der indiv. Rolle nr(MinNr,MaxNr,DefNr),
10097	* dem Rollenfueller der indiv. Rolle vr(ICName2),
10098 * Und zwar in modal context [] und current environment.
10099 * Domain dieser Rolle ist eine Teilmenge von indiv. Konzept ICName1.
10100 *------------------------------------------*/
10101
10102
10103
10104sb_defelem(ICName1,SpecListe) :-
10105	getCurrentEnvironment(EnvName),
10106	sb_defelem(EnvName,[],ICName1,SpecListe).
10107
10108
10109
10110/*-----------------------------------------------------------------------------
10111 *Wie oben, nur in A=environment und modal context [], bzw. in current environment
10112 *und modal context A=MS.
10113 *-----------------------------------------*/
10114
10115
10116sb_defelem(A,ICName1,[]).
10117
10118sb_defelem(A,ICName1,SpecList) :-
10119	environment(A,_,_),
10120	!,
10121	sb_defelem(A,[],ICName1,SpecList).
10122sb_defelem(A,ICName1,SpecList) :-
10123	(A = [] ; A = [_|_]),
10124	!,
10125	getCurrentEnvironment(EnvName),
10126	sb_defelem(EnvName,A,ICName1,SpecList).
10127
10128
10129/*-----------------------------------------------------------------------------
10130 *Wie oben, nur explizit in environment EnvName und modal context MS.
10131 *-----------------------------------------*/
10132
10133
10134sb_defelem(EnvName,MS,ICName1,[]).
10135 
10136sb_defelem(EnvName,MS,ICName1,[X|T]):-
10137	make_defelem(EnvName,MS,ICName1,X),
10138	sb_defelem(EnvName,MS,ICName1,T). 
10139
10140
10141/************************ sb_ask-Selektoren fuer die TBox **********************/
10142 
10143
10144sb_ask(Q) :-
10145	getCurrentEnvironment(EnvName),
10146	sb_ask(EnvName,[],Q).
10147
10148sb_ask(E,Q) :-
10149	environment(E,_,_),
10150	!,
10151	sb_ask(E,[],Q).
10152sb_ask(M,Q) :-
10153	(M = [] ; M = [_|_]),
10154	!,
10155	getCurrentEnvironment(EnvName),
10156	sb_ask(EnvName,M,Q).
10157
10158/*----------------------------------------------------------------------
10159 *
10160 * sb_ask(supers(CName1,CName2)))
10161 *
10162 * ueberprueft, ob CName1 ein direktes Subkonzept von CName2 ist.
10163 *
10164 *
10165 *-----------------------------------------------------------------------*/
10166
10167
10168 sb_ask(EnvName,MS,(supers(CName1,CName2))) :-
10169	getDirectSuperConcepts(EnvName,MS,CName1,CL),
10170	member(CName2,CL).
10171
10172/*---------------------------------------------------------------------------
10173 * sb_ask(EnvName,MS,(supers*(CName1,CName2)))
10174 *
10175 * ueberprueft auch transitive supers-Beziehungen (Subsumptionsbeziehungen) zwischen
10176 * Subkonzept CName1 und Superkonzept CName2.
10177 *-----------------------------------------*/
10178
10179 sb_ask(EnvName,MS,(supers*(CName1,CName2))) :-
10180	getAllSuperConcepts(EnvName,MS,CName1,CL),
10181	member(CName2,CL).
10182
10183
10184/*------------------------------------------------------------------------------
10185 * sb_ask(EnvName,MS,(role(RName,CNameDom,CNameRan)))
10186 *
10187 * dieser Selektor dient zum Zugriff bzw. zur Anfrage nach Rollenbeziehungen
10188 * zwischen zwei Konzepten, oder aber auch nach Domain und Range einer Rolle.
10189 *--------------------------------------------*/
10190
10191 sb_ask(EnvName,MS,(role(RName,Subconcept,CNameRan))) :-
10192	  environment(EnvName,Env,_),
10193	sb_ask(EnvName,MS,(supers*(Subconcept,Superconcept))),
10194	getRoleDomain(Env,MS,RName,Superconcept),
10195	getRoleRange(Env,MS,RName,CNameRan).
10196
10197 sb_ask(EnvName,MS,(role(RName,CNameDom,CNameRan))) :-
10198	  environment(EnvName,Env,_),
10199	  !,
10200	getRoleDomain(Env,MS,RName,CNameDom),
10201	getRoleRange(Env,MS,RName,CNameRan).
10202
10203
10204
10205/*-----------------------------------------------------------------------------
10206 * sb_ask(EnvName,MS,(roleDef(RName,CNameDef)))
10207 *
10208 * dient zur Anfrage bzgl. DefaultRange einer Rolle.
10209 *--------------------------------------------*/
10210
10211
10212 sb_ask(EnvName,MS,(roleDef(RName,CNameDef))) :-   
10213	  environment(EnvName,Env,_),
10214	  !,
10215	getRoleDefault(Env,MS,RName,CNameDef).
10216
10217/*----------------------------------------------------------------------------
10218 * sb_ask(EnvName,MS,(roleNr(RName,MinNr,MaxNr)))
10219 *
10220 * der Selektor dient zum Zugriff auf die Number Restriction einer Rolle RName
10221 * am einem Konzept CName.
10222 *-------------------------------------------*/
10223
10224
10225 sb_ask(EnvName,MS,(roleNr(RName,MinNr,MaxNr))) :-  
10226	  environment(EnvName,Env,_),
10227	  !,
10228	getRoleNr(Env,MS,RName,MinNr,MaxNr).
10229				       
10230                                       
10231              
10232
10233/*----------------------------------------------------------------------------
10234 * sb_ask(EnvName,MS,(roleDefNr(RName,DefNr)))
10235 *
10236 * der Selektor dient zum Zugriff auf die Default- Number Restriction einer Rolle
10237 * RName am einem Konzept CName.
10238 *-------------------------------------------*/
10239
10240
10241 sb_ask(EnvName,MS,(roleDefNr(RName,DefNr))) :-  
10242	  environment(EnvName,Env,_),
10243	  !,
10244	getRoleDefNr(Env,MS,RName,DefNr).
10245
10246
10247
10248/*----------------------------------------------------------------------------
10249 ************************* sb_ask-Selektoren fuer die ABox ***********************
10250 *
10251 * sb_ask(EnvName,MS,(isa(ICName,CName)))
10252 *
10253 * ermoeglicht den Zugriff Initial.-Beziehung einer Instanz ICName zum entsprech-
10254 * ende generellen Konzept CName.
10255 *------------------------------------------*/
10256
10257
10258 sb_ask(EnvName,MS,(isa(ICName,CName))) :- 
10259	ask(EnvName,MS,elementOf(ICName,CName),_).
10260
10261
10262sb_ask(EnvName,MS,(attributes(CN,Attribute,Value))) :-
10263	attribute(concept,EnvName,MS,CN,[Attribute,Value]).
10264sb_ask(EnvName,MS,(attributes(CN,RN,Attribute,Value))) :-
10265	attribute(role,EnvName,MS,[CN,RN],[Attribute,Value]).
10266
10267sb_ask(EnvName,MS,(subrole(Subrole,Superrole))) :-
10268	getDirectFatherRoles(EnvName,MS,Subrole,RL),
10269	member(Superrole,RL).
10270
10271sb_ask(EnvName,MS,(irole(R,X,Y))) :-
10272	environment(EnvName,Env,_),
10273	convertMS(negative,Env,[[],true],MS,[],[W1,G1],_),
10274	EqLiteral = eq(Env,rn(_AX,_RN,_S,_O),modal(W1),Y,CON,A1,X,Pc,call(_CALLS),ab(noAb),Exp,Gd,Od,L1,L2,Anc1,Anc2,Y,_PT),
10275%	EqLiteral = eq(Env,rn(_AX,_RN,_S,_O),modal(W1),Y,app((FF:R),X),hyp(_HYPS),ab(noAb),call(_CALLS),_PT),
10276	call(G1),
10277	call(EqLiteral).
10278
10279
10280%----------------------------------------------------------------------
10281% sb_ask(EnvName,MS,(allRoles(+CName,-Info)))
10282% Arguments: Cname ConceptName
10283%	     Info is a list consisting of lists with elements:
10284%               Rnames role-name
10285%	        Cnames domain 
10286% 	        Min    Minimalnr. der role
10287%              	Max    Maximalnr. der role
10288%           	Defnr  Defaultnr. der role  
10289%
10290% liefert alle rolen,domains,Minnr,Maxnr,Defnr die von einem konzept ausgehen
10291% bsp : sb_ask(env,ms,allRoles(golf,X))    X = ((has_part golf windshield 1 1 1)(consumes golf gasoline 1 1 1))
10292% Author: Patrick Brandmeier
10293
10294
10295sb_ask(EnvName,MS,(allRoles(CName,Info_list))) :-
10296	setofOrNil(Info,[RName,CNameRan,Info]^(sb_ask(EnvName,MS,(role(RName,CName,CNameRan))),
10297	 	 sb_ask(EnvName,MS,(allRoles(RName,CName,CNameRan,Info)))),Info_list).
10298	
10299sb_ask(EnvName,MS,(allRoles(RName,CName,CNameRan,[RName,CName,CNameRan,Min,Max,Defnr]))) :-
10300	sb_ask(EnvName,MS,(roleNr(RName,Min,Max))),
10301	sb_ask(EnvName,MS,(roleDefNr(RName,Defnr))),
10302	!.
10303sb_ask(EnvName,MS,(allRoles(RName,CName,CNameRan,[RName,CName,CNameRan]))) :-
10304	!.
10305
10306
10307%----------------------------------------------------------------------
10308% sb_fact(EnvName,MS,(all_roles(+CName,-Info)))
10309% Arguments: Cname ConceptName
10310%	     Info is a list consisting of lists with elements:
10311%               Rnames role-name
10312%	        Cnames domain 
10313% 	        Min    Minimalnr. der role
10314%              	Max    Maximalnr. der role
10315%           	Defnr  Defaultnr. der role  
10316%
10317% liefert alle rolen,domains,Minnr,Maxnr,Defnr die von einem konzept ausgehen
10318% bsp : sb_fact(env,ms,allRoles(golf,X))    X = ((has_part golf windshield 1 1 1)(consumes golf gasoline 1 1 1))
10319% Author: Patrick Brandmeier
10320
10321
10322sb_fact(EnvName,MS,(allRoles(CName,Info_list))) :-
10323	setofOrNil(Info,[RName,CNameRan]^(sb_fact(EnvName,MS,(role(RName,CName,CNameRan))),
10324	 	 sb_fact(EnvName,MS,(allRoles(RName,CName,CNameRan,Info)))),Info_list).
10325	
10326sb_fact(EnvName,MS,(allRoles(RName,CName,CNameRan,[RName,CName,CNameRan,Min,Max,Defnr]))) :-
10327	sb_fact(EnvName,MS,(roleNr(RName,Min,Max))),
10328	sb_fact(EnvName,MS,(roleDefNr(RName,Defnr))),
10329	!.
10330sb_fact(EnvName,MS,(allRoles(RName,CName,CNameRan,[RName,CName,CNameRan]))) :-
10331	!.
10332
10333
10334%----------------------------------------------------------------------
10335% sb_ask(EnvName,MS,(direct_super_role(+-RName,+-CNameDom,+-CNameRan,+-SRName,+-SCNameDom,+-SCNameRan))) :-
10336% Arguments: Rname RoleName
10337%	     CNameDom Domain
10338%            CNameRan Range 
10339%	     SRName Super-Role-Name
10340% 	     SRNameDom Super-Role_domain
10341%            SRNameRan Super-Role-range
10342%
10343% ist erfuellt wenn (SRName SCNameDom SCNameRan) die direkte super-rolle von (RName CNameDom CNameRan) ist
10344%
10345% Author: Patrick Brandmeier
10346
10347sb_ask(EnvName,MS,(direct_super_role(RName,CNameDom,CNameRan,RName,SCNameDom,CNameRan))) :-
10348	var(CNameDom),
10349	getDirectSubConcepts(EnvName,MS,SCNameDom,CL),
10350	member(CNameDom,CL),
10351	!.
10352
10353sb_ask(EnvName,MS,(direct_super_role(RName,CNameDom,CNameRan,RName,SCNameDom,CNameRan))) :-
10354	sb_ask(EnvName,MS,(supers(CNameDom,SCNameDom))),
10355	!.
10356%	sb_ask(EnvName,MS,(role(SRName,SCNameDom,SCNameRan))),	
10357%	!.
10358
10359
10360%----------------------------------------------------------------------
10361% sb_ask(EnvName,MS,(risa(+-IRName,+-ICNameDom,+-ICNameRan,+-GRName,+-GCNameDom,+-GCNameRan))) :-
10362% Arguments: IRname    individ.-RoleName
10363%	     ICNameDom individ.-Domain
10364%            ICNameRan individ.-Range 
10365%	     GRName    genereller Super-Role-Name
10366% 	     GRNameDom genereller Super-Role_domain
10367%            GRNameRan genereller Super-Role-range
10368% ist erfuellt wenn (GRName GCNameDom GCNameRan) die generelle - rolle von (IRName ICNameDom ICNameRan) ist
10369%  
10370% Author: Patrick Brandmeier
10371
10372sb_ask(EnvName,MS,(risa(IRName,ICNameDom,ICNameRan,GRName,GCNameDom,GCNameRan))) :-
10373	environment(EnvName,Env,_),
10374	getConceptElement(Env,MS,ICNameDom,GCNameDom),
10375	sb_ask(EnvName,MS,role(GRName,GCNameDom,GCNameRan)).
10376
10377sb_ask(EnvName,MS,(risa(IRName,ICNameDom,ICNameRan,IRName,GCNameDom,ICNameRan))) :-
10378	environment(EnvName,Env,_),
10379	getConceptElement(Env,MS,ICNameDom,GCNameDom),
10380	!.
10381
10382%----------------------------------------------------------------------
10383% sb_ask(EnvName,MS,(rall(+-RName,+-CNameDom,+-CNameRan,CNameDef,+-MinNr,+-MaxNr,+-DefNr))) :-
10384% Arguments: Rname    RoleName
10385%	     CNameDom RoleDomain
10386%            CNameRan RoleRange 
10387%	     CNameDef ConceptNameDef.
10388% 	     MinNR    Minim.Nr
10389%            MaxNr    Maxi. Nr
10390% 
10391% ist erfuellt wenn 
10392%
10393
10394
10395sb_ask(EnvName,MS1,(rall(RName,CNameDom,CNameRan,CNameDef,MinNr,MaxNr,DefNr))) :-
10396	environment(EnvName,Env,_),
10397	hop_map(negate,MS1,MS2),
10398	hop_map(normalizeNot,MS2,MS3),
10399	convertMS(negative,Env,[[],true],MS3,[],[W1,G1],_),
10400	call(G1),
10401	roleAll(Env,W1,RName,CNameDom,CNameRan,CNameDef,MinNr,MaxNr,DefNr),
10402	!.
10403
10404
10405/*-----------------------------------------------------------------------------
10406 */
10407
10408sb_attributes(CN,AList) :-
10409	getCurrentEnvironment(EnvName),
10410	sb_assert_attributes(concept,EnvName,[],CN,AList).
10411sb_attributes(A1,CN,AList) :-
10412	environment(A1,_,_),
10413	!,
10414	sb_assert_attributes(concept,A1,[],CN,AList).
10415sb_attributes(A1,CN,AList) :-
10416	(A1 = [] ; A1 = [_|_]),
10417	!,
10418	getCurrentEnvironment(EnvName),
10419	sb_assert_attributes(concept,EnvName,A1,CN,AList).
10420sb_attributes(CN,R,AList) :-
10421	getCurrentEnvironment(EnvName),
10422	sb_assert_attributes(role,EnvName,[],[CN,R],AList).
10423sb_attributes(A1,A2,CN,AList) :-
10424	environment(A1,_,_),
10425	(A2 = [] ; A2 = [_|_]),
10426	!,
10427	sb_assert_attributes(concept,A1,A2,CN,AList).
10428sb_attributes(EnvName,CN,RN,AList) :-
10429	sb_assert_attributes(role,EnvName,[],[CN,RN],AList).
10430sb_attributes(EnvName,MS,CN,RN,AList) :-	
10431	sb_assert_attributes(role,EnvName,MS,[CN,RN],AList).
10432
10433sb_assert_attributes(Type,EnvName,MS,Spec,[]) :-
10434	!.
10435sb_assert_attributes(Type,EnvName,MS,Spec,[Pair|AList]) :-
10436	assertz_logged(attribute(Type,EnvName,MS,Spec,Pair)),
10437	sb_assert_attributes(Type,EnvName,MS,Spec,AList).
10438
10439/*------------------------------------------------------------------------------
10440 * Die 2- bis 4-stellige Funktion sb_unprimconcept([Env],[MS],CN,SpecListe) erwartet 
10441 * als letztes Argument  eine Liste von Konstruktoren: supers,restr-inh,nr, die irgendwann
10442 * mit sb_primconcept bzgl. CN definiert wurde.
10443 * Die Fkt. sb_unprimconcept eliminiert die entsprechenden Subsumption-Beziehungen wieder 
10444 * und l"oscht ebenfalls Eintr"age der Rollen wie: roleDomain,roleNr,etc..
10445 *-----------------------------*/
10446
10447 
10448
10449 unmake_primconcept(EnvName,MS,CN,supers([])) :- !.
10450
10451 unmake_primconcept(EnvName,MS,CN,supers([X|T])) :-
10452			    environment(EnvName,Env,_),
10453			    conceptSubsets(Env,_user,MS,CN,X,AX),
10454			    undefprimconcept(EnvName,MS,CN,X),
10455			    unmake_primconcept(EnvName,MS,CN,supers(T)).
10456
10457unmake_primconcept(EnvName,MS,CName1,
10458		   'restr-inh'(R1,restricts(R2,range(CName2,CNameDef)))):-
10459          unmake_primconcept(EnvName,MS,CName1,
10460			    restrict_inh(R1, restricts(R2 ,
10461							   range(CName2,CNameDef)))).
10462
10463
10464
10465unmake_primconcept(EnvName,MS,CName1,
10466		   'restrict-inh'(R1,restricts(R2,range(CName2,CNameDef)))) :-
10467        unmake_primconcept(EnvName,MS,CName1,
10468			 restrict_inh(R1, restricts(R2 ,
10469							range(CName2,CNameDef)))).
10470 
10471
10472
10473unmake_primconcept(EnvName,MS,CName1,
10474	           restrict_inh(R1,restricts(R2,range(CName2,CNameDef)))) :-
10475	environment(EnvName,Env,_),
10476	!,
10477	getRoleDomain(Env,MS,R1,CNameDom),
10478	!,
10479	getRoleRange(Env,MS,R1,CName2),
10480	undefprimconcept(EnvName,MS,CNameDom,CName1),
10481	retract((roleDomain(Env,MS,R1,CNameDom) :- _)),
10482	retract((roleRange(Env,MS,R1,CName2) :- _)),
10483	retract((roleDefault(Env,MS,R1,CNameDef) :- _)),
10484	retract((roleTripel(Env,MS,R1,CNameDom,CName2,CNameDef))),
10485        !.
10486
10487
10488
10489unmake_primconcept(EnvName,MS,CName1 , nr(R1, MinNr,MaxNr,DefNr)) :-
10490	environment(EnvName,Env,_),
10491	!,
10492	getRoleNr(Env,MS,R1,MinNr,MaxNr),
10493	!,
10494	getRoleDefNr(Env,MS,R1,DefNr),
10495	conceptEqualSets(Env,_user,MS,CNameDom,
10496			and([atleast(MinNr,R1),atmost(MaxNr,R1)]),AX),
10497        undefconcept(EnvName,MS,CNameDom,and([atleast(MinNr,R1),
10498	 				      atmost(MaxNr,R1)])),
10499        undefconcept(EnvName,MS,CNameDom,some(R1,'top')),
10500        undefprimconcept(EnvName,MS,CNameDom,CName1),
10501        retract((roleNr(Env,MS,R1,MinNr,MaxNr) :- _)),
10502	retract((roleDefNr(Env,MS,R1,DefNr) :- _)),
10503	!.
10504
10505
10506
10507/*------------------------------------------------------------------------
10508 * sb_unprimconcept(EnvName,MS,CName1,SpecList)
10509 *
10510 *------------------------------------------*/
10511
10512
10513sb_unprimconcept(CName1,[]) :- !.
10514
10515sb_unprimconcept(CName1,[X|T]) :-
10516			    getCurrentEnvironment(EnvName),
10517			    sb_unprimconcept(EnvName,[],CName1,[X|T]),
10518			    !.
10519
10520
10521
10522sb_unprimconcept(A,CName1,[X|T]) :-
10523			     environment(A,_,_),
10524			     !,
10525			     sb_unprimconcept(A,[],CName1,[X|T]),
10526			     !.
10527
10528
10529sb_unprimconcept(A,CName1,[X|T]) :-
10530			     (A=[] ; A=[_,_]),
10531			     getCurrentEnvironment(EnvName),
10532			     !,
10533			     sb_unprimconcept(EnvName,A,CName1,[X|T]).
10534
10535
10536
10537sb_unprimconcept(EnvName,MS,CName1,[]) :- !.
10538
10539sb_unprimconcept(EnvName,MS,CName1,[X|T]) :-
10540			     unmake_primconcept(EnvName,MS,CName1,X),
10541			     sb_unprimconcept(EnvName,MS,CName1,T),
10542			     !.
10543
10544
10545
10546
10547/*------------------------------------------------------------------------------
10548 * Die 4-stellige Fkt. unmake_defconcept(Env,MS,CN,supers(List_of_concepts)) erwartet
10549 * als letztes Argument  eine Liste (Menge) von Konzepten. Eine Obermenge dieser Menge wurde
10550 * irgendwann in AND-Verkn"upfung mit CN "aquivalent gemacht und zwar in der Form:
10551 *
10552 *    sb_defconcept(CN, [supers([C1,C2,...,Cn]),..]) ----->   defconcept(CN,and([C1,C2,..,Cn])).    
10553 *
10554 *-------------------------------------------------*/
10555
10556
10557
10558unmake_defconcept(EnvName,MS,CName1,restrict_inh(RName1, restricts(RName2 ,
10559				    range(CName2,CNameDef))),CNameDom):-
10560        unmake_defconcept(EnvName,MS,CName1,'restr-inh'(RName1, restricts(RName2 ,
10561			  range(CName2,CNameDef))),CNameDom).
10562
10563
10564unmake_defconcept(EnvName,MS,CName1,'restrict-inh'(RName1, restricts(RName2 ,
10565					range(CName2,CNameDef))),CNameDom):-
10566        unmake_defconcept(EnvName,MS,CName1,'restr-inh'(RName1, restricts(RName2 ,
10567			  range(CName2,CNameDef))),CNameDom).
10568
10569
10570unmake_defconcept(EnvName,MS,CName1,'restr-inh'(RName1, restricts(RName2 ,
10571				  range(CName2,CNameDef))),CNameDom):-
10572
10573        environment(EnvName,Env,_),
10574	!,
10575	roleEqualSets(Env,_user,MS,RName1,restr(RName2,CName2)),
10576	undefrole(EnvName,MS,RName1,restr(RName2,CName2)),
10577	!,
10578	getRoleDomain(Env,MS,RName1,CNameDom),
10579	!,
10580	conceptEqualSets(Env,_usr,MS,CNameDom,some(RName2,'top')),
10581	undefconcept(CNameDom,some(RName2,'top')),
10582        retract((roleDomain(Env,MS,RName1,CNameDom) :- _)),
10583	retract((roleRange(Env,MS,RName1,CName2) :- _)),
10584	retract((roleDefault(Env,MS,RName1,CNameDef) :- _)),
10585	retract((roleTripel(Env,MS,RName1,CNameDom,CName2,CNameDef))),
10586	retract((roleAll(Env,MS,RName1,CNameDom,CName2,CNameDef,_,_,_))),
10587	!.
10588
10589
10590unmake_defconcept(EnvName,MS,CName1, nr(RTerm,MinNr,MaxNr,DefNr),CNameDom) :-
10591	environment(EnvName,Env,_),
10592        unexpand_role(EnvName,MS,RTerm,R1),
10593	!,
10594	conceptEqualSets(Env,_usr,MS,CNameDom,and([atleast(MinNr,R1),
10595					           atmost(MaxNr,R1)]),AX),
10596        undefconcept(EnvName,MS,CNameDom,and([atleast(MinNr,R1),
10597						   atmost(MaxNr,R1)])),
10598	!,
10599	conceptEqualSets(Env,_usr,MS,CNameDom,some(R1,'top')),
10600        undefconcept(EnvName,MS,CNameDom,some(R1,'top')),       
10601	retract((roleNr(Env,MS,R1,MinNr,MaxNr) :- _)),
10602	retract((roleDefNr(Env,MS,R1,DefNr) :- _)),
10603	!.
10604        						   
10605        
10606	    
10607unmake_defconcept(EnvName,MS,CName1, necres(RTerm, nec),CNameDom):-
10608	!,
10609	environment(EnvName,Env,_),
10610	unexpand_role(EnvName,MS,RTerm,R1),
10611	conceptEqualSets(Env,_usr,MS,CNameDom,atleast(1,R1),AX),
10612	undefconcept(EnvName,MS,CNameDom,atleast(1,R1)),
10613	retract((roleDomain(Env,MS,R1,CNameDom) :- _)),
10614	!.
10615
10616
10617
10618unmake_defconcept(EnvName,MS,CName1 , necres(RTerm, _),CNameDom):-
10619        !,
10620	environment(EnvName,Env,_),
10621	unexpand_role(EnvName,MS,RTerm,R1),
10622	retract((roleDomain(Env,MS,RName1,CNameDom) :- _)),
10623	!.
10624
10625
10626/*---------------------------------------------------------------------
10627 * unexpand_role(EnvName,MS,RTerm,R1)
10628 *
10629 *-------------------------------------*/
10630
10631
10632unexpand_role(_,_,RTerm,RTerm) :-
10633	 atomic(RTerm),
10634	 !.
10635
10636unexpand_role(EnvName,MS,RTerm,R1) :-
10637         RTerm = sb_unprimelemrole(EnvName,MS,R1, 'domain-range'(C1,C2,CNameDef)),
10638         !,
10639	 call(RTerm).
10640	   
10641
10642
10643unexpand_role(EnvName,MS,RTerm,R1) :-
10644         RTerm = sb_unprimelemrole(MS,R1, 'domain-range'(C1,C2,CNameDef)),
10645         !,
10646	 call(RTerm).
10647
10648
10649unexpand_role(EnvName,MS,RTerm,R1) :-
10650         RTerm = sb_unprimelemrole(R1, 'domain-range'(C1,C2,CNameDef)),
10651         !,
10652	 call(RTerm).
10653
10654
10655
10656unexpand_role(_,_,RTerm,RTerm).
10657
10658
10659
10660/**********************************************************************
10661 *
10662 * sb_undefconcept(EnvName,MS,CName1,SpecList)
10663 *
10664 *
10665 */
10666
10667sb_undefconcept(CName1,[]):- !.
10668
10669sb_undefconcept(CName1,SpecList) :-
10670         getCurrentEnvironment(EnvName),
10671	 sb_undefconcept(EnvName,[],CName1,SpecList),
10672	 !.
10673
10674
10675
10676sb_undefconcept(A,CName1,SpecList) :-
10677         environment(A,_,_),
10678       	 !,
10679	 sb_undefconcept(A,[],CName1,SpecList),
10680	 !.
10681
10682
10683sb_undefconcept(A,CName1,SpecList) :-
10684         (A=[] ; A=[_,_]),
10685         getCurrentEnvironment(EnvName),
10686         !,
10687         sb_undefconcept(EnvName,A,CName1,SpecList).
10688
10689
10690sb_undefconcept(EnvName,MS,CName1,SpecList) :-
10691           sb_undefconcept(EnvName,MS,CName1,SpecList,[]).
10692
10693
10694sb_undefconcept(EnvName,MS,CName1,[],[]) :- !.
10695
10696sb_undefconcept(EnvName,MS,CName1,[],HList) :-
10697	   environment(EnvName,Env,_),
10698	   !,
10699	   conceptEqualSets(Env,_user,MS,CName1,and(HList),AX),
10700	   undefconcept(EnvName,MS,CName1,and(HList)),
10701	   !.
10702
10703
10704sb_undefconcept(EnvName,MS,CName1,[supers(L)|SpecList],CL1) :-
10705	   !,
10706	   append(L,CL1,CL2),
10707	   sb_undefconcept(EnvName,MS,CName1,SpecList,CL2).
10708
10709sb_undefconcept(EnvName,MS,CName1,[Spec1|SpecList],CL1) :-
10710           unmake_defconcept(EnvName,MS,CName1,Spec1,Concept),
10711           sb_undefconcept(EnvName,MS,CName1,SpecList,[Concept|CL1]).
10712
10713
10714
10715sb_undefconcept(EnvName,MS,CName1,supers(L),[]) :-
10716           environment(EnvName,Env,_),
10717	   !,
10718	   conceptEqualSets(Env,_user,MS,CName1,and(L),AX),
10719	   undefconcept(EnvName,MS,CName1,and(L)).
10720
10721
10722
10723/**********************************************************************
10724 *
10725 * sb_unprimelemrole(EnvName,MS,RName1, 'domain-range'(CName1,CName2,CNameDef)) *
10726 *
10727 */
10728
10729
10730sb_unprimelemrole(RName1, 'domain-range'(CName1,CName2,CNameDef)):-
10731           getCurrentEnvironment(EnvName),
10732	   sb_unprimelemrole(EnvName,[],RName1, 'domain-range'(CName1,CName2,CNameDef)).
10733
10734
10735
10736sb_unprimelemrole(X,RName1, 'domain-range'(CName1,CName2,CNameDef)):-
10737	   getCurrentEnvironment(X),
10738	   sb_unprimelemrole(X,[],RName1, 'domain-range'(CName1,CName2,CNameDef)).
10739
10740
10741sb_unprimelemrole(X,RName1, 'domain-range'(CName1,CName2,CNameDef)):-
10742	   (X = [] ; X = [_|_]),
10743	   getCurrentEnvironment(EnvName),
10744	   sb_unprimelemrole(EnvName,X,RName1, 'domain-range'(CName1,CName2,CNameDef)).
10745
10746
10747
10748
10749sb_unprimelemrole(EnvName,MS,RName1, 'domain-range'(CName1,CName2,CNameDef)):-
10750	   environment(EnvName,Env,_),
10751	   !,
10752	   conceptSubsets(Env,_usr,MS,CName1,some(RName1,'top')),
10753	   undefprimconcept(EnvName,MS,CName1,some(RName1,'top')),
10754	   !,
10755	   conceptSubsets(Env,_usr,MS,some(inverse(RName1),'top')),
10756	   undefprimconcept(EnvName,MS,some(inverse(RName1),'top'),CName2),
10757	   retract((roleDomain(Env,MS,RName1,CName1) :- _)),
10758	   retract((roleRange(Env,MS,RName1,CName2) :- _)),
10759	   retract((roleDefault(Env,MS,RName1,CNameDef) :- _)),
10760	   retract((roleTripel(Env,MS,RName1,CName1,CName2,CNameDef))),
10761	   retract((roleAll(Env,MS,RName1,CName1,CName2,CNameDef,_,_,_))),
10762	   !.
10763
10764
10765/**********************************************************************
10766 *
10767 * sb_undefelemrole(RName1,restricts(RName2,range(CName1,CNameDef)))
10768 * 
10769 */
10770
10771
10772sb_undefelemrole(RName1, restricts(RName2, range(CName1,CNameDef))):-
10773        getCurrentEnvironment(EnvName),
10774	sb_undefelemrole(EnvName,[],RName1, restricts(RName2, range(CName1,CNameDef))),
10775	!.
10776
10777
10778sb_undefelemrole(X,RName1, restricts(RName2, range(CName1,CNameDef))):-
10779	getCurrentEnvironment(X),
10780	!,
10781	sb_undefelemrole(X,[],RName1, restricts(RName2, range(CName1,CNameDef))),
10782        !.
10783
10784
10785sb_undefelemrole(X,RName1, restricts(RName2, range(CName1,CNameDef))):-
10786        (X = [] ; X = [_|_]),
10787	!,
10788	getCurrentEnvironment(EnvName),
10789	sb_undefelemrole(EnvName,X,RName1, restricts(RName2, range(CName1,CNameDef))),
10790	!.
10791
10792
10793sb_undefelemrole(EnvName,MS,RName1, restricts(RName2, range(CName1,CNameDef))) :-
10794        environment(EnvName,Env,_),
10795	!,
10796	roleEqualSets(Env,_user,MS,RName1,restr(RName2,CName1)),
10797	undefrole(EnvName,MS,RName1,restr(RName2,CName1)),
10798	!,
10799	roleRange(Env,MS,RName1,CName1),
10800	!,
10801	roleDefault(Env,MS,RName1,CNameDef),
10802	retract((roleRange(Env,MS,RName1,CName1) :- _)),
10803	retract((roleDefault(Env,MS,RName1,CNameDef) :- _)),
10804	!.
10805
10806/*----------------------------------------------*/
10807
10808
10809unmake_irole(EnvName,MS,ICName1,irole(RName,iname(IRName),
10810				      nr(MinNr,MaxNr,DefNr))):-
10811        environment(EnvName,Env,_),
10812	!,
10813	roleSubsets(Env,_user,MS,IRName,restr(RName,and([atleast(MinNr,RName),
10814				            	    atmost(MaxNr,RName),
10815						    some(inverse(RName),'top')]))),
10816
10817	undefprimrole(EnvName,MS,IRName,restr(RName,
10818					      and([atleast(MinNr,RName),
10819					      atmost(MaxNr,RName),
10820					      some(inverse(RName),'top')]))),
10821        !,
10822	getRoleNr(Env,MS,IRName,MinNr,MaxNr),
10823	!,
10824	getRoleDefNr(Env,MS,IRName,DefNr),
10825	retract((roleNr(Env,MS,IRName,MinNr,MaxNr) :- _)),
10826	retract((roleDefNr(Env,MS,IRName,DefNr) :- _)),
10827	!.
10828
10829unmake_irole(EnvName,MS,ICName1,irole(RName,iname(IRName),vr(ICName2))) :-
10830	environment(EnvName,Env,_),
10831	!,
10832	roleSubsets(Env,_user,MS,IRName,restr(RName,CName2)),
10833	undefprimrole(EnvName,MS,IRName, restr(RName,CName2)),
10834        delete_ind(EnvName,MS,ICName1,ICName2,IRName).
10835
10836unmake_defelem(EnvName,MS,ICName,isa(CName)):-
10837	delete_ind(EnvName,MS,ICName,CName),
10838	!.
10839
10840unmake_defelem(EnvName,MS,ICName1,irole(RName,iname(IRName),SpecList)) :-
10841	constructRestriction(IRName,SpecList,RestrList),
10842	!,
10843	roleSubsets(Env,_user,MS,IRName,restr(RName,and(RestrList))),
10844	undefprimrole(EnvName,MS,IRName,restr(RName,and(RestrList))),
10845	unmake_defelem_list(EnvName,MS,ICName1,irole(RName,iname(IRName),SpecList)).
10846
10847
10848unmake_defelem_list(EnvName,MS,ICName1,irole(RName,iname(IRName),[])) :-
10849	!.
10850unmake_defelem_list(EnvName,MS,ICName1,irole(RName,iname(IRName),[X|T])) :-
10851	unmake_irole(EnvName,MS,ICName1,irole(RName,iname(IRName),X)),
10852	unmake_defelem_list(EnvName,MS,ICName1,irole(RName,iname(IRName),T)).
10853
10854
10855
10856/**********************************************************************
10857 *
10858 * sb_undefelem(EnvName,MS,ICName1,[X|T])
10859 *
10860 */
10861
10862sb_undefelem(ICName1,SpecListe) :-
10863	getCurrentEnvironment(EnvName),
10864	sb_undefelem(EnvName,[],ICName1,SpecListe).
10865
10866
10867
10868sb_undefelem(A,ICName1,SpecList) :-
10869        environment(A,_,_),
10870	!,
10871	sb_undefelem(A,[],ICName1,SpecList).
10872
10873sb_undefelem(A,ICName1,SpecList) :-
10874	(A = [] ; A = [_|_]),
10875	!,
10876	getCurrentEnvironment(EnvName),
10877	sb_undefelem(EnvName,A,ICName1,SpecList).
10878
10879
10880sb_undefelem(EnvName,MS,ICName1,[]) :-
10881	!.
10882
10883sb_undefelem(EnvName,MS,ICName1,[X|T]):-
10884	unmake_defelem(EnvName,MS,ICName1,X),
10885        sb_undefelem(EnvName,MS,ICName1,T).
10886
10887
10888/**********************************************************************
10889 *
10890 * sb_fact(EnvName,MS,elementOf(X,C),P)
10891 *
10892 */
10893
10894sb_fact(P1) :-
10895	completeParameter([P1],EnvName,MS,Query,Proof),
10896	sb_fact(EnvName,MS,Query,Proof).
10897sb_fact(P1,P2) :-
10898	completeParameter([P1,P2],EnvName,MS,Query,Proof),
10899	sb_fact(EnvName,MS,Query,Proof).
10900sb_fact(P1,P2,P3) :-
10901	completeParameter([P1,P2,P3],EnvName,MS,Query,Proof),
10902	sb_fact(EnvName,MS,Query,Proof).
10903
10904sb_fact(EnvName,MS,isa(X,C),Exp) :-
10905	retractall_head(hypothesis(_)),
10906 	environment(EnvName,Env,_),
10907 	convertMS(negative,Env,[[],true],MS,[],[W1,G1],_),
10908 	getNegatedConcept(C,C1),
10909 	getFactQuery(Env,W1,C,X,Exp,Goal),
10910 	call((call(G1), Goal)),
10911 	atomic(X),
10912	allowedAnswerConcept(Env,C).
10913sb_fact(EnvName,MS,(role(RName,CNameDom,CNameRan)),proved(fact,basedOn(tbox))) :-
10914	environment(EnvName,Env,_),
10915	!,
10916	getRoleDomain(Env,MS,RName,CNameDom),
10917	getRoleRange(Env,MS,RName,CNameRan).
10918sb_fact(EnvName,MS,(attributes(CN,Attribute,Value)),proved(fact,basedOn(tbox))) :-
10919	attribute(concept,EnvName,MS,CN,[Attribute,Value]).
10920sb_fact(EnvName,MS,(attributes(CN,RN,Attribute,Value)),proved(fact,basedOn(tbox))) :-
10921	attribute(role,EnvName,MS,[CN,RN],[Attribute,Value]).
10922sb_fact(EnvName,MS,irole(R,X,Y),Exp) :-
10923	retractall_head(hypothesis(_)),
10924	environment(EnvName,Env,_),
10925	convertMS(negative,Env,[[],true],MS,[],[W1,G1],_),
10926	getFactQuery(Env,W1,Y,R,X,Exp,Goal),
10927	call(G1), 
10928	call(Goal).
10929	
10930getFactQuery(Env,W1,C0,X,Exp,Goal) :-
10931	constructMLHead(Env,_RN1,W1,C0,CON,A1,X,Pc,_CALLS,_D,Exp,Gd,Od,L1,L2,Anc1,Anc2,Y,PT,InHead),
10932%	constructMLHead(Env,_RN1,W1,C0,X,_HYPS,_D,_CALLS,PT,InHead),
10933	getExplanation(InHead,Exp),
10934	Goal = (clause(InHead,Body), ((PT == abox), call(Body))),
10935	!.
10936getFactQuery(Env,W1,Y,R,X,PT,Goal) :-
10937	constructEqHead(Env,_RN1,W1,Y,CON,A1,X,Pc,_CALLS,_D,Exp,Gd,Od,L1,L2,Anc1,Anc2,Y,PT,EqHead),
10938%	constructEqHead(Env,_RN1,W1,Y,_F,R,X,_HYPS,_D,_CALLS,PT,EqHead),
10939	Goal = (clause(EqHead,Body), ((PT == abox), call(Body))),
10940	!.
10941
10942getRoleDomain(Env,MS1,RName,CDom) :-
10943	convertMS(negative,Env,[[],true],MS1,[],[W1,G1],_),
10944	clause(roleDomain(Env,W1,RName,CDom),_),
10945	once((call(G1),roleDomain(Env,W1,RName,CDom))).
10946
10947getRoleRange(Env,MS1,RName,CRange) :-
10948	convertMS(negative,Env,[[],true],MS1,[],[W1,G1],_),
10949	clause(roleRange(Env,W1,RName,CRange),_),
10950	once((call(G1),roleRange(Env,W1,RName,CRange))).
10951
10952getRoleDefault(Env,MS1,RName,CNameDef) :-
10953	convertMS(negative,Env,[[],true],MS1,[],[W1,G1],_),
10954	clause(roleDefault(Env,W1,RName,CNameDef),_),
10955	once((call(G1),roleDefault(Env,W1,RName,CNameDef))).
10956
10957getRoleNr(Env,MS1,RName,MinNr,MaxNr) :- 
10958	convertMS(negative,Env,[[],true],MS1,[],[W1,G1],_),
10959	clause(roleNr(Env,W1,RName,MinNr,MaxNr),_),
10960	once((call(G1),roleNr(Env,W1,RName,MinNr,MaxNr))).
10961
10962getRoleDefNr(Env,MS1,R1,DefNr) :-
10963	convertMS(negative,Env,[[],true],MS1,[],[W1,G1],_),
10964	clause(roleDefNr(Env,W1,R1,DefNr),_),
10965	once((call(G1),roleDefNr(Env,W1,R1,DefNr))).
10966
10967getRoleTripel(Env,MS1,RName1,CNameDomT,CNameT,CNameDefT) :-
10968	convertMS(Env,[[],true],MS1,[],[W1,G1],_),
10969	clause(roleTripel(Env,W1,RName1,CNameDomT,CNameT,CNameDefT),_),
10970	once((call(G1),roleTripel(Env,W1,RName1,CNameDomT,CNameT,CNameDefT))).
10971
10972getConceptElement(Env,MS1,X,C) :-
10973	convertMS(Env,[[],true],MS1,[],[W1,G1],_),
10974	clause(conceptElement(Env,_W1,_,X,C,_),_),
10975	once((call(G1),conceptElement(Env,W1,_,X,C,_))).
10976/**********************************************************************
10977 *
10978 * @(#) tellConcept.pl 1.9@(#)
10979 *
10980 */
10981
10982/***********************************************************************
10983 *
10984 * assertConceptRInL(+ModalSequence,+CT1,+CT2,+AxiomName)
10985 * asserts membership clauses for the inclusion of CT2 into CT1 in 
10986 * modal context ModalSequence.
10987 *
10988 */
10989
10990assertConceptRInL(Env,Name,MS,CT1,CT2) :-
10991	assertConceptLInR(Env,Name,MS,CT2,CT1).
10992
10993/***********************************************************************
10994 *
10995 * assertConceptLInR(+ModalSequence,+ConceptName,+Constraint,+AxiomName)
10996 * asserts membership clauses for the inclusion of ConceptName into
10997 * Constraint in modal context ModalSequence.
10998 *
10999 */
11000
11001assertConceptLInR(_Env,rn(_,_,_),_MS,_CN,or([])) :- 
11002	!.
11003assertConceptLInR(Env,rn(AxiomName,Source,Orientation),MS,CN,or([CT1|CTs])) :-
11004	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11005	hop_map(negate,[CT1|CTs],NCTs),
11006	convertInAntecedentList(Env,rn(AxiomName,_AnySource1,Orientation),
11007                                bodyMC(W1),headMC(W1),NCTs,X,
11008				HYPS,AB,CALLS,PTL,INCTs),
11009	convertInAntecedent(Env,rn(AxiomName,_AnySource2,Orientation),
11010                            bodyMC(W1),headMC(W1),
11011			    CN,X,HYPS,AB,CALLS,PT1,Body),
11012	assertOrConceptLInR(Env,rn(AxiomName,Source,Orientation),
11013                            MS,PT1,Body,W1,G1,X,HYPS,AB,CALLS,
11014                            or([CT1|CTs]),[],[],PTL,INCTs).
11015assertConceptLInR(_Env,rn(_,_,_),_MS,_CN,and([])) :-
11016	!.
11017assertConceptLInR(Env,rn(AxiomName,O2,Orientation2),MS,CN1,and([CN2|CTs])) :-
11018	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11019	convertInAntecedent(Env,rn(AxiomName,_AnySource1,Orientation2),
11020                            bodyMC(W1),headMC(W1),
11021			    CN1,X,HYPS,AB,CALLS,PT1,Body),
11022	gensym(rule,RuleName),
11023	ruleName(AxiomName,RuleName,O2,Orientation2,RN2),
11024	convertInConsequence(Env,pr(3),RN2,MS,W1,CN2,X,HYPS,AB,CALLS,PT1,InHead2),
11025	constructMLMark(InHead2,Mark2),
11026%	asserta_logged((InHead2 :- (cCS(CALLS,Mark2), (call(G1), once(Body))))),
11027	asserta_logged((InHead2 :- (cCS(CALLS,Mark2), (call(G1), Body)))),
11028	assertConceptLInR(Env,rn(AxiomName,O2,Orientation2),MS,CN1,and(CTs)),
11029	!.
11030assertConceptLInR(Env,rn(AxiomName,O,Orientation),MS,CN,set(Set1)) :-
11031	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11032	gensym(rule,RuleName),
11033	ruleName(AxiomName,RuleName,O,Orientation,RN1),
11034	convertInConsequence(Env,pr(3),RN1,MS,W1,set(Set1),X,HYPS,AB,CALLS,PT1,InHead1),
11035	constructMLMark(InHead1,Mark1),
11036	convertInAntecedent(Env,rn(AxiomName,_AnySource1,Orientation),
11037			    bodyMC(W1),headMC(W1),
11038	                    CN,X,HYPS,AB,CALLS,PT1,Body),
11039	asserta_logged((InHead1 :- (cCS(CALLS,Mark1), (call(G1), once(Body))))),
11040	!.
11041assertConceptLInR(Env,rn(AxiomName,O,Orientation),MS,CN,not(set(Set1))) :-
11042	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11043	gensym(rule,RuleName),
11044	ruleName(AxiomName,RuleName,O,Orientation,RN1),
11045	convertInConsequence(Env,pr(3),RN1,MS,W1,not(set(Set1)),X,HYPS,AB,CALLS,PT1,InHead1),
11046	constructMLMark(InHead1,Mark1),
11047	convertInAntecedent(Env,rn(AxiomName,_AnySource1,Orientation),
11048			    bodyMC(W1),headMC(W1),
11049	                    CN,X,HYPS,AB,CALLS,PT1,Body),
11050	asserta_logged((InHead1 :- (cCS(CALLS,Mark1), (call(G1), once(Body))))),
11051	!.
11052assertConceptLInR(Env,rn(AxiomName,O,Orientation),MS,CN,not(D)) :-
11053	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11054	gensym(rule,RuleName),
11055	ruleName(AxiomName,RuleName,O,Orientation,RN1),
11056	convertInConsequence(Env,pr(3),RN1,MS,W1,not(D),X,HYPS,AB,CALLS,PT1,InHead1),
11057	constructMLMark(InHead1,Mark1),
11058	convertInAntecedent(Env,rn(AxiomName,_AnySource1,Orientation),
11059			    bodyMC(W1),headMC(W1),
11060	                    CN,X,HYPS,AB,CALLS,PT1,Body),
11061	asserta_logged((InHead1 :- (cCS(CALLS,Mark1), (call(G1), once(Body))))),
11062	!.
11063assertConceptLInR(Env,rn(AxiomName,O,Orientation),MS,CN,naf(D)) :-
11064	% in the consequence not and naf have the same meaning
11065	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11066	gensym(rule,RuleName),
11067	ruleName(AxiomName,RuleName,O,Orientation,RN1),
11068	convertInConsequence(Env,pr(3),RN1,MS,W1,not(D),X,HYPS,AB,CALLS,PT1,InHead1),
11069	constructMLMark(InHead1,Mark1),
11070	convertInAntecedent(Env,rn(AxiomName,_AnySource1,Orientation),
11071			    bodyMC(W1),headMC(W1),
11072	                    CN,X,HYPS,AB,CALLS,PT1,Body),
11073	asserta_logged((InHead1 :- (cCS(CALLS,Mark1), (call(G1), once(Body))))),
11074	!.
11075assertConceptLInR(Env,rn(AxiomName,O,Orientation),MS,CN,all(R,D)) :-
11076	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11077	gensym(rule,RuleName),
11078	ruleName(AxiomName,RuleName,O,Orientation,RN1),
11079	convertInConsequence(Env,pr(3),RN1,MS,W1,all(R,D),X,HYPS,AB,CALLS,PT1,
11080	                     (EqLiteral, InHead1)),
11081	constructMLMark(InHead1,Mark1),
11082	convertInAntecedent(Env,rn(AxiomName,_AnySource1,Orientation),
11083                            bodyMC(W1),headMC(W1),CN,X,HYPS,AB,CALLS,PT1,Body),
11084	asserta_logged((InHead1 :- (cCS(CALLS,Mark1), (call(G1), (EqLiteral, Body))))),
11085	!.
11086assertConceptLInR(Env,rn(AxiomName,O,Orientation),MS,CN,some(R,D)) :-
11087	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11088	gensym(rule,RuleName),
11089	ruleName(AxiomName,RuleName,O,Orientation,RN1),
11090	convertInConsequence(Env,pr(3),RN1,MS,W1,some(R,D),X,
11091	                     HYPS,AB,CALLS,PT1,(EqLiteral, InHead1)),
11092	constructMLMark(InHead1,Mark1),
11093	convertInAntecedent(Env,rn(AxiomName,_AnySource1,Orientation),
11094	                    bodyMC(W1),headMC(W1),CN,X,HYPS,AB,CALLS,PT1,Body),
11095	asserta_logged((InHead1 :- (cCS(CALLS,Mark1), (call(G1), (EqLiteral, Body))))),
11096	gensym(rule,RuleName2),
11097	ruleName(AxiomName,RuleName2,system,Orientation,RN2),
11098	convertInConsequence(Env,pr(3),RN2,MS,W1,normal(R),X,
11099			     HYPS,AB,CALLS,PT2,InHead2),
11100	constructMLMark(InHead2,Mark2),
11101	asserta_logged((InHead2 :- cCS(CALLS,Mark2), (call(G1), Body))),
11102	!.
11103assertConceptLInR(Env,rn(AxiomName,_S,Orientation),MS,CN,atleast(N,R)) :-
11104	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11105	convertInAntecedent(Env,rn(AxiomName,_AnySource1,Orientation),
11106	                    bodyMC(W1),headMC(W1),CN,X,HYPS,AB,CALLS,PT1,Body),
11107	typeOfDefinition(Env,MS,R,S1),
11108	gensym(rule,RuleName),
11109	ruleName(AxiomName,RuleName,S1,Orientation,RN1),
11110	convertInConsequence(Env,pr(3),RN1,MS,W1,atleast(N,R),X,
11111			     HYPS,AB,CALLS,PT1,InHead1),
11112	constructConMark(InHead1,Mark1),
11113	asserta_logged((InHead1 :- (cCS(CALLS,Mark1), (call(G1), once(Body))))),
11114	!.
11115assertConceptLInR(Env,rn(AxiomName,O,Orientation),MS,CN,atmost(N,R)) :-
11116	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11117	convertInAntecedent(Env,rn(AxiomName,_AnySource1,Orientation),
11118                            bodyMC(W1),headMC(W1),CN,X,HYPS,AB,CALLS,PT1,Body),
11119	typeOfDefinition(Env,MS,R,O1),
11120	gensym(rule,RuleName),
11121	ruleName(AxiomName,RuleName,O1,Orientation,RN1),
11122	convertInConsequence(Env,pr(3),RN1,MS,W1,atmost(N,R),X,
11123			     HYPS,AB,CALLS,PT1,InHead1),
11124	constructConMark(InHead1,Mark1),
11125	asserta_logged((InHead1 :- (cCS(CALLS,Mark1), once((call(G1), Body))))),
11126	!.
11127assertConceptLInR(Env,rn(AxiomName,O,Orientation),MS,CN,b(MOp,P1,D)) :-
11128	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11129	gensym(rule,RuleName),
11130	ruleName(AxiomName,RuleName,O,Orientation,RN1),
11131	genagent(P1,free,P),
11132	C1 = rel(Env,_,m(MOp,P),W1,W2),
11133	convertInAntecedent(Env,rn(AxiomName,_AnySource1,Orientation),
11134	                    bodyMC(W1),headMC(W2),CN,X,HYPS,AB,CALLS,PT1,Body),
11135	constructMLHead(Env,RN1,W2,D,X,HYPS,AB,CALLS,and([C1,PT1]),InHead1),
11136	constructMLMark(InHead1,Mark1),
11137	asserta_logged((InHead1 :- (cCS(CALLS,Mark1), (call(G1), (C1, Body))))),
11138	!.
11139assertConceptLInR(Env,rn(AxiomName,O,Orientation),MS,CN,bc(MOp,C,D)) :-
11140	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11141	gensym(rule,RuleName),
11142	ruleName(AxiomName,RuleName,O,Orientation,RN1),
11143	genagent(P1,free,P),
11144	C1 = rel(Env,_,m(MOp,P),W1,W2),
11145	convertInAntecedent(Env,rn(AxiomName,_AnySource1,Orientation),
11146	                    bodyMC(W1),headMC(W2),CN,X,HYPS,AB,CALLS,PT1,Body1),
11147	convertInAntecedent(Env,rn(AxiomName,_AnySource2,Orientation),
11148			    bodyMC(W1),headMC(W2),C,P,HYPS,AB,CALLS,PT2,Body2),
11149	constructMLHead(Env,RN1,W2,D,X,HYPS,AB,CALLS,and([C1,PT1,PT2]),InHead1),
11150	constructMLMark(InHead1,Mark1),
11151	asserta_logged((InHead1 :- (cCS(CALLS,Mark1), (call(G1), (C1, (Body1, Body2)))))),
11152	!.
11153assertConceptLInR(Env,rn(AxiomName,_S1,Orientation),MS,CN,d(MOp,P1,D)) :-
11154	convertMS(positive,Env,[[],true],MS,[],[W1,G1],WVL),
11155	gensym(rule,RuleName),
11156	typeOfDefinition(Env,MS,D,S2),
11157	ruleName(AxiomName,RuleName,S2,Orientation,RN1),
11158	gensym(wp,WP),
11159	WPTerm =.. [WP,WVL],
11160	genagent(P1,skolemize,P),
11161	W2 = app(WPTerm:m(MOp,P),W1),
11162	convertInAntecedent(Env,rn(AxiomName,_AnySource1,Orientation),
11163                            bodyMC(W1),headMC(W2),CN,X,HYPS,AB,CALLS,PT1,Body),
11164	constructMLHead(Env,RN1,W2,D,X,HYPS,AB,CALLS,
11165			PT1,InHead1),
11166	constructMLMark(InHead1,Mark1),
11167	asserta_logged((InHead1 :- (cCS(CALLS,Mark1), (call(G1), Body)))),
11168	!.
11169assertConceptLInR(Env,rn(AxiomName,_S1,Orientation),MS,CN,dc(MOp,C,D)) :-
11170	convertMS(positive,Env,[[],true],MS,[],[W1,G1],WVL),
11171	gensym(rule,RuleName),
11172	typeOfDefinition(Env,MS,D,S2),
11173	ruleName(AxiomName,RuleName,S2,Orientation,RN1),
11174	gensym(wp,WP),
11175	WPTerm =.. [WP,WVL],
11176	genagent(P1,skolemize,P),
11177	W2 = app(WPTerm:m(MOp,P),W1),
11178	convertInAntecedent(Env,rn(AxiomName,_AnySource1,Orientation),
11179                            bodyMC(W1),headMC(W2),CN,X,HYPS,AB,CALLS,PT1,Body1),
11180	convertInAntecedent(Env,rn(AxiomName,_AnySource2,Orientation),
11181                            bodyMC(W1),headMC(W2),C,P,HYPS,AB,CALLS,PT2,Body2),
11182	constructMLHead(Env,RN1,W2,D,X,HYPS,AB,CALLS,
11183			and([PT1,PT2]),InHead1),
11184	constructMLMark(InHead1,Mark1),
11185	asserta_logged((InHead1 :- (cCS(CALLS,Mark1), (call(G1), (Body1, Body2))))),
11186	!.
11187assertConceptLInR(Env,rn(AxiomName,O,Orientation),MS,CN,ConceptTerm) :-
11188	assertConceptLInR(Env,rn(AxiomName,O,Orientation),MS,CN,and([ConceptTerm])).
11189
11190/***********************************************************************
11191 * 
11192 * assertOrConceptLInR(Env,MS,InHead,or([CT1|CTs]),[INCT|CTs]),AxiomName).
11193 *
11194 */
11195
11196assertOrConceptLInR(_Env,rn(_,_,_),_MS,_PT1,_InHead,_W1,_G1,_X,_HYPS,_,_CALLS,
11197		    or([]),_FPTL,_First,_PTL,_INCTs) :-
11198	!.
11199assertOrConceptLInR(Env,rn(AxiomName,O,Orientation),
11200                    MS,PT2,InHead2,W1,G1,X,HYPS,AB,CALLS,or([CT1|CTs]),
11201	            FPTL,First,[PT1|PTL1],[INCT1|INCTs]) :-
11202	append([InHead2|First],INCTs,L1),
11203	append([PT2|FPTL],PTL1,PTL),
11204%	hop_map(getInExplanation,L1,PTL),
11205	constructBody(L1,Body),
11206	gensym(rule,RuleName),
11207	typeOfDefinition(Env,MS,CT1,S1),
11208	ruleName(AxiomName,RuleName,S1,Orientation,RN1),
11209	constructMLHead(Env,RN1,W1,CT1,X,HYPS,AB,CALLS,and(PTL),InHead1),
11210	constructMLMark(InHead1,Mark1),
11211	asserta_logged((InHead1 :- (cCS(CALLS,Mark1), once((call(G1), Body))))),
11212	append(First,[INCT1],L2),
11213	append(FPTL,[PT1],FPTL2),
11214	!,
11215	assertOrConceptLInR(Env,rn(AxiomName,O,Orientation),
11216                            MS,PT2,InHead2,W1,G1,X,HYPS,AB,CALLS,or(CTs),
11217                            FPTL2,L2,PTL1,INCTs).
11218
11219constructBody([],true) :-
11220	!.
11221constructBody([I1],I1) :-
11222	!.
11223constructBody([I1|IL],(I1,B1)) :-
11224	constructBody(IL,B1).
11225
11226
11227/***********************************************************************
11228 *
11229 * convertAndList(+ModalSequence,+ConceptTermList,
11230 *                   -X,-CallStack,-InTermStructure,+AxiomName)
11231 *
11232 */
11233
11234convertAndList(_,_,_,_,[],_X,_HYPS,_,_CALLS,true,[]).
11235convertAndList(Env,Name,MC1,MC2,[CTerm],X,HYPS,AB,CALLS,InCTerm,[PT1]) :-
11236	convertInAntecedent(Env,Name,MC1,MC2,
11237                            CTerm,X,HYPS,AB,CALLS,PT1,InCTerm).
11238convertAndList(Env,Name,MC1,MC2,[CTerm|CTermList],X,HYPS,AB,CALLS,
11239	       (InCTerm,InCTermList),[PT1|PTL]) :-
11240	convertInAntecedent(Env,Name,MC1,MC2,
11241                            CTerm,X,HYPS,AB,CALLS,PT1,InCTerm),
11242	convertAndList(Env,Name,MC1,MC2,CTermList,X,
11243		       HYPS,AB,CALLS,InCTermList,PTL).
11244
11245/***********************************************************************
11246 *
11247 * convertOrList(+ModalSequence,+ConceptTermList,
11248 *                   -X,-CallStack,-InTermStructure,+AxiomName)
11249 *
11250 */
11251
11252convertOrList(_,_,_,[],_X,_HYPS,_AB,_CALLS,_AX,false,[]).
11253convertOrList(Env,Name,MC1,MC2,[CTerm],X,HYPS,AB,CALLS,InCTerm,[PT1]) :-
11254	convertInAntecedent(Env,Name,MC1,MC2,
11255                            CTerm,X,HYPS,AB,CALLS,PT1,InCTerm),
11256	!.
11257convertOrList(Env,Name,bodyMC(W1),MC2,[CTerm|CTermList],X,
11258	      HYPS,AB,CALLS,
11259              (InCTerm ; (InCTermList)),[PT1|PTL]) :-
11260	HYPS = [or(H1),rl(H2),fl(H3)],
11261	Name = rn(AX,_S,_O),
11262	convertInAntecedent(Env,Name,bodyMC(W1),MC2,
11263                            CTerm,X,HYPS,AB,CALLS,PT1,InCTerm),
11264	convertOrList(Env,Name,bodyMC(W1),MC2,CTermList,X,
11265		      HYPS,AB,CALLS,InCTermList,PTL).
11266% convertOrList(Env,Name,bodyMC(W1),MC2,[CTerm|CTermList],X,
11267% 	      HYPS,AB,CALLS,
11268%               (InCTerm ; (NewH1 = [HypTerm|H1], InCTermList)),[PT1|PTL]) :-
11269% 	HYPS = [or(H1),rl(H2),fl(H3)],
11270% 	Name = rn(AX,_S,_O),
11271% 	convertInAntecedent(Env,Name,bodyMC(W1),MC2,
11272%                             CTerm,X,HYPS,AB,CALLS,PT1,InCTerm),
11273% 	negate(CTerm,CTermN),
11274% 	constructMLHead(Env,rn(AX,_RN1,_S1,_O1),W1,CTermN,X,
11275% 			HYPS,AB,_CALLS,_,HypMLTerm),
11276% 	constructMLMark(HypMLTerm,HypTerm),
11277% 	convertOrList(Env,Name,bodyMC(W1),MC2,CTermList,X,
11278%		      [or(NewH1),rl(H2),fl(H3)],AB,CALLS,InCTermList,PTL).
11279
11280/***********************************************************************
11281 *
11282 * convertInAntecedent(Env,+ModalSequence,+ConceptTerm,-Variable,
11283 *              -Hypotheses,-CallStack,+AxiomName,-InLiteral)
11284 *
11285 */
11286
11287convertInAntecedent(Env,rn(AX,S1,_O),MC1,MC2,D,X,HYPS,AB,CALLS,PT1,InHead) :-
11288	(var(D) ; atomic(D)),
11289	!,
11290	constructMLCall(Env,rn(AX,_RN1,S1,_O1),MC1,MC2,
11291	                D,X,HYPS,AB,CALLS,PT1,InHead).
11292convertInAntecedent(Env,rn(AX,Source,_O),MC1,MC2,set(S1),X,HYPS,AB,CALLS,PT1,Body) :-
11293	constructMLCall(Env,rn(AX,_RN1,Source,_O1),MC1,MC2,
11294	                set(S1),X,HYPS,AB,CALLS,PT1,InHead1),
11295	Body = ((nonvar(S1), (nongeneric(X), member(X,S1))) ; InHead1),
11296	!.
11297convertInAntecedent(Env,rn(AX,Source,_O),MC1,MC2,not(set(S1)),X,HYPS,AB,CALLS,PT1,Body) :-
11298	constructMLCall(Env,rn(AX,_RN1,Source,_O1),MC1,MC2,
11299	                not(set(S1)),X,HYPS,AB,CALLS,PT1,InHead1),
11300	Body = ((nonvar(S1), (atomic(X), (nongeneric(X), not(member(X,S1)))) ; InHead1)),
11301	!.
11302convertInAntecedent(Env,Name,MC1,MC2,and(L),X,HYPS,AB,CALLS,and(PTL),Body) :-
11303	convertAndList(Env,Name,MC1,MC2,L,X,HYPS,AB,CALLS,Body,PTL),
11304	!.
11305convertInAntecedent(Env,Name,MC1,MC2,or(L),X,HYPS,AB,CALLS,or(PTL),Body) :-
11306	convertOrList(Env,Name,MC1,MC2,L,X,HYPS,AB,CALLS,Body,PTL),
11307	!.
11308convertInAntecedent(Env,rn(AX,S1,_O1),MC1,MC2,
11309	            not(D),X,HYPS,AB,CALLS,PT1,Body) :-
11310	constructMLCall(Env,rn(AX,_RN,S1,_O2),MC1,MC2,
11311	                not(D),X,HYPS,AB,CALLS,PT1,InHead),
11312	Body = InHead,
11313	!.
11314convertInAntecedent(Env,rn(AX,S1,_O1),bodyMC(MS1),MC2,
11315	            naf(D),X,HYPS,AB,CALLS,PT1,Body) :-
11316	% in the antecedent `x in naf(D) is provable' means 
11317	% `x in D is not provable'
11318	atomic(D),
11319	!,
11320	HYPS = [or(H1),rl(H2),fl(H3)],
11321	NewHYPS = [or(H1),rl([]),fl(H3)],
11322	convertInAntecedent(Env,rn(AX,S1,_O1),bodyMC(MS1),MC2,D,X,NewHYPS,
11323			    AB,CALLS,PT2,BodyD),
11324	PT1 = byDefault(in(MS1,not(D),X),hyp(NewHYPS),basedOn([])),
11325	constructMLHead(Env,rn(AX,_RN3,_S3,_O3),MS1,not(D),X,
11326			HYPS,AB,_CALLS,_,DefaultMLTerm),
11327	constructMLMark(DefaultMLTerm,DefaultTerm),
11328	L1 = addDefaultML(DefaultTerm,H3),
11329%	L1 = asserta_logged(hypothesis(in(Env,modal(MS1),not(D),X,hyp(HYPS),ab(AB),PT1))),
11330	constructMLMark(BodyD,MarkD),
11331	Body = (member(MarkD,HYPS) ; (nongeneric(X), (not(BodyD), nongeneric(X), L1))),
11332	!.
11333convertInAntecedent(Env,rn(AX,S1,_O1),bodyMC(MS1),MC2,
11334	            naf(D),X,HYPS,AB,CALLS,PT1,Body) :-
11335	% in the antecedent `x in naf(D) is provable' means 
11336	% `x in D is not provable'
11337	HYPS = [or(H1),rl(H2),fl(H3)],
11338	NewHYPS = [or(H1),rl([]),fl(H3)],
11339	convertInAntecedent(Env,rn(AX,S1,_O1),bodyMC(MS1),MC2,D,X,NewHYPS,
11340			    AB,CALLS,PT2,BodyD),
11341	constructMLMark(BodyD,MarkD),
11342	normalizeNot(not(D),D1),
11343	PT1 = byDefault(in(MS1,D1,X),hyp(NewHYPS),basedOn([])),
11344	constructMLHead(Env,rn(AX,_RN3,_S3,_O3),MS1,D1,X,
11345			HYPS,AB,_CALLS,_,DefaultMLTerm),
11346	constructMLMark(DefaultMLTerm,DefaultTerm),
11347	L1 = addDefaultML(DefaultTerm,H3),
11348%	L1 = asserta_logged(hypothesis(in(Env,modal(MS1),D1,X,hyp(HYPS),ab(AB),PT1))),
11349	Body = (nongeneric(X), (not(BodyD), nongeneric(X), L1)),
11350	!.
11351convertInAntecedent(Env,rn(AX,S1,_O1),MC1,MC2,
11352	            all(R,D),X,HYPS,AB,CALLS,or([and([PT2,PT1]),PT3]),
11353                    ((EqLiteral, Body); (InHead2; (C1, (C2, C3))))) :-
11354	% create a new mskolem constant 
11355	gensymbol(mskolem,[X,Y],SF),
11356	% construct equational literal
11357	constructEqCall(Env,rn(AX,_RN1,_S2,_O2),MC1,MC2,Y,SF,R,X,HYPS,AB,CALLS,PT2,EqLiteral),
11358	convertInAntecedent(Env,rn(AX,S1,_O3),MC1,MC2,D,Y,HYPS,AB,CALLS,PT1,Body),
11359	constructMLCall(Env,rn(AX,_RN4,_S4,_O4),MC1,MC2,not(normal(R)),X,HYPS,AB,CALLS,PT3,InHead2),
11360	MC1 = bodyMC(W1),
11361	C1 = closed(Env,MS,X,_,R),
11362	C2 = collectAllFillers(Env,W1,R,X,HYPS,D,CALLS,S),
11363	C3 = mapGoal(Body,Y,S),
11364	!.
11365convertInAntecedent(Env,rn(AX,S1,_O1),MC1,MC2,
11366	            some(R,D),X,HYPS,AB,CALLS,and([PT2,PT1]),(EqLiteral, once((Body, ground(Y,true))))) :-
11367	/* construct equational literal */
11368	constructEqCall(Env,rn(AX,_RN1,S1,_O2),MC1,MC2,Y,_FF,R,X,
11369			HYPS,AB,CALLS,PT2,EqLiteral),
11370	convertInAntecedent(Env,rn(AX,_S2,_O3),MC1,MC2,D,Y,
11371			    HYPS,AB,CALLS,PT1,Body),
11372	!.
11373convertInAntecedent(Env,rn(AX,_S,_O1),bodyMC(MS1),headMC(MS2),
11374	            atleast(N,R),X,HYPS,AB,CALLS,PT1,Body) :-
11375	% construct equational literal
11376	constructSolveConMark(rn(AX,_RN1,_S1,_O2),MS2,_FF1,R,X,'>=',N,
11377			 HYPS,AB,CALLS,Mark),
11378	Body = solveConstraint(Env,MS1,(card,app((_FF2:R),X),'>=',N),_,
11379			       hyp(HYPS),ab(AB),call([Mark|CALLS]),PT1),
11380	!.
11381convertInAntecedent(Env,rn(AX,_S,_O),bodyMC(MS1),headMC(MS2),
11382	            atmost(N,R),X,HYPS,AB,CALLS,PT1,Body) :-
11383	/* construct right term */
11384        constructSolveConMark(rn(AX,_RN1,_S1,_O1),MS2,_FF1,R,X,'=<',N,
11385			 HYPS,AB,CALLS,Mark),
11386	Body = solveConstraint(Env,MS1,(card,app((_FF2:R),X),'=<',N),_,
11387			       hyp(HYPS),ab(AB),call([Mark|CALLS]),PT1),
11388	!.
11389convertInAntecedent(Env,rn(AX,S1,_O),bodyMC(MSnew),headMC(MSold),
11390	            b(MOp,P1,D),X,HYPS,AB,CALLS,PT1,InHead) :-
11391	/* construct right term */
11392	gensym(wp,WP),
11393	genagent(P1,skolemize,P),
11394	MS1 = app(WP:m(MOp,P),MSnew),
11395        constructMLCall(Env,rn(AX,_RN1,S1,_O1),bodyMC(MS1),headMC(MSold),
11396	                D,X,HYPS,AB,CALLS,PT1,InHead),
11397	!.
11398convertInAntecedent(Env,rn(AX,S1,_O),bodyMC(MSnew),headMC(MSold),
11399	            bc(MOp,C,D),X,HYPS,AB,CALLS,and([PT1,PT2]),(InHead, Body)) :-
11400	/* construct right term */
11401	gensym(wp,WP),
11402	genagent(P1,skolemize,P),
11403	MS1 = app(WP:m(MOp,P),MSnew),
11404        constructMLCall(Env,rn(AX,_RN1,S1,_O1),bodyMC(MS1),headMC(MSold),
11405	                D,X,HYPS,AB,CALLS,PT1,InHead),
11406	convertInAntecedent(Env,rn(AX,_RN2,_S2),bodyMC(MSold),headMC(MSold),
11407			    C,P,HYPS,AB,CALLS,PT2,Body),
11408	!.
11409convertInAntecedent(Env,rn(AX,S1,_O),bodyMC(MSnew),headMC(MSold),
11410	            d(MOp,P1,D),X,HYPS,AB,CALLS,PT1,InHead) :-
11411	/* construct right term */
11412        genagent(P1,free,P),
11413        MS1 = app(_FF:m(MOp,P),MSnew),
11414        constructMLCall(Env,rn(AX,_RN1,S1,_O1),bodyMC(MS1),headMC(MSold),
11415	                D,X,HYPS,AB,CALLS,PT1,InHead),
11416	!.
11417convertInAntecedent(Env,rn(AX,S1,_O),bodyMC(MSnew),headMC(MSold),
11418	            dc(MOp,C,D),X,HYPS,AB,CALLS,and([PT1,PT2]),(InHead, Body)) :-
11419	/* construct right term */
11420        genagent(P1,free,P),
11421        MS1 = app(_FF:m(MOp,P),MSnew),
11422        constructMLCall(Env,rn(AX,_RN1,S1,_O1),bodyMC(MS1),headMC(MSold),
11423	                D,X,HYPS,AB,CALLS,PT1,InHead),
11424	convertInAntecedent(Env,rn(AX,_RN2,_S2),bodyMC(MSold),headMC(MSold),
11425			    C,P,HYPS,AB,CALLS,PT2,Body),
11426	!.
11427convertInAntecedent(Env,rn(AX,S,_O),MC1,MC2,D,X,HYPS,AB,CALLS,PT1,InHead) :-
11428	CON = X,
11429	constructMLCall(Env,rn(AX,_RN1,S,_O1),MC1,MC2,
11430	                D,CON,HYPS,AB,CALLS,PT1,InHead),
11431	!.
11432
11433convertInAntecedentList(_Env,_,_,_,[],_X,_HYPS,_AB,_CALLS,[],[]) :-
11434	!.
11435convertInAntecedentList(Env,Name,MC1,MC2,[NCT],X,HYPS,AB,CALLS,[PT1],[INCT]) :-
11436	convertInAntecedent(Env,Name,MC1,MC2,NCT,X,HYPS,AB,CALLS,PT1,INCT),
11437	!.
11438convertInAntecedentList(Env,Name,MC1,MC2,[NCT|NCTs],X,
11439                        HYPS,AB,CALLS,[PT1|PTL],[INCT|INCTs]) :-
11440	convertInAntecedent(Env,Name,MC1,MC2,NCT,X,HYPS,AB,CALLS,PT1,INCT),
11441	convertInAntecedentList(Env,Name,MC1,MC2,NCTs,X,
11442				HYPS,AB,CALLS,PTL,INCTs).
11443
11444
11445/***********************************************************************
11446 *
11447 * convertInConsequence(Env,+ModalSequence,+ConceptTerm,-X,
11448 *               -Hypotheses, -CallStack, +AxiomName,
11449 *               -InLiteral)
11450 *
11451 */
11452
11453convertInConsequence(Env,Pr,rn(AX,RN,_S,O),MS,W1,D,X,HYPS,AB,CALLS,PT1,InHead) :-
11454	(var(D) ; atomic(D)),
11455	!,
11456	typeOfDefinition(Env,MS,D,S2),
11457	constructKBHead(Env,Pr,rn(AX,RN,S2,O),W1,D,X,HYPS,AB,CALLS,PT1,InHead).
11458convertInConsequence(Env,Pr,rn(AX,RN,_S,O),MS,W1,some(R,D),X,
11459                     HYPS,AB,CALLS,PT1,(EqLiteral, InHead)) :-
11460	% construct equational literal
11461	gensymbol(mskolem,[X,Y],SF),
11462	constructEqCall(Env,rn(AX,_RN2,_S2,_O2),bodyMC(W1),headMC(W1),
11463	                Y,SF,R,X,HYPS,AB,CALLS,PT2,EqLiteral),
11464	typeOfDefinition(Env,MS,D,S2),
11465	convertInConsequence(Env,Pr,rn(AX,RN,S2,O),MS,W1,D,Y,HYPS,AB,CALLS,
11466                             and([PT1,PT2]),InHead),
11467	!.
11468convertInConsequence(Env,Pr,rn(AX,RN,_S,O),MS,W1,all(R,D),X,
11469                     HYPS,AB,CALLS,PT1,((EqCall, ground(Y,true)), InHead)) :-
11470	% construct equation literal
11471	constructEqCall(Env,rn(AX,_RN2,_S2,_O2),bodyMC(W1),headMC(W1),
11472	                Y,_FF,R,X,HYPS,AB,CALLS,PT2,EqCall),
11473	typeOfDefinition(Env,MS,D,S2),
11474	convertInConsequence(Env,Pr,rn(AX,RN,S2,O),MS,W1,D,Y,HYPS,AB,CALLS,
11475                             and([PT1,PT2]),InHead),
11476	!.
11477convertInConsequence(Env,_Pr,Name,_MS,W1,atleast(N,R),X,
11478                     HYPS,AB,CALLS,PT1,InHead) :-
11479	/* construct role term */
11480        constructConHead(Env,Name,W1,_FF,R,X,'>=',N,HYPS,AB,CALLS,PT1,InHead),
11481	!.
11482convertInConsequence(Env,_Pr,Name,_MS,W1,atmost(N,R),X,HYPS,AB,CALLS,PT1,InHead) :-
11483	/* construct role term */
11484        constructConHead(Env,Name,W1,_FF,R,X,'=<',N,HYPS,AB,CALLS,PT1,InHead),
11485	!.
11486convertInConsequence(Env,Pr,rn(AX,RN,_S,O),MS,W1,not(D),X,
11487                     HYPS,AB,CALLS,PT1,InHead) :-
11488	typeOfDefinition(Env,MS,D,S2),
11489	constructKBHead(Env,Pr,rn(AX,RN,S2,O),W1,not(D),X,
11490			HYPS,AB,CALLS,PT1,InHead),
11491	!.
11492convertInConsequence(Env,Pr,rn(AX,RN,_S,O),MS,W1,naf(D),X,
11493                     HYPS,AB,CALLS,PT1,InHead) :-
11494	% in the consequence not and naf have the same meaning
11495	typeOfDefinition(Env,MS,D,S2),
11496	constructKBHead(Env,Pr,rn(AX,RN,S2,O),W1,not(D),X,
11497			HYPS,AB,CALLS,PT1,InHead),
11498	!.
11499convertInConsequence(Env,Pr,rn(AX,RN,_S,O),MS,W1,set(Set1),X,
11500                     HYPS,AB,CALLS,PT1,InHead) :-
11501	typeOfDefinition(Env,MS,D,S2),
11502	constructKBHead(Env,Pr,rn(AX,RN,S2,O),W1,set(Set1),X,
11503			HYPS,AB,CALLS,PT1,InHead),
11504	!.
11505%convertInConsequence(Env,rn(AX,RN,_S,O),MS,W1,b(MOp,P,D),X,
11506%                      HYPS,AB,CALLS,PT1,InHead) :-
11507%	gensym(wp,WP),
11508%	MS1 = app(WP:m(MOp,P),W1),
11509%	typeOfDefinition(Env,MS,D,S2),
11510%	constructMLHead(Env,rn(AX,RN,S2,O),MS1,D,X,HYPS,AB,CALLS,PT1,InHead),
11511%	!.
11512%convertInConsequence(Env,rn(AX,RN,_S,O),MS,W1,d(MOp,P,D),X,HYPS,CALLS,PT1,InHead) :-
11513%	MS1 = app(WP:m(MOp,P),W1),
11514%	typeOfDefinition(Env,MS,D,S2),
11515%	constructMLHead(Env,rn(AX,RN,S2,O),MS1,D,X,HYPS,AB,CALLS,PT1,InHead),
11516%	!.
11517convertInConsequence(Env,Pr,rn(AX,RN,_S,O),MS,W1,D,X,HYPS,AB,CALLS,PT1,InHead) :-
11518	/* add loop check to control list */
11519        CON = X,
11520	typeOfDefinition(Env,MS,D,S2),
11521	constructKBHead(Env,Pr,rn(AX,RN,S2,O),W1,D,CON,HYPS,AB,CALLS,PT1,InHead).
11522
11523convertInConsequenceList(_Env,_Pr,_Name,_MS,[],_X,_HYPS,_AB,_CALLS,no,[]) :-
11524	!.
11525convertInConsequenceList(Env,Pr,Name,MS,[NCT],X,HYPS,AB,CALLS,[INCT]) :-
11526	convertInConsequence(Env,Pr,Name,MS,NCT,X,HYPS,AB,CALLS,INCT),
11527	!.
11528convertInConsequenceList(Env,Pr,Name,MS,[NCT|NCTs],X,
11529                         HYPS,AB,CALLS,[INCT|INCTs]) :-
11530	convertInConsequence(Env,Pr,Name,MS,NCT,X,HYPS,AB,CALLS,INCT),
11531	convertInConsequenceList(Env,Pr,Name,MS,NCTs,X,HYPS,AB,CALLS,INCTs).
11532
11533/***********************************************************************
11534 *
11535 * convert_loop(LoopTerm,+CALLS,+Constraint,-CALLS)
11536 *
11537 */
11538
11539convert_loop(no,CALLS,_,CALLS).
11540convert_loop(_,CALLS,CON,[CON|CALLS]).
11541
11542convertToGoal(Env,RN,MS1,CN,X,HYPS,AB,CALLS,PT,G) :-
11543	convertMS(negative,Env,[[],true],MS1,[],[W1,G1],_),
11544	getQuery(Env,W1,CN,X,PT,G),
11545%	G = call((in(Env,RN,modal(W1),CN,X,hyp(HYPS),ab(AB),call(CALLS),PT), G1)),
11546	!.
11547
11548	
11549nongeneric(X) :-
11550	var(X),
11551	!.
11552nongeneric(aaa) :-
11553	!,
11554	fail.
11555nongeneric(_) :-
11556	!.
11557
11558
11559ground(T,Result) :-
11560	var(T),
11561	!,
11562	Result = false.
11563ground(T,Result) :-
11564	atomic(T),
11565	!,
11566	Result = true.
11567ground(T,Result) :-
11568	T =.. [F|Args],
11569	map(ground,Args,Results),
11570	member(false,Results),
11571	!,
11572	Result = false.
11573ground(T,true) :-
11574	!.
11575/**********************************************************************
11576 *
11577 * @(#) tellRole.pl 1.5@(#)
11578 *
11579 */
11580
11581/***********************************************************************
11582 *
11583 * assertRoleLInR(Env,+MS,+RN,+RT,+AN)
11584 *
11585 */
11586
11587assertRoleLInR(Env,MS,R1,inverse(R2),AN) :-
11588	!,
11589	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11590	gensym(rule,RN),
11591	ruleName(AN,RN,user,lInR,Name),
11592	constructEqHead(Env,Name,W1,X,inverse(F),R2,app((F:R1),X),HYPS,AB,CALLS,true,EqLiteral1), 
11593%	asserta_logged((EqLiteral1 :- G1)),
11594	constructEqHead(Env,Name,W1,X,F,R2,app((inverse(F):R1),X),HYPS,AB,CALLS,true,EqLiteral2), 
11595%	asserta_logged((EqLiteral2 :- G1)),
11596	gensym(rule,RN3),
11597	constructEqHead(Env,rn(AN,RN3,user,rInL),W1,X,inverse(F),inverse(R2),
11598			Y,HYPS,AB,CALLS,PT1,EqLiteral3),
11599	constructEqCall(Env,rn(AN,RN3,_S3,_O3),bodyMC(W1),headMC(W1),X,F,R1,Y,
11600	                HYPS,AB,CALLS,PT1,EqLiteral4),
11601%	asserta_logged((EqLiteral3 :- cCS(CALLS,true), (call(G1), EqLiteral4))),
11602	gensym(rule,RN4),
11603	constructEqHead(Env,rn(AN,RN4,user,rInL),W1,Y,inverse(F1),R2,X,HYPS,AB,CALLS,
11604			PT2,EqLiteral5),
11605	constructEqCall(Env,rn(AN,RN4,_S3,_O3),bodyMC(W1),headMC(W1),
11606			X,F1,R1,Y,HYPS,AB,CALLS,PT2,EqLiteral6),
11607	asserta_logged((EqLiteral5 :- cCS(CALLS,true), (call(G1), EqLiteral6))),
11608	true.
11609assertRoleLInR(Env,MS,R1,and(RL),AN) :-
11610	!,
11611	assertAndConstraintLInR(Env,MS,R1,and(RL),AN),
11612        assertAndRoleLInR(Env,MS,R1,and(RL),AN).
11613assertRoleLInR(Env,MS,R1,restr(R2,C),AN) :-
11614	!,
11615	assertRoleLInRRestr1(Env,MS,R1,restr(R2,C),AN),
11616	assertRoleLInRRestr3(Env,MS,R1,restr(R2,C),AN),
11617	getComplementRole(Env,MS,R1,restr(R2,C),R3,restr(R2,CNF)),
11618	assertRoleLInRRestr4(Env,MS,R1,restr(R2,C),R3,restr(R2,CNF),AN).
11619assertRoleLInR(Env,MS,R1,R2,AN) :-
11620	!,
11621	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11622	gensymbol(mskolem,[X,Y],SF1),
11623	gensym(rule,RN1),
11624	ruleName(AN,RN1,user,lInR,Name1),
11625	constructEqHead(Env,Name1,W1,Y,SF1,R2,X,HYPS,AB,CALLS,PT1,EqLiteral2),
11626	constructEqMark(rn(AN,RN1,_S2,_O2),W1,Y,SF1,R2,X,HYPS,AB,CALLS,EqMark2),
11627	constructEqCall(Env,rn(AN,RN1,_S3,_O3),bodyMC(W1),headMC(W1),Y,_FF,R1,X,HYPS,AB,CALLS,PT1,EqLiteral1),
11628	asserta_logged((EqLiteral2 :- (cCS(CALLS,EqMark2), (call(G1), EqLiteral1)))),
11629	gensymbol(mskolem,[X,Y],SF2),
11630	gensym(rule,RN2),
11631	constructConHead(Env,rn(AN,RN2,user,lInR),W1,SF2,R2,X,'>=',N,
11632                         HYPS,AB,CALLS,PT1,C2),
11633	constructConMark(C2,Mark2),
11634	constructSolveConMark(rn(AN,RN2,_S4,_O4),W1,_FF3,R1,X,'>=',N,HYPS,AB,CALLS,Mark1),
11635	C1 = solveConstraint(Env,W1,(card,app((_FF:R1),X),'>=',N),_,hyp(HYPS),ab(AB),call([Mark1|CALLS]),PT1),
11636	asserta_logged((C2 :- (cCS(CALLS,Mark2), (call(G1), C1)))),
11637	gensym(rule,RN5),
11638	gensym(mskolem,SF3),
11639	constructConHead(Env,rn(AN,RN5,user,lInR),W1,SF3,R1,X,'=<',N,
11640                         HYPS,AB,CALLS,PT1,C4),
11641	constructConMark(C4,Mark4),
11642	constructSolveConMark(rn(AN,RN5,_S6,_O6),W1,_FF4,R2,X,'=<',N,HYPS,AB,CALLS,Mark5),
11643	C5 = solveConstraint(Env,MS,(card,app((_FF2:R2),X),'=<',N),_,hyp(HYPS),ab(AB),call([Mark5|CALLS]),PT1),
11644	asserta_logged((C4 :- (cCS(CALLS,Mark4), (call(G1), C5)))).
11645	
11646/**********************************************************************
11647 *
11648 * assertRoleLInRRestr1(+MS,+R1,restr(+R2,+C),+AN)
11649 * handles the case R1 is included in restr(R2,C).
11650 * asserts the constraints and membership clauses describing the 
11651 * relationship of R1 and R2.
11652 *
11653 */
11654
11655assertRoleLInRRestr1(Env,MS,R1,restr(R2,C),AN) :-
11656	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11657	gensym(rule,RN1),
11658	typeOfDefinition(Env,MS,C,S1),
11659	constructMLHead(Env,rn(AN,RN1,S1,lInR),W1,C,Y,HYPS,AB,CALLS,PT1,InHead),
11660	constructMLMark(InHead,InMark),
11661	constructEqCall(Env,rn(AN,RN1,_S2,_O2),bodyMC(W1),headMC(W1),
11662	                Y,F,R1,X,HYPS,AB,[InMark|CALLS],PT1,EqLiteral11),
11663	asserta_logged((InHead :- (cCS(CALLS,InMark), (call(G1), (EqLiteral11, ground(Y,true)))))),
11664	gensym(mskolem,SF),
11665	gensym(rule,RN2),
11666	typeOfDefinition(Env,MS,C,S2),
11667	constructEqHead(Env,rn(AN,RN2,S2,lInR),W1,Y,SF,R2,X,
11668                        HYPS,AB,CALLS,PT2,EqLiteral2),
11669	constructEqMark(rn(AN,RN2,_S3,_O3),W1,Y,SF,R2,X,HYPS,AB,CALLS,EqMark2),
11670	constructEqCall(Env,rn(AN,RN2,_S4,_O4),bodyMC(W1),headMC(W1),
11671                        Y,F,R1,X,HYPS,AB,[EqMark2|CALLS],PT2,EqLiteral21),
11672	asserta_logged((EqLiteral2 :- (cCS(CALLS,EqMark2), (call(G1), EqLiteral21)))),
11673	!.
11674
11675
11676assertRoleLInRRestr3(Env,MS,R1,restr(R2,C),AN) :-
11677	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11678	gensym(rule,RN1),
11679	typeOfDefinition(Env,MS,C,S1),
11680	constructConHead(Env,rn(AN,RN1,S1,lInR),W1,G,R2,X,'>=',N,
11681	                 HYPS,AB,CALLS,and([PT2,PT4]),C1),
11682	constructConMark(C1,Mark1),
11683	constructSolveConMark(rn(AN,_RN2,_S2,_O2),
11684                         W1,_FF1,R1,X,'>=',N,HYPS,AB,CALLS,Mark2),
11685	C2 = solveConstraint(Env,W1,(card,app((F:R1),X),'>=',N),_,hyp(HYPS),ab(AB),call([Mark2|CALLS]),PT2),
11686	asserta_logged((C1 :- (cCS(CALLS,Mark1), (call(G1), C2)))),
11687	gensym(rule,RN3),
11688	constructConHead(Env,rn(AN,RN3,S1,lInR),W1,G,R1,X,'=<',N,
11689                         HYPS,AB,CALLS,PT4,C3),
11690	constructConMark(C3,Mark3),
11691	constructSolveConMark(rn(AN,RN3,_S4,_O4),
11692                         W1,_FF3,R2,X,'=<',N,HYPS,AB,CALLS,Mark4),
11693	C4 = solveConstraint(Env,W1,(card,app((F:R2),X),'=<',N),_,hyp(HYPS),ab(AB),call([Mark4|CALLS]),PT4),
11694	asserta_logged((C3 :- (cCS(CALLS,Mark3), (call(G1), C4)))).
11695
11696
11697/**********************************************************************
11698 *
11699 * assertRoleLInRRestr2(Env,+MS,+R1,restr(+R2,+C1),
11700 *                          +R3,restr(+R2,+C2),+AN)
11701 * handles the case R1 is included in restr(R2,C).
11702 * asserts the constraints describing the relationship between 
11703 * R1 = restr(R2,C1), R3 = restr(R2,not(C1)) and R2.
11704 *
11705 */
11706
11707assertRoleLInRRestr4(Env,MS,R1,restr(R2,_C),R3,restr(R2,_CNF),AN1) :-
11708	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11709	gensym(rule,RN),
11710	constructConHead(Env,rn(AN1,RN,user,lInR),W1,_FF,R1,X,'=<',N1,
11711	                 HYPS,AB,CALLS,and([PT2,PT3]),C1),
11712	constructConMark(C1,Mark1),
11713	constructSolveConMark(rn(AN1,RN,_S2,_O2),W1,FF1,R2,X,'=<',N2,HYPS,AB,CALLS,Mark2),
11714	C2 = solveConstraint(Env,W1,(card,app((FF1:R2),X),'=<',N2),_,hyp(HYPS),ab(AB),call([Mark2|CALLS]),PT2),
11715	constructSolveConMark(rn(AN1,RN,_S3,_O3),W1,FF2,R3,X,'>=',N3,HYPS,AB,CALLS,Mark3),
11716	C3 = solveConstraint(Env,W1,(card,app((FF2:R3),X),'>=',N3),_,hyp(HYPS),ab(AB),call([Mark3|CALLS]),PT3),
11717	asserta_logged((C1 :- (cCS(CALLS,Mark1), (call(G1), (C2, (C3, (M is N2 - N3, comparison('=<',M,N1)))))))),
11718	!.
11719
11720
11721
11722
11723/***********************************************************************
11724 *
11725 * assertAndRoleLInR(+MS,+Lit,+X,+Y,+RT,+CALLS,+AN)
11726 *
11727 */
11728
11729assertAndRoleLInR(_,_MS,_,and([]),_AN) :-
11730	!.
11731assertAndRoleLInR(Env,MS,R1,and([R2|RL]),AN) :-
11732	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11733	gensym(mskolem,SF),
11734	gensym(rule,RN),
11735	constructEqHead(Env,rn(AN,RN,user,lInR),
11736                        W1,Y,SF,R2,X,HYPS,AB,CALLS,PT1,EqLiteral2),
11737	constructEqMark(rn(AN,RN,_S1,_O1),W1,Y,SF,R2,X,HYPS,AB,CALLS,EqMark2),
11738	constructEqCall(Env,rn(AN,RN,_S2,_O2),bodyMC(W1),headMC(W1),Y,_F,R1,X,
11739	                HYPS,AB,[EqMark2|CALLS],PT1,EqLiteral1),
11740	asserta_logged((EqLiteral2 :- (cCS(CALLS,EqMark2), (call(G1), EqLiteral1)))),
11741	assertAndRoleLInR(Env,MS,R1,and(RL),AN).
11742
11743/***********************************************************************
11744 *
11745 * assertAndConstraintLInR(+MS,+RN,+RT,+AN)
11746 *
11747 */
11748
11749assertAndConstraintLInR(_,_MS,_,and([]),_AN) :-
11750	!.
11751assertAndConstraintLInR(Env,MS,R1,and([R2|RL]),AN) :-
11752	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11753	gensym(rule,RN),
11754	ruleName(AN,RN,user,lInR,Name),
11755	constructConHead(Env,Name,W1,_FF,R1,X,Rel,N,HYPS,AB,CALLS,PT2,C1),
11756	constructConMark(C1,Mark1),
11757	constructSolveConMark(rn(AN,RN,_S1,_O1),W1,_FF2,R2,X,Rel,N,HYPS,AB,CALLS,Mark2),	
11758	gensymbol(mskolem,[X],SF),
11759	C2 = solveConstraint(Env,W1,(card,app((SF:R2),X),Rel,N),_,hyp(HYPS),ab(AB),call([Mark2|CALLS]),PT2),
11760	asserta_logged((C1 :- cCS(CALLS,Mark1), (call(G1), C2))),
11761	assertAndConstraintLInR(Env,MS,R1,and(RL),AN).
11762
11763/***********************************************************************
11764 *
11765 * assertAndConstraintRInL(+MS,+RN,+RT,+AN)
11766 *
11767 */
11768
11769assertAndConstraintRInL(_,_MS,_,and([]),_AN) :-
11770	!.
11771assertAndConstraintRInL(Env,MS,R1,and([R2|RL]),AN) :-
11772	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11773	gensym(rule,RN),
11774	constructConHead(Env,rn(AN,RN,user,rInL),W1,_FF,R2,X,'>=',N,
11775	                 HYPS,AB,CALLS,PT2,C1),
11776	constructConMark(C1,Mark1),
11777	constructSolveConMark(rn(AN,RN,_S1,_O1),W1,_FF1,R1,X,'>=',N,HYPS,AB,CALLS,Mark2),
11778	gensymbol(mskolem,[X],SF),
11779	C2 = solveConstraint(Env,W1,(card,app((SF:R1),X),'>=',N),_,hyp(HYPS),ab(AB),call([Mark2|CALLS]),PT2),
11780	asserta_logged((C1 :- cCS(CALLS,Mark1), (call(G1), C2))),
11781	assertAndConstraintRInL(Env,MS,R1,and(RL),AN).
11782
11783
11784/***********************************************************************
11785 *
11786 * assertRoleRInL(Env,+MS,+RN,+RT,+AN)
11787 *
11788 */
11789
11790assertRoleRInL(Env,MS,R1,inverse(R2),_AN) :-
11791	!,
11792	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11793	gensym(rule,RN1),
11794	constructEqHead(Env,rn(AN,RN1,user,rInL),W1,X,F,R1,
11795                        app((inverse(F):R2),X),HYPS,AB,CALLS,
11796			true,EqLiteral1),
11797%	asserta_logged((EqLiteral1 :- call(G1))),
11798	gensym(rule,RN2),
11799	constructEqHead(Env,rn(AN,RN2,user,rInL),
11800                        W1,X,inverse(F),R1,app((F:R2),X),HYPS,AB,CALLS,
11801			true,EqLiteral2),
11802%	asserta_logged((EqLiteral2 :- call(G1))),
11803	gensym(rule,RN3),
11804	constructEqHead(Env,rn(AN,RN3,user,rInL),W1,Y,inverse(F),inverse(R2),
11805			X,HYPS,AB,CALLS,PT1,EqLiteral3),
11806	constructEqCall(Env,rn(AN,RN3,_S3,_O3),bodyMC(W1),headMC(W1),X,F,R1,Y,
11807	                HYPS,AB,CALLS,PT1,EqLiteral4),
11808%	asserta_logged((EqLiteral3 :- cCS(CALLS,true), (call(G1), EqLiteral4))),
11809	gensym(rule,RN4),
11810	constructEqHead(Env,rn(AN,RN4,user,rInL),W1,Y,inverse(F1),R1,X,HYPS,AB,CALLS,
11811			PT2,EqLiteral5),
11812	constructEqCall(Env,rn(AN,RN4,_S3,_O3),bodyMC(W1),headMC(W1),
11813			X,F1,R2,Y,HYPS,AB,CALLS,PT2,EqLiteral6),
11814	asserta_logged((EqLiteral5 :- cCS(CALLS,true), (call(G1), EqLiteral6))).
11815assertRoleRInL(Env,MS,R1,restr(R2,C), AN) :-
11816	!,
11817	assertRoleRInLRestr1(Env,MS,R1,restr(R2,C),AN),
11818	getComplementRole(Env,MS,R1,restr(R2,C),R3,restr(R2,CNF)),
11819	assertRoleRInLRestr2(Env,MS,R1,restr(R2,C),R3,restr(R2,CNF),AN),
11820	assertRoleRInLRestr3(Env,MS,R1,restr(R2,C),AN),
11821	assertRoleRInLRestr4(Env,MS,R1,restr(R2,C),R3,restr(R2,CNF),AN).
11822assertRoleRInL(Env,MS,R1,and(RL),AN) :-
11823	!,
11824	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11825	gensymbol(mskolem,[X,Y],SF),
11826	gensym(rule,RN1),
11827	roleBody(Env,W1,and(RL),X,Y,HYPS,AB,CALLS,AN,Body,PTL),
11828	constructEqHead(Env,rn(AN,RN1,user,rInL),
11829                        W1,Y,SF,R1,X,HYPS,AB,CALLS,and([PTL]),EqLiteral1),
11830	constructEqMark(rn(AN,RN1,_S2,_O2),W1,Y,SF,R1,X,HYPS,AB,CALLS,EqMark1),
11831	asserta_logged((EqLiteral1 :- (cCS(CALLS,EqMark1), (call(G1), Body)))),
11832	assertAndConstraintRInL(Env,MS,R1,and(RL),AN).
11833assertRoleRInL(Env,MS,R1,R2,AN) :-
11834	!,
11835	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11836	gensymbol(mskolem,[X,Y],SF),
11837	gensym(rule,RN1),
11838	constructEqHead(Env,rn(AN,RN1,user,rInL),W1,X,SF,R1,Y,
11839	                HYPS,AB,CALLS,PT1,EqLiteral1),
11840	constructEqMark(rn(AN,RN1,_S2,_O2),W1,X,SF,R1,Y,HYPS,AB,CALLS,EqMark1),
11841	constructEqCall(Env,rn(AN,RN1,_S3,_O3),bodyMC(W1),headMC(W1),X,_F,R2,Y,
11842	                HYPS,AB,CALLS,PT1,EqLiteral2),
11843	asserta_logged((EqLiteral1 :- (cCS(CALLS,EqMark1), (call(G1), EqLiteral2)))),
11844	gensym(rule,RN2),
11845	constructConHead(Env,rn(AN,RN2,user,rInL),W1,_FF5,R1,X,'>=',N,
11846	                 HYPS,AB,CALLS,PT1,C2),
11847	constructConMark(C2,Mark2),
11848	constructSolveConMark(rn(AN,RN2,_S4,_O4),W1,_FF3,R2,X,'>=',N,HYPS,AB,CALLS,Mark1),
11849	C1 = solveConstraint(Env,W1,(card,app((_FF:R2),X),'>=',N),_,hyp(HYPS),ab(AB),call([Mark1|CALLS]),PT1),
11850	asserta_logged((C2 :- (cCS(CALLS,Mark2), (call(G1), C1)))),
11851	gensym(rule,RN5),
11852	gensym(mskolem,SF3),
11853	constructConHead(Env,rn(AN,RN5,user,rInL),W1,SF3,R2,X,'=<',N,
11854	                 HYPS,AB,CALLS,PT5,C4),
11855	constructConMark(C4,Mark4),
11856	constructSolveConMark(rn(AN,RN5,_S6,_O6),W1,_FF4,R1,X,'=<',N,HYPS,AB,CALLS,Mark5),
11857	C5 = solveConstraint(Env,W1,(card,app((_FF2:R1),X),'=<',N),_,hyp(HYPS),ab(AB),call([Mark5|CALLS]),PT5),
11858	asserta_logged((C4 :- (cCS(CALLS,Mark4), (call(G1), C5)))).
11859
11860/**********************************************************************
11861 *
11862 * getComplementRole(+MS,restr(+R2,C),-R3,restr(+R2,-CNF))
11863 * CNF is the normalform of not(C).
11864 * If there is already a role name R for restr(R2,CNF) then R3
11865 * will be instantiated with R.
11866 * If there is no role name for restr(R2,CNF) then a role name R
11867 * is generated, clauses for R will be provided, and R3 will be
11868 * instantiated with R.
11869 *
11870 */
11871 
11872getComplementRole(Env,MS,_R1,restr(R2,C),R3,restr(R2,CNF)) :-
11873	negate(C,CN),
11874	cnf(CN,CNF),
11875	roleEqualSets(Env,system,MS,R3,restr(R2,CNF),_AX),
11876	!.
11877getComplementRole(Env,MS,_R1,restr(R2,C),R3,restr(R2,CNF)) :-
11878	gensym(role,R3),
11879	negate(C,CN),
11880	cnf(CN,CNF),
11881	gensym(axiom,AN),
11882	asserta_logged(roleEqualSets(Env,system,MS,R3,restr(R2,CNF),AN)),
11883	assertRoleLInRRestr1(Env,MS,R3,restr(R2,CNF),AN),
11884	assertRoleLInRRestr3(Env,MS,R3,restr(R2,CNF),AN),
11885	assertRoleRInLRestr1(Env,MS,R3,restr(R2,CNF),AN),
11886%	assertRoleRInLRestr2(Env,MS,R1,restr(R2,CNF),R3,restr(R2,C),AN),
11887	assertRoleRInLRestr3(Env,MS,R3,restr(R2,CNF),AN),
11888	!.
11889	
11890
11891/**********************************************************************
11892 *
11893 * assertRoleRInLRestr1(+MS,+R1,restr(+R2,C),+AN)
11894 * handles the case restr(R2,C) is included in R1.
11895 * asserts the constraints and membership clauses describing the 
11896 * relationship of R1 and R2.
11897 *
11898 */
11899
11900assertRoleRInLRestr1(Env,MS,R1,restr(R2,C),AN) :-
11901	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11902	gensymbol(mskolem,[X,Y],SF),
11903	gensym(rule,RN),
11904	constructEqHead(Env,rn(AN,RN,user,rInL),
11905                        W1,Y,SF,R1,X,HYPS,AB,CALLS,and([PTEq2,PTIn]),EqLiteral1),
11906	constructEqMark(rn(AN,RN,_S1,_O1),
11907                        W1,Y,SF,R1,X,HYPS,AB,CALLS,EqMark1),
11908	constructEqCall(Env,rn(AN,RN,_S2,_O2),
11909                        bodyMC(W1),headMC(W1),Y,_FF,R2,X,HYPS,AB,CALLS,PTEq2,EqLiteral2),
11910	constructMLCall(Env,rn(AN,RN,_S3,_O3),
11911                        bodyMC(W1),headMC(W1),C,Y,HYPS,AB,CALLS,PTIn,InHead),
11912	asserta_logged((EqLiteral1 :- (cCS(CALLS,EqMark1), (call(G1), (EqLiteral2, (ground(Y,true), once(InHead))))))).
11913
11914
11915assertRoleRInLRestr2(Env,MS,R1,restr(R2,_C),R3,restr(R2,_CNF),AN) :-
11916	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11917	gensym(rule,RN),
11918	constructConHead(Env,rn(AN,RN,user,rInL),W1,_FF,R1,X,'>=',N1,
11919	                 HYPS,AB,CALLS,and([PT2,PT3]),D1),
11920	constructConMark(D1,Mark1),
11921	constructSolveConMark(rn(AN,RN,_S1,_O1),W1,_FF2,R2,X,'>=',_N2,HYPS,AB,CALLS,Mark2),
11922	D2 = solveConstraint(Env,W1,(card,app((_FF3:R2),X),'>=',N2),_,hyp(HYPS),ab(AB),call([Mark2|CALLS]),PT2),
11923	constructSolveConMark(rn(AN,RN,_S3,_O3),W1,_FF4,R3,X,'=<',_N3,HYPS,AB,CALLS,Mark3),
11924	D3 = solveConstraint(Env,W1,(card,app((_FF5:R3),X),'=<',N3),_,hyp(HYPS),ab(AB),call([Mark3|CALLS]),PT3),
11925	asserta_logged((D1 :- (cCS(CALLS,Mark1), (call(G1), (D2, (D3, (M is N2 - N3, comparison('>=',M,N1)))))))),
11926	!.
11927	
11928	
11929assertRoleRInLRestr3(Env,MS,R1,restr(R2,C),AN) :-
11930	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11931	negate(C,CN),
11932	cnf(CN,CNF),
11933	gensym(rule,RN),
11934	typeOfDefinition(Env,W1,CNF,S),
11935	constructMLHead(Env,rn(AN,RN,S,user),
11936                        W1,CNF,Y,HYPS,AB,CALLS,and([PT1,PT2]),InHead),
11937	constructMLMark(InHead,Mark),
11938%	constructConMark(rn(AN,RN,_S2,_O2),W1,_FF2,R2,X,'>=',_N2,
11939%	                 HYPS,CALLS,Mark2),
11940%	D2 = solveConstraint(Env,MS,(card,app((_FF4:R2),X),'>=',N2),(M2,L2),
11941%	                     hyp(HYPS),ab(AB),call([Mark2|CALLS]),PT3),
11942	constructSolveConMark(rn(AN,RN,_S1,_O1),W1,_FF1,R1,X,'=<',_N1,
11943	                 HYPS,AB,CALLS,Mark1),
11944	D1 = solveConstraint(Env,W1,(card,app((_FF3:R1),X),'=<',N1),(M1,L1),
11945	                     hyp(HYPS),ab(AB),call([Mark1|CALLS]),PT2),
11946	constructEqCall(Env,rn(AN,RN,_S3,_O3),bodyMC(W1),headMC(W1),
11947                        Y,_FF,R2,X,HYPS,AB,CALLS,PT1,EqLiteral2),
11948% Removed this rule for test purposes
11949% uh 03.10.92
11950%	asserta_logged((InHead :- (cCS(CALLS,Mark), (call(G1), (EqLiteral2, (ground(Y,true), (D1, (not(member((Y,_),L1)), N1 = M1)))))))),
11951	!.
11952
11953assertRoleRInLRestr4(Env,MS,R1,restr(R2,_C),R3,restr(R2,_CNF),AN) :-
11954	convertMS(positive,Env,[[],true],MS,[],[W1,G1],_),
11955	gensym(rule,RN),
11956	constructConHead(Env,rn(AN,RN,user,rInL),W1,_FF2,R2,X,Rel,N2,
11957	                 HYPS,AB,CALLS,and([PT1,PT3]),D2),
11958	constructConMark(D2,Mark2),
11959	constructSolveConMark(rn(AN,RN,_S1,_O1),W1,_FF1,R1,X,Rel,_N1,
11960	                 HYPS,AB,CALLS,Mark1),
11961	D1 = solveConstraint(Env,W1,(card,app((_FF4:R1),X),Rel,N1),_,
11962	                     hyp(HYPS),ab(AB),call([Mark1|CALLS]),PT1),
11963	constructSolveConMark(rn(AN,RN,_S3,_O3),W1,_FF3,R3,X,Rel,_N3,
11964	                 HYPS,AB,CALLS,Mark3),
11965	D3 = solveConstraint(Env,W1,(card,app((_FF5:R3),X),Rel,N3),_,
11966	                     hyp(HYPS),ab(AB),call([Mark3|CALLS]),PT3),
11967	asserta_logged((D2 :- (cCS(CALLS,Mark2), (call(G1), (D1, (D3, (M is N1 + N3, comparison(Rel,M,N2)))))))),
11968	!.
11969	
11970
11971/***********************************************************************
11972 *
11973 * roleBody(+MS,+RT,+X,+Y,+CALLS,+AN,-Body)
11974 *
11975 */
11976
11977roleBody(_,_MS,and([]),_X,_Y,_,_,_,_,true,[]) :- 
11978	!.
11979roleBody(Env,MS,and([R1]),X,Y,HYPS,AB,CALLS,AN,Body,[PT1]) :-
11980	constructEqCall(Env,rn(AN,_RN,_S1,_O1),bodyMC(MS),headMC(MS),Y,_FF,R1,X,HYPS,AB,CALLS,PT1,Body),
11981	!.
11982roleBody(Env,MS,and([R1|RL]),X,Y,HYPS,AB,CALLS,AN,(EqLiteral, Body),[PT1|PTL]) :-
11983	constructEqCall(Env,rn(AN,_RN1,_S1,_O1),
11984                        bodyMC(MS),headMC(MS),Y,_FF,R1,X,HYPS,AB,CALLS,PT1,EqLiteral),
11985	roleBody(Env,MS,and(RL),X,Y,HYPS,AB,CALLS,AN,Body,PTL).
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997/***********************************************************************
11998 *
11999 * verifySolution(+TestSol,+ExpectedSol)
12000 *
12001 *	prints an error message if TestSol and ExpectedSol do not
12002 *	match.
12003 */
12004
12005verifySolution(TestSol,ExpectedSol) :-
12006	nonvar(ExpectedSol),
12007	nonvar(TestSol),
12008	!,
12009	TestSol = ExpectedSol.
12010verifySolution(TestSol,ExpectedSol) :-
12011	print('Solutions differ: test solution is '),
12012	print(TestSol),
12013	print(', while expected solution is '),
12014	print(ExpectedSol).
12015
12016
12017
12018
12019
12020/**********************************************************************
12021 *
12022 * @(#) unfold.pl 1.3@(#)
12023 *
12024 */
12025
12026/***********************************************************************
12027 *
12028 * unfold(Env,+Type,+CT,+List1,-CN,-List2)
12029 * Parameter: Type      'concept' or 'role'
12030 *            CT        concept term
12031 *            List1     List of tuples (Origin,T,T1,T2)
12032 *                      where Origin is either 'user' or 'system'
12033 *                            T      is either 'concept' or 'role'
12034 *                            T1     is a concept term or role term
12035 *                            T2     is a concept term or role term
12036 *            CN        concept name
12037 *            List2     List of triples (Origin,CN,CT)
12038 * unfolds concept terms or role terms so that in List2 for all tuples
12039 * (O,T,CN,all(R1,C1)), (O,T,CN,and([C1,...,Cn])), 
12040 * (O,T,RN,and([R1,...,Rn])), ...
12041 * CN and the Ci are concept names and RN and the Ri are role names.
12042 *
12043 * 130892   UH   (c)
12044 *
12045 */
12046
12047
12048unfold(_Env,O,_Side,_Type,CT,DL1,O,CT,DL1) :-
12049	atomicConcept(CT),
12050	!.
12051unfold(_Env,_O,_Side,concept,not(CT),DL1,system,not(CT),DL1) :-
12052	atomicConcept(CT),
12053	!.
12054unfold(Env,_O,_Side,concept,CT,DL1,user,C,DL1) :-
12055	conceptEqualSets(Env,system,_,C,CT,_),
12056	clause(conceptName(Env,_,_C),_),
12057	!.
12058unfold(Env,_O,_Side,concept,CT,DL1,system,C,DL1) :-
12059	conceptEqualSets(Env,system,_,C,CT,_),
12060	!.
12061unfold(Env,_O,left,concept,CT,DL1,system,CT1,DL2) :-
12062	gensym(concept,C),
12063	unfold(Env,[(system,concept,C,CT)],[(_,concept,C,CT,CT1)|DL]),
12064	append(DL1,DL,DL2).
12065unfold(Env,_O,right,concept,CT,DL1,system,C,DL2) :-
12066	gensym(concept,C),
12067	unfold(Env,[(system,concept,C,CT)],[(system,concept,C,CT,CT1)|DL]),
12068	append(DL1,[(system,concept,C,CT,CT1)|DL],DL2).
12069unfold(Env,_O,_Side,role,RT,DL1,system,R,DL2) :-
12070	gensym(role,R),
12071	unfold(Env,[(system,role,R,RT)],[(system,role,R,RT,RT1)|DL]),
12072	append(DL1,[(system,role,R,RT,RT1)|DL],DL2).
12073
12074/***********************************************************************
12075 *
12076 * unfold(Env,+List1,-List2)
12077 * Parameter: List1     List of tuples (Origin,Type,T1,T2)
12078 *                      where Origin is either 'user' or 'system'
12079 *                            T      is either 'concept' or 'role'
12080 *                            T1     is a concept or role term
12081 *                            T2     is a concept or role term
12082 *            List2     List of tuples (Origin,Type,CN,CT)
12083 * unfolds concept terms or role terms so that in List2 for all tuples
12084 * (O,T,CN,all(R1,C1)), (O,T,CN,and([C1,...,Cn])), 
12085 * (O,T,RN,and([R1,...,Rn])), ...
12086 * CN and the Ci are concept names and RN and the Ri are role names.
12087 *
12088 * 130892   UH   (c)
12089 *
12090 */
12091
12092unfold(Env,[(Origin,concept,A,all(R,B))|L1],[(NewOrigin,concept,A1,all(R,B),all(R1,B1))|DL3]) :-
12093	unfold(Env,L1,L2),
12094	unfold(Env,Origin,left,concept,A,L2,NewOrigin,A1,DL1),
12095	unfold(Env,Origin,right,role,R,DL1,_NewOriginR1,R1,DL2),
12096	unfold(Env,Origin,right,concept,B,DL2,_NewOriginB1,B1,DL3),
12097        !.
12098unfold(Env,[(Origin,concept,A,some(R,B))|L1],[(NewOrigin,concept,A1,some(R,B),some(R1,B1))|DL3]) :-
12099	unfold(Env,L1,L2),
12100	unfold(Env,Origin,left,concept,A,L2,NewOrigin,A1,DL1),
12101	unfold(Env,Origin,right,role,R,DL1,_NewOriginR1,R1,DL2),
12102	unfold(Env,Origin,right,concept,B,DL2,_NewOriginB1,B1,DL3),
12103        !.
12104unfold(Env,[(Origin,concept,A,atmost(N,R))|L1],[(NewOrigin,concept,A1,atmost(N,R),atmost(N,R1))|DL2]) :-
12105	integer(N),
12106	unfold(Env,L1,L2),
12107	unfold(Env,Origin,left,concept,A,L2,NewOrigin,A1,DL1),
12108	unfold(Env,Origin,right,role,R,DL1,_NewOriginR1,R1,DL2),
12109        !.
12110unfold(Env,[(Origin,concept,A,atleast(N,R))|L1],[(NewOrigin,concept,A1,atleast(N,R),atleast(N,R1))|DL2]) :-
12111	integer(N),
12112	unfold(Env,L1,L2),
12113	unfold(Env,Origin,left,concept,A,L2,NewOrigin,A1,DL1),
12114	unfold(Env,Origin,right,role,R,DL1,_NewOriginR1,R1,DL2),
12115        !.
12116unfold(Env,[(Origin,Type,A,and(L1))|L2],[(NewOrigin,Type,A1,and(L1),and(L3))|L5]) :-
12117	unfold(Env,L2,DL1),
12118	unfold(Env,Origin,left,Type,A,DL1,NewOrigin,A1,L4),
12119	unfoldList(Env,Type,L1,L3,CL3),
12120	append(L4,CL3,L5),
12121	!.
12122unfold(Env,[(Origin,Type,A,set(L1))|L2],[(NewOrigin,Type,A1,set(L1),C)|L4]) :-
12123	unfold(Env,L2,DL1),
12124	unfoldSetToConcept(set(L1),C),
12125	unfold(Env,Origin,left,Type,A,DL1,NewOrigin,A1,L4),
12126	!.
12127unfold(Env,[(Origin,concept,A,or(L1))|L2],[(NewOrigin,concept,A1,or(L1),or(L3))|L5]) :-
12128	unfold(Env,L2,DL1),
12129	unfold(Env,Origin,left,concept,A,DL1,NewOrigin,A1,L4),
12130	unfoldList(Env,concept,L1,L3,CL3),
12131	append(L4,CL3,L5),
12132	!.
12133unfold(Env,[(Origin,concept,A,not(B))|L2],[(NewOrigin,concept,A1,not(B),not(B1))|L3]) :-
12134	unfold(Env,L2,L4),
12135	unfold(Env,Origin,left,concept,A,L4,NewOrigin,A1,L5),
12136	unfold(Env,Origin,right,concept,B,L5,_NewOriginB,B1,L3),
12137	!.
12138unfold(Env,[(Origin,concept,A,naf(B))|L2],[(NewOrigin,concept,A1,naf(B),naf(B1))|L3]) :-
12139	unfold(Env,L2,L4),
12140	unfold(Env,Origin,left,concept,A,L4,NewOrigin,A1,L5),
12141	unfold(Env,Origin,right,concept,B,L5,_NewOriginB,B1,L3),
12142	!.
12143unfold(Env,[(Origin,concept,A,b(O,P,B))|L2],[(NewOrigin,concept,A1,b(O,P,B),b(O,P,B1))|L3]) :-
12144	unfold(Env,L2,L4),
12145	unfold(Env,Origin,left,concept,A,L4,NewOrigin,A1,DL1),
12146	unfold(Env,Origin,right,concept,B,DL1,_NewOriginB1,B1,L3),
12147	!.
12148unfold(Env,[(Origin,concept,A,bc(O,C,B))|L2],[(NewOrigin,concept,A1,bc(O,P,B),bc(O,C1,B1))|L5]) :-
12149	unfold(Env,L2,L4),
12150	unfold(Env,Origin,left,concept,A,L4,NewOrigin,A1,DL1),
12151	unfold(Env,Origin,right,concept,C,DL1,_NewOriginB1,C1,L3),
12152	unfold(Env,Origin,right,concept,B,DL1,_NewOriginB1,B1,L4),
12153	append(L3,L4,L5),
12154	!.
12155unfold(Env,[(Origin,concept,A,d(O,P,B))|L2],[(NewOrigin,concept,A1,d(O,P,B1),d(O,P,B1))|L3]) :-
12156	unfold(Env,L2,L4),
12157	unfold(Env,Origin,left,concept,A,L4,NewOrigin,A1,DL1),
12158	unfold(Env,Origin,right,concept,B,DL1,_NewOriginB1,B1,L3),
12159	!.
12160unfold(Env,[(Origin,role,RN,restr(R,C))|L1],[(NewOrigin,role,RN1,restr(R,C),restr(R1,C1))|L2]) :-
12161	unfold(Env,L1,L3),
12162	unfold(Env,Origin,left,role,RN,L3,NewOrigin,RN1,L4),
12163	unfold(Env,Origin,right,role,R,L4,_NewOriginR1,R1,L5),
12164	unfold(Env,Origin,right,concept,C,L5,_NewOriginC1,C1,L2),
12165        !.
12166unfold(Env,[(Origin,role,RN,inverse(R))|L1],[(NewOrigin,role,RN1,inverse(R),inverse(R1))|L5]) :-
12167	unfold(Env,L1,L3),
12168	unfold(Env,Origin,left,role,RN,L3,NewOrigin,RN1,L4),
12169	unfold(Env,Origin,right,role,R,L4,_NewOriginR1,R1,L5),
12170        !.
12171unfold(_Env,[(Origin,Type,A,B)],[(Origin,Type,A,B,B)]) :-
12172	atomicConcept(B),
12173	!.
12174unfold(_Env,[],[]) :- !.
12175	
12176
12177unfoldList(_Env,_,[],[],[]) :- !.
12178unfoldList(Env,Type,[CT1|CTL1],[CT1|CTL2],DL1) :-
12179	atomicConcept(CT1),
12180	unfoldList(Env,Type,CTL1,CTL2,DL1),
12181	!.
12182unfoldList(Env,Type,[CT1|CTL1],[CN|CNL2],DL3) :-
12183	conceptEqualSets(Env,system,_,CN,CT1,_),
12184	!,
12185	unfoldList(Env,Type,CTL1,CNL2,DL3).
12186unfoldList(Env,Type,[CT1|CTL1],[CN|CNL2],DL1) :-
12187	gensym(Type,CN),
12188	unfold(Env,[(system,Type,CN,CT1)],DL),
12189	unfoldList(Env,Type,CTL1,CNL2,DL3),
12190	append(DL,DL3,DL1).
12191
12192
12193/**********************************************************************
12194 *
12195 * atomicConcept(+CT) 
12196 * succeeds if the concept term CT can be regarded as a atomic concept
12197 * for our translation. In the current implementation variables, 
12198 * identifiers, singleton sets, and their negation are regarded as 
12199 * atomic.
12200 *
12201 * 130892   UH   (c)
12202 * 140892   UH   Added clauses for sets and negation of variables
12203 * 140892   UH   Documented
12204 *
12205 */
12206
12207atomicConcept(CT) :-
12208	var(CT),
12209	!.
12210atomicConcept(CT) :-
12211	atomic(CT),
12212	!.
12213atomicConcept(not(CT)) :-
12214	var(CT),
12215	!.
12216atomicConcept(not(CT)) :-
12217	atomic(CT),
12218	!.
12219atomicConcept(set([E1])) :-
12220	!.
12221%atomicConcept(not(set([E1]))) :-
12222%	!.
12223	
12224/**********************************************************************
12225 *
12226 * unfoldElementToSet(+Set,-CT)
12227 * for a given set Set the concept term CT consisting of a disjunktion 
12228 * of all singleton set contained in Set is computed.
12229 *
12230 * 130892   UH   (c)  
12231 * 140892   UH   Documented
12232 *
12233 */
12234
12235unfoldElementToSet(E1,set([E1])).
12236
12237unfoldSetToConcept(set([]),'bot') :-
12238	!.
12239unfoldSetToConcept(set([E1]),set([E1])) :-
12240	!.
12241unfoldSetToConcept(set([E1|L1]),or(L2)) :-
12242	hop_map(unfoldElementToSet,[E1|L1],L2),
12243	!.
12244
12245
12246/**********************************************************************
12247 *
12248 * %A%
12249 *
12250 */
12251
12252/***********************************************************************
12253 *
12254 * initializeMotel
12255 * cleans TBox, ABox, hierarchies, ...
12256 *
12257 */
12258
12259initializeMotel :-
12260	retractCompiledPredicates(_),
12261	retractallEnv(_,in/9),
12262	retractallEnv(_,kb_in/10),
12263	retractallEnv(_,eq/9),
12264	retractallEnv(_,constraint/8),
12265	retractallEnv(_,rel/5),
12266	retractallEnv(_,closed/5),
12267	retractallEnv(_,compiledPredicate/2),
12268	retractallEnv(_,conceptElement/7),
12269	retractallEnv(_,conceptEqualSets/6),
12270	retractallEnv(_,conceptHierarchy/3),
12271	retractallEnv(_,conceptName/4),
12272	retractallEnv(_,conceptSubsets/6),
12273	retractallEnv(_,environment/3),
12274	retractallEnv(_,given_change/4),
12275	retractallEnv(_,given_inflLink/4),
12276	retractallEnv(_,modalAxioms/6),
12277	retractallEnv(_,roleAttributes/5),
12278	retractallEnv(_,roleDefault/4),
12279	retractallEnv(_,roleDefNr/4),
12280	retractallEnv(_,roleDomain/4),
12281	retractallEnv(_,roleElement/8),
12282	retractallEnv(_,roleEqualSets/6),
12283	retractallEnv(_,roleHierarchy/3),
12284	retractallEnv(_,roleName/4),
12285	retractallEnv(_,roleNr/4),
12286	retractallEnv(_,roleRange/4),
12287	retractallEnv(_,roleSubsets/6),
12288	retractallEnv(_,sub/4),
12289	retractallEnv(_,succ/4),
12290	retractallEnv(_,abductiveDerivation/3),
12291	retractallEnv(_,consistencyDerivation/3),
12292	retractallEnv(_,hypothesis/1),
12293	retractallEnv(_,inconsistencyCheck/3),
12294	retractallEnv(_,motel_option/2),
12295	retractallEnv(_,nsub/4),
12296	retractallEnv(_,nsub3/2),
12297	retractallEnv(_,sub3/2),
12298	retractallEnv(_,succ3/2),
12299        retractallEnv(_,query/6),
12300        % retractallEnv(_,value/2),
12301	asserta_logged(environment(initial,env(e0),'Initial Environment')),
12302	asserta_logged(currentEnvironment(env(e0))),
12303	initEnvironment(initial),
12304	!.
12305
12306retractRoles(Env) :-
12307 	clause(roleName(Env,_MS,_,RN),_),
12308 	Head =.. [RN,_,_],
12309 	retractall_head(Head),
12310	fail.
12311retractRoles(_).
12312
12313
12314/**********************************************************************
12315 *
12316 * loadKB(+FileName)
12317 * 
12318 */
12319
12320loadKB(FileName) :-
12321	see(FileName),
12322	repeat,
12323	read(Goal),
12324	doFileGoal(Goal),
12325	!.
12326loadKB(_) :-
12327	seen,
12328	!,
12329	fail.
12330
12331loadKB(FileName,EnvName) :-
12332	var(EnvName),
12333	loadKB(FileName),
12334	% The file FileName should contain a call to makeEnvironment
12335	% Due to the definition of makeEnvironment the new environment
12336	% should be described by the first environment/3 fact in the
12337	% database.
12338	environment(EnvName,_,_),
12339	!.
12340
12341
12342doFileGoal('end_of_file') :-
12343	seen,
12344	!.
12345doFileGoal(Goal) :-
12346	once(call(Goal)),
12347	fail.
12348
12349/**********************************************************************
12350 *
12351 * getKB(+Name,-Set)
12352 * Set contains all terminological and assertional axioms in 
12353 * knowledge base Name.
12354 *
12355 */
12356
12357getKB(Set) :- 
12358	getCurrentEnvironment(Name),
12359	getKB(Name,Set),
12360	!.
12361
12362getKB(EnvName,Set08) :-
12363	environment(EnvName,Name,_Comment),
12364        rtrace,
12365	bagofOrNil(Clause1,
12366                   [K1,C1,MOp1,A1]^(modalAxioms(Name,user,K1,C1,MOp1,A1), 
12367                   Clause1 = modalAxioms(K1,MOp1,A1)),Set1),
12368        nortrace,
12369	bagofOrNil(Clause2,
12370                   [MS2,W1,G1,A2,C2,Ax2]^(clause(conceptElement(Name,MS2,W1,user,A2,C2,Ax2),G1),
12371                   Clause2 = assert_ind(MS2,A2,C2)),Set2),
12372	bagofOrNil(Clause3,
12373                   [MS3,W1,G1,A3,B3,R3,Ax3]^(clause(roleElement(Name,MS3,W1,user,A3,B3,R3,Ax3)),
12374	           Clause3 = assert_ind(MS3,A3,B3,R3)),Set3),
12375	bagofOrNil(Clause4,
12376                   [MS4,CN4,CT4,Ax4]^(conceptEqualSets(Name,user,MS4,CN4,CT4,Ax4),
12377                   Clause4 = defconcept(MS4,CN4,CT4)),Set4),
12378	bagofOrNil(Clause5,
12379                   [MS5,CN5,CT5,Ax5]^(conceptSubsets(Name,user,MS5,CN5,CT5,Ax5),
12380                   Clause5 = defprimconcept(MS5,CN5,CT5)),Set5),
12381	bagofOrNil(Clause6,
12382		   [MS6,CN6,CT6,Ax6]^(roleEqualSets(Name,user,MS6,CN6,CT6,Ax6),
12383                   Clause6 = defrole(MS6,CN6,CT6)),Set6),
12384	bagofOrNil(Clause7,
12385                   [MS7,CN7,CT7,Ax7]^(roleSubsets(Name,user,MS7,CN7,CT7,Ax7),
12386		   Clause7 = defprimrole(MS7,CN7,CT7)),Set7),
12387	bagofOrNil(Clause8,
12388		   [MS8,X8,Y8,R8]^(closed(Name,MS8,X8,Y8,R8),
12389		   Clause8 = defclosed(MS8,X8,Y8,R8)),Set8),
12390	append(   [],Set1,Set01),
12391	append(Set01,Set2,Set02),
12392	append(Set02,Set3,Set03),
12393	append(Set03,Set4,Set04),
12394	append(Set04,Set5,Set05),
12395	append(Set05,Set6,Set06),
12396	append(Set06,Set7,Set07),
12397	append(Set07,Set8,Set08),
12398	!.
12399
12400/**********************************************************************
12401 *
12402 * saveKB(+EnvName,+FileName)
12403 *
12404 */
12405
12406saveKB(FileName) :-
12407	getCurrentEnvironment(EnvName),
12408	!,
12409	saveKB(EnvName,FileName).
12410
12411saveKB(EnvName,FileName) :-
12412	environment(EnvName,Env,C),
12413	tell(FileName),
12414	writeq(makeEnvironment(EnvName,C)), write('.'), nl,
12415	writeq(initEnvironment(EnvName)), write('.'), nl,
12416	transformAndWrite(conceptEqualSets(Env,user,MS1,C11,C12,_E4),
12417	                  defconcept(EnvName,MS1,C11,C12)),
12418	transformAndWrite(conceptSubsets(Env,user,MS2,C21,C22,_E5),
12419                          defprimconcept(EnvName,MS2,C21,C22)),
12420	transformAndWrite(roleEqualSets(Env,user,MS3,R31,R32,_E6),
12421	                  defrole(EnvName,MS3,R31,R32)),
12422	transformAndWrite(roleSubsets(Env,user,MS4,R41,R42,_E7),
12423                          defprimrole(EnvName,MS4,R41,R42)),
12424	transformAndWrite(conceptElement(Env,MS5,_W1,user,A51,C52,_E14),
12425                          assert_ind(EnvName,MS5,A51,C52)),
12426	transformAndWrite(roleElement(Env,MS6,_W1,user,A61,A62,R61,_F15),
12427	                  assert_ind(EnvName,MS6,A61,A62,R61)),
12428	transformAndWrite(modalAxioms(Env,user,A71,_A72,A73,A74),
12429                          modalAxioms(Env,A71,A73,A74)),
12430	transformAndWrite(roleAttributes(Env,A71,B71,C71),
12431			  (environment(EnvName,NewEnv,_), assert_logged(roleAttributes(NewEnv,A71,B71,C71)))),
12432	transformAndWrite(roleDefault(Env,A81,B81,C81),
12433			  (environment(EnvName,NewEnv,_), assert_logged(roleDefault(NewEnv,A81,B81,C81)))),
12434	transformAndWrite(roleDefNr(Env,A91,B91,C91),
12435			  (environment(EnvName,NewEnv,_), assert_logged(roleDefNr(NewEnv,A91,B91,C91)))),
12436	transformAndWrite(roleDomain(Env,A82,B82,C82),
12437			  (environment(EnvName,NewEnv,_), assert_logged(roleDomain(NewEnv,A82,B82,C82)))),
12438	transformAndWrite(roleNr(Env,A83,B83,C83),
12439			  (environment(EnvName,NewEnv,_), assert_logged(roleNr(NewEnv,A83,B83,C83)))),
12440	transformAndWrite(roleRange(Env,A84,B84,C84),
12441			  (environment(EnvName,NewEnv,_), assert_logged(roleRange(NewEnv,A84,B84,C84)))),
12442        told,
12443        !.
12444saveKB(_,_) :-
12445	told,
12446	!,
12447	fail.
12448			  
12449transformAndWrite(G1,G2) :-
12450	clause(G1,_Body),
12451	writeq(G2), write('.'), nl,
12452	fail.
12453transformAndWrite(_,_) :-
12454	!.
12455
12456
12457/***********************************************************************
12458 *
12459 * deduce(EnvName,MS,Query,Proof)
12460 *
12461 */
12462
12463deduce(P1) :-
12464	completeParameter([P1],EnvName,MS,Query,Proof),
12465	deduce(EnvName,MS,Query,Proof).
12466deduce(P1,P2) :-
12467	completeParameter([P1,P2],EnvName,MS,Query,Proof),
12468	deduce(EnvName,MS,Query,Proof).
12469deduce(P1,P2,P3) :-
12470	completeParameter([P1,P2,P3],EnvName,MS,Query,Proof),
12471	deduce(EnvName,MS,Query,Proof).
12472
12473deduce(EnvName,MS,elementOf(X,C),Exp) :-
12474	motel_option(useSetheo,yes),
12475	!,
12476	deduceSetheo(EnvName,MS,elementOf(X,C),Exp).
12477deduce(EnvName,MS,elementOf(X,C),Exp) :-
12478	deduceMOTEL(EnvName,MS,elementOf(X,C),Exp).
12479
12480deduceMOTEL(EnvName,MS,elementOf(X,C),Exp) :-
12481	retractall_head(hypothesis(_)),
12482 	environment(EnvName,Env,_),
12483 	convertMS(negative,Env,[[],true],MS,[],[W1,G1],_),
12484	clause(query(Env,W1,C,X,Exp,Goal),_).
12485deduceMOTEL(EnvName,MS,elementOf(X,C),Exp) :-
12486	retractall_head(hypothesis(_)),
12487 	environment(EnvName,Env,_),
12488 	convertMS(negative,Env,[[],true],MS,[],[W1,G1],_),
12489 	getNegatedConcept(C,C1),
12490	constructMLMark(rn(_AN5,_RN5,_S5,_O5),W1,C1,X,_HYPS,_D1,InHead1),
12491 	getQuery(Env,W1,C,X,Exp,Goal),
12492	performQuery(X,G1,Goal),
12493	allowedAnswerConcept(Env,C),
12494%	getExplanation(InHead,Exp),
12495% 	anlegen einer clausel die in undefconcept wieder geloescht wird...
12496 	setQuery(Env,W1,C,X,Exp,Goal).
12497deduceMOTEL(EnvName,MS,roleFiller(X,R,L,N),Exp) :-
12498	retractall_head(hypothesis(_)),
12499	environment(EnvName,Env,_),
12500	convertMS(negative,Env,[[],true],MS,[],[W1,G1],_),
12501	call(G1),
12502	solveConstraint(Env,W1,(card,app((_FF:R),X),'>=',_N1),(_N,L),
12503	                hyp([]),ab(noAb),call([]),Exp),
12504	nonvar(X),
12505	length(L,N).
12506
12507deduceSetheo(EnvName,MS,elementOf(X,C),Exp) :-
12508 	environment(EnvName,Env,_),
12509 	convertMS(negative,Env,[[],true],MS,[],[W1,G1],_),
12510	getQuery(Env,MS,C,X,GL),
12511	write('Trying '), print(X), write(' in '), print(C), write('.'),nl,
12512	tell('/tmp/aaa.lop'),
12513	envToFOL(Env,CL),
12514	printNHProlog(CL),
12515	printNHProlog(GL),
12516	told,
12517	shell('/HG/local/provers/setheo/bin.sun4/inwasm -cons -nosgreord /tmp/aaa',S1),
12518	S1 = 0,
12519	shell('/HG/local/provers/setheo/bin.sun4/wasm /tmp/aaa',S2),
12520	S2 = 0, 
12521	shell('/HG/local/provers/setheo/bin.sun4/sam -dr -cons /tmp/aaa',S3),
12522	S3 = 0.
12523
12524
12525setQuery(Env,W1,C,X,Exp,Goal) :-
12526	assert_logged(query(Env,W1,C,X,Exp,Goal)),
12527	!.
12528setQuery(Env,W1,C,X,Exp,Goal) :-
12529	!.
12530
12531splitGoal([(in(A1,B1,C1) <== true)],[(false <== in(A1,B1,C1))]) :-
12532	!.
12533splitGoal([(~ in(A1,B1,C1) <== true)],[(false <== ~ in(A1,B1,C1))]) :-
12534	!.
12535splitGoal([B|C],[B|D]) :-
12536	splitGoal(C, D).
12537
12538getQuery(Env,MS1,C0,X,C3) :-
12539	var(C0),
12540	var(X),
12541	clause(conceptName(Env,_,_,C0),_),
12542        conceptElement(Env,_,user,X,_,_),
12543	hop_map(negate,MS1,MS2),
12544	hop_map(normalizeNot,MS2,MS3),
12545	axiomToFOL(MS3,[X],_,in,C0,F),
12546	translate(F,C1),
12547	clausesToLOP(C1,C2),
12548	splitGoal(C2,C3).
12549getQuery(Env,MS1,C0,X,C3) :-
12550	var(C0),
12551	var(X),
12552	clause(conceptName(Env,_,_,C0),_),
12553        conceptElement(Env,_,user,X,_,_),
12554	hop_map(negate,MS1,MS2),
12555	hop_map(normalizeNot,MS2,MS3),
12556	axiomToFOL(MS3,[X],_,in,not(C0),F),
12557	translate(F,C1),
12558	clausesToLOP(C1,C2),
12559	splitGoal(C2,C3).
12560getQuery(Env,MS1,C0,X,C3) :-
12561	var(C0),
12562	nonvar(X),
12563	setof(F1,[A1,A2,A3]^clause(conceptName(Env,A1,A2,F1),A3),L1),
12564	member(C0,L1),
12565	hop_map(negate,MS1,MS2),
12566	hop_map(normalizeNot,MS2,MS3),
12567	axiomToFOL(MS3,[X],_,in,C0,F),
12568	translate(F,C1),
12569	clausesToLOP(C1,C2),
12570	splitGoal(C2,C3).
12571getQuery(Env,MS1,C0,X,C3) :-
12572	var(C0),
12573	nonvar(X),
12574	setof(F1,[A1,A2,A3]^clause(conceptName(Env,A1,A2,F1),A3),L1),
12575	member(C0,L1),
12576	hop_map(negate,MS1,MS2),
12577	hop_map(normalizeNot,MS2,MS3),
12578	axiomToFOL(MS3,[X],_,in,not(C0),F),
12579	translate(F,C1),
12580	clausesToLOP(C1,C2),
12581	splitGoal(C2,C3).
12582getQuery(Env,MS1,C0,X,C3) :-
12583	nonvar(C0),
12584	var(X),
12585        conceptElement(Env,_,user,X,_,_),
12586	hop_map(negate,MS1,MS2),
12587	hop_map(normalizeNot,MS2,MS3),
12588	axiomToFOL(MS3,[X],_,in,C0,F),
12589	translate(F,C1),
12590	clausesToLOP(C1,C2),
12591	splitGoal(C2,C3).
12592getQuery(Env,MS1,C0,X,C3) :-
12593	nonvar(C0),
12594	nonvar(X),
12595	hop_map(negate,MS1,MS2),
12596	hop_map(normalizeNot,MS2,MS3),
12597	axiomToFOL(MS3,[X],_,in,C0,F),
12598	translate(F,C1),
12599	clausesToLOP(C1,C2),
12600	splitGoal(C2,C3).
12601getQuery(Env,W1,C0,X,Exp,Goal) :-
12602	var(C0),
12603	clause(conceptName(Env,_,_,C0),_),
12604	constructMLCall(Env,rn(no,_RN1,user,_O1),bodyMC(W1),headMC(_),
12605			C0,X,[or([]),rl([]),fl(_DML1)],noAb,[],Exp,Goal).
12606
12607performQuery(X,G1,Goal) :-
12608	nonvar(X),
12609	!,
12610 	once((call((call(G1), Goal)), atomic(X))).
12611performQuery(X,G1,Goal) :-
12612	!,
12613 	call((call(G1), Goal)), 
12614	atomic(X).
12615
12616/***********************************************************************
12617 *
12618 * deduce(+EnvName,+MS,:+-Info,-E)
12619 *
12620 *	If instantiated, Info is one of 
12621 *		infl(+-X,+-Y,+-W),
12622 *		posInfl(+-X,+-Y), negInfl(+-X,+-Y), noInfl(+-X,+-Y),
12623 *		simultInfl(+-Xs,+-Y,+-W), 
12624 *		simultPosInfl(+-Xs,+-Y), simultNegInfl(+-Xs,+-Y), 
12625 *		simultNoInfl(+-Xs,+-Y), 
12626 *		change(+-X,+-W),
12627 *		increase(+-X), decrease(+-X), noChange(+-X).
12628 *
12629 *	Succeeds if Info can be inferred by deduction.
12630 */
12631
12632deduce(EnvName,MS,infl(X,Y,W),[]) :-
12633	get_Env_World(EnvName,MS,Env,World),
12634	infl(Env,World,X,Y,W).
12635deduce(EnvName,MS,simultInfl(X,Y,W),[]) :-
12636	get_Env_World(EnvName,MS,Env,World),
12637	simultInfl(Env,World,X,Y,W).
12638deduce(EnvName,MS,leastInfl(X,Y),[]) :-
12639	get_Env_World(EnvName,MS,Env,World),
12640	leastInfl(Env,World,X,Y).
12641deduce(EnvName,MS,leastInfls(Xs,Y),[]) :-
12642	get_Env_World(EnvName,MS,Env,World),
12643	leastInfls(Env,World,Xs,Y).
12644deduce(EnvName,MS,greatestInfl(X,Y),[]) :-
12645	get_Env_World(EnvName,MS,Env,World),
12646	greatestInfl(Env,World,X,Y).
12647deduce(EnvName,MS,greatestInfls(Xs,Y),[]) :-
12648	get_Env_World(EnvName,MS,Env,World),
12649	greatestInfls(Env,World,Xs,Y).
12650deduce(EnvName,MS,maxPosInfl(X,Y,W),[]) :-
12651	get_Env_World(EnvName,MS,Env,World),
12652	maxPosInfl(Env,World,X,Y,W).
12653deduce(EnvName,MS,maxNegInfl(X,Y,W),[]) :-
12654	get_Env_World(EnvName,MS,Env,World),
12655	maxNegInfl(Env,World,X,Y,W).
12656deduce(EnvName,MS,change(Y,W),[]) :-
12657	get_Env_World(EnvName,MS,Env,World),
12658	change(Env,World,Y,W).
12659
12660%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12661
12662deduce(EnvName,MS,posInfl(X,Y),[]) :-
12663	get_Env_World(EnvName,MS,Env,World),
12664	posInfl(Env,World,X,Y).
12665deduce(EnvName,MS,negInfl(X,Y),[]) :-
12666	get_Env_World(EnvName,MS,Env,World),
12667	negInfl(Env,World,X,Y).
12668deduce(EnvName,MS,simultPosInfl(Xs,Y),[]) :-
12669	get_Env_World(EnvName,MS,Env,World),
12670	simultPosInfl(Env,World,Xs,Y).
12671deduce(EnvName,MS,simultNegInfl(Xs,Y),[]) :-
12672	get_Env_World(EnvName,MS,Env,World),
12673	simultNegInfl(Env,World,Xs,Y).
12674deduce(EnvName,MS,simultNoInfl(Xs,Y),[]) :-
12675	get_Env_World(EnvName,MS,Env,World),
12676	simultNoInfl(Env,World,Xs,Y).
12677deduce(EnvName,MS,noInfl(X,Y),[]) :-
12678	get_Env_World(EnvName,MS,Env,World),
12679	noInfl(Env,World,X,Y).
12680deduce(EnvName,MS,increase(X),[]) :-
12681	get_Env_World(EnvName,MS,Env,World),
12682	increase(Env,World,X).
12683deduce(EnvName,MS,decrease(X),[]) :-
12684	get_Env_World(EnvName,MS,Env,World),
12685	decrease(Env,World,X).
12686deduce(EnvName,MS,noChange(Y),[]) :-
12687	get_Env_World(EnvName,MS,Env,World),
12688	noChange(Env,World,Y).
12689
12690
12691
12692
12693getQuery(Env,W1,C0,X,Exp,Goal) :-
12694	var(C0),
12695	clause(conceptName(Env,_,_,C0),_),
12696	constructMLCall(Env,rn(no,_RN1,user,_O1),bodyMC(W1),headMC(_),
12697			C0,X,[or([]),rl([]),fl(_DML1)],noAb,[],Exp,Goal).
12698getQuery(Env,W1,C0,X,Exp,Goal) :-
12699	var(C0),
12700	clause(conceptName(Env,_,_,C1),_),
12701	C0 = not(C1),
12702	constructMLCall(Env,rn(no,_RN1,user,_O1),bodyMC(W1),headMC(_),
12703			C0,X,[or([]),rl([]),fl(_DML1)],noAb,[],Exp,Goal).
12704%getQuery(Env,W1,C0,X,Exp,Goal) :-
12705% 	var(C0),
12706%	!,
12707%	constructMLCall(Env,rn(no,_RN1,user,_O1),bodyMC(W1),headMC(_),
12708%			C0,X,[or([]),rl([]),fl(_DML1)],noAb,[],Exp,Goal),
12709%	!.
12710getQuery(Env,W1,C0,X,Exp,Goal) :-
12711	nonvar(C0),
12712	convertInAntecedent(Env,rn(no,user,_O1),bodyMC(W1),headMC(_),
12713			    C0,X,[or([]),rl([]),fl(_DML1)],noAb,[],Exp,Goal),
12714	!.
12715
12716/***********************************************************************
12717 *
12718 * abduce(+-Hypothesis,+-Consequent).
12719 *
12720 *	Succeeds if Consequent follows under the hypothesis Hypothesis.
12721 */
12722abduce(Hyps,elementOf(X,Y)) :-
12723	!,
12724	getCurrentEnvironment(EnvName),
12725	abduce(EnvName,[],Hyps,elementOf(X,Y),_).
12726abduce(Hypothesis,Consequent) :-
12727        getCurrentEnvironment(EnvName),
12728	abduce(EnvName,[],Hypothesis,Consequent,[]).
12729
12730/***********************************************************************
12731 *
12732 * abduce(+EnvName,+-Hypothesis,+-Consequent).
12733 *
12734 *	Succeeds if Consequent follows under the hypothesis Hypothesis.
12735 */
12736
12737abduce(EnvName,Hypothesis,elementOf(X,C)) :-
12738	nonvar(EnvName),
12739	environment(EnvName,_,_),
12740	!,
12741	abduce(EnvName,[],elementOf(X,C),_Exp).
12742abduce(MS,Hypothesis,elementOf(X,C)) :-
12743	nonvar(MS),
12744	(MS = [] ; MS = [_|_]),
12745        getCurrentEnvironment(EnvName),
12746	!,
12747	abduce(EnvName,MS,Hypothesis,elementOf(X,C),_Exp).
12748abduce(Hypothesis,elementOf(X,C),Exp) :-
12749	getCurrentEnvironment(EnvName),
12750	!,
12751	abduce(EnvName,[],Hypothesis,elementOf(X,C),Exp).
12752abduce(EnvName,Hypothesis,Consequent) :-
12753        environment(EnvName,_,_),
12754	!,
12755	abduce(EnvName,[],Hypothesis,Consequent,[]).
12756abduce(MS,Hypothesis,Consequent) :-
12757	nonvar(MS),
12758        (MS = [] ; MS = [_|_]),
12759        getCurrentEnvironment(EnvName),
12760	!,
12761	abduce(EnvName,MS,Hypothesis,Consequent,[]).
12762
12763abduce(EnvName,Hyps,elementOf(X,Y),Exp) :-
12764	nonvar(EnvName),
12765	environment(EnvName,_,_),
12766	!,
12767	abduce(EnvName,[],Hyps,elementOf(X,Y),Exp).
12768abduce(MS,Hyps,elementOf(X,Y),Exp) :-
12769	nonvar(MS),
12770	(MS = [] ; MS = [_|_]),
12771	getCurrentEnvironment(EnvName),
12772	!,
12773	abduce(EnvName,MS,Hyps,elementOf(X,Y),Exp).
12774abduce(EnvName,MS,Hyps,elementOf(X,Y)) :-
12775	!,
12776	abduce(EnvName,MS,Hyps,elementOf(X,Y),_Exp).
12777
12778abduce(EnvName,MS,Hyps,elementOf(X,C),Exp) :-
12779	environment(EnvName,Env,_),
12780	convertMS(negative,Env,[[],true],MS,[],[W1,G1],_),
12781	constructMLCall(Env,rn(no,_RN1,user,_O1),bodyMC(W1),headMC(_),
12782			C,X,[or([]),rl([]),fl(_DML1)],D,[],_Exp1,InHead),
12783	call((call(G1), InHead)),
12784	getConstraint(InHead,X),
12785	atomic(X),
12786	allowedAnswerConcept(Env,C),
12787	getAbductionHyps(D,GL),
12788	once((doConsistencyCheck([],GL),doMinimalityCheck([],GL))),
12789	getExplanation(InHead,Exp),
12790	getAbductionHypotheses(D,Hyps).
12791
12792getAbductionHypotheses(L,[]) :-
12793	var(L),
12794	!.
12795getAbductionHypotheses([],[]) :-
12796	!.
12797getAbductionHypotheses([in(Env,RN,modal(MS),C,X,_,_,_,_)|L1],
12798	                [assert_ind(MS1,X,C)|L2]) :- 
12799	!,
12800	translateModalContext(MS,MS1),
12801	getAbductionHypotheses(L1,L2).
12802getAbductionHypotheses([C1|L1],
12803	                [C1|L2]) :- 
12804	!,
12805	getAbductionHypotheses(L1,L2).
12806
12807
12808translateModalContext([],[]) :-
12809	!.
12810translateModalContext(app([WP,_]:m(M,A),W),MS) :-
12811	var(WP),
12812	!,
12813	translateAgent(A,A1),
12814	translateModalContext(W,MS2),
12815	append(MS2,[d(M,A1)],MS).
12816translateModalContext(app([WP,_]:m(M,A),W),MS) :-
12817	!,
12818	translateAgent(A,A1),
12819	translateModalContext(W,MS2),
12820	append(MS2,[b(M,A1)],MS).
12821
12822translateAgent(A,all) :-
12823	var(A),
12824	!.
12825translateAgent(A,A) :-
12826	!.
12827
12828/***********************************************************************
12829 *
12830 * abduce(+EnvName,+MS,+-change(+-X,+-Wx),+-change(+-Y,+-Wy),[]).
12831 *
12832 *	Succeeds if, under the hypothesis of change(+-X,+-Wx), 
12833 *	change(+-Y,+-Wy) follows.
12834 */
12835
12836abduce(EnvName,MS,change(X,Wx),change(Y,Wy),[]) :-
12837	get_Env_World(EnvName,MS,Env,World),
12838	wellDefined_ChangeWeight(Wx),
12839	infl(Env,World,X,Y,Wxy),
12840	not(given_change(Env,World,X,_)),
12841	bagof(W,Z^changingInfl(Env,World,Z,Y,W),Ws),
12842	weightOf_change(Wx,Wxy,Wy1),
12843	weightOf_SimultChange([Wy1|Ws],Wy).
12844
12845abduce(EnvName,MS,change(X,Wx),change(Y,Wy),[]) :-
12846	get_Env_World(EnvName,MS,Env,World),
12847	var(Wx),
12848	wellDefined_ChangeWeight(Wy),
12849	infl(Env,World,X,Y,Wxy),
12850	not(given_change(Env,World,X,_)),
12851	bagof(W,Z^changingInfl(Env,World,Z,Y,W),Ws),
12852	weightOf_SimultChange([Wy1|Ws],Wy),
12853	weightOf_change(Wx,Wxy,Wy1).
12854
12855abduce(EnvName,MS,change(X,Wx),change(Y,Wy),[]) :-
12856	get_Env_World(EnvName,MS,Env,World),
12857	infl(Env,World,X,Y,Wxy),
12858	not(given_change(Env,World,_,_)),
12859	weightOf_change(Wx,Wxy,Wy).
12860
12861abduce(EnvName,MS,change(X,Wx),change(X,Wx),[]) :-
12862	get_Env_World(EnvName,MS,Env,World),
12863	wellDefined_attribute(Env,World,X),
12864	not(given_change(Env,World,X,_)),
12865	wellDefined_ChangeWeight(Wx).
12866
12867/***********************************************************************
12868 *
12869 * abduce(+EnvName,+MS,+-Hypothesis,+-Consequent).
12870 *
12871 *	Succeeds if Consequent follows under the hypothesis Hypothesis.
12872 *
12873 *	Hypothesis and Consequent are of the form:
12874 *		increase(+-X), decrease(+-X), noChange(+-X). 
12875 */
12876
12877abduce(EnvName,MS,Hypothesis,Consequent,[]) :-
12878	var(Hypothesis),
12879	nonvar(Consequent),
12880	once(default_change(Consequent,WeightedConsequent)),
12881	abduce(EnvName,MS,change(X,Wx),WeightedConsequent,[]),
12882	once(default_change(Hypothesis,change(X,Wx),[])).
12883
12884abduce(EnvName,MS,Hypothesis,Consequent,[]) :-
12885	nonvar(Hypothesis),
12886%	var(Consequent),
12887	once(default_change(Hypothesis,WeightedHypothesis)),
12888	abduce(EnvName,MS,WeightedHypothesis,change(Y,Wy),[]),
12889	once(default_change(Consequent,change(Y,Wy))).
12890
12891/***********************************************************************
12892 *
12893 * abduce(+EnvName,+MS,+Changes,+-change(+-Y,+-W)).
12894 *
12895 *	Succeeds if change(+-Y,+-W) follows under the hypotheses of 
12896 *	Changes.
12897 *	Changes is a list of changes of the form change(+-X,+W).
12898 */
12899
12900abduce(EnvName,MS,Hs,change(Y,W),[]) :-
12901	get_Env_World(EnvName,MS,Env,World),
12902	nonvar(Hs),
12903	aux_abduce(Env,World,Hs,change(Y,W),Ws),
12904	wellDefined_SimultChanges(Hs),
12905	weightOf_SimultChange(Ws,W).
12906
12907/***********************************************************************
12908 *
12909 * abduce(+EnvName,+MS,+-Hypotheses,+-Consequent).
12910 *
12911 *	Succeeds if Consequent follows under the hypotheses Hypotheses.
12912 *
12913 *	Hypotheses  is a list of 
12914 *		increase(+-X), decrease(+-X), noChange(+-X). 
12915 *	predicates and Consequent is one of these.
12916 */
12917
12918abduce(EnvName,MS,Hypotheses,Consequent,[]) :-
12919	nonvar(Hypotheses),
12920	nonvar(Consequent),
12921	once(default_changes(Hypotheses,WeightedHypotheses)),
12922	once(default_change(Consequent,WeightedConsequent)),
12923	abduce(EnvName,MS,WeightedHypotheses,WeightedConsequent,[]).
12924
12925abduce(EnvName,MS,Hypotheses,Consequent,[]) :-
12926	nonvar(Hypotheses),
12927%	var(Consequent),
12928	once(default_changes(Hypotheses,WeightedHypotheses)),
12929	abduce(EnvName,MS,WeightedHypotheses,change(Y,Wy),[]),
12930	once(default_change(Consequent,change(Y,Wy))).
12931
12932/***********************************************************************
12933 *
12934 * abduce(+EnvName,+MS,+-Change,+-Changes).
12935 *
12936 *	Succeeds if Changes are hold under the hypothesis that Change
12937 *	holds.
12938 *
12939 *	Changes (respectively Change) is a list of changes 
12940 *	(respectively a change) of the form change(+-X,+-W).
12941 */
12942
12943abduce(EnvName,MS,Change,Changes,[]) :-
12944	nonvar(Changes),
12945	aux2_abduce(EnvName,MS,Change,Changes).
12946
12947/***********************************************************************
12948 *
12949 * abduce(EnvName,MS,+-Hypothesis,+-Consequents).
12950 *
12951 *	Succeeds if Consequents follow under the hypothesis Hypothesis.
12952 *
12953 *	Hypothesis is of the form:
12954 *		increase(+-X), decrease(+-X), noChange(+-X). 
12955 *	Consequents is a list of these.
12956 */
12957
12958abduce(EnvName,MS,Hypothesis,Consequents,[]) :-
12959	nonvar(Hypothesis),
12960	nonvar(Consequents),
12961	once(default_change(Hypothesis,WeightedHypothesis)),
12962	once(default_changes(Consequents,WeightedConsequents)),
12963	abduce(EnvName,MS,WeightedHypothesis,WeightedConsequents,[]).
12964
12965abduce(EnvName,MS,Hypothesis,Consequents,[]) :-
12966	var(Hypothesis),
12967	nonvar(Consequents),
12968	once(default_changes(Consequents,WeightedConsequents)),
12969	abduce(EnvName,MS,change(X,Wx),WeightedConsequents,[]),
12970	once(default_change(Hypothesis,change(X,Wx))).
12971
12972
12973
12974
12975
12976/***********************************************************************
12977 *
12978 * allowedAnswerConcept(+C)
12979 * true iff C is a concept introduced by the user. No concept names
12980 * introduces by the system or concept terms are allowed as answer.
12981 *
12982 */
12983
12984allowedAnswerConcept(Env,C) :-
12985	atomic(C),
12986	!,
12987	clause(conceptName(Env,_,_,C),_),
12988	!.
12989% allowedAnswerConcept(Env,C) :-
12990%	atomic(C),
12991%	conceptEqualSets(Env,user,_,C,_,_),
12992%	!.
12993% allowedAnswerConcept(Env,C) :-
12994%	atomic(C),
12995%	conceptEqualSets(Env,user,_,_,C,_),
12996%	!.
12997%allowedAnswerConcept(Env,C) :-
12998%	atomic(C),
12999%	conceptSubsets(Env,user,_,C,_,_),
13000%	!.
13001%allowedAnswerConcept(Env,C) :-
13002%	atomic(C),
13003%	conceptSubsets(Env,user,_,_,C,_).
13004allowedAnswerConcept(Env,C) :-
13005	nonvar(C),
13006	C = not(D),
13007	!,
13008	nonvar(D),
13009	not(D = not(E)),
13010	!,
13011	allowedAnswerConcept(Env,D).
13012allowedAnswerConcept(_,normal(_)) :-
13013	!,
13014	fail.
13015allowedAnswerConcept(_,not(normat(_))) :-
13016	!,
13017	fail.
13018allowedAnswerConcept(Env,C) :-
13019	not(atomic(C)),
13020	!.
13021
13022/***********************************************************************
13023 *
13024 * inconsistent(+EnvName)
13025 *
13026 */
13027
13028
13029inconsistent :-
13030	getCurrentEnvironment(EnvName),
13031	inconsistent(EnvName,[]).
13032
13033inconsistent(EnvName) :-
13034	nonvar(EnvName),
13035	environment(EnvName,_,_),
13036	inconsistent(EnvName,[]).
13037inconsistent(MS) :-
13038	(MS = [] ; MS = [_|_]),
13039	getCurrentEnvironment(EnvName),
13040	inconsistent(EnvName,MS).
13041
13042inconsistent(EnvName,MS) :-
13043	environment(EnvName,Env,_),
13044	convertMS(negative,Env,[[],true],MS,[],[W1,G1],_),
13045	constructMLHead(Env,rn(_AX1,_RN1,user,_O1),W1,C,X,[or([]),rl([]),fl(_DML1)],noAb,[],_,InHead1),
13046	call((call(G1), InHead1)),
13047	getConstraint(InHead1,X),
13048	atomic(X),
13049	constructMLHead(Env,rn(_AX2,_RN2,_S2,_O2),W1,not(C),X,[or([]),rl([]),fl(_DML1)],noAb,[],_,InHead2),
13050	call((call(G1), InHead2)).
13051
13052/***********************************************************************
13053 *
13054 * consistent(+EnvName)
13055 *
13056 */
13057
13058consistent :-
13059	not(inconsistent).
13060
13061consistent(EnvName) :-
13062	nonvar(EnvName),
13063	environment(EnvName,_,_),
13064	not(inconsistent(EnvName,[])).
13065consistent(MS) :-
13066	(MS = [] ; MS = [_|_]),
13067	getCurrentEnvironment(EnvName),
13068	not(inconsistent(EnvName,MS)).
13069
13070consistent(EnvName,MS) :-
13071	not(inconsistent(EnvName,MS)).
13072
13073/***********************************************************************
13074 *
13075 *
13076 */
13077
13078metaReasoning :-
13079	constructMLHead(Env,rn(ti,ti,system,lInR),W1,C,X,
13080			_HYPS,_D,_CALLS,inconsistency,InHead1),
13081	constructMLHead(Env,rn(ti,ti,system,lInR),W1,not(C),X,
13082			_HYPS,_D,_CALLS,inconsistency,InHead2),
13083	Lit11 = not(inconsistencyCheck(_,_,_)),
13084	Lit13 = asserta_logged(InHead2),
13085	Lit14 = asserta_logged(inconsistencyCheck(MS,C,X)),
13086	Lit15 = tryInconsistency(MS,C,X,InHead2),
13087	assertz_logged((InHead1 :- atomic(C), atomic(X), Lit11, Lit13, Lit14, Lit15)),
13088	Lit23 = asserta_logged(InHead1),
13089	Lit24 = asserta_logged(inconsistencyCheck(MS,C,X)),
13090	Lit25 = tryInconsistency(MS,C,X,InHead1),
13091	assertz_logged((InHead2 :- atomic(C), atomic(X), Lit11, Lit23, Lit24, Lit25)).
13092
13093
13094tryInconsistency(MS,C,X,InHead) :-
13095	inconsistent(MS),
13096	!,
13097	retract(inconsistencyCheck(MS,C,X)),
13098	retract(InHead).
13099tryInconsistency(MS,C,X,InHead) :-
13100	!,
13101	retract(inconsistencyCheck(MS,C,X)),
13102	retract(InHead),
13103	!,
13104	fail.
13105
13106/**********************************************************************
13107 *
13108 *
13109 *
13110 */
13111
13112realize(EnvName,MS,X,CL) :-
13113	environment(EnvName,Env,_),
13114	conceptHierarchy(Env,MS,Tree1),
13115	conceptElement(Env,MS,_,user,X,C1,_),
13116	search(C1,Tree1,TreeList),
13117	realizeArgs(EnvName,MS,X,TreeList,CL).
13118
13119realizeDag(EnvName,MS,X,node(CL,AL),CL1) :-
13120	realizeArgs(EnvName,MS,X,AL,CL0),
13121	realizeNode(EnvName,MS,X,CL,CL0,CL1).
13122
13123realizeArgs(_,_,_,[],[]) :-
13124	!.
13125realizeArgs(EnvName,MS,X,[C|AL],CL3) :-
13126	realizeDag(EnvName,MS,X,C,CL1),
13127	realizeArgs(EnvName,MS,X,AL,CL2),
13128	append(CL1,CL2,CL3).
13129
13130realizeNode(EnvName,MS,X,_CL,[C0|CL0],[C0|CL0]) :-
13131	!.
13132realizeNode(EnvName,MS,X,[C|CL],[],CL1) :-
13133	deduce(EnvName,MS,elementOf(X,C),_),
13134	!,
13135	CL1 = [C|CL].
13136realizeNode(_,_,_,_,_,[]) :-
13137	!.
13138
13139sbAsk(EnvName,MS,X,CL) :-
13140	environment(EnvName,Env,_),
13141	conceptHierarchy(Env,MS,Tree1),
13142	askDag(EnvName,MS,X,Tree1,CL).
13143
13144askDag(EnvName,MS,X,node(CL,AL),CL1) :-
13145	askArgs(EnvName,MS,X,AL,CL0),
13146	askNode(EnvName,MS,X,CL,CL0,CL1).
13147
13148askArgs(_,_,_,[],[]) :-
13149	!.
13150askArgs(EnvName,MS,X,[C|AL],CL3) :-
13151	askDag(EnvName,MS,X,C,CL1),
13152	askArgs(EnvName,MS,X,AL,CL2),
13153	append(CL1,CL2,CL3).
13154
13155askNode(_EnvName,_MS,_esX,CL,[C0|CL0],CL1) :-
13156	!,
13157	append([C0|CL0],CL,CL1).
13158askNode(EnvName,MS,X,[C|CL],[],CL1) :-
13159	deduce(EnvName,MS,elementOf(X,C),_),
13160	!,
13161	CL1 = [C|CL].
13162askNode(_,_,_,_,_,[]) :-
13163	!.
13164
13165/**********************************************************************
13166 *
13167 * completeParameter(+ParameterList,-EnvName,-MS,-Query,-Proof)
13168 * takes a list of parameters ParameterList and instantiates the
13169 * variables EnvName,MS,Query,Proof correctly.
13170 *
13171 */
13172
13173completeParameter([Query],EnvName,[],Query,_Proof) :-
13174	getCurrentEnvironment(EnvName),
13175	!.
13176completeParameter([P1,P2],P1,_MS,P2,_Proof) :-
13177	var(P1),
13178	!.
13179completeParameter([P1,P2],P1,_MS,P2,_Proof) :-
13180	nonvar(P1),
13181	atomic(P1),
13182	P1 \== [],
13183	!.
13184completeParameter([P1,P2],EnvName,P1,P2,_Proof) :-
13185	nonvar(P1),
13186	(P1 = [] ; P1 = [_|_]),
13187	!,
13188	getCurrentEnvironment(EnvName).
13189completeParameter([P1,P2],EnvName,[],P1,P2) :-
13190	nonvar(P1),
13191	!,
13192	getCurrentEnvironment(EnvName).
13193completeParameter([P1,P2,P3],P1,P2,P3,_Proof) :-
13194	var(P1),
13195	var(P2),
13196	!.
13197completeParameter([P1,P2,P3],P1,P2,P3,_Proof) :-
13198	nonvar(P1),
13199	atomic(P1),
13200	P1 \== [],
13201	var(P2),
13202	!.
13203completeParameter([P1,P2,P3],EnvName,P1,P2,P3) :-
13204	nonvar(P1),
13205	(P1 = [] ; P1 = [_|_]),
13206	!,
13207	getCurrentEnvironment(EnvName).
13208completeParameter([P1,P2,P3],P1,P2,P3,_Proof) :-
13209	nonvar(P2),
13210	(P2 = [] ; P2 = [_|_]),
13211	!.
13212completeParameter([P1,P2,P3],P1,[],P2,P3) :-
13213	nonvar(P1),
13214	atomic(P1),
13215	P1 \== [],
13216	!.
13217completeParameter([P1,P2,P3],P1,[],P2,P3) :-
13218	!.
13219completeParameter([P1,P2,P3,P4],P1,P2,P3,P4) :-
13220	!.
13221
13222/**********************************************************************
13223 *
13224 * @(#) initCall.pl 1.4@(#)
13225 *
13226 */
13227
13228motelBanner:-
13229 dmsg('Welcome to MOTEL (Version 0.8 July 1993)'),
13230 dmsg('Copyright (c) 1993, Patrick Brandmeier, Ullrich Hustadt'),
13231 dmsg('                    Renate Schmidt, Jan Timm. All rights preserved.'),
13232 dmsg('MOTEL is distributed in the hope that it will be useful, but'),
13233 dmsg('WITHOUT ANY WARRANTY;  without even the implied warranty of,'),
13234 dmsg('MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.'),
13235 dmsg('Hacked for logicmoo'),nop(prolog).
13236 
13237:- initialization(motelBanner,restore).13238:- initialization(motelBanner).13239:- initialization(initializeMotel,restore).13240:- initialization(initializeMotel).13241
13242%- fss.load.kb
13243%  This is the translation of fss.sb-one into MOTEL syntax.
13244%- fss.save.kb
13245%  This is what you get if you save the knowledge base fss.
13246%- fss.env
13247%  This is what you get if you save the environment fssEnv.
13248
13249
13250
13251
13252%- fss.load.kb
13253%  This is the translation of fss.sb-one into MOTEL syntax.
13254
13255
13256prolog_statistics_time(A) :-
13257  prolog_statistics:((
13258        time_state(B),
13259        (   call_cleanup(catch((user:A,dmsg(prolog_statistics_time(A))),
13260                               C,
13261                               (report(B, 10), throw(C))),
13262                         D=true),
13263            time_true(B),
13264            (   D==true
13265            ->   !
13266            ;   true
13267            )
13268        ;   report(B, 11),
13269            fail
13270        ))).
13271
13272% prolog_statistics_time(G):-prolog_statistics:time(user:G).
13273
13274setupTest(N,Body):- dmsg(runOneExample(N)),!,must(once(Body)),dmsg(succeed(Body)).
13275runTest(N,Body):- dmsg(runTest(N)),!,must(once(Body)),dmsg(succeed(Body)).
13276
13277
13278testMotel :-
13279       testAllMotelExamples(1),
13280	!.
13281
13282
13283testMotel(N) :-
13284	testAllMotelExamples(N),
13285	!.
13286
13287testAllMotelExamples(61) :-
13288	print('Test complete'), nl, nl,
13289	!.
13290testAllMotelExamples(N) :-
13291        runOneExample(N),	
13292        M is N + 1,
13293	testAllMotelExamples(M).
13294
13295runOneExample(N):-  clause(testMotelExample(N) , no_goal),!.
13296runOneExample(N):-  nl,  nl, 
13297   initializeMotel,
13298   dmsg(runOneExample(N)), nl, 
13299   listing(example(N)),
13300   listing(testMotelExample(N)),
13301   prolog_statistics_time(must(once(example(N)))),
13302   showHierarchy,
13303   prolog_statistics_time(forall(clause(testMotelExample(N),Body),ignore(runTest(N,Body)))).
13304   
13305
13306no_goal:-
13307  print('No goal for this example'), nl.
13308
13309
13310
13311tryGoal(G) :-
13312	call(G),
13313	!,
13314	print('Goal '), print(G), print(' succeeded'), nl.
13315tryGoal(G) :-
13316	print('Goal '), print(G), print(' failed'), nl.
13317
13318
13319
13320/**********************************************************************
13321 *
13322 * @(#) testMotel.pl 1.6@(#)
13323
13324
13325% examples.pl  mpred_code_unused.txt  mpred_expansion.pl.txt  mpred_mote_examples_76l.pl
13326  mpred_motel_env.pl  mpred_motel_examples_63.pl  mpred_motel_fssKb.pl 
13327   mpred_motel.pl  mpred_pfc_d.unused  mpred_pfc_utils.unused  testMotel.pl
13328
13329
13330
13331   ensure_loaded(library('logicmoo/motel/mpred_motel')).
13332
13333   testMotel.
13334
13335
13336
13337 :-  ensure_loaded(library('logicmoo/motel/mpred_motel_fssKb')).
13338 % :-  ensure_loaded(library('logicmoo/motel/mpred_motel_env')).
13339 :-  ensure_loaded(library('logicmoo/motel/examples')).
13340 :-  ensure_loaded(library('logicmoo/motel/testMotel')).
13341
13342
13343
13344 *
13345 */
13346 :-  ensure_loaded(library('logicmoo/motel/test_motel_examples_63')).13347
13348 :- fixup_exports.