PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/code/ComposedDef.java

http://silkin.googlecode.com/
Java | 368 lines | 326 code | 20 blank | 22 comment | 77 complexity | 75f19b1c628f4866b089301e0747d207 MD5 | raw file
  1. /**
  2. *
  3. * @author Gary Morris, Northern Virginia Community College garymorris2245@verizon.net
  4. */
  5. import java.util.*;
  6. import java.io.*;
  7. public class ComposedDef extends Issue {
  8. KinTermDef ktd;
  9. ArrayList<Object> refutation, // A pair = {type, CB}
  10. languagesUsed, cbsUsed, posUncovered,
  11. negsCovered, auxiliaries, origBCBs, origXCBs;
  12. public ComposedDef() {
  13. } // 0-arg constructor for Serialization
  14. public String typeString() { return "Proposed Deinition"; }
  15. public ComposedDef(String localKinTerm, ArrayList<Object> questns, ArrayList<Object> octuple) {
  16. kinTerm = localKinTerm;
  17. questions = questns;
  18. ktd = (KinTermDef) octuple.get(0);
  19. languagesUsed = (ArrayList<Object>) octuple.get(1);
  20. cbsUsed = (ArrayList<Object>) octuple.get(2);
  21. posUncovered = (ArrayList<Object>) octuple.get(3);
  22. negsCovered = (ArrayList<Object>) octuple.get(4);
  23. auxiliaries = (ArrayList<Object>) octuple.get(5);
  24. origBCBs = (ArrayList<Object>) octuple.get(6);
  25. origXCBs = (ArrayList<Object>) octuple.get(7);
  26. } // end of normal constructor
  27. public boolean sameAs(KinTermDef realDef, int ignorable, Learned_DT lrnDT, DomainTheory sourceDT, String roundLearned)
  28. throws KSParsingErrorException, JavaSystemException, KSInternalErrorException, KSConstraintInconsistency,
  29. KSBadHornClauseException, KSNoChainOfRelations2Alter, ClassNotFoundException, FileNotFoundException,
  30. KinshipSystemException, ClassNotFoundException {
  31. boolean oldFlag = Context.simulation;
  32. ArrayList<Object> correctKinTypes = realDef.decodeString(realDef.eqcSigExact),
  33. coveredKinTypes = ktd.decodeString(ktd.eqcSigExact);
  34. String kinTm = ktd.kinTerm.substring(9);
  35. // The Kin Type coverage of this CompDef may not cover all the extended meanings of the
  36. // realDef. But it should not cover any extraneous KTs with dyads (probably noise).
  37. ArrayList<Object> extraKTs = LiteralAbstract2.setDifference(coveredKinTypes, correctKinTypes),
  38. suspectDyads = new ArrayList<Object>();
  39. TreeMap pos = (TreeMap) lrnDT.dyadsUndefined.get(kinTm);
  40. for (int i = 0; i < extraKTs.size(); i++) {
  41. String kType = (String) extraKTs.get(i);
  42. ArrayList<Object> dyads = (ArrayList<Object>) pos.get(kType);
  43. if (dyads != null) {
  44. suspectDyads.addAll(dyads);
  45. }
  46. }
  47. if (Library.errorCorrectionOn && !suspectDyads.isEmpty()) {
  48. Context.simulation = true;
  49. }
  50. for (int i = 0; i < suspectDyads.size(); i++) { // scan for noise dyads
  51. Dyad badDy = (Dyad) suspectDyads.get(i);
  52. if (!badDy.confirmed) {
  53. Dyad goodDy = lrnDT.wiseGuy.correction(badDy, lrnDT, sourceDT, roundLearned);
  54. if (goodDy == null) { // it must be a good one
  55. badDy.challenged = false;
  56. badDy.confirmed = true;
  57. }
  58. } // end of not-confirmed
  59. } // end of loop thru suspectDyads
  60. if (!suspectDyads.isEmpty()) {
  61. return false;
  62. }
  63. // OK. KinType coverage looks good.
  64. KinTermDef subRealDef = new KinTermDef(realDef); // A shallow copy
  65. subRealDef.eqcSigExact = ktd.eqcSigExact;
  66. subRealDef.eqcSigStruct = ktd.eqcSigStruct;
  67. Iterator iter = subRealDef.expandedDefs.iterator();
  68. ArrayList<Object> ktsInPOS = new ArrayList<Object>(pos.keySet());
  69. while (iter.hasNext()) {
  70. ClauseBody expCB = (ClauseBody) iter.next();
  71. if (!ktsInPOS.contains(expCB.pcString)) {
  72. iter.remove();
  73. }
  74. }
  75. // Now subRealDef has the portion of realDef applicable to the KinTypes seen so far
  76. ktd.headPred = new Predicate(ktd.kinTerm, new CulturalCategory());
  77. Variable arg0 = new Variable("Alter"),
  78. arg1 = new Variable("Ego");
  79. ktd.clauseHead = new Literal(ktd.headPred, arg0, arg1);
  80. Context.simulation = false;
  81. TreeMap neg = lrnDT.makeNEG(kinTm, pos);
  82. // The 3 tests - subRealDef cannot: 1) cover any of PosUncovered
  83. // 2) fail to cover any NegsCovered, or 3) cover any dyad in NEG that's not in NegsCovered
  84. if (threeTests(subRealDef, neg)) { // This CompDef must be functionally correct -- ACCEPT
  85. ktd.kinTerm = kinTm;
  86. Context.simulation = oldFlag;
  87. return true;
  88. }
  89. // Ooops. CompDef is not correct
  90. String alert = "\n\tWhile testing CompDef for " + kinTerm + " covering " + ktd.eqcSigExact;
  91. // alert += "\n\tCompDef = " + ktd;
  92. // alert += "\n\tSubRealDef = " + subRealDef;
  93. alert += "\n***************************************************************************";
  94. System.out.println(alert);
  95. if (Library.errorCorrectionOn) {
  96. scanMisfitsForNoise(pos, neg, ignorable, coveredKinTypes, lrnDT, sourceDT, roundLearned);
  97. }
  98. Context.simulation = oldFlag;
  99. return false;
  100. } // end of method sameAs()
  101. public void refute(ClauseBody cb, ArrayList<Object> egoBag, DomainTheory sourceDT, Learned_DT lrnDT)
  102. throws KSBadHornClauseException, KSInternalErrorException, KSConstraintInconsistency,
  103. ClassNotFoundException, KinshipSystemException, KSNoChainOfRelations2Alter,
  104. KSParsingErrorException, JavaSystemException, ClassNotFoundException {
  105. // Using the people in egoBag, create dyads from CB in refutation.
  106. // CB is a 'surplus CB' (beyond the correct definition). Find out if it has a defined
  107. // term in this language. If so, assign it to the dyads; if not, assign "no__term"
  108. if (lrnDT.dyadsDefined == null) {
  109. lrnDT.dyadsDefined = new DyadTMap();
  110. }
  111. boolean oldVal = Context.simulation;
  112. Context.simulation = false;
  113. String refuKinTerm = "no__term"; // default if this language has no term for this kin type
  114. boolean foundIt = false;
  115. Iterator ktIter = sourceDT.theory.entrySet().iterator();
  116. while (ktIter.hasNext() && !foundIt) { // search for KTD containing this kin type
  117. Map.Entry entry = (Map.Entry) ktIter.next();
  118. KinTermDef ktd = (KinTermDef) entry.getValue();
  119. ArrayList<Object> kinTypes = null;
  120. if (lrnDT.printableTerm(ktd)) {
  121. kinTypes = ktd.decodeString(ktd.eqcSigExact);
  122. }
  123. if (kinTypes != null && kinTypes.contains(cb.pcString)) {
  124. if (kinTerm.equals("aiya")) {
  125. Context.breakpoint();
  126. }
  127. for (int i = 0; i < ktd.expandedDefs.size(); i++) {
  128. ClauseBody claws = (ClauseBody) ktd.expandedDefs.get(i);
  129. if (cb.isEquivalent(claws)) {
  130. i = ktd.expandedDefs.size();
  131. foundIt = true;
  132. refuKinTerm = (String) entry.getKey();
  133. } // end of they're equivalent
  134. } // end of loop thru expDefs
  135. } // end of kin types contains pcString
  136. } // end of search for KTD containing this kin type
  137. Individual ego = (Individual) egoBag.remove(0);
  138. for (int j = 0; j < MainPane.NUMBER_OF_EGOS; j++) {
  139. lrnDT.ctxt.changeEgoTo(ego);
  140. egoBag.add(0, ego);
  141. Dyad dyad = lrnDT.makeExampleDyad(cb, lrnDT.ctxt, egoBag);
  142. dyad.kinTerm = refuKinTerm;
  143. lrnDT.dyadsDefined.dyAdd(dyad);
  144. ego = MainPane.topPane.randomEgo(lrnDT.ctxt, egoBag, ego.gender);
  145. } // end of loop thru egos
  146. Context.simulation = oldVal;
  147. } // end of method refute
  148. public boolean threeTests(KinTermDef subRealDef, TreeMap neg)
  149. throws KSBadHornClauseException, KSNoChainOfRelations2Alter, KSInternalErrorException,
  150. KSConstraintInconsistency, ClassNotFoundException {
  151. boolean acceptance = true;
  152. DomainTheory dt = new DomainTheory();
  153. // If subRealDef covers any of PosUncovered, REJECT
  154. for (int i = 0; i < subRealDef.expandedDefs.size(); i++) {
  155. ClauseBody expCB = (ClauseBody) subRealDef.expandedDefs.get(i);
  156. for (int j = 0; j < posUncovered.size(); j++) {
  157. Dyad dy = (Dyad) posUncovered.get(j);
  158. if (dy.pcString.equals(expCB.pcString) && dt.fit(expCB, dy)) {
  159. String alert = "\n*******************************REJECT**************************************";
  160. alert += "\n\tReal Def covers this dyad, which was in posUncovered:\n";
  161. alert += dy;
  162. alert += "\ncovering expCB was " + expCB;
  163. System.out.println(alert);
  164. return false;
  165. }
  166. } // end of j-loop
  167. } // end of i-loop
  168. // If subRealDef does not cover all of NegsCovered, REJECT
  169. ArrayList<Object> negsCover2 = new ArrayList<Object>(negsCovered);
  170. for (int i = 0; i < subRealDef.expandedDefs.size() && !negsCover2.isEmpty(); i++) {
  171. ClauseBody expCB = (ClauseBody) subRealDef.expandedDefs.get(i);
  172. Iterator iter = negsCover2.iterator();
  173. while (iter.hasNext()) {
  174. Dyad dy = (Dyad) iter.next();
  175. if (dy.pcString.equals(expCB.pcString) && dt.fit(expCB, dy)) {
  176. iter.remove(); // it's covered
  177. }
  178. } // end of loop thru negsCover2
  179. } // end of i-loop
  180. if (negsCover2.size() > 0) {
  181. String alert = "\n*******************************REJECT**************************************";
  182. alert += "\n\tReal Def fails to cover dyads in negsCovered:\n";
  183. alert += negsCover2;
  184. System.out.println(alert);
  185. return false;
  186. }
  187. // If subRealDef covers any dyad in NEG that is not in NegsCovered, REJECT
  188. for (int i = 0; i < subRealDef.expandedDefs.size(); i++) {
  189. ClauseBody expCB = (ClauseBody) subRealDef.expandedDefs.get(i);
  190. ArrayList<Object> negs = (ArrayList<Object>) neg.get(expCB.pcString);
  191. if (negs != null) {
  192. for (int j = 0; j < negs.size(); j++) {
  193. Dyad dy = (Dyad) negs.get(j);
  194. if (!negsCovered.contains(dy) && dt.fit(expCB, dy)) {
  195. String alert = "\n*******************************REJECT**************************************";
  196. alert += "\n\tReal Def covers a dyad in NEG that is not in negsCovered:\n";
  197. alert += dy;
  198. System.out.println(alert);
  199. return false;
  200. }
  201. } // end of j-loop
  202. } // end of negs != null
  203. } // end of i-loop
  204. return true;
  205. } // end of method threeTests
  206. public void scanMisfitsForNoise(TreeMap pos, TreeMap neg, int ignorable, ArrayList<Object> coveredKinTypes,
  207. Learned_DT lrnDT, DomainTheory sourceDT, String roundLearned) {
  208. ArrayList<Object> oddBalls = new ArrayList<Object>(negsCovered);
  209. oddBalls.addAll(posUncovered);
  210. int posSize = lrnDT.countLeaves(pos),
  211. limit = (int) Math.floor(posSize * ignorable / 100d);
  212. for (int i = 0; i < coveredKinTypes.size(); i++) {
  213. String kType = (String) coveredKinTypes.get(i);
  214. ArrayList<Object> negDys = (ArrayList<Object>) neg.get(kType);
  215. if (negDys != null && negDys.size() < limit + 4) {
  216. oddBalls.addAll(negDys);
  217. }
  218. } // end of loop thru all KTs this def tried to cover that had negs
  219. Context.simulation = true;
  220. for (int i = 0; i < oddBalls.size(); i++) { // scan for noise dyads
  221. Dyad badDy = (Dyad) oddBalls.get(i);
  222. if (!badDy.confirmed) {
  223. Dyad goodDy = lrnDT.wiseGuy.correction(badDy, lrnDT, sourceDT, roundLearned);
  224. if (goodDy != null) {
  225. int where = negsCovered.indexOf(badDy);
  226. if (where > -1) {
  227. negsCovered.remove(where);
  228. } else {
  229. where = posUncovered.indexOf(badDy);
  230. if (where > -1) {
  231. posUncovered.remove(where);
  232. }
  233. }
  234. } else { // it must be a good one
  235. badDy.challenged = false;
  236. badDy.confirmed = true;
  237. }
  238. } // end of not-confirmed
  239. } // end of loop thru oddBalls
  240. } // end of method scanMisfitsForNoise
  241. public boolean sameAsOLD(KinTermDef realDef, int ignorable, Learned_DT lrnDT,
  242. DomainTheory sourceDT, String roundLearned) throws KSParsingErrorException {
  243. ArrayList<Object> correctKinTypes = realDef.decodeString(realDef.eqcSigExact),
  244. coveredKinTypes = ktd.decodeString(ktd.eqcSigExact);
  245. if (coveredKinTypes.isEmpty() || correctKinTypes.isEmpty()) {
  246. System.out.println(); // FYI only. If either is empty, superSet will return false.
  247. }
  248. if (! lrnDT.isSupersetOf(correctKinTypes, coveredKinTypes)) {
  249. return false;
  250. }
  251. // The Kin Type coverage of this CompDef may not cover all the extended meanings of the
  252. // realDef. But it should not cover any extraneous ones.
  253. TreeMap allDyads = (TreeMap) lrnDT.dyadsUndefined.get(realDef.kinTerm);
  254. int posSize = lrnDT.countLeaves(allDyads),
  255. limit = (int) Math.floor(posSize * ignorable / 100d),
  256. misses = posUncovered.size() + negsCovered.size(), where;
  257. if (misses > 0 && Library.errorCorrectionOn) {
  258. ArrayList<Object> oddBalls = new ArrayList<Object>(negsCovered);
  259. oddBalls.addAll(posUncovered);
  260. boolean oldSimVal = Context.simulation;
  261. Context.simulation = true;
  262. for (int i = 0; i < oddBalls.size(); i++) { // scan for noise dyads
  263. Dyad badDy = (Dyad) oddBalls.get(i);
  264. if (!badDy.confirmed) {
  265. Dyad goodDy = lrnDT.wiseGuy.correction(badDy, lrnDT, sourceDT, roundLearned);
  266. if (goodDy != null) {
  267. misses--;
  268. where = negsCovered.indexOf(badDy);
  269. if (where > -1) {
  270. negsCovered.remove(where);
  271. }
  272. } else { // it must be a good one
  273. badDy.challenged = false;
  274. badDy.confirmed = true;
  275. }
  276. } // end of not-confirmed
  277. } // end of loop thru oddBalls
  278. Context.simulation = oldSimVal;
  279. } // end of misses > 0
  280. if (misses > limit) {
  281. return false;
  282. } else if (misses > 0) {
  283. System.out.println("Accepting composed definition of " + realDef.kinTerm + " as a working hypothesis.\n"
  284. + misses + " problem dyads will be studied further.");
  285. }
  286. ktd.kinTerm = ktd.kinTerm.substring(9);
  287. Predicate headPred = new Predicate(ktd.kinTerm, new CulturalCategory());
  288. Variable arg0 = new Variable("Alter"),
  289. arg1 = new Variable("Ego");
  290. ktd.clauseHead = new Literal(headPred, arg0, arg1);
  291. return true;
  292. } // end of method sameAsOLD()
  293. public String toThyString() {
  294. String s = "(";
  295. // insert guts of method here
  296. return s + ")";
  297. }
  298. public void toSILKFile(PrintWriter pw) {
  299. String spacer = "\t", dblSpacer = "\t\t";
  300. pw.println(spacer + "<proposed-def kinTerm=\"" + kinTerm + "\"> ");
  301. pw.println(ktd.toXML(dblSpacer));
  302. pw.println(questionsToSILKString(dblSpacer));
  303. pw.println(dblSpacer + "<pos-dyads-uncovered> \n");
  304. for (int i = 0; i < posUncovered.size(); i++) {
  305. Dyad dy = (Dyad) posUncovered.get(i);
  306. pw.println(dy.toXML(dblSpacer + spacer));
  307. }
  308. pw.println(dblSpacer + " </pos-dyads-uncovered>");
  309. pw.println(dblSpacer + "<neg-dyads-covered> ");
  310. for (int i = 0; i < negsCovered.size(); i++) {
  311. Dyad dy = (Dyad) negsCovered.get(i);
  312. pw.println(dy.toXML(dblSpacer + spacer));
  313. }
  314. pw.println(dblSpacer + " </neg-dyads-covered>\n");
  315. pw.println(spacer + " </proposed-def>");
  316. }
  317. public String toString() {
  318. int problems = posUncovered.size() + negsCovered.size();
  319. String rep = "ComposedDef for: " + kinTerm;
  320. String pl1 = (cbsUsed.size() > 1 ? "s" : ""),
  321. pl2 = (languagesUsed.size() > 1 ? "s" : ""),
  322. pl3 = (problems > 1 ? "s" : "");
  323. if (cbsUsed.size() > 0) {
  324. rep += "\n\tBased on a definition composed from " + cbsUsed.size() + " base clause" + pl1 + " in ";
  325. rep += languagesUsed.size() + " language" + pl2 + ".\n";
  326. } else {
  327. rep += "\n\tBased on induction.\n";
  328. }
  329. if (problems == 0) {
  330. rep += "\tA perfect fit.";
  331. } else {
  332. rep += "\n\tHas " + problems + " problem dyads (errors?):";
  333. for (int i = 0; i < posUncovered.size(); i++) {
  334. rep += "\n\t\t" + posUncovered.get(i);
  335. }
  336. for (int i = 0; i < negsCovered.size(); i++) {
  337. rep += "\n\t\t" + negsCovered.get(i);
  338. }
  339. }
  340. return rep + "\n";
  341. } // end of over-riding toString
  342. public void removeChallengedDyads() {
  343. return;
  344. }
  345. public void presentToUser() {
  346. System.out.println(toString()); // for now, until GUI completed.
  347. } // end of method presentToUser
  348. } // end of class ComposedDef