Clause.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { JavaObject, cast_java_lang_Object } from '../../../java/lang/JavaObject';
  2. import { JavaInteger, cast_java_lang_Integer } from '../../../java/lang/JavaInteger';
  3. import { AVLSet, cast_de_nrw_schule_svws_core_adt_set_AVLSet } from '../../../core/adt/set/AVLSet';
  4. import { Comparable, cast_java_lang_Comparable } from '../../../java/lang/Comparable';
  5. import { Variable, cast_de_nrw_schule_svws_core_kursblockung_satsolver_Variable } from '../../../core/kursblockung/satsolver/Variable';
  6. import { NullPointerException, cast_java_lang_NullPointerException } from '../../../java/lang/NullPointerException';
  7. import { JavaIterator, cast_java_util_Iterator } from '../../../java/util/JavaIterator';
  8. import { JavaString, cast_java_lang_String } from '../../../java/lang/JavaString';
  9. export class Clause extends JavaObject implements Comparable<Clause> {
  10. readonly variables : Array<Variable>;
  11. free : number = 0;
  12. sat : number = 0;
  13. /**
  14. * Konstruktor für eine 1-CNF-Klausel.
  15. *
  16. * @param pX Die 1. Variable in dieser Klausel.
  17. */
  18. public constructor(pX : Variable);
  19. /**
  20. * Konstruktor für eine 2-CNF-Klausel.
  21. *
  22. * @param pX Die 1. Variable in dieser Klausel.
  23. * @param pY Die 2. Variable in dieser Klausel.
  24. */
  25. public constructor(pX : Variable, pY : Variable);
  26. /**
  27. * Konstruktor für eine 3-CNF-Klausel.
  28. *
  29. * @param pX Die 1. Variable in dieser Klausel.
  30. * @param pY Die 2. Variable in dieser Klausel.
  31. * @param pZ Die 3. Variable in dieser Klausel.
  32. */
  33. public constructor(pX : Variable, pY : Variable, pZ : Variable);
  34. /**
  35. * Implementation for method overloads of 'constructor'
  36. */
  37. public constructor(__param0 : Variable, __param1? : Variable, __param2? : Variable) {
  38. super();
  39. if (((typeof __param0 !== "undefined") && ((__param0 instanceof JavaObject) && (__param0.isTranspiledInstanceOf('de.nrw.schule.svws.core.kursblockung.satsolver.Variable')))) && (typeof __param1 === "undefined") && (typeof __param2 === "undefined")) {
  40. let pX : Variable = cast_de_nrw_schule_svws_core_kursblockung_satsolver_Variable(__param0);
  41. this.variables = [pX];
  42. this.free = 1;
  43. this.sat = 0;
  44. } else if (((typeof __param0 !== "undefined") && ((__param0 instanceof JavaObject) && (__param0.isTranspiledInstanceOf('de.nrw.schule.svws.core.kursblockung.satsolver.Variable')))) && ((typeof __param1 !== "undefined") && ((__param1 instanceof JavaObject) && (__param1.isTranspiledInstanceOf('de.nrw.schule.svws.core.kursblockung.satsolver.Variable')))) && (typeof __param2 === "undefined")) {
  45. let pX : Variable = cast_de_nrw_schule_svws_core_kursblockung_satsolver_Variable(__param0);
  46. let pY : Variable = cast_de_nrw_schule_svws_core_kursblockung_satsolver_Variable(__param1);
  47. this.variables = [pX, pY];
  48. this.free = 2;
  49. this.sat = 0;
  50. } else if (((typeof __param0 !== "undefined") && ((__param0 instanceof JavaObject) && (__param0.isTranspiledInstanceOf('de.nrw.schule.svws.core.kursblockung.satsolver.Variable')))) && ((typeof __param1 !== "undefined") && ((__param1 instanceof JavaObject) && (__param1.isTranspiledInstanceOf('de.nrw.schule.svws.core.kursblockung.satsolver.Variable')))) && ((typeof __param2 !== "undefined") && ((__param2 instanceof JavaObject) && (__param2.isTranspiledInstanceOf('de.nrw.schule.svws.core.kursblockung.satsolver.Variable'))))) {
  51. let pX : Variable = cast_de_nrw_schule_svws_core_kursblockung_satsolver_Variable(__param0);
  52. let pY : Variable = cast_de_nrw_schule_svws_core_kursblockung_satsolver_Variable(__param1);
  53. let pZ : Variable = cast_de_nrw_schule_svws_core_kursblockung_satsolver_Variable(__param2);
  54. this.variables = [pX, pY, pZ];
  55. this.free = 3;
  56. this.sat = 0;
  57. } else throw new Error('invalid method overload');
  58. }
  59. public toString() : String {
  60. let s : String = "";
  61. for (let v of this.variables) {
  62. if (v.index === -1) {
  63. return "[SAT]";
  64. }
  65. if (v.index >= 0) {
  66. s = s.valueOf() + " " + v.nr;
  67. }
  68. }
  69. return "[" + s.valueOf() + "]";
  70. }
  71. private getSet() : AVLSet<Number> {
  72. let set : AVLSet<Number> = new AVLSet();
  73. for (let v of this.variables) {
  74. if (v.index >= 0) {
  75. set.add(v.nr);
  76. }
  77. }
  78. return set;
  79. }
  80. public compareTo(o : Clause) : number {
  81. let set1 : AVLSet<Number> = this.getSet();
  82. let set2 : AVLSet<Number> = o.getSet();
  83. if (set1.size() < set2.size())
  84. return -1;
  85. if (set1.size() > set2.size())
  86. return +1;
  87. let i1 : JavaIterator<Number> | null = set1.iterator();
  88. let i2 : JavaIterator<Number> | null = set2.iterator();
  89. if ((i1 === null) || (i2 === null))
  90. throw new NullPointerException()
  91. while (i1.hasNext()) {
  92. let cmp : number = JavaInteger.compare(i1.next().valueOf(), i2.next().valueOf());
  93. if (cmp !== 0)
  94. return cmp;
  95. }
  96. return 0;
  97. }
  98. isTranspiledInstanceOf(name : string): boolean {
  99. return ['java.lang.Comparable', 'de.nrw.schule.svws.core.kursblockung.satsolver.Clause'].includes(name);
  100. }
  101. }
  102. export function cast_de_nrw_schule_svws_core_kursblockung_satsolver_Clause(obj : unknown) : Clause {
  103. return obj as Clause;
  104. }