-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotor.cpp
More file actions
executable file
·1076 lines (941 loc) · 40.2 KB
/
Copy pathMotor.cpp
File metadata and controls
executable file
·1076 lines (941 loc) · 40.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Motor.h"
/*NOTA:= Todas las funciona seran revisada y seran evaluadas
con un porcentaje de riesgo o falla "si es posible una acotacion"
DENOTACION:
ESTADO: %(PORCENTAJE)
ACOTACION:
RESPONSABLE:
*/
//---------------------------------------------------------------------------
#pragma package(smart_init)
/*------------------------------------------------------------------------*/
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// MOTOR
/*------------------------------------------------------------------------*/
/* ---------------------- Destructores especificos ------------------*/
/*------------------------------------------------------------------------*/
// libera el arbol
void Motor::LiberaArbol(Nodo *Raiz)
{
if (Raiz!=NULL)
{
LiberaArbol(Raiz->Hijo_izq);
LiberaArbol(Raiz->Hijo_der);
delete(Raiz);
}
}
/*------------------------------------------------------------------------*/
/* ------------- Fin de Destructores especificos--------------------------*/
/*------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*/
/*------------------------------- Nivel 1 --------------------------------*/
/*------- Aplica la consulta. Pertenece al Nivel 0 (Motor Inferencia) ----*/
bool Motor::Consulta_a_motor(Predicado *predicado,int n)
{
Prepara_consulta(predicado,n);
bool Verdad=false;
bool CambioVariable=false;
bool CambioConflicto=false;
bool Condicion_BORDE=false;
do
{
do
{
Verdad=Analiza_verdad(Nodo_raiz);
//---------------------------------------------------------------//
if (Numeros_de_nodos>MAX_NODOS) Condicion_BORDE=true;
if (Condicion_BORDE==true) break;
// Manejo de una expecion posible
//---------------------------------------------------------------//
if (Verdad==true)
{
if (Analiza_solucion()==true) break;
else Almacena_solucion();
}
CambioVariable=Cambio_variable();
}
while(CambioVariable==true);
if (Analiza_solucion()==true && Verdad==true) break;
//---------------------------------------------------------------//
if (Condicion_BORDE==true) break;
// Manejo de una expecion posible
//---------------------------------------------------------------//
CambioConflicto=Cambio_conflicto();
}
while(CambioConflicto==true);
return Verdad;
}
/*-------------------- fin Nivel 1 ---------------------------------------*/
/*------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*/
/*------------------------- Prepara consulta -----------------------------*/
/*------------------------(pertenece al nivel 1)--------------------------*/
bool Motor::Prepara_consulta(Predicado *hecho,int n)
{
bool logro=false;
// una consulta (n=1)
if (n==1)
{
int IDVA1=0;
int IDVA2=0;
if (hecho[0].obtieneTipoArgumento1()==1)
//si es atomo el argumento =1
{
IDVA1=Lista_variable->Agrega_variable(hecho->obtieneArgumento1());
}
else
{
// si no es atomo
IDVA1=Lista_variable->Agrega_variable(NULL);
}
Nodo_raiz= new Nodo(hecho[0].obtieneNombre(),IDVA1,NULL);
if (hecho[0].obtieneTipo()==2&&hecho[0].obtieneArgumento1()==hecho[0].obtieneArgumento2())
Nodo_raiz->CambiarID_variable2(Nodo_raiz->IDVA1);
// si el hecho tiene el argumento en las 2 partes
if (hecho[0].obtieneTipo()==2&&Nodo_raiz->IDVA2==NULL) // si es binario
{
if (hecho[0].obtieneTipoArgumento2()==1)
//si es atomo el argumento =2
{
IDVA2=Lista_variable->Agrega_variable(hecho[0].obtieneArgumento2());
}
else
{
// si no es atomo
IDVA2=Lista_variable->Agrega_variable(NULL);
}
Nodo_raiz->CambiarID_variable2(IDVA2);
}
if (hecho[0].obtieneTipoArgumento1()==1 && (hecho[0].obtieneTipo()!=2 ||
hecho[0].obtieneTipoArgumento2()==1))
Una_solucion=true;
// si todos eran atomos
logro=true;
Numeros_de_nodos=1;
}
// fin de una consulta
if (n==2) //dos consulta (n=2)
{
int IDVA1=0;
int IDVA2=0;
int IDVA_creada=0;
Nodo_raiz= new Nodo(NULL,NULL,NULL);
Nodo *Hijo_izq=NULL;
Nodo *Hijo_der=NULL;
if (hecho[0].obtieneTipoArgumento1()==1)
//si es atomo el Argumento1
{
IDVA1=Lista_variable->Agrega_variable(hecho[0].obtieneArgumento1());
}
else
{
// si no es atomo
IDVA1=Lista_variable->Agrega_variable(NULL);
}
Hijo_izq = new Nodo(hecho[0].obtieneNombre(),IDVA1,NULL);
if (hecho[0].obtieneTipo()==2&&hecho[0].obtieneArgumento1()==hecho[0].obtieneArgumento2())
Hijo_izq->CambiarID_variable2(Hijo_izq->IDVA1);
if (hecho[0].obtieneTipo()==2&&Hijo_izq->IDVA2==NULL) // si es binario el primer antecendente
{
if (hecho[0].obtieneTipoArgumento2()==1)
//si es atomo el argumento2
{
IDVA2=Lista_variable->Agrega_variable(hecho[0].obtieneArgumento2());
}
else
{
// si no es atomo
IDVA2=Lista_variable->Agrega_variable(NULL);
}
Hijo_izq->CambiarID_variable2(IDVA2);
}
// final del hijo de izquierdo
// Hijo derecho comenzado
Hijo_der= new Nodo(hecho[1].obtieneNombre(),NULL,NULL);
// Comienzo de variable de hijo de derecho
// verificando si exite esta variable antes, es decir,
// si la primera variable del segundo argumento existia antes
// entoces aplica el mismo identificador, sino crea una variable
if (hecho[1].obtieneArgumento1()==hecho[0].obtieneArgumento1())
Hijo_der->CambiarID_variable1(Hijo_izq->IDVA1);
if (hecho[0].obtieneTipo()==2 &&
hecho[1].obtieneArgumento1()==hecho[0].obtieneArgumento2())
Hijo_der->CambiarID_variable1(Hijo_izq->IDVA2);
// si no existia antes
if (Hijo_der->IDVA1==NULL)
{
if (hecho[1].obtieneTipoArgumento1()==1)
//si es atomo el argumento1 del segundo hecho
{
if(IDVA2!=0) {IDVA_creada=Lista_variable->Agrega_variable(hecho[1].obtieneArgumento1());Hijo_der->CambiarID_variable1(IDVA_creada);}
else {IDVA2=Lista_variable->Agrega_variable(hecho[1].obtieneArgumento1());Hijo_der->CambiarID_variable1(IDVA2);}
}
else
{
if(IDVA2!=0) {IDVA_creada=Lista_variable->Agrega_variable(NULL);Hijo_der->CambiarID_variable1(IDVA_creada);}
else {IDVA2=Lista_variable->Agrega_variable(NULL);Hijo_der->CambiarID_variable1(IDVA2);}
}
}
if( hecho[1].obtieneTipo()==2 && hecho[1].obtieneArgumento2()==hecho[1].obtieneArgumento1())
Hijo_der->CambiarID_variable2(Hijo_der->IDVA1);
if (hecho[1].obtieneTipo()==2 && Hijo_der->IDVA2==NULL) // si es binario del segundo antecendente
{
// Comienzo de la creacion de variable de la binaria variable de hijo de derecho
// verificando si exite esta variable antes, es decir,
// si la segunda variable del segundo argumento existia antes
// entoces aplica el mismo identificador
if (hecho[1].obtieneArgumento2()==hecho[0].obtieneArgumento1())
Hijo_der->CambiarID_variable2(Hijo_izq->IDVA1);
if (hecho[0].obtieneTipo()==2 &&
hecho[1].obtieneArgumento2()==hecho[0].obtieneArgumento2())
Hijo_der->CambiarID_variable2(Hijo_izq->IDVA2);
if (Hijo_der->IDVA2==NULL)
{
if (hecho[1].obtieneTipoArgumento2()==1)
//si es atomo el argumento2 del segundo hecho
{
IDVA_creada=Lista_variable->Agrega_variable(hecho[1].obtieneArgumento2());
Hijo_der->CambiarID_variable2(IDVA_creada);
}
else
{
IDVA_creada=Lista_variable->Agrega_variable(NULL);
Hijo_der->CambiarID_variable2(IDVA_creada);
}
}
}
Nodo_raiz->CambiarID_variable1(IDVA1);
Nodo_raiz->CambiarID_variable2(IDVA2);
Nodo_raiz->CambiarID_variableCreada(IDVA_creada);
Nodo_raiz->Hijo_izq=Hijo_izq;
Nodo_raiz->Hijo_der=Hijo_der;
Hijo_izq->Padre=Nodo_raiz;
Hijo_der->Padre=Nodo_raiz;
Numeros_de_nodos=3;
if ( Lista_variable->Busca_valor(IDVA1)!=NULL &&
Lista_variable->Busca_valor(IDVA2)!=NULL && (IDVA_creada==NULL ||
Lista_variable->Busca_valor(IDVA_creada)!=NULL) )
Una_solucion=true;
// si todos eran atomos
logro=true;
}
// fin de doble consulta
return logro;
}
/*------------------------------------------------------------------------*/
/*--------------------------- Analiza verdad -----------------------------*/
/*----------------------- (Pertenece al nivel 1) -------------------------*/
bool Motor::Analiza_verdad(Nodo *nodo_analizado)
{
bool Estado_regla=false;
bool Verdad_analizada=false;
// si al llegar la condicion de borde se cumplio
if (MAX_NODOS < Numeros_de_nodos) return false;
// Manteniendo bajo informacion esta condicion (OJO) //
if (nodo_analizado->Hijo_izq==NULL) // no tiene hijos
{
//Todos los Argumentos tienen Valores//
if (Lista_variable->Busca_valor(nodo_analizado->IDVA1)!=NULL &&
( nodo_analizado->IDVA2==NULL ||
Lista_variable->Busca_valor(nodo_analizado->IDVA2)!=NULL ))
{
// es un predicado con valores(atomos)
// si es unario
if (nodo_analizado->IDVA2==NULL && BH.Verdad_hecho(
nodo_analizado->Predicado,Lista_variable->Busca_valor
(nodo_analizado->IDVA1),NULL))
Verdad_analizada=true;
//si es binario
if (nodo_analizado->IDVA2!=NULL && BH.Verdad_hecho(
nodo_analizado->Predicado,Lista_variable->Busca_valor
(nodo_analizado->IDVA1),Lista_variable->Busca_valor(
nodo_analizado->IDVA2)))
Verdad_analizada=true;
if (Verdad_analizada==false)
Estado_regla=true;
}
else //si no son todos tiene valores
// hay hecho asociado //
if (BH.Hecho_asociado(nodo_analizado->Predicado))
{
bool Inicializar_variable=false;
// se puede inicializar unario
if (nodo_analizado->IDVA2==NULL &&
BH.Iniciar_variable_hecho_unario(nodo_analizado->Predicado,1)!=NULL)
{
Inicializar_variable=true;
Verdad_analizada=true;
Inicializar_variables(nodo_analizado);
//inciando variable unarias
}
// se puede inicializar binario
if (nodo_analizado->IDVA2!=NULL && BH.Iniciar_variable_en_hecho
(nodo_analizado->Predicado,Lista_variable->Busca_valor(
nodo_analizado->IDVA1),Lista_variable->Busca_valor(
nodo_analizado->IDVA2),1)!=NULL)
{
Inicializar_variable=true;
Verdad_analizada=true;
Inicializar_variables(nodo_analizado);
//inciando variable binarias
/*-------------------------------------------------------------*/
// aplicando parche de la consulta X,X
if ( nodo_analizado->INVA1==nodo_analizado->INVA2 &&
BH.Verdad_hecho(
nodo_analizado->Predicado,Lista_variable->Busca_valor
(nodo_analizado->IDVA1),Lista_variable->Busca_valor(
nodo_analizado->IDVA2))==false)
Verdad_analizada=false;
/*-------------------------------------------------------------*/
}
if (Inicializar_variable==false)
Estado_regla=true;
}
else
{
Estado_regla=true;
//si no es hecho asociado a regla
}
}
else Estado_regla=true; // debe ser una regla
if (Estado_regla==false)
nodo_analizado->Hecho=true;
/* si la busqueda a traves de los hechos fue infructuosa =>
indica que pasara a regla */
if (Estado_regla==true)
{
// tiene regla asociada
if(BC.Existe_regla(nodo_analizado->Predicado)!=NULL
|| nodo_analizado->Predicado==NULL )
{
//no tiene hijos
if(nodo_analizado->Hijo_izq==NULL)
Crea_hijos(nodo_analizado,1);
///sss
int cuantosizq=0;
int cuantosder=0;
cuantosizq=atributosConocidos(nodo_analizado->Hijo_izq);
cuantosder=atributosConocidos(nodo_analizado->Hijo_der);
///sss
bool Verdad_hijo_izq=false;
bool Verdad_hijo_der=true; //¿porque?
//valores temporales de la verdad de los hijos
if (cuantosizq>=cuantosder)
{
Verdad_hijo_izq=Analiza_verdad(nodo_analizado->Hijo_izq);
//fue verdadero
if (Verdad_hijo_izq==true)
{
if (nodo_analizado->Hijo_der!=NULL)
{
Verdad_hijo_der=Analiza_verdad(nodo_analizado->Hijo_der);
if (Verdad_hijo_der==true)
Verdad_analizada=true;
}
else Verdad_analizada=true;
}
} //cuantosizq>cuantosder
if (cuantosizq<cuantosder)
{
Verdad_hijo_der=Analiza_verdad(nodo_analizado->Hijo_der);
if (Verdad_hijo_der==true)
{Verdad_hijo_izq=Analiza_verdad(nodo_analizado->Hijo_izq);
if (Verdad_hijo_izq==true)
Verdad_analizada=true;
}
} //cuantosizq<cuantosder
}
}
// estado_regla_fin;
return Verdad_analizada;
}
/*------------------------------------------------------------------------*/
/*---------------------------Crea Hijos-----------------------------------*/
/*---------------------(Pertence Analiza Verdad)--------------------------*/
void Motor::Crea_hijos(Nodo *Padre, int indice_de_regla)
{
Regla *Regla_aux;
Regla_aux = BC.Entregar_regla(Padre->Predicado,indice_de_regla);
if (Regla_aux==NULL)
ShowMessage("fue nula");
int IDVA_creada=0;
Nodo *hijo_izq=NULL;
Nodo *hijo_der=NULL;
if (indice_de_regla==1 && Regla_aux->obtieneConflicto()>0)
{
Padre->Conflicto = 1;
Padre->Max_conflicto = Regla_aux->obtieneConflicto();
}
hijo_izq = new Nodo(Regla_aux->obtieneAntecedente1().obtieneNombre(), NULL, NULL);
//el primer antecedente ,es decir, hijo izquierdo
if (Regla_aux->obtieneConsecuente().obtieneArgumento1() == Regla_aux->obtieneAntecedente1().obtieneArgumento1())
hijo_izq->IDVA1 = Padre->IDVA1;
if (Regla_aux->obtieneAntecedente1().obtieneTipo()==2 && Regla_aux->obtieneConsecuente().obtieneArgumento1() == Regla_aux->obtieneAntecedente1().obtieneArgumento2())
hijo_izq->IDVA2 = Padre->IDVA1;
if (Regla_aux->obtieneConsecuente().obtieneTipo()==2) //si es binario el consecuente
{
if (Regla_aux->obtieneConsecuente().obtieneArgumento2() == Regla_aux->obtieneAntecedente1().obtieneArgumento1())
hijo_izq->IDVA1 = Padre->IDVA2;
if (Regla_aux->obtieneAntecedente1().obtieneTipo()==2 && Regla_aux->obtieneConsecuente().obtieneArgumento2() == Regla_aux->obtieneAntecedente1().obtieneArgumento2())
hijo_izq->IDVA2 = Padre->IDVA2;
}
Padre->Hijo_izq=hijo_izq;
hijo_izq->Padre=Padre;
Numeros_de_nodos++; //condicion de Borde
//si hay otro antecedente ,es decir, un hijo derecho
if (Regla_aux->obtieneTipo()==2)
{
hijo_der = new Nodo(Regla_aux->obtieneAntecedente2().obtieneNombre(), NULL, NULL);
if (Regla_aux->obtieneConsecuente().obtieneArgumento1() == Regla_aux->obtieneAntecedente2().obtieneArgumento1())
hijo_der->IDVA1 = Padre->IDVA1;
if (Regla_aux->obtieneAntecedente2().obtieneTipo()==2 && Regla_aux->obtieneAntecedente2().obtieneTipo()==2 && Regla_aux->obtieneConsecuente().obtieneArgumento1() == Regla_aux->obtieneAntecedente2().obtieneArgumento2())
hijo_der->IDVA2 = Padre->IDVA1;
if (Regla_aux->obtieneConsecuente().obtieneTipo()==2) // si es binario es consecuente
{
if (Regla_aux->obtieneConsecuente().obtieneArgumento2() == Regla_aux->obtieneAntecedente2().obtieneArgumento1())
hijo_der->IDVA1 = Padre->IDVA2;
if (Regla_aux->obtieneAntecedente2().obtieneTipo()==2 && Regla_aux->obtieneConsecuente().obtieneArgumento2() == Regla_aux->obtieneAntecedente2().obtieneArgumento2())
hijo_der->IDVA2 = Padre->IDVA2;
}
Padre->Hijo_der=hijo_der;
hijo_der->Padre=Padre;
Numeros_de_nodos++; //condicion de Borde
}
/* Creacion de Variable
Analizar de nuevo este caso para la agregacion de variable por
lo que no se tien siempre al hijo derecho*/
/*-------------SEGUNDA FORMA DE CREACION DE VARIABLE---------------------------*/
if (Regla_aux->obtieneTipo()==2) // si tiene 2 antecedente
{
if (hijo_izq->IDVA1==NULL)
{
IDVA_creada=Lista_variable->Agrega_variable(NULL);
Padre->ID_creaVariable=IDVA_creada;
hijo_izq->CambiarID_variable1(IDVA_creada);
if (Regla_aux->obtieneAntecedente1().obtieneTipo()==2 && hijo_izq->IDVA2==NULL)
hijo_izq->CambiarID_variable2(IDVA_creada);
if (hijo_der->IDVA1==NULL)
hijo_der->CambiarID_variable1(IDVA_creada);
if (Regla_aux->obtieneAntecedente2().obtieneTipo()==2 && hijo_der->IDVA2==NULL)
hijo_der->CambiarID_variable2(IDVA_creada);
}
else
{
if (Regla_aux->obtieneAntecedente1().obtieneTipo()==2 && hijo_izq->IDVA2==NULL)
{
IDVA_creada=Lista_variable->Agrega_variable(NULL);
Padre->ID_creaVariable=IDVA_creada;
hijo_izq->CambiarID_variable2(IDVA_creada);
if (hijo_der->IDVA1==NULL)
hijo_der->CambiarID_variable1(IDVA_creada);
if (Regla_aux->obtieneAntecedente2().obtieneTipo()==2 && hijo_der->IDVA2==NULL)
hijo_der->CambiarID_variable2(IDVA_creada);
}
else
{
if(hijo_der->IDVA1==NULL)
{
IDVA_creada=Lista_variable->Agrega_variable(NULL);
Padre->ID_creaVariable=IDVA_creada;
hijo_der->CambiarID_variable1(IDVA_creada);
if (Regla_aux->obtieneAntecedente2().obtieneTipo()==2 && hijo_der->IDVA2==NULL)
hijo_der->CambiarID_variable2(IDVA_creada);
}
else
{
if(Regla_aux->obtieneAntecedente2().obtieneTipo()==2 && hijo_der->IDVA2==NULL)
{
IDVA_creada=Lista_variable->Agrega_variable(NULL);
Padre->ID_creaVariable=IDVA_creada;
hijo_der->CambiarID_variable2(IDVA_creada);
}
}
}
}
}
/*------------------------------------------------------------------------------------------------------*/
/*---------------------------FIN DE LA de cracion de variable-------------------------------------------*/
// si la regla tiene 1 ANTECEDNETE(un solo hijo)
if (Regla_aux->obtieneTipo()==1)
{
if(Regla_aux->obtieneAntecedente1().obtieneTipo()==1) // si es unario
{
//(X)
if(hijo_izq->IDVA1==NULL)
{
IDVA_creada=Lista_variable->Agrega_variable(NULL);
Padre->ID_creaVariable=IDVA_creada;
hijo_izq->IDVA1=IDVA_creada;
}
}
/// fin es unario
else
{
// 3casos
if (hijo_izq->IDVA1==NULL && hijo_izq->IDVA2==NULL)
// (X,X)
// si creo dos variables en ambos argumentos
{
IDVA_creada=Lista_variable->Agrega_variable(NULL);
Padre->ID_creaVariable=IDVA_creada;
hijo_izq->IDVA1=IDVA_creada;
hijo_izq->IDVA2=IDVA_creada;
}
if (hijo_izq->IDVA1!=NULL && hijo_izq->IDVA2==NULL)
//(algo,X)
// si creo una variable en el segundo argumento
{
IDVA_creada=Lista_variable->Agrega_variable(NULL);
Padre->ID_creaVariable=IDVA_creada;
hijo_izq->IDVA2=IDVA_creada;
}
if (hijo_izq->IDVA1==NULL && hijo_izq->IDVA2!=NULL)
// (X,algo)
// si creo una variable en el primer argumento
{
IDVA_creada=Lista_variable->Agrega_variable(NULL);
Padre->ID_creaVariable=IDVA_creada;
hijo_izq->IDVA1=IDVA_creada;
}
}
}
// fin de un antecedente ( un solo hijo )
delete(Regla_aux);
}
/*------------------------------------------------------------------------*/
/*----------------------- Inicializa variables ---------------------------*/
/*---------------------(Pertence Analiza Verdad)--------------------------*/
void Motor::Inicializar_variables(Nodo *nodo_a_inicializar)
{
Predicado *hechoIni=NULL;
if(nodo_a_inicializar->IDVA2==NULL)//si nodo a inicializar es unario
{
if(Lista_variable->Busca_valor(nodo_a_inicializar->IDVA1) == NULL)//si primer argumento
// necesita inicializarse
{
nodo_a_inicializar->INVA1=1;
hechoIni=BH.Iniciar_variable_hecho_unario(nodo_a_inicializar->Predicado,1);
Lista_variable->Cambia_valor_elemento_lista(nodo_a_inicializar->IDVA1,
hechoIni->obtieneArgumento1());
}
}
else
// si es binario el hecho
{
if ((Lista_variable->Busca_valor(nodo_a_inicializar->IDVA1) == NULL) &&
(Lista_variable->Busca_valor(nodo_a_inicializar->IDVA2) == NULL) )//si primer y segundo argumento
//es variable
{ //si ambos fueran átomos no se inicializa ninguna variable
nodo_a_inicializar->INVA1=1;
nodo_a_inicializar->INVA2=1;
hechoIni=BH.Iniciar_variable_en_hecho(nodo_a_inicializar->Predicado,NULL,NULL,1);
Lista_variable->Cambia_valor_elemento_lista(nodo_a_inicializar->IDVA1,
hechoIni->obtieneArgumento1());
Lista_variable->Cambia_valor_elemento_lista(nodo_a_inicializar->IDVA2,
hechoIni->obtieneArgumento2());
}
else
{
if((Lista_variable->Busca_valor(nodo_a_inicializar->IDVA1)!= NULL)&&
(Lista_variable->Busca_valor(nodo_a_inicializar->IDVA2) == NULL))//si primer arg es átomo
// y segundo arg es variable
{
nodo_a_inicializar->INVA2=1;
hechoIni=BH.Iniciar_variable_en_hecho(nodo_a_inicializar->Predicado,
Lista_variable->Busca_valor(nodo_a_inicializar->IDVA1),NULL,1);
Lista_variable->Cambia_valor_elemento_lista(nodo_a_inicializar->IDVA2,
hechoIni->obtieneArgumento2());
// y asi cambiar los demas
}
else
{
if((Lista_variable->Busca_valor(nodo_a_inicializar->IDVA1) == NULL)&&
(Lista_variable->Busca_valor(nodo_a_inicializar->IDVA2) != NULL))//si primer arg es
//variable y segundo arg es átomo
{
nodo_a_inicializar->INVA1=1;
hechoIni=BH.Iniciar_variable_en_hecho(nodo_a_inicializar->Predicado,NULL,
Lista_variable->Busca_valor(nodo_a_inicializar->IDVA2),1);
Lista_variable->Cambia_valor_elemento_lista(nodo_a_inicializar->IDVA1,
hechoIni->obtieneArgumento1());
}
}
}
}
delete(hechoIni);
}
/*------------------------------------------------------------------------*/
/*-------------------- Contenedor de soluciones --------------------------*/
/*------------------- (Pertenece al Nivel 1) -----------------------------*/
void Motor::Almacena_solucion()
{
UnaSolucion X1(Nodo_raiz->IDVA1,Lista_variable->Busca_valor(Nodo_raiz->IDVA1));
UnaSolucion X2(Nodo_raiz->IDVA2,Lista_variable->Busca_valor(Nodo_raiz->IDVA2));
UnaSolucion X3;
if (Nodo_raiz->Predicado==NULL)
X3=UnaSolucion(Nodo_raiz->ID_creaVariable,Lista_variable->Busca_valor(Nodo_raiz->ID_creaVariable));
else X3=UnaSolucion();
// tal ves estas lineas queden
ElementoSoluciones *Solucion = new ElementoSoluciones(X1,X2,X3);
Soluciones->Agregasoluciones(Solucion);
/* ShowMessage("Numero:"+IntToStr(Numeros_de_nodos)+" "+Nodo_raiz->Predicado+" "+X1.getIdvax()+"="+Lista_variable->Busca_valor(Nodo_raiz->IDVA1)+
" "+X2.getIdvax()+"="+Lista_variable->Busca_valor(Nodo_raiz->IDVA2));*/
}
/*------------------------------------------------------------------------*/
/*-------------------------- (primera prioridad) -------------------------*/
/*-------------------- (Nivel 2. Cambio de variables) --------------------*/
bool Motor::Cambio_variable()
{
bool existen;
bool respuesta = true;
Nodo *nodo_hoja=NULL;
do
{
respuesta = true;
nodo_hoja=NULL;
if (Buscar_hoja_mas_derecha(Nodo_raiz,&nodo_hoja)==true)
{
existen = Verif_existen_vars(nodo_hoja);
if (existen!=true)
{
Quitar_valores(nodo_hoja);
}
else
{
Cambiar_variables(nodo_hoja);
respuesta=true;
//break; // paul
//return true;
}
}
else
{
respuesta=false;
break; //paul
}
}while(existen != true);
return respuesta;
}
/*------------------- (fin Nivel 2. Cambio de variable) -----------------*/
/*-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
/*----- Buscar hoja más a la derecha con instanciación de variables -----*/
/*----------- (Pertenece al nivel 2 del cambio de variables) ------------*/
bool Motor::Buscar_hoja_mas_derecha(Nodo *nodo_h, Nodo **hoja_devuelta)
{
bool devuelve=false;
if (nodo_h!=NULL)
{
if ((nodo_h->INVA1 == 0) && (nodo_h->INVA2 == 0))
//si no tiene instaciación de variables
{
// bool encontro=Buscar_hoja_mas_derecha(nodo_h->Hijo_der, hoja_devuelta);
devuelve=Buscar_hoja_mas_derecha(nodo_h->Hijo_der, hoja_devuelta);
//cambiado por paul
if( devuelve==false ) //encontro==false
{
devuelve=Buscar_hoja_mas_derecha(nodo_h->Hijo_izq, hoja_devuelta);
}
}
else //si tiene instanciación de variables
{
*hoja_devuelta = nodo_h;
devuelve=true;
}
}
return devuelve;
}
/*-----------------------------------------------------------------------*/
/*--------- Verificar si existen más variables para instanciar ----------*/
/*----------- (Pertenece al nivel 2 del cambio de variables) ------------*/
bool Motor::Verif_existen_vars(Nodo *hoja)
{
bool devuelve=false;
//verificar primero si el nodo llamado hoja es realmente una hoja,
//es decir, si es un hecho
if (hoja->Hijo_izq == NULL) //si el nodo no tiene hijo izquierdo
{ //entonces es una hoja
if (hoja->IDVA2 == 0) //si nodo es unario (si no tiene valor el segundo
{ //argumento)
if (BH.Iniciar_variable_hecho_unario(hoja->Predicado,
(hoja->INVA1)+1) != NULL) //si existen más variables del mismo
{ //predicado
devuelve = true;
}
}
else //nodo no unario (binario)
{
if ( hoja->INVA1 > 0 && hoja->INVA2 > 0 )
//si se cambiar inicializar dos variables
{
if (BH.Iniciar_variable_en_hecho
(hoja->Predicado,NULL,NULL,(hoja->INVA1)+1)!= NULL)
//si existen más pares de variables del mismo predicado
devuelve = true;
}
if ( hoja->INVA1>0 && hoja->INVA2==0)
//si se necesita cambiar sólo la primera variable
//(el segundo argumento es un átomo)
{
if (BH.Iniciar_variable_en_hecho(hoja->Predicado,
NULL, Lista_variable->Busca_valor(hoja->IDVA2) ,
(hoja->INVA1)+1) != NULL)//si existen más variables del
//mismo predicado
devuelve = true;
}
if ( hoja->INVA1==0 && hoja->INVA2>0)
//si se necesita inicializar sólo la segunda variable
//(el primer argumento es un átomo)
{
if (BH.Iniciar_variable_en_hecho(hoja->Predicado,
Lista_variable->Busca_valor(hoja->IDVA1), NULL ,
(hoja->INVA2)+1) != NULL)//si existen más variables del
//mismo predicado
devuelve = true;
}
}
}
return devuelve;
}
/*-----------------------------------------------------------------------*/
/*------------ Quita los valores y prioridad del nodo -------------------*/
/*----------- (Pertenece al nivel 2 del cambio de variables) ------------*/
void Motor::Quitar_valores(Nodo *nodo_muerto)
{
// si el primer argumento a sido inicializado le quita la inicializacion
if (nodo_muerto->INVA1!=0)
{
Lista_variable->Cambia_valor_elemento_lista(nodo_muerto->IDVA1,NULL);
nodo_muerto->INVA1 = 0;
}
// si el segundo argumento a sido inicializado le quita la inicializacion
if (nodo_muerto->INVA2!=0)
{
Lista_variable->Cambia_valor_elemento_lista(nodo_muerto->IDVA2,NULL);
nodo_muerto->INVA2 = 0;
}
}
/*-----------------------------------------------------------------------*/
/*---------- Cambia las variables por las nuevas encontradas ------------*/
/*----------- (Pertenece al nivel 2 del cambio de variables) ------------*/
void Motor::Cambiar_variables(Nodo *nodo_vivo)
{
Predicado *obj;
//si nodo es unario
if (nodo_vivo->IDVA2 == 0)//inicializa la primera variable
{
nodo_vivo->INVA1++;
obj=BH.Iniciar_variable_hecho_unario(nodo_vivo->Predicado,
nodo_vivo->INVA1);
Lista_variable->Cambia_valor_elemento_lista(nodo_vivo->IDVA1,
obj->obtieneArgumento1());
}
else //nodo no es unario
{
//Cambiar las dos variables
if ( nodo_vivo->INVA1 > 0 && nodo_vivo->INVA2 > 0 )
{
nodo_vivo->INVA1++;
nodo_vivo->INVA2++;
obj=BH.Iniciar_variable_en_hecho(nodo_vivo->Predicado, NULL, NULL,
nodo_vivo->INVA1);
Lista_variable->Cambia_valor_elemento_lista(nodo_vivo->IDVA1,
obj->obtieneArgumento1());
Lista_variable->Cambia_valor_elemento_lista(nodo_vivo->IDVA2,
obj->obtieneArgumento2());
}
else
{
//Cambia sólo la segunda variable
if ( nodo_vivo->INVA1 == 0 && nodo_vivo->INVA2>0 )
{
nodo_vivo->INVA2++;
obj=BH.Iniciar_variable_en_hecho(nodo_vivo->Predicado,
Lista_variable->Busca_valor(nodo_vivo->IDVA1),NULL,
nodo_vivo->INVA2);
Lista_variable->Cambia_valor_elemento_lista(nodo_vivo->IDVA2,
obj->obtieneArgumento2());
}
else
{
//Cambia sólo la primera variable
if ( nodo_vivo->INVA1>0 && nodo_vivo->INVA2==0)
{
nodo_vivo->INVA1++;
obj=BH.Iniciar_variable_en_hecho(nodo_vivo->Predicado, NULL,
Lista_variable->Busca_valor(nodo_vivo->IDVA2),
nodo_vivo->INVA1);
Lista_variable->Cambia_valor_elemento_lista(nodo_vivo->IDVA1,
obj->obtieneArgumento1());
}
}
}
}
delete(obj);
}
/*-----------------------------------------------------------------------*/
/*------------------- Cambio Conflicto NIvel 2 --------------------------*/
bool Motor::Cambio_conflicto()
{
bool cambioconflicto=false;
bool estadoConflicto=false;
Nodo *nodoconflicto=Nodo_raiz;
// Aqui traspasara un hecho a un regla si es que se puede
Pasar_Hecho_a_Regla(nodoconflicto,&cambioconflicto);
if (cambioconflicto==true) return true;
// si no hay hechos que traspase a una regla
do
{
nodoconflicto=NULL;
estadoConflicto=false;
Busca_nodo_interno_mas_derecho(Nodo_raiz,&nodoconflicto);
if (nodoconflicto!=NULL)
{
if(nodoconflicto->Conflicto==nodoconflicto->Max_conflicto)
{
// volviendo a la priemra regla
Elimina_hijos(nodoconflicto->Hijo_izq);
Elimina_hijos(nodoconflicto->Hijo_der);
// borrando la variable si creo una variable el nodo
if (nodoconflicto->ID_creaVariable!=0)
{
Lista_variable->Borra_elemento_lista(nodoconflicto->ID_creaVariable);
nodoconflicto->ID_creaVariable=0;
}
nodoconflicto->Hijo_izq=NULL;
nodoconflicto->Hijo_der=NULL;
Crea_hijos(nodoconflicto,1);
nodoconflicto->Conflicto=-2;
//-2 es un indicador temporal del nodo para que no cambie
//temporalmente su regla (sólo afecta a esta función)
}
else estadoConflicto=true;
}
}while (nodoconflicto!=NULL && estadoConflicto==false);
/// Empezar a cambiar el conflicto a una regla
if (estadoConflicto==true)
{
Elimina_hijos(nodoconflicto->Hijo_izq);
Elimina_hijos(nodoconflicto->Hijo_der);
// borrando la variable si creo una variable el nodo
if(nodoconflicto->ID_creaVariable!=0)
{
Lista_variable->Borra_elemento_lista(nodoconflicto->ID_creaVariable);
nodoconflicto->ID_creaVariable=0;
}
nodoconflicto->Hijo_izq=NULL;
nodoconflicto->Hijo_der=NULL;
Crea_hijos(nodoconflicto,++nodoconflicto->Conflicto);
Borrar_indicador_conflicto(Nodo_raiz);
cambioconflicto=true;
}
return cambioconflicto;
}
/*------------------------------------------------------------------------*/
/*---------------------------Elimina hijos -------------------------------*/
/*------------- (pertenece al Nivel 2. Cambio de conflicto) --------------*/
void Motor::Elimina_hijos(Nodo *nodo_Conflicto_hijo)
{
// aqui elimina el nodo, las variables credas por el mimsmo
// y tambien ordena borra los valores que ha inicializado
if (nodo_Conflicto_hijo!=NULL)
{
Elimina_hijos(nodo_Conflicto_hijo->Hijo_izq);
Elimina_hijos(nodo_Conflicto_hijo->Hijo_der);
Quitar_valores(nodo_Conflicto_hijo);
if (nodo_Conflicto_hijo->ID_creaVariable!=0)
Lista_variable->Borra_elemento_lista(nodo_Conflicto_hijo->ID_creaVariable);
Numeros_de_nodos--;
delete(nodo_Conflicto_hijo);
}
}
/*------------------------------------------------------------------------*/
/*---------- Busca nodo interno mas derecho con conflicto-----------------*/
/*------------- (pertenece al Nivel 2. Cambio de conflicto) --------------*/
void Motor::Busca_nodo_interno_mas_derecho(Nodo *Raiz,Nodo **NodoConflicto)
{
// busca la interna mas derecha con un conflicto
if (Raiz!=NULL && *NodoConflicto==NULL)
{
if (*NodoConflicto==NULL) Busca_nodo_interno_mas_derecho(Raiz->Hijo_der,NodoConflicto);
if (*NodoConflicto==NULL) Busca_nodo_interno_mas_derecho(Raiz->Hijo_izq,NodoConflicto);
if (*NodoConflicto==NULL && Raiz->Conflicto>0)
*NodoConflicto=Raiz;
}
}
/*------------------------------------------------------------------------*/
/*--------------- Borrar indicador temporal de conflicto -----------------*/
/*------------- (pertenece al Nivel 2. Cambio de conflicto) --------------*/
void Motor::Borrar_indicador_conflicto(Nodo *Raiz)
{
// setae nuevamente los valores de conflicto a la normalidad