Skip to content

Commit 5fef25d

Browse files
author
git
committed
[Terry N.] more efficient use of single StringBuilder
1 parent f8fe804 commit 5fef25d

1 file changed

Lines changed: 14 additions & 15 deletions

File tree

src/java/com/articulate/sigma/PredVarInst.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public static void init() {
4949
/** ***************************************************************
5050
* There are two type conditions:
5151
* one type condition is extracted from domain expression;
52-
* second type condition is specifically define in the antecedent
53-
* of a rule with an instance or subclass expression;
52+
* second type condition is specifically defined in the antecedent
53+
* of a rule with an instance or subclass expression
5454
*
5555
* @param input formula
5656
* @param types type condition extracted from domain expression.
@@ -98,8 +98,6 @@ private static boolean isTypeExpansion(String rel) {
9898
* (?REL1 @ROW2))
9999
* (not
100100
* (?REL2 @ROW2)))
101-
*
102-
103101
*/
104102
private static Set<Formula> handleDouble1(KB kb) {
105103

@@ -108,7 +106,7 @@ private static Set<Formula> handleDouble1(KB kb) {
108106
Set<Formula> result = new HashSet<>();
109107
String arg1, arg2;
110108
int arity1, arity2;
111-
StringBuilder vars;
109+
StringBuilder vars = new StringBuilder();
112110
Formula newf, orig;
113111
for (String s : kb.kbCache.disjointRelations) {
114112
arg1 = s.substring(0,s.indexOf("\t"));
@@ -117,10 +115,10 @@ private static Set<Formula> handleDouble1(KB kb) {
117115
arity2 = kb.kbCache.getArity(arg2);
118116
if (arity1 != arity2)
119117
continue;
120-
vars = new StringBuilder();
118+
vars.setLength(0);
121119
for (int i = 1; i <= arity1; i++)
122120
vars.append(" ?ROW").append(i);
123-
newf = new Formula("(=> (\" + arg1 + vars + \") (not (\" + arg2 + vars + \")))");
121+
newf = new Formula("(=> (" + arg1 + vars + ") (not (" + arg2 + vars + ")))");
124122
newf.sourceFile = "Merge.kif";
125123
orig = kb.formulaMap.get(origForm);
126124
if (orig != null) {
@@ -149,6 +147,9 @@ private static Set<Formula> handleDouble2(KB kb) {
149147
"(instance ?REL2 Predicate) (?REL1 @ROW)) (?REL2 @ROW))";
150148
if (debug) System.out.println("PredVarInst.handleDouble2(): " + origForm);
151149
Set<Formula> result = new HashSet<>();
150+
int arity1, arity2;
151+
StringBuilder vars = new StringBuilder();
152+
Formula newf, orig;
152153
for (String r1 : kb.kbCache.relations) {
153154
if (debug) System.out.println("handleDouble2(): relation: " + r1);
154155
if (debug) System.out.println("handleDouble2(): subrelation children: " + kb.kbCache.children.get("subrelation"));
@@ -158,20 +159,17 @@ private static Set<Formula> handleDouble2(KB kb) {
158159
continue;
159160
Set<String> children = kb.kbCache.children.get("subrelation").get(r1);
160161
if (children != null) {
161-
int arity1, arity2;
162-
StringBuilder vars;
163-
Formula newf;
164162
for (String r2 : children) {
165163
arity1 = kb.kbCache.getArity(r1);
166164
arity2 = kb.kbCache.getArity(r2);
167165
if (arity1 != arity2)
168166
continue;
169-
vars = new StringBuilder();
167+
vars.setLength(0);
170168
for (int i = 1; i <= arity1; i++)
171169
vars.append(" ?ROW").append(i);
172170
newf = new Formula("(=> (" + r2 + vars + ") (" + r1 + vars + "))");
173171
newf.sourceFile = "Merge.kif";
174-
Formula orig = kb.formulaMap.get(origForm);
172+
orig = kb.formulaMap.get(origForm);
175173
if (orig != null) {
176174
newf.startLine = orig.startLine;
177175
newf.derivation.operator = "predvar";
@@ -252,6 +250,7 @@ public static Set<Formula> instantiatePredVars(Formula input, KB kb) {
252250
Integer arityInteger;
253251
boolean ok;
254252
Formula f, f2;
253+
int arity;
255254
for (String var : varTypes.keySet()) {
256255
if (predVarArity == null || var == null || predVarArity.get(var) == null)
257256
System.out.println("instantiatePredVars(): pred var arity null for: " + var +
@@ -277,7 +276,7 @@ public static Set<Formula> instantiatePredVars(Formula input, KB kb) {
277276
}
278277
if (predVarArity == null || var == null) System.out.println("instantiatePredVars(): pred var arity null for: " + var);
279278
arityInteger = predVarArity.get(var);
280-
int arity = 0;
279+
arity = 0;
281280
if (arityInteger != null)
282281
arity = arityInteger;
283282
if (kb.kbCache.valences.get(rel).equals(arity) || arity == 0) { // 0 arity means "any"
@@ -481,8 +480,8 @@ protected static Map<String, Set<String>> findPredVarTypes(Formula f, KB kb) {
481480
Set<String> predVars = gatherPredVars(kb,f);
482481
if (debug) System.out.println("findPredVarTypes(): predVars: " + predVars);
483482
FormulaPreprocessor fp = new FormulaPreprocessor();
484-
//HashMap<String,HashSet<String>> typeMap = fp.computeVariableTypes(f, kb); // <- this skips explicit types
485-
//HashMap<String,HashSet<String>> typeMap = fp.findTypeRestrictions(f, kb); // <- won't get instance relations
483+
//Map<String,Set<String>> typeMap = fp.computeVariableTypes(f, kb); // <- this skips explicit types
484+
//Map<String,Set<String>> typeMap = fp.findTypeRestrictions(f, kb); // <- won't get instance relations
486485
Map<String,Set<String>> typeMap = fp.findAllTypeRestrictions(f, kb);
487486
if (debug) System.out.println("findPredVarTypes(): typeMap: " + typeMap);
488487
Map<String,Set<String>> result = new HashMap<>();

0 commit comments

Comments
 (0)