PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/code/DT_Abstract2.java

http://silkin.googlecode.com/
Java | 1048 lines | 905 code | 66 blank | 77 comment | 356 complexity | e2104e604c3ff2b9b85f5dc3698166ba MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. import java.util.* ;
  2. import java.text.* ;
  3. import java.io.* ;
  4. /** This class continues the definition of a Domain Theory in Prolog syntax. In this system, a Domain Theory is a grammar
  5. defining a Kinship System. This is the level at which 90% of the action takes place in Active Learning.
  6. <p>
  7. Because so many methods are defined at the Domain Theory level, the code files are broken into 4 classes:
  8. DT_Abstract1, DT_Abstract2, & DomainTheory, plus Learned_DT.
  9. @author Gary Morris, Northern Virginia Community College garymorris2245@verizon.net
  10. */
  11. public abstract class DT_Abstract2 extends DT_Abstract1 {
  12. public static ArrayList<Object> neuterPreds = loadNeuters(),
  13. malePreds = loadMales(),
  14. femalePreds = loadFemales(),
  15. bothSexPreds = loadBothSex();
  16. public static ArrayList<Object> loadNeuters() {
  17. ArrayList<Object> lst = new ArrayList<Object>();
  18. lst.add("parent"); lst.add("child");
  19. lst.add("spouse"); lst.add("sibling");
  20. return lst;
  21. }
  22. public static ArrayList<Object> loadMales() {
  23. ArrayList<Object> lst = new ArrayList<Object>();
  24. lst.add("father"); lst.add("son");
  25. lst.add("husband"); lst.add("brother");
  26. return lst;
  27. }
  28. public static ArrayList<Object> loadFemales() {
  29. ArrayList<Object> lst = new ArrayList<Object>();
  30. lst.add("mother"); lst.add("daughter");
  31. lst.add("wife"); lst.add("sister");
  32. return lst;
  33. }
  34. public static ArrayList<Object> loadBothSex() {
  35. ArrayList<Object> lst = new ArrayList<Object>();
  36. lst.add("parents"); lst.add("children");
  37. lst.add("spice"); lst.add("siblings");
  38. return lst;
  39. }
  40. public static ArrayList<Object> kinTypeSymbols = loadKTSymbols(),
  41. kinTypePreds = loadKTPreds(),
  42. pluralKTPreds = loadPluralKTPreds(),
  43. pluralKTSymbols = loadPluralKTSymbols();
  44. public static ArrayList<Object> loadKTSymbols() {
  45. ArrayList<Object> lst = new ArrayList<Object>();
  46. lst.add("So"); lst.add("Da"); lst.add("Stso"); lst.add("Stda"); lst.add("C");
  47. lst.add("Fa"); lst.add("Mo"); lst.add("Stfa"); lst.add("Stmo"); lst.add("P");
  48. lst.add("Hu"); lst.add("Wi"); lst.add("Bro"); lst.add("Sis"); lst.add("Sp");
  49. lst.add("Hbro"); lst.add("Hsis"); lst.add("Stbro"); lst.add("Stsis"); lst.add("Sib"); lst.add("*");
  50. return lst;
  51. } // end of method loadKTSymbols
  52. public static ArrayList<Object> loadKTPreds() {
  53. ArrayList<Object> lst = new ArrayList<Object>();
  54. lst.add("son"); lst.add("daughter"); lst.add("step_son"); lst.add("step_daughter"); lst.add("child");
  55. lst.add("father"); lst.add("mother"); lst.add("step_father"); lst.add("step_mother"); lst.add("parent");
  56. lst.add("husband"); lst.add("wife"); lst.add("brother"); lst.add("sister"); lst.add("spouse");
  57. lst.add("half_brother"); lst.add("half_sister"); lst.add("step_brother"); lst.add("step_sister");
  58. lst.add("sibling"); lst.add("*");
  59. return lst;
  60. } // end of method loadKTPreds
  61. public static ArrayList<Object> loadPluralKTPreds() {
  62. ArrayList<Object> lst = new ArrayList<Object>();
  63. lst.add("children"); lst.add("siblings"); lst.add("parents");
  64. return lst;
  65. } // end of method loadPluralKTPreds
  66. public static ArrayList<Object> loadPluralKTSymbols() {
  67. ArrayList<Object> lst = new ArrayList<Object>(),
  68. symbols = new ArrayList<Object>();
  69. symbols.add("So"); symbols.add("Da");
  70. lst.add(symbols);
  71. symbols = new ArrayList<Object>();
  72. symbols.add("Bro"); symbols.add("Sis");
  73. lst.add(symbols);
  74. symbols = new ArrayList<Object>();
  75. symbols.add("Fa"); symbols.add("Mo");
  76. lst.add(symbols);
  77. return lst;
  78. } // end of method loadPluralKTSymbols
  79. public static String symbolToPred(String symbol) throws KSInternalErrorException {
  80. int where = kinTypeSymbols.indexOf(symbol);
  81. if (where == -1)
  82. throw new KSInternalErrorException("Illegal kinType symbol '" + symbol + "' in symbolToPred.");
  83. return (String)kinTypePreds.get(where);
  84. }
  85. public static String predToSymbol(String pred) throws KSInternalErrorException {
  86. int where = kinTypePreds.indexOf(pred);
  87. if (where == -1)
  88. throw new KSInternalErrorException("Illegal kinType '" + pred + "' in predToSymbol.");
  89. return (String)kinTypeSymbols.get(where);
  90. }
  91. public PredCategory determineCategory(String symbol) {
  92. // Identify the 19 Primitive Predicates as special
  93. // Also consider userDefinedProperties and Math predicates as primitive predicates
  94. if ((symbol.equals("father")) || (symbol.equals("mother")) || (symbol.equals("son"))
  95. || (symbol.equals("daughter")) || (symbol.equals("husband")) || (symbol.equals("wife"))
  96. || (symbol.equals("male")) || (symbol.equals("female")) || (symbol.equals("elder"))
  97. || (symbol.equals("younger")) || (symbol.equals("dead")) || (symbol.equals("divorced"))
  98. || (symbol.equals("not")) || (symbol.equals("equal")) || (symbol.equals("parent"))
  99. || (symbol.equals("child")) || (symbol.equals("spouse"))
  100. || (symbol.equals("allowCreation")) || (symbol.equals("gender"))
  101. || ((userDefinedProperties != null) && userDefinedProperties.containsKey(symbol)))
  102. return new PrimitiveCategory();
  103. // Treat the Math predicates as a special kind of Primitive
  104. else if ((symbol.equals("lessThan")) || (symbol.equals("greaterThan")) ||
  105. (symbol.equals("lessOrEql")) || (symbol.equals("greaterOrEql")) ||
  106. (symbol.equals("contains")))
  107. return new MathCategory();
  108. else return new CulturalCategory();
  109. } // end of method determineCategory
  110. public boolean allTermsDefined(String umbTerm, ArrayList<Object> subTerms) {
  111. String baseTerm = umbTerm; // baseTerm = the term or it's synonym base term
  112. if (synonyms != null && synonyms.get(umbTerm) != null) baseTerm = (String)synonyms.get(umbTerm);
  113. if (! theory.containsKey(baseTerm)) return false;
  114. for (int i=0; i < subTerms.size(); i++) {
  115. baseTerm = (String)subTerms.get(i);
  116. if (synonyms != null && synonyms.get(baseTerm) != null)
  117. baseTerm = (String)synonyms.get(baseTerm);
  118. if (! theory.containsKey(baseTerm)) return false;
  119. }
  120. return true;
  121. } // end of method allTermsDefined
  122. public void generateIndexes() throws KSBadHornClauseException, KSInternalErrorException, KSParsingErrorException,
  123. FileNotFoundException, KSConstraintInconsistency, ClassNotFoundException, JavaSystemException {
  124. if (theory == null) {
  125. return;
  126. }
  127. addrTerms = addressTerms;
  128. current = (DomainTheory) this;
  129. System.out.println("Making Indexes for " + languageName);
  130. // Loop thru theory and make entries in Library.predEncodings & Library.predDecodings
  131. Iterator iter = theory.keySet().iterator(); // each key is a kinTerm
  132. TreeMap predCodes = new TreeMap();
  133. Library.predEncodings.put(languageName, predCodes);
  134. int seqNmbr = 0,
  135. udpSize = (userDefinedProperties == null ? 0 : userDefinedProperties.size());
  136. String[] deCodes = new String[theory.size() - standardMacroTree.size() + udpSize];
  137. Library.predDecodings.put(languageName, deCodes);
  138. while (iter.hasNext()) {
  139. String kterm = (String) iter.next();
  140. if (!standardMacroTree.containsKey(kterm)) { // don't encode std macros
  141. predCodes.put(kterm, new Library.KTD_Coder(seqNmbr, ""));
  142. deCodes[seqNmbr++] = kterm;
  143. }
  144. }
  145. if (userDefinedProperties != null) {
  146. iter = userDefinedProperties.values().iterator();
  147. while (iter.hasNext()) {
  148. UserDefinedProperty udp = (UserDefinedProperty) iter.next();
  149. predCodes.put(udp.starName, new Library.KTD_Coder(seqNmbr, "*"));
  150. deCodes[seqNmbr++] = udp.starName;
  151. }
  152. }
  153. ClauseBody.priorPred = "";
  154. ClauseBody.seqTotal = 0;
  155. ClauseBody.dupTotal = 0;
  156. // Terms deemed 'non_terms' in the domain theory (or those with non_term Flags) are not indexed
  157. Library.ClauseIndex cbIndex = Library.cbIndex,
  158. baseCBIndex = Library.baseCBIndex;
  159. Library.ClauseCounts cbCounts = Library.cbCounts;
  160. ArrayList<Object> egoBag = maleAndFemaleCreatedHeThem();
  161. iter = theory.values().iterator(); // reload for second loop
  162. while (iter.hasNext()) {
  163. KinTermDef ktd = (KinTermDef) iter.next();
  164. if (printableBaseTerm(ktd)) { // generate examples for all base terms (incl synonyms)
  165. ktd.assureExamplesGenerated(egoBag);
  166. if (printableTerm(ktd)) { // only index printable terms
  167. for (int i = 0; i < ktd.expandedDefs.size(); i++) {
  168. ClauseBody cb = (ClauseBody) ktd.expandedDefs.get(i);
  169. cbIndex.addToIndex(new Library.CB_Ptr(cb));
  170. cbCounts.addCB(cb);
  171. cb.postToBaseSigString();
  172. }
  173. Library.addToKTSigTrees(ktd);
  174. Library.KTD_Coder coder = (Library.KTD_Coder) predCodes.get(ktd.kinTerm);
  175. coder.exactSigStr = ktd.eqcSigExact;
  176. coder.baseCB_SigStrings = new ArrayList<Object>();
  177. for (int i = 0; i < ktd.definitions.size(); i++) {
  178. ClauseBody cb = (ClauseBody) ktd.definitions.get(i);
  179. cb.sortSigString();
  180. Library.BaseCB_Ptr ptr = new Library.BaseCB_Ptr(cb, ktd.expandedDefs);
  181. baseCBIndex.addToIndex(ptr);
  182. coder.baseCB_SigStrings.add(cb.pcString);
  183. } // end of loop thru multiple base clauses
  184. } // end of index-it
  185. }
  186. } // end of while hasNext()
  187. if (Library.parseClauseCounterOn) {
  188. String pad = "";
  189. if (++ClauseBody.seq < 10) {
  190. pad = " ";
  191. } else if (ClauseBody.seq < 100) {
  192. pad = " ";
  193. }
  194. System.out.println(ClauseBody.priorPred + ": " + pad + ClauseBody.seq + " - " + ClauseBody.dups
  195. + " = " + (ClauseBody.seq - ClauseBody.dups));
  196. ClauseBody.seqTotal += ClauseBody.seq;
  197. System.out.println("\nTotal clauses for " + languageName + " is " + ClauseBody.seqTotal
  198. + " - " + ClauseBody.dupTotal + " = " + (ClauseBody.seqTotal - ClauseBody.dupTotal) + "\n\n");
  199. }
  200. if (Context.current != Library.contextUnderConstruction) {
  201. Context.current.resetTo(0, 0); // get rid of all hypothetical people created
  202. }
  203. } // end of method generateIndexes
  204. /** This method is called during analysis of the Library's Domain Theories. It assembles the lists
  205. of Head Clause Variable Constraints and other features for this Domain theory.
  206. @returns a FeatureVectorObj with all the characteristics of this DomainTheory
  207. */
  208. public FeatureVectorObj computeFeatureVector(ArrayList<Object> egoList) throws KSInternalErrorException, KSBadHornClauseException,
  209. KSInternalErrorException, KSConstraintInconsistency, ClassNotFoundException {
  210. // hcvc = a list of Head Clause Variable Constraints. ivc = Internal Variable Constraints
  211. ArrayList<Object> hcvc = new ArrayList<Object>(), ivc = new ArrayList<Object>();
  212. TreeSet exactSet = new TreeSet(), structSet = new TreeSet();
  213. Iterator ktdIter = theory.values().iterator();
  214. KinTermDef ktd;
  215. ArrayList<Object> egoBag = maleAndFemaleCreatedHeThem();
  216. addrTerms = addressTerms;
  217. current = (DomainTheory) this;
  218. ClauseBody.priorPred = ""; // Needed for Clause Counts
  219. ClauseBody.seqTotal = 0;
  220. ClauseBody.dupTotal = 0;
  221. int totalCBs = 0, totalKTDs = 0;
  222. boolean[] boolArray = {false, false, false}; // elements 0,6,7
  223. int[] params = {0, 0, 0}; // RecipCnt, MultiGenCnt, TotalLat
  224. while (ktdIter.hasNext()) {
  225. ktd = (KinTermDef) ktdIter.next();
  226. if (printableTerm(ktd)) { // generate examples for all terms except non-terms
  227. ktd.assureExamplesGenerated(egoBag);
  228. if (printableTerm(ktd)) {
  229. if (ktd.eqcSigExact == null || ktd.eqcSigStruct == null) {
  230. ktd.makeSigStrings();
  231. }
  232. ktd.analyzeFeatures(hcvc, ivc, params, boolArray);
  233. exactSet.add(ktd.eqcSigExact);
  234. structSet.add(ktd.eqcSigStruct);
  235. totalCBs += ktd.expandedDefs.size();
  236. totalKTDs++;
  237. } // end of printableTerm
  238. } // end of printableBaseTerm
  239. } // end of loop through the KinTermDefs in this Domaintheory
  240. // We're now sure all defs have been expanded and the level, etc. fields computed.
  241. // Print out the Clause Counts, etc.
  242. if (Library.parseClauseCounterOn) {
  243. String pad = "";
  244. if (++ClauseBody.seq < 10) {
  245. pad = " ";
  246. } else if (ClauseBody.seq < 100) {
  247. pad = " ";
  248. }
  249. System.out.println(ClauseBody.priorPred + ": " + pad + ClauseBody.seq + " - " + ClauseBody.dups
  250. + " = " + (ClauseBody.seq - ClauseBody.dups));
  251. ClauseBody.seqTotal += ClauseBody.seq;
  252. System.out.println("\nTotal clauses for " + languageName + " is " + ClauseBody.seqTotal
  253. + " - " + ClauseBody.dupTotal + " = " + (ClauseBody.seqTotal - ClauseBody.dupTotal) + "\n\n");
  254. }
  255. // Disabled - now done in overlapping term analysis
  256. // if (Context.current != Library.contextUnderConstruction)
  257. // Context.current.resetTo(0, 0); // get rid of all hypothetical people created
  258. // Now compute the FeatureVector (the original objective, remember?)
  259. FeatureVectorObj fv = new FeatureVectorObj();
  260. fv.languageName = languageName;
  261. fv.addressTerms = addressTerms;
  262. fv.genSkewing = boolArray[0];
  263. if (totalKTDs > 0) {
  264. fv.percentRecip = 100f * params[0] / totalKTDs;
  265. fv.percentMultiGen = 100f * params[1] / totalKTDs;
  266. fv.avgLateralCount = 1.0f * params[2] / totalCBs;
  267. } else {
  268. fv.percentRecip = 0f;
  269. fv.percentMultiGen = 0f;
  270. fv.avgLateralCount = 0f;
  271. }
  272. fv.ivcList = ivc;
  273. fv.hcvcList = hcvc;
  274. fv.stepTerms = boolArray[1];
  275. fv.udps = boolArray[2];
  276. fv.exactSigSet = exactSet;
  277. fv.structSigSet = structSet;
  278. egoList.addAll(egoBag);
  279. return fv;
  280. } // end of method computeFeatureVector
  281. public void validateSynonymsAndUmbrellas() throws KSConstraintInconsistency {
  282. // Check the synonyms and umbrella terms for this DT. (Could be learned or read from disk.)
  283. // Any circular references are illegal -- throw an exception.
  284. // Else, put synonyms and umbrellas in canonical order (all chains of reference resolved).
  285. if (synonyms != null && synonyms.size() > 0) {
  286. // Ck for circular references in synonyms
  287. // syntax of synonyms is: synTerm -> baseTerm
  288. boolean changes = true;
  289. while (changes) {
  290. Iterator iter = synonyms.entrySet().iterator();
  291. changes = false;
  292. while (iter.hasNext()) {
  293. Map.Entry entry = (Map.Entry)iter.next();
  294. String syn = (String)entry.getKey();
  295. String baseTerm = (String)entry.getValue();
  296. KinTermDef ktDef = (KinTermDef)theory.get(baseTerm);
  297. if (ktDef == null && (Library.contextUnderConstruction == null
  298. || Library.contextUnderConstruction != ctxt))
  299. throw new KSConstraintInconsistency("Synonym declaration makes '"
  300. + baseTerm + "' a base term, but that is not a defined term.");
  301. if (synonyms.containsKey(baseTerm) && ((String)synonyms.get(baseTerm)).equals(syn))
  302. throw new KSConstraintInconsistency("Circular synonym declarations found for '"
  303. + baseTerm + "' & '" + syn + "'");
  304. if (! printableBaseTerm(ktDef))
  305. throw new KSConstraintInconsistency("Synonym declaration makes '"
  306. + baseTerm + "' a base term, but that is not a printable term.");
  307. if (synonyms.containsKey(baseTerm)) { // non-circular; a chain reference
  308. entry.setValue(synonyms.get(baseTerm));
  309. changes = true;
  310. }
  311. } // end of loop thru all entries
  312. } // end of while(changes)
  313. } // end of synonyms != null
  314. if (overlaps != null && overlaps.size() > 0) {
  315. // Ck for undefined terms in overlaps
  316. // syntax of overlaps is: term => ArrayList<Object> of overlapping terms
  317. Iterator iter = overlaps.entrySet().iterator();
  318. while (iter.hasNext()) {
  319. Map.Entry entry = (Map.Entry)iter.next();
  320. String term1 = (String)entry.getKey();
  321. ArrayList<Object> oLapTerms = (ArrayList<Object>)entry.getValue();
  322. KinTermDef ktDef = (KinTermDef)theory.get(term1);
  323. if (ktDef == null && (Library.contextUnderConstruction == null
  324. || Library.contextUnderConstruction != ctxt))
  325. throw new KSConstraintInconsistency("Overlapping Term declaration mentions '"
  326. + term1 + "' but that is not a defined term.");
  327. if (! printableBaseTerm(ktDef))
  328. throw new KSConstraintInconsistency("Overlapping Term declaration mentions '"
  329. + term1 + "' but that is not a printable term.");
  330. for (int i=0; i < oLapTerms.size(); i++) {
  331. String term2 = (String)oLapTerms.get(i);
  332. ktDef = (KinTermDef)theory.get(term2);
  333. if (ktDef == null && (Library.contextUnderConstruction == null
  334. || Library.contextUnderConstruction != ctxt))
  335. throw new KSConstraintInconsistency("Overlapping Term declaration mentions '"
  336. + term2 + "' but that is not a defined term.");
  337. if (! printableBaseTerm(ktDef))
  338. throw new KSConstraintInconsistency("Overlapping Term declaration mentions '"
  339. + term2 + "' but that is not a printable term.");
  340. } // end of loop thru oLapTerms
  341. } // end of loop thru all entries
  342. } // end of overlaps != null
  343. if (umbrellas != null && umbrellas.size() > 0) {
  344. // Ck for circular references in umbrellas
  345. Iterator iter = umbrellas.entrySet().iterator();
  346. while (iter.hasNext()) {
  347. Map.Entry entry = (Map.Entry)iter.next();
  348. String umbTerm = (String)entry.getKey();
  349. ArrayList<Object> subTerms = (ArrayList<Object>)entry.getValue();
  350. if (searchFor(umbTerm, subTerms))
  351. throw new KSConstraintInconsistency("Circular umbrella declarations found for '"
  352. + umbTerm + "'");
  353. } // end of loop thru all entries in umbrellas
  354. // Ck for chain references in umbrellas
  355. boolean changes = true;
  356. while (changes) {
  357. changes = false;
  358. iter = umbrellas.entrySet().iterator();
  359. while (iter.hasNext()) {
  360. Map.Entry entry = (Map.Entry)iter.next();
  361. String umbTerm = (String)entry.getKey();
  362. ArrayList<Object> subTerms = (ArrayList<Object>)entry.getValue();
  363. for (int i=0; i < subTerms.size(); i++) {
  364. String subTerm = (String)subTerms.get(i);
  365. if (umbrellas.containsKey(subTerm)) {
  366. ArrayList<Object> subSubTerms = (ArrayList<Object>)umbrellas.get(subTerm);
  367. for (int j=0; j < subSubTerms.size(); j++) {
  368. String subSubTerm = (String)subSubTerms.get(j);
  369. if (! subTerms.contains(subSubTerm)) {
  370. changes = true;
  371. subTerms.add(subSubTerm);
  372. } // end of found-new-subSubTerm
  373. } // end of loop thru subSubTerms
  374. } // end of subTerm-is-an-umbrella-also
  375. } // end of loop thru subTerms
  376. } // end of loop thru all entries in umbrellas
  377. } // end of while-changes
  378. } // end of umbrellas != null
  379. } // end of method validateSynonymsAndUmbrellas
  380. public boolean searchFor(String term, ArrayList<Object> subTerms) {
  381. if (subTerms.contains(term)) return true;
  382. for (int i=0; i < subTerms.size(); i++) {
  383. String subTerm = (String)subTerms.get(i);
  384. ArrayList<Object> subSubTerms = (ArrayList<Object>)umbrellas.get(subTerm);
  385. if (subSubTerms != null && searchFor(term, subSubTerms))
  386. return true;
  387. } // end of loop thru subTerms
  388. return false;
  389. }
  390. public void resolveSynonymsInDyads() {
  391. // If any dyads on dyadsUndefined are for known synonyms, change them to
  392. // dyads for the base term shown on the synonyms TMap. Prior to (or during) learning.
  393. if (synonyms == null || synonyms.isEmpty()) return;
  394. if (dyadsUndefined == null || dyadsUndefined.isEmpty()) return;
  395. TreeMap newbies = new TreeMap();
  396. Iterator synIter = synonyms.entrySet().iterator();
  397. while (synIter.hasNext()) { // synonyms is synTerm => baseTerm
  398. Map.Entry entry = (Map.Entry)synIter.next();
  399. String synTerm = (String)entry.getKey();
  400. String baseTerm = (String)entry.getValue();
  401. if (dyadsUndefined.containsKey(synTerm)) { // DyadTMap is kinTerm => PC_Str => ArrayList<Object> of dyads
  402. TreeMap synTree = (TreeMap)dyadsUndefined.remove(synTerm);
  403. TreeMap baseTree = (TreeMap)newbies.get(baseTerm);
  404. if (baseTree == null) newbies.put(baseTerm, synTree);
  405. else newbies.put(baseTerm, mergeTrees(baseTree, synTree));
  406. } // end of found dyads for a synonym
  407. } // end of loop thru synonyms
  408. if (newbies.size() > 0) {
  409. Iterator dyadIter = newbies.entrySet().iterator();
  410. while (dyadIter.hasNext()) {
  411. Map.Entry entry = (Map.Entry)dyadIter.next();
  412. String baseTerm = (String)entry.getKey(); // the base term
  413. TreeMap termTM = (TreeMap)entry.getValue();
  414. if (dyadsUndefined.containsKey(baseTerm)) {
  415. TreeMap baseTermTM = (TreeMap)dyadsUndefined.get(baseTerm);
  416. dyadsUndefined.put(baseTerm, mergeTrees(baseTermTM, termTM));
  417. }else dyadsUndefined.put(baseTerm, termTM);
  418. } // end of loop thru newbies
  419. } // end of new item processing
  420. } // end of method resolveSynonymsInDyads
  421. public void analyzeSynonymsAndUmbrellas() {
  422. // Analyze all the IssuesForUser generated as part of learning round. When we find a proposedSynonym,
  423. // if this propSyn covers 2 terms that both have propDefs (or accepted definitions) on IssuesForUser
  424. // then kill the propSyn.
  425. Iterator termIter = issuesForUser.entrySet().iterator();
  426. while (termIter.hasNext()) {
  427. Map.Entry entry = (Map.Entry)termIter.next();
  428. String kinTerm = (String)entry.getKey();
  429. ArrayList<Object> issueList = (ArrayList<Object>)entry.getValue();
  430. Iterator issuIter = issueList.iterator();
  431. while (issuIter.hasNext()) {
  432. Issue issue = (Issue)issuIter.next();
  433. if (issue instanceof SynonymCandidate) {
  434. SynonymCandidate syn = (SynonymCandidate)issue;
  435. int slash = syn.pairOfTerms.indexOf("/");
  436. String first = syn.pairOfTerms.substring(0, slash),
  437. second = syn.pairOfTerms.substring(slash +1);
  438. if (distinctDefsProposed(first, second)) issuIter.remove();
  439. } // end of processing for SynCandidates
  440. else if (issue instanceof UmbrellaCandidate) {
  441. UmbrellaCandidate umbCand = (UmbrellaCandidate)issue;
  442. ArrayList<Object> subTerms = new ArrayList<Object>(umbCand.subTerms.keySet());
  443. if (allTermsDefined(umbCand.kinTerm, subTerms)) issuIter.remove();
  444. } // end of processing for UmbrellaCandidates
  445. else if (issue instanceof OverlapCandidate) {
  446. OverlapCandidate olapCand = (OverlapCandidate)issue;
  447. if (distinctDefsProposed(olapCand.kinTerm, olapCand.otherTerm)) issuIter.remove();
  448. } // end of processing for OverlapCandidates
  449. } // end of loop thru issues for this kinTerm
  450. } // end of loop through kinTerms
  451. } // end of method analyzeSynonymsAndUmbrellas
  452. public boolean distinctDefsProposed(String first, String second) {
  453. KinTermDef firstDef = null, secondDef = null;
  454. ArrayList<Issue> proposals = issuesForUser.get(first);
  455. if (proposals == null) return false;
  456. for (int i=0; i < proposals.size(); i++) {
  457. Issue issue = (Issue)proposals.get(i);
  458. if (issue instanceof ProposedDef) {
  459. firstDef = ((ProposedDef)issue).ktd;
  460. i = proposals.size();
  461. } // end of it's a PropDef
  462. } // end of loop thru first proposals
  463. if (firstDef == null) return false;
  464. proposals = issuesForUser.get(second);
  465. if (proposals == null) return false;
  466. for (int i=0; i < proposals.size(); i++) {
  467. Issue issue = (Issue)proposals.get(i);
  468. if (issue instanceof ProposedDef) {
  469. secondDef = ((ProposedDef)issue).ktd;
  470. i = proposals.size();
  471. } // end of it's a PropDef
  472. } // end of loop thru second proposals
  473. if (secondDef == null) return false;
  474. if (firstDef == secondDef) return false;
  475. else return true;
  476. } // end of method distinctDefsProposed
  477. public void findOverlappingTerms(ArrayList<Object> egoList) throws KSBadHornClauseException, KSInternalErrorException,
  478. KSConstraintInconsistency, KSNoChainOfRelations2Alter, ClassNotFoundException {
  479. // This method may only be called after examples have been generated.
  480. // First, if possible do Fill_in_Names for all egos. Then loop
  481. // thru all Individuals in this context, looking for multiple kinTerms
  482. // on some Alter for the same Ego. This is an overlapping pair of kinTerms.
  483. // If neither term is a non-term or an umbrella or synonym for the other, record Overlapping Terms.
  484. // If these terms are not already registered as Overlapping Terms, generate report.
  485. for (int i=0; i < egoList.size(); i++) fillInNames((Individual)egoList.get(i));
  486. TreeMap oLaps = new TreeMap();
  487. Iterator iter;
  488. for (int i=0; i < ctxt.individualCensus.size(); i++) {
  489. Individual ind = (Individual)ctxt.individualCensus.get(i);
  490. TreeMap sorter = new TreeMap();
  491. if (ind.nameHistory.size() > 1) // single item can't lead to overlap
  492. for (int j=0; j < ind.nameHistory.size(); j++) {
  493. String histItem = (String)ind.nameHistory.get(j),
  494. term, egoNum = "-99";
  495. int colon = histItem.indexOf(":"),
  496. leftParen = histItem.indexOf("("),
  497. rightParen = histItem.indexOf(")");
  498. if (leftParen == -1) term = histItem;
  499. else {
  500. term = histItem.substring(0,leftParen) + histItem.substring(colon);
  501. egoNum = histItem.substring(leftParen +1, rightParen);
  502. }
  503. String rootTerm = term.substring(0, term.indexOf(":"));
  504. if (! nonTerm(rootTerm)) { // non-terms do overlap -- that's OK.
  505. if (synonyms != null && synonyms.get(rootTerm) != null)
  506. term = synonyms.get(rootTerm) + term.substring(term.indexOf(":"));
  507. if (sorter.get(egoNum) == null) sorter.put(egoNum, new ArrayList<Object>());
  508. ArrayList<Object> allTerms = (ArrayList<Object>)sorter.get(egoNum);
  509. if (! allTerms.contains(term)) allTerms.add(term);
  510. }
  511. } // end of loop thru Name History
  512. iter = sorter.values().iterator();
  513. while (iter.hasNext()) {
  514. ArrayList<Object> allTerms = (ArrayList<Object>)iter.next();
  515. if (allTerms.size() > 1) { // 2 or more terms for same kin type
  516. for (int k=0; k < allTerms.size() -1; k++) {
  517. String term1 = (String)allTerms.get(k);
  518. for (int k2=1; k2 < allTerms.size(); k2++) {
  519. String term2 = (String)allTerms.get(k2);
  520. if (term1.compareTo(term2) > 0) { // term1 always earlier
  521. String hold = term1;
  522. term1 = term2;
  523. term2 = hold;
  524. }
  525. String term1root = term1.substring(0, term1.indexOf(":")),
  526. term2root = term2.substring(0, term2.indexOf(":")),
  527. rootKey = term1root + "/" + term2root,
  528. termKey = term1 + "/" + term2;
  529. // if (rootKey.equals("bucuru/jibuwaimo")) Context.breakpoint();
  530. String[] pair = {term1root, term2root};
  531. if (! umbrellaRel(pair) && ! synonymRel(pair)
  532. && ! term1root.equals(term2root)) { // overlapping terms!
  533. if (oLaps.get(rootKey) == null) oLaps.put(rootKey, new ArrayList<Object>());
  534. ArrayList<Object> details = (ArrayList<Object>)oLaps.get(rootKey);
  535. if (! details.contains(termKey)) details.add(termKey);
  536. } // end of non-umbrella & non-synonym
  537. } // end of k2-loop
  538. } // end of k-loop
  539. } // end of allTerms > 1
  540. } // end of loop thru sorter
  541. } // end of loop thru individuals in the population
  542. iter = oLaps.keySet().iterator();
  543. TreeMap suggestedNewOverlaps = (overlaps == null ? new TreeMap() : new TreeMap(overlaps));
  544. while(iter.hasNext()) {
  545. String termPair = (String)iter.next();
  546. int slash = termPair.indexOf("/");
  547. // if (termPair.equals("bucuru/jibuwaimo")) Context.breakpoint();
  548. String trm1 = termPair.substring(0, slash),
  549. trm2 = termPair.substring(slash +1);
  550. if (overlaps != null && overlaps.get(trm1) != null
  551. && ((ArrayList<Object>)overlaps.get(trm1)).contains(trm2))
  552. iter.remove();
  553. else { // found-a-new-one
  554. if (suggestedNewOverlaps.get(trm1) == null) suggestedNewOverlaps.put(trm1, new ArrayList<Object>());
  555. ArrayList<Object> oLapTerms = (ArrayList<Object>)suggestedNewOverlaps.get(trm1);
  556. oLapTerms.add(trm2);
  557. } // end of found-a-new-one
  558. }
  559. if (oLaps.size() > 0) {
  560. System.out.println("\n*** Un-Reported Overlapping Terms for " + languageName);
  561. iter = oLaps.entrySet().iterator();
  562. while (iter.hasNext()) {
  563. Map.Entry entry = (Map.Entry)iter.next();
  564. String termPair = (String)entry.getKey();
  565. ArrayList<Object> details = (ArrayList<Object>)entry.getValue();
  566. System.out.println("\t" + termPair + " (" + details.size() + ") " + details);
  567. }
  568. System.out.print("***\n\nSuggested New Overlaps declaration:\n(overlaps, ");
  569. String holder = "", smpl = "[]()";
  570. char leftSq = smpl.charAt(0), rightSq = smpl.charAt(1),
  571. leftParen = smpl.charAt(2), rightParen = smpl.charAt(3);
  572. iter = suggestedNewOverlaps.entrySet().iterator();
  573. while (iter.hasNext()) {
  574. Map.Entry entry = (Map.Entry)iter.next();
  575. holder += "(" + entry.getKey() + ", " + entry.getValue() + ")";
  576. if (iter.hasNext()) holder += ", ";
  577. }
  578. holder = holder.replace(leftSq, leftParen);
  579. holder = holder.replace(rightSq, rightParen);
  580. System.out.println(holder + ")\n");
  581. }
  582. if (ctxt != Library.contextUnderConstruction)
  583. ctxt.resetTo(0, 0); // get rid of all hypothetical people created
  584. } // end of method findOverlappingTerms()
  585. boolean umbrellaRel(String[] pair) {
  586. if (umbrellas == null || umbrellas.isEmpty()) return false;
  587. if (umbrellas.get(pair[0]) != null
  588. && ((ArrayList<Object>)umbrellas.get(pair[0])).contains(pair[1])) return true;
  589. if (umbrellas.get(pair[1]) != null
  590. && ((ArrayList<Object>)umbrellas.get(pair[1])).contains(pair[0])) return true;
  591. return false;
  592. } // end of method umbrellaRel
  593. boolean synonymRel(String[] pair) {
  594. if (synonyms == null || synonyms.isEmpty()) return false;
  595. if (synonyms.get(pair[0]) != null
  596. && ((String)synonyms.get(pair[0])).equals(pair[1])) return true;
  597. if (synonyms.get(pair[1]) != null
  598. && ((String)synonyms.get(pair[1])).equals(pair[0])) return true;
  599. return false;
  600. } // end of method synonymRel
  601. public void findHiddenNeuterEgos() throws KSInternalErrorException {
  602. // This method may only be called after examples have been generated.
  603. // Scan all KTDs, looking for a specific problem: CBs that specify Ego's spouse
  604. // with no gender requirement for Ego (and therefor spouse). Such "neuter" definitions
  605. // allow examples to be generated of just the (default) male Ego and fail to generate
  606. // with a female Ego. These are bad definitions, and should be pointed out to User.
  607. TreeMap sorter = new TreeMap();
  608. ArrayList<Object> problems = new ArrayList<Object>();
  609. Iterator iter = theory.values().iterator();
  610. boolean needTitle = true;
  611. while (iter.hasNext()) {
  612. KinTermDef ktd = (KinTermDef)iter.next();
  613. for (int i=0; i < ktd.expandedDefs.size(); i++) {
  614. ClauseBody cb = (ClauseBody)ktd.expandedDefs.get(i);
  615. if (cb.pcString != null && cb.pcString.length() > 3 // Begins with Hu/Wi & has 2+ links
  616. && (cb.pcString.indexOf("Hu") == 0 || cb.pcString.indexOf("Wi") == 0)) {
  617. String restOfIt = cb.pcString.substring(2);
  618. if (sorter.get(restOfIt) == null) sorter.put(restOfIt, new ArrayList<Object>());
  619. ArrayList<Object> record = new ArrayList<Object>(2);
  620. record.add(cb.pcString.substring(0,2));
  621. record.add(cb);
  622. ((ArrayList<Object>)sorter.get(restOfIt)).add(record);
  623. } // end of found-a-Hu/Wi string
  624. } // end of loop thru CBs
  625. Iterator sortIter = sorter.values().iterator();
  626. while (sortIter.hasNext()) {
  627. ArrayList<Object> records = (ArrayList<Object>)sortIter.next();
  628. boolean huSeen = false, wiSeen = false;
  629. for (int j=0; j < records.size(); j++) {
  630. String spStr = (String)((ArrayList<Object>)records.get(j)).get(0);
  631. if (spStr.equals("Hu")) huSeen = true;
  632. else if (spStr.equals("Wi")) wiSeen = true;
  633. } // end of loop thru records
  634. if (! (huSeen && wiSeen)) { // only 1 gender for Ego in examples
  635. // Check for a gender spec on Ego
  636. boolean specFound = false;
  637. ClauseBody cb = (ClauseBody)((ArrayList<Object>)records.get(0)).get(1);
  638. for (int k=0; k < cb.body.size(); k++) {
  639. Literal lit = (Literal)cb.body.get(k);
  640. if (egoGenderSpecified(lit)) {
  641. specFound = true;
  642. k = cb.body.size();
  643. } // end of found-ego-gender-spec
  644. } // end of loop thru body
  645. if (! specFound) problems.add(cb);
  646. } // end of only-1-gender-for-Ego in examples
  647. } // end of loop thru sorter
  648. sorter.clear();
  649. if (problems.size() > 0) {
  650. if (needTitle) {
  651. System.out.println("\n**** Potential Problem Neuter Egos:\n");
  652. needTitle = false;
  653. }
  654. System.out.println("\n\t\tFor " + ktd.kinTerm);
  655. ArrayList<Object> setIntersection = new ArrayList<Object>(),
  656. starterSet = ((ClauseBody)problems.get(0)).expansionPath;
  657. for (int i=0; i < starterSet.size(); i++)
  658. if (! setIntersection.contains(starterSet.get(i))) setIntersection.add(starterSet.get(i));
  659. // will intersect with itself first time, then with others
  660. int sampleSize = Math.min(3, problems.size());
  661. for (int i=0; i < sampleSize; i++) {
  662. ClauseBody cb = (ClauseBody)problems.get(i);
  663. System.out.println(cb.toThyString() + "\n");
  664. Integer seqNum = new Integer(cb.seqNmbr);
  665. sorter.put(seqNum, seqNum);
  666. setIntersection = listSetIntersectionOfStrings(setIntersection, cb.expansionPath);
  667. } // end of loop thru problems
  668. for (int i=sampleSize; i < problems.size(); i++) {
  669. ClauseBody cb = (ClauseBody)problems.get(i);
  670. Integer seqNum = new Integer(cb.seqNmbr);
  671. sorter.put(seqNum, seqNum);
  672. setIntersection = listSetIntersectionOfStrings(setIntersection, cb.expansionPath);
  673. }
  674. sortIter = sorter.values().iterator();
  675. if (sortIter.hasNext()) System.out.print("etc. Includes Expanded Clause numbers: ");
  676. while (sortIter.hasNext()) {
  677. System.out.print(sortIter.next());
  678. if (sortIter.hasNext()) System.out.print(", ");
  679. else System.out.print("\n");
  680. }
  681. if (sampleSize < problems.size() && ! setIntersection.isEmpty())
  682. System.out.println("Common elements: " + setIntersection);
  683. System.out.println();
  684. problems.clear();
  685. sorter.clear();
  686. } // end of if-problems-exist
  687. } // end of loop thru all KTDs
  688. } // end of findHiddenNeuterEgos
  689. public boolean egoGenderSpecified(Literal lit) {
  690. // true if this lit requires a specific gender for Ego
  691. String[] genderPreds = {"male", "female", "father", "mother", "son", "daughter", "husband", "wife"};
  692. boolean isGenderPred = false;
  693. for (int i=0; i < 8; i++) if (lit.predicate.name.equals(genderPreds[i])) isGenderPred = true;
  694. if (isGenderPred
  695. && ((Argument)lit.args.get(0)).argName != null
  696. && ((String)((Argument)lit.args.get(0)).argName).equals("Ego"))
  697. return true;
  698. else if ((lit.predicate.name.equals("gender") || lit.predicate.name.equals("husband")
  699. || lit.predicate.name.equals("wife"))
  700. && ((Argument)lit.args.get(1)).argName != null
  701. && ((String)((Argument)lit.args.get(1)).argName).equals("Ego"))
  702. return true;
  703. else return false;
  704. } // end of method egoGenderSpecified
  705. public ArrayList<Object> getExact(String exactStr, Library.ClauseIndex cbIndex, String langName)
  706. throws KSInternalErrorException {
  707. // Find the list of EQCs in cbIndex where the key is exactStr.
  708. // Union all their member lists (of CB_Ptrs) & filter out any with the same langName. Then return it.
  709. ArrayList<Object> result = new ArrayList<Object>();
  710. ArrayList<Object> eqcs = (ArrayList<Object>)cbIndex.tMap.get(exactStr);
  711. for (int j=0; j < eqcs.size(); j++)
  712. result.addAll(((Library.CB_EQC)eqcs.get(j)).members);
  713. Iterator cbpIter = result.iterator();
  714. while (cbpIter.hasNext()) {
  715. Library.CB_Ptr cbp = (Library.CB_Ptr)cbpIter.next();
  716. if (cbp.languageName.equals(langName)) cbpIter.remove();
  717. }
  718. return result;
  719. } // end of method getExact
  720. public ArrayList<Object> getStruct(String structStr, Library.ClauseIndex cbIndex, String langName)
  721. throws KSInternalErrorException {
  722. // Find every list of EQCs in cbIndex where the key is structurally equivalent to structStr.
  723. // Union all their member lists (of CB_Ptrs) & filter out any with the same langName. Then return it.
  724. ArrayList<Object> result = new ArrayList<Object>(),
  725. exactStrings = expandStruct(structStr);
  726. for (int i=0; i < exactStrings.size(); i++) {
  727. ArrayList<Object> eqcs = (ArrayList<Object>)cbIndex.tMap.get(exactStrings.get(i));
  728. if (eqcs != null) for (int j=0; j < eqcs.size(); j++)
  729. result.addAll(((Library.CB_EQC)eqcs.get(j)).members);
  730. }
  731. Iterator cbpIter = result.iterator();
  732. while (cbpIter.hasNext()) {
  733. Library.CB_Ptr cbp = (Library.CB_Ptr)cbpIter.next();
  734. if (cbp.languageName.equals(langName)) cbpIter.remove();
  735. }
  736. return result;
  737. } // end of method getStruct
  738. public ArrayList<Object> expandStruct(String structStr) throws KSInternalErrorException {
  739. // Pop the first PC_Str symbol off structStr, expand it, and paste
  740. // it on the front of all sub-expansions.
  741. ArrayList<Object> result = new ArrayList<Object>();
  742. if (structStr.length() == 0) {
  743. result.add("");
  744. return result;
  745. }
  746. int start = 0, end;
  747. char ch = structStr.charAt(start), asterisk = "*".charAt(0);
  748. if (! JavaLex.check(ch, "CapLtr") && ch != asterisk)
  749. throw new KSInternalErrorException("Illegal String given to expandStruct: " + structStr);
  750. for (end=1; end < structStr.length(); end++) {
  751. ch = structStr.charAt(end);
  752. if (JavaLex.check(ch, "CapLtr")) break;
  753. }
  754. String symb = structStr.substring(start, end),
  755. remainder = structStr.substring(end);
  756. ArrayList<Object> subList = expandStruct(remainder);
  757. if (symb.equals("P")) {
  758. for (int i=0; i < subList.size(); i++) {
  759. result.add("Fa" + subList.get(i));
  760. result.add("Mo" + subList.get(i));
  761. }
  762. }else if (symb.equals("C")) {
  763. for (int i=0; i < subList.size(); i++) {
  764. result.add("So" + subList.get(i));
  765. result.add("Da" + subList.get(i));
  766. }
  767. }else if (symb.equals("Sp")) {
  768. for (int i=0; i < subList.size(); i++) {
  769. result.add("Hu" + subList.get(i));
  770. result.add("Wi" + subList.get(i));
  771. }
  772. }else if (symb.equals("Sib")) {
  773. for (int i=0; i < subList.size(); i++) {
  774. result.add("Bro" + subList.get(i));
  775. result.add("Sis" + subList.get(i));
  776. }
  777. }else if (symb.equals("Stsib")) {
  778. for (int i=0; i < subList.size(); i++) {
  779. result.add("Stbro" + subList.get(i));
  780. result.add("Stsis" + subList.get(i));
  781. }
  782. }else if (symb.equals("Hsib")) {
  783. for (int i=0; i < subList.size(); i++) {
  784. result.add("Hbro" + subList.get(i));
  785. result.add("Hsis" + subList.get(i));
  786. }
  787. }else if (symb.equals("Stp")) {
  788. for (int i=0; i < subList.size(); i++) {
  789. result.add("Stfa" + subList.get(i));
  790. result.add("Stmo" + subList.get(i));
  791. }
  792. }else if (symb.equals("Stc")) {
  793. for (int i=0; i < subList.size(); i++) {
  794. result.add("Stson" + subList.get(i));
  795. result.add("Stda" + subList.get(i));
  796. }
  797. }else if (symb.equals("*")) {
  798. for (int i=0; i < subList.size(); i++)
  799. result.add("*" + subList.get(i));
  800. }
  801. return result;
  802. } // end of method expandStruct
  803. public TreeSet setUnionOfCB_Ptrs(TreeSet structSet, ArrayList<Object> union) {
  804. // A normal set union, except that 2 CB_Ptrs are considered
  805. // equal if they match on languageName and kinTerm (we ignore cbSeqNmbr).
  806. TreeSet result = new TreeSet(structSet);
  807. result.addAll(union); // may create duplicates on lang/kinTerm
  808. if (result.size() < 2) return result;
  809. Iterator iter = result.iterator();
  810. Library.CB_Ptr a = (Library.CB_Ptr)iter.next(), b;
  811. while(iter.hasNext()) {
  812. b = (Library.CB_Ptr)iter.next();
  813. if (a.languageName.equals(b.languageName) && a.kinTerm.equals(b.kinTerm)) iter.remove();
  814. else a = b;
  815. }
  816. return result;
  817. } // end of method setUnionOfCB_Ptrs
  818. public TreeSet setIntersectionOfCB_Ptrs(TreeSet set1, ArrayList<Object> list2) {
  819. // A normal set intersection, except that 2 CB_Ptrs are considered
  820. // equal if they match on languageName and kinTerm (we ignore cbSeqNmbr).
  821. TreeSet result = new TreeSet(), set2 = new TreeSet(list2); // set1 & set2 both now in 'natural order'
  822. if (set1.isEmpty() || list2.isEmpty()) return result;
  823. Iterator iter1 = set1.iterator(), iter2 = set2.iterator();
  824. Library.CB_Ptr cb1 = (Library.CB_Ptr)iter1.next(), prior = null,
  825. cb2 = (Library.CB_Ptr)iter2.next();
  826. try {
  827. while(cb1 != null && cb2 != null) {
  828. int comp = cb1.compareTo(cb2);
  829. if (comp == 0) { // equal on languageName and kinTerm; BINGO
  830. if (cb1.compareTo(prior) != 0) result.add(cb1);
  831. prior = cb1;
  832. cb1 = (iter1.hasNext() ? (Library.CB_Ptr)iter1.next() : null);
  833. cb2 = (iter2.hasNext() ? (Library.CB_Ptr)iter2.next() : null);
  834. }else if (comp < 0) { // cb1 is prior
  835. cb1 = (iter1.hasNext() ? (Library.CB_Ptr)iter1.next() : null);
  836. }else cb2 = (iter2.hasNext() ? (Library.CB_Ptr)iter2.next() : null); // cb2 is prior
  837. } // endof while-loop
  838. }catch(Exception exc) { Context.breakpoint(); }
  839. return result;
  840. } // end of method setIntersectionOfCB_Ptrs
  841. public ArrayList<Object> listSetIntersectionOfStrings(ArrayList<Object> list1, ArrayList<Object> list2) {
  842. ArrayList<Object> intersection = new ArrayList<Object>();
  843. if (list1.isEmpty() || list2.isEmpty()) return intersection;
  844. for (int i=0; i < list1.size(); i++) {
  845. String item = (String)list1.get(i);
  846. if (list2.contains(item)) intersection.add(item);
  847. } // end of loop thru list1
  848. return intersection;
  849. } // end of method listSetIntersectionOfStrings
  850. public void matchDyads() throws JavaSystemException, KSBadHornClauseException, KSNoChainOfRelations2Alter,
  851. KSInternalErrorException, KSConstraintInconsistency, ClassNotFoundException, FileNotFoundException {
  852. if (Library.cbIndex == null) {
  853. BufferedReader file = new BufferedReader(new FileReader(Library.libraryDirectory + "ClauseIndex"));
  854. Library.cbIndex = new Library.ClauseIndex(file);
  855. }
  856. if ((dyadsUndefined == null) || (Library.cbIndex.tMap.isEmpty())) {
  857. return;
  858. }
  859. String fileName = Library.libraryDirectory + languageName, image, kinTerm, langTerm = "";
  860. TreeMap cbiLvlTM, setsOfCBsThatMatch;
  861. TreeSet langTermMatches, setOfCBs, intersectionOfLangTerms;
  862. ArrayList<Object> dyadList, cbiPCList, listOfDyadMatches;
  863. Map.Entry ktmEntry, lvlEntry;
  864. Iterator ktmIter = dyadsUndefined.entrySet().iterator(), lvlIter, valIter;
  865. Integer lvlInt;
  866. int dyadCtr = 0, nmbrInvolved, totalCBs;
  867. Dyad dad;
  868. ClauseBody cb;
  869. boolean hdr1 = true;
  870. try {
  871. PrintWriter outFile1 = new PrintWriter(new BufferedWriter(new FileWriter(fileName + "_1.match")));
  872. while (ktmIter.hasNext()) {
  873. ktmEntry = (Map.Entry) ktmIter.next();
  874. kinTerm = (String) ktmEntry.getKey();
  875. dyadCtr = 0;
  876. listOfDyadMatches = new ArrayList<Object>();
  877. setsOfCBsThatMatch = new TreeMap();
  878. lvlIter = ((Collection) ((TreeMap) ktmEntry.getValue()).entrySet()).iterator();
  879. while (lvlIter.hasNext()) {
  880. lvlEntry = (Map.Entry) lvlIter.next();
  881. lvlInt = (Integer) lvlEntry.getKey();
  882. dyadList = (ArrayList<Object>) lvlEntry.getValue();
  883. cbiLvlTM = (TreeMap) Library.cbIndex.tMap.get(lvlInt);
  884. for (int i = 0; i < dyadList.size(); i++) {
  885. dad = (Dyad) dyadList.get(i);
  886. dyadCtr++;
  887. langTermMatches = new TreeSet();
  888. cbiPCList = (ArrayList<Object>) cbiLvlTM.get(new Integer(dad.level));
  889. valIter = cbiPCList.iterator();
  890. while (valIter.hasNext()) {
  891. // Whew! Thought I'd never get here!
  892. cb = (ClauseBody) valIter.next();
  893. if (!kinTerm.equals(cb.ktd.kinTerm)) { // no sense matching yourself
  894. if (compare2(dad, cb, kinTerm)) {
  895. langTerm = cb.ktd.domTh.languageName + ":" + cb.ktd.kinTerm;
  896. langTermMatches.add(langTerm);
  897. if (setsOfCBsThatMatch.get(langTerm) == null) {
  898. setsOfCBsThatMatch.put(langTerm, new TreeSet(cb));
  899. }
  900. setOfCBs = (TreeSet) setsOfCBsThatMatch.get(langTerm);
  901. setOfCBs.add(cb);
  902. } // end of matched-on-comparison#2
  903. } // end of if not a match on itself
  904. } // end of loop thru clausebodies in cbIndex for this level & pcCount
  905. listOfDyadMatches.add(langTermMatches);
  906. } // end of loop thru dyadsUndefined for this kinTerm & level
  907. } // end of loop thru dyadLevels
  908. // Now that all dyadsUndefined and matching cbs for this kinTerm have been found, analyze the results
  909. intersectionOfLangTerms = setIntersection(listOfDyadMatches);
  910. valIter = intersectionOfLangTerms.iterator();
  911. while (valIter.hasNext()) {
  912. // langTerm matched every dyad for this kinTerm
  913. langTerm = (String) valIter.next();
  914. if (hdr1) {
  915. hdr1 = false;
  916. outFile1.println("kinTerm\t# of dyadsUndefined\tLang:Term\t# CBs\tmatching CBs");
  917. } // end of writing-file-column-headers
  918. outFile1.print(kinTerm + "\t " + dyadCtr + "\t" + langTerm);
  919. // Now check to see if all the clauses of LangTerm were involved in the match, or just some
  920. setOfCBs = (TreeSet) setsOfCBsThatMatch.get(langTerm);
  921. nmbrInvolved = setOfCBs.size();
  922. if (nmbrInvolved == 0) {
  923. totalCBs = -1;
  924. } else {
  925. cb = (ClauseBody) setOfCBs.first();
  926. totalCBs = cb.ktd.expandedDefs.size();
  927. } // end of else-more-than-0
  928. outFile1.println("\t" + totalCBs + "\t" + nmbrInvolved);
  929. } // end of loop thru set intersection
  930. } // end of loop thru all kinTerms in dyadsUndefined
  931. outFile1.flush();
  932. outFile1.close();
  933. } catch (IOException e) {
  934. throw new JavaSystemException(".match File Creation failed:\n" + e);
  935. }
  936. } // end of method matchDyads
  937. public boolean compare2(Dyad dad, ClauseBody cb, String kinTerm) throws KSBadHornClauseException, KSNoChainOfRelations2Alter,
  938. KSInternalErrorException, KSConstraintInconsistency, ClassNotFoundException {
  939. // Strategy: Start fillInNames_bool from ego (with alter as goallPerson) and see if
  940. // it can reach alter using the definition contained in cb. If it does, and PC-String is identical,
  941. // return true. TRACK PC-String BY CAPTURING P/C/S/* AS FIND_ALL EXECUTES.
  942. int size = cb.body.size();
  943. ConstraintObj constraints = new ConstraintObj();
  944. ArrayList<Object> genderStuff = new ArrayList<Object>(), starStuff = new ArrayList<Object>(), starBindings = new ArrayList<Object>();
  945. for (int i=0; i < size; i++)
  946. // if any literal specifies an ego gender != ego.gender, we fail.
  947. if (! (((Literal)cb.body.get(i)).constraintCheck(dad.ego.gender, constraints, genderStuff, starStuff)))
  948. return false;
  949. // constraintCheck, by side-effect, builds all constraints
  950. TreeMap bindings = new TreeMap(), badBindings = new TreeMap();
  951. bindings.put("Ego", dad.ego);
  952. if (! LiteralAbstract1.finalConstraintCheck(dad.ego.gender, bindings, constraints, cb.body, genderStuff, starStuff))
  953. return false;
  954. // finalConstraintCheck does post-processing & a final conflict-check.
  955. ArrayList<Object> bodyCopy = new ArrayList<Object>(cb.body), path = new ArrayList<Object>(), starStuffCopy = new ArrayList<Object>(starStuff);
  956. Literal next = null;
  957. while ((bodyCopy.size() > 0) && (next == null))
  958. next = cb.pop(bodyCopy, starStuffCopy, bindings, kinTerm); // next = first non-constraint literal in body
  959. if (next == null) return false;
  960. else { // start the process with next. First, find any star-props for Ego
  961. Variable egoVar = null;
  962. for (int i=0; i < next.args.size(); i++)
  963. if (((

Large files files are truncated, but you can click here to view the full file