3
3
* Copyright (c) 2009-2020 Sebastian Kraemer, basti.kr@gmail.com and others SPDX-License-Identifier: BSD-2-Clause
4
4
*/
5
5
6
- const { isEqual, uniqWith, get, sortBy, some, concat, groupBy, forOwn} = require ( "lodash" ) ;
6
+ const { isEqual, uniqWith, get, sortBy, some, concat, groupBy, forOwn, uniq , isObject } = require ( "lodash" ) ;
7
7
8
8
const TOKEN_AND = 1 ;
9
9
const TOKEN_OR = 2 ;
@@ -410,6 +410,7 @@ let simplify = or => {
410
410
} ;
411
411
412
412
function forceDisjunctive ( or , not = false ) {
413
+ console . log ( "forceDisj" ) ;
413
414
let res = [ ]
414
415
or . map ( and => {
415
416
let orsFromAnd = [ ]
@@ -432,23 +433,41 @@ function forceDisjunctive(or, not=false) {
432
433
} )
433
434
}
434
435
} else if ( an . brack ) {
436
+
435
437
let brackRes = forceDisjunctive ( an . brack , not ?! an . not :an . not )
436
438
if ( orsFromAnd . length === 0 ) {
437
439
orsFromAnd = brackRes
438
440
} else {
439
441
let newOrsFromAnd = [ ]
440
442
brackRes . map ( b => {
441
443
orsFromAnd . map ( a => {
442
- newOrsFromAnd . push ( [ ...a , ...b ] )
444
+ b . map ( c => {
445
+ if ( ! some ( a , c ) )
446
+ newOrsFromAnd . push ( uniq ( [ ...a , ...b ] ) )
447
+ } )
448
+ /*if(!isEqual(a, b)) {
449
+ let addElement = uniq([...a, ...b]);
450
+ if(!some(newOrsFromAnd, addElement))
451
+ newOrsFromAnd.push(addElement)
452
+ }*/
443
453
} )
454
+ //newOrsFromAnd = cleanSameLiteral(newOrsFromAnd)
444
455
} )
456
+ if ( ! isEqual ( newOrsFromAnd , cleanSameLiteral ( newOrsFromAnd ) ) ) {
457
+ console . log ( "excess1!" )
458
+ }
459
+ newOrsFromAnd = cleanSameLiteral ( newOrsFromAnd )
445
460
orsFromAnd = newOrsFromAnd
446
461
}
447
462
} else {
448
463
throw new Error ( "Unexpected type" ) ;
449
464
}
450
465
} )
451
466
res = [ ...res , ...orsFromAnd ]
467
+
468
+ if ( ! isEqual ( res , cleanSameLiteral ( res ) ) ) {
469
+ console . log ( "excess2!" )
470
+ }
452
471
} )
453
472
if ( not ) {
454
473
res = [ res . map ( i => {
@@ -457,6 +476,94 @@ function forceDisjunctive(or, not=false) {
457
476
res = forceDisjunctive ( res )
458
477
}
459
478
res = cleanTrivialLiterals ( res )
479
+ if ( ! isEqual ( res , cleanSameLiteral ( res ) ) ) {
480
+ console . log ( "excess3!" )
481
+ }
482
+ return res ;
483
+ }
484
+
485
+ function forceDisjunctive2 ( or , not = false ) {
486
+ //console.log("forceDisj2");
487
+ let or2 = or ;
488
+ if ( not ) {
489
+ or2 = [ or2 . map ( i => {
490
+ return { brack : i . filter ( j => ! isEqual ( j , { } ) ) . map ( j => {
491
+ if ( isObject ( j ) ) {
492
+ j . not = ! j . not
493
+ } else {
494
+ j = j . map ( k => {
495
+ k . not = ! k . not
496
+ } )
497
+ }
498
+ return [ j ]
499
+ } ) }
500
+ } ) ]
501
+ not = false
502
+ }
503
+ let res = [ ]
504
+ or2 . map ( and => {
505
+ let orsFromAnd = [ ]
506
+ and . filter ( i => ! isEqual ( i , { } ) ) . map ( an => {
507
+ if ( an . sym ) {
508
+ let symObj = { ...an , not : an . not } ;
509
+ if ( symObj . not && get ( symObj , "sym.type" ) === TOKEN_TRUE ) {
510
+ symObj . not = false
511
+ symObj . sym . type = TOKEN_FALSE
512
+ } else if ( symObj . not && get ( symObj , "sym.type" ) === TOKEN_FALSE ) {
513
+ symObj . not = false
514
+ symObj . sym . type = TOKEN_TRUE
515
+ }
516
+ if ( orsFromAnd . length === 0 )
517
+ orsFromAnd . push ( [ symObj ] ) ;
518
+ else {
519
+ orsFromAnd . map ( a => {
520
+ a . push ( symObj )
521
+ return a
522
+ } )
523
+ }
524
+ } else if ( an . brack ) {
525
+
526
+ let brackRes = forceDisjunctive2 ( an . brack , an . not )
527
+ if ( orsFromAnd . length === 0 ) {
528
+ orsFromAnd = brackRes
529
+ } else {
530
+ let newOrsFromAnd = [ ]
531
+ brackRes . map ( b => {
532
+ orsFromAnd . map ( a => {
533
+ /*b.map(c => {
534
+ if(!some(a, c))
535
+ newOrsFromAnd.push(uniq([...a, ...b]))
536
+ })*/
537
+ let addElement = uniq ( [ ...a , ...b ] ) ;
538
+ newOrsFromAnd . push ( addElement )
539
+ /*if(!isEqual(a, b)) {
540
+ let addElement = uniq([...a, ...b]);
541
+ if(!some(newOrsFromAnd, addElement))
542
+ newOrsFromAnd.push(addElement)
543
+ }*/
544
+ } )
545
+ //newOrsFromAnd = cleanSameLiteral(newOrsFromAnd)
546
+ } )
547
+ /*if(!isEqual(newOrsFromAnd, cleanSameLiteral(newOrsFromAnd))) {
548
+ console.log("excess1!")
549
+ }*/
550
+ newOrsFromAnd = cleanSameLiteral ( newOrsFromAnd )
551
+ orsFromAnd = newOrsFromAnd
552
+ }
553
+ } else {
554
+ throw new Error ( "Unexpected type" ) ;
555
+ }
556
+ } )
557
+ res = [ ...res , ...orsFromAnd ]
558
+
559
+ /*if(!isEqual(res, cleanSameLiteral(res))) {
560
+ console.log("excess2!")
561
+ }*/
562
+ } )
563
+ res = cleanTrivialLiterals ( res )
564
+ /*f(!isEqual(res, cleanSameLiteral(res))) {
565
+ console.log("excess3!")
566
+ }*/
460
567
return res ;
461
568
}
462
569
@@ -584,7 +691,7 @@ let simp = (expr) => {
584
691
} ;
585
692
586
693
let disj = ( expr ) => {
587
- let res = forceDisjunctive ( parse ( tokenize ( expr ) ) ) ;
694
+ let res = forceDisjunctive2 ( parse ( tokenize ( expr ) ) ) ;
588
695
res = cleanNotSatisfiableAndInDisj ( res )
589
696
res = cleanSameLiteral ( res )
590
697
res = sortAnds ( res )
@@ -593,7 +700,7 @@ let disj = (expr) => {
593
700
}
594
701
595
702
let impl = ( expr ) => {
596
- let res = forceDisjunctive ( parse ( tokenize ( expr ) ) ) ;
703
+ let res = forceDisjunctive2 ( parse ( tokenize ( expr ) ) ) ;
597
704
res = cleanNotSatisfiableAndInDisj ( res )
598
705
res = cleanSameLiteral ( res )
599
706
res = sortAnds ( res )
@@ -635,7 +742,7 @@ let cleanDuplicates = (ors) => {
635
742
}
636
743
637
744
let lcrCode = ( expr ) => {
638
- let res = forceDisjunctive ( parse ( tokenize ( expr ) ) ) ;
745
+ let res = forceDisjunctive2 ( parse ( tokenize ( expr ) ) ) ;
639
746
res = cleanNotSatisfiableAndInDisj ( res )
640
747
res = cleanSameLiteral ( res )
641
748
res = sortAnds ( res )
@@ -680,7 +787,7 @@ let lcr = (rule, otherRules) => {
680
787
} )
681
788
}
682
789
683
- res = forceDisjunctive ( res ) ;
790
+ res = forceDisjunctive2 ( res ) ;
684
791
res = cleanNotSatisfiableAndInDisj ( res )
685
792
res = cleanSameLiteral ( res )
686
793
res = sortAnds ( res )
@@ -719,9 +826,9 @@ let replaceToVal = (ors, truthy = [], falsy = [], makeOthers = null) => {
719
826
720
827
721
828
let eval = ( expr , truthy = [ ] , falsy = [ ] , makeOthers = null ) => {
722
- let res = forceDisjunctive ( parse ( tokenize ( expr ) ) ) ;
829
+ let res = forceDisjunctive2 ( parse ( tokenize ( expr ) ) ) ;
723
830
res = replaceToVal ( res , truthy , falsy , makeOthers ) ;
724
- res = forceDisjunctive ( res ) ;
831
+ res = forceDisjunctive2 ( res ) ;
725
832
res = cleanNotSatisfiableAndInDisj ( res )
726
833
res = cleanSameLiteral ( res )
727
834
res = sortAnds ( res )
@@ -731,8 +838,14 @@ let eval = (expr, truthy = [], falsy= [], makeOthers = null) => {
731
838
return res ;
732
839
}
733
840
734
- console . log ( impl ( "1+1" ) )
735
- console . log ( eval ( "-x+-y" , [ ] , [ ] , TOKEN_FALSE ) ) ;
841
+ console . log ( impl ( "a+(a|b)" ) )
842
+ //let lcrAnswer = lcr("1", ["A", "A+B"]);
843
+ //console.log(lcrAnswer)
844
+ //console.log(impl("-(A+(B+(C+-D+-E)))"))
845
+ //console.log(impl("(A+B)|(A+C)"))
846
+ //console.log(impl("-(421+513+(531|365)+-K26+(5XXL+-581L|2XXL)|(K34+(M256|M176)|ME05)+(460|494))|K26)"))
847
+ //onsole.log(impl("(421+513+(531|365)+-K26+((B01+(M256|M176|M177)|ME05)+(5XXL+-512L+-516L+-521L+-526L+-581L|2XXL)|(K34+B01+(M256|M176|M177)|ME05)+(460|494))|K26)"))
848
+ //onsole.log(eval("-x+-y", [], [], TOKEN_FALSE));
736
849
737
850
738
851
0 commit comments