Skip to content

Commit 87fa7fb

Browse files
committed
Unnesting added
1 parent ad85024 commit 87fa7fb

File tree

16 files changed

+551
-228
lines changed

16 files changed

+551
-228
lines changed

PSOA2X/src/main/java/org/ruleml/psoa/psoa2x/common/PSOATranslatorUtil.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,42 @@ public static String randomConjectureId() {
180180

181181
public static String inverseTranslateTerm(String term)
182182
{
183-
// TODO: change to correct term translation
184-
if (term.charAt(0) != 'Q')
183+
// TODO: correct term translation
184+
185+
StringBuilder sb = new StringBuilder(term);
186+
187+
boolean inString = false;
188+
char lastChar = ' ';
189+
for (int i = 0; i < sb.length(); i++)
185190
{
186-
// if (term.startsWith("'") && term.endsWith("'"))
187-
// return term.substring(1, term.length() - 1);
188-
// else
189-
return term;
191+
char c = sb.charAt(i);
192+
if (c == '"')
193+
inString = !inString;
194+
else if(!inString)
195+
{
196+
if (c == ',')
197+
{
198+
sb.setCharAt(i, ' ');
199+
c = ' ';
200+
}
201+
else if (c == 'Q' && (lastChar == ' ' || lastChar == '('))
202+
sb.setCharAt(i, '?');
203+
}
204+
lastChar = c;
190205
}
191-
else
192-
return '?' + term.substring(1);
206+
207+
// System.out.println(term + "," + sb.toString());
208+
return sb.toString();
209+
210+
// if (term.charAt(0) != 'Q')
211+
// {
212+
//// if (term.startsWith("'") && term.endsWith("'"))
213+
//// return term.substring(1, term.length() - 1);
214+
//// else
215+
// return term;
216+
// }
217+
// else
218+
// return '?' + term.substring(1);
193219
}
194220

195221
public static StringBuilder builder() { return builder(""); }

PSOA2X/src/main/java/org/ruleml/psoa/psoa2x/psoa2prolog/PrologTranslator.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.antlr.runtime.RecognitionException;
44
import org.antlr.runtime.TokenStream;
55
import org.antlr.runtime.tree.CommonTreeNodeStream;
6+
import org.ruleml.psoa.FreshNameGenerator;
67
import org.ruleml.psoa.analyzer.KBInfoCollector;
78
import org.ruleml.psoa.normalizer.*;
89
import org.ruleml.psoa.psoa2x.common.ANTLRBasedTranslator;
@@ -26,6 +27,7 @@ protected TranslatorWalker createTranslatorWalker(
2627
@Override
2728
protected CommonTreeNodeStream preprocess(CommonTreeNodeStream treeNodeStream, boolean isQuery) throws RecognitionException
2829
{
30+
Unnester unnester;
2931
DiscriminativeObjectifier objectifier;
3032
Skolemizer skolemizer;
3133
SlotTupributor slotTupributor;
@@ -35,7 +37,13 @@ protected CommonTreeNodeStream preprocess(CommonTreeNodeStream treeNodeStream, b
3537
TokenStream tokens = treeNodeStream.getTokenStream();
3638

3739
if (isQuery)
38-
{
40+
{
41+
debugPrintln("Unnest Query");
42+
unnester = new Unnester(treeNodeStream);
43+
treeNodeStream = new CommonTreeNodeStream(unnester.query().getTree());
44+
treeNodeStream.setTokenStream(tokens);
45+
debugPrintTree(treeNodeStream);
46+
3947
if (m_config.dynamicObjectification)
4048
{
4149
debugPrintln("Rewrite query");
@@ -69,7 +77,15 @@ protected CommonTreeNodeStream preprocess(CommonTreeNodeStream treeNodeStream, b
6977
debugPrintTree(treeNodeStream);
7078
}
7179
else
72-
{
80+
{
81+
FreshNameGenerator.reset();
82+
83+
debugPrintln("Unnest KB");
84+
unnester = new Unnester(treeNodeStream);
85+
treeNodeStream = new CommonTreeNodeStream(unnester.document().getTree());
86+
treeNodeStream.setTokenStream(tokens);
87+
debugPrintTree(treeNodeStream);
88+
7389
if (m_config.dynamicObjectification)
7490
{
7591
m_KBInfo = new KBInfoCollector(treeNodeStream);

PSOATransRun/src/main/java/org/ruleml/psoa/psoatransrun/prolog/PSOATransRunProlog.java

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import gnu.getopt.LongOpt;
55

66
import java.io.*;
7+
import java.nio.file.Files;
78
import java.util.Map.Entry;
89
import java.util.*;
910

@@ -20,7 +21,7 @@
2021
public class PSOATransRunProlog
2122
{
2223
private static PrologEngine _engine;
23-
private static boolean _outputTrans = false, _getAllAnswers = false;
24+
private static boolean _outputTrans = false, _getAllAnswers = false, _showOrigKB = false;
2425
private static PSOA2PrologConfig _config = new PSOA2PrologConfig();
2526
// private static int _maxDepth = 0;
2627

@@ -38,10 +39,12 @@ public static void main(String[] args) throws TranslatorException, IOException
3839
// new LongOpt("termDep", LongOpt.REQUIRED_ARGUMENT, null, 'd'),
3940
// new LongOpt("timeout", LongOpt.REQUIRED_ARGUMENT, null,'t'),
4041
new LongOpt("repClass", LongOpt.NO_ARGUMENT, null, 'r'),
41-
new LongOpt("staticObj", LongOpt.NO_ARGUMENT, null, 's')
42+
new LongOpt("staticOnly", LongOpt.NO_ARGUMENT, null, 's'),
43+
new LongOpt("echoInput", LongOpt.NO_ARGUMENT, null, 'e')
4244
};
4345

44-
Getopt optionsParser = new Getopt("", args, "?i:po:x:q:ad:t:rs", opts);
46+
Getopt optionsParser = new Getopt("", args, "?i:po:x:q:ad:t:rse", opts);
47+
File kbFile = null;
4548
FileInputStream kbStream = null,
4649
queryStream = null;
4750
// boolean outputTrans = false;
@@ -60,7 +63,8 @@ public static void main(String[] args) throws TranslatorException, IOException
6063
arg = optionsParser.getOptarg();
6164
try
6265
{
63-
kbStream = new FileInputStream(arg);
66+
kbFile = new File(arg);
67+
kbStream = new FileInputStream(kbFile);
6468
}
6569
catch (FileNotFoundException e)
6670
{
@@ -114,6 +118,8 @@ public static void main(String[] args) throws TranslatorException, IOException
114118
case 's':
115119
_config.dynamicObjectification = false;
116120
break;
121+
case 'e':
122+
_showOrigKB = true;
117123
default:
118124
assert false;
119125
}
@@ -168,6 +174,7 @@ else if (OS.isFamilyWindows())
168174

169175
PrologTranslator translator = new PrologTranslator(_config);
170176
String transKB = translator.translateKB(kbStream);
177+
// System.out.println("Translation finished.");
171178

172179
if (transKBFile == null)
173180
transKBFile = tmpFile("tmp-", ".pl");
@@ -187,6 +194,22 @@ else if (OS.isFamilyWindows())
187194
writer.print(transKB);
188195
writer.close();
189196

197+
if (_showOrigKB)
198+
{
199+
println("Original KB:");
200+
BufferedReader reader = new BufferedReader(new FileReader(kbFile));
201+
do
202+
{
203+
String line = reader.readLine();
204+
if (line != null)
205+
println(line);
206+
else
207+
break;
208+
} while (true);
209+
reader.close();
210+
println();
211+
}
212+
190213
if (_outputTrans)
191214
{
192215
println("Translated KB:");
@@ -213,7 +236,7 @@ else if (OS.isFamilyWindows())
213236
{
214237
String transQuery = translator.translateQuery(queryStream);
215238

216-
answerQuery(transQuery, translator.getQueryVarMap());
239+
answerQuery(transQuery, translator);
217240
}
218241
else
219242
{
@@ -230,7 +253,7 @@ else if (OS.isFamilyWindows())
230253
try
231254
{
232255
String transQuery = translator.translateQuery(psoaQuery);
233-
answerQuery(transQuery, translator.getQueryVarMap());
256+
answerQuery(transQuery, translator);
234257
}
235258
catch (Exception e)
236259
{
@@ -246,7 +269,7 @@ else if (OS.isFamilyWindows())
246269
_engine.shutdown();
247270
}
248271

249-
private static void answerQuery(String transQuery, Map<String, String> queryVarMap)
272+
private static void answerQuery(String transQuery, PrologTranslator translator)
250273
{
251274
Set<Entry<String, String>> varMapEntries;
252275
TermModel result;
@@ -257,7 +280,7 @@ private static void answerQuery(String transQuery, Map<String, String> queryVarM
257280
println(transQuery + ".");
258281
println();
259282
}
260-
varMapEntries = queryVarMap.entrySet();
283+
varMapEntries = translator.getQueryVarMap().entrySet();
261284

262285
println("Answer(s):");
263286
if (varMapEntries.isEmpty())
@@ -287,7 +310,7 @@ else if (_getAllAnswers)
287310
{
288311
for (TermModel term : bindings.flatList())
289312
{
290-
ansBuilder.append(term);
313+
ansBuilder.append(translator.inverseTranslateTerm(term.toString()));
291314
if (separator)
292315
ansBuilder.append(",");
293316
separator = !separator;
@@ -333,7 +356,7 @@ else if (_getAllAnswers)
333356
boolean separator = false;
334357
for (TermModel term : m.flatList())
335358
{
336-
ansBuilder.append(term);
359+
ansBuilder.append(translator.inverseTranslateTerm(term.toString()));
337360
if (separator)
338361
ansBuilder.append(",");
339362
separator = !separator;
@@ -355,19 +378,20 @@ else if (input.equals(""))
355378

356379
private static void printUsage()
357380
{
358-
println("Usage: java -jar PSOATransRun.jar -i <kb> [-q <query>] [-p] [-o <translated KB output>] [-x <xsb folder>]");
381+
println("Usage: java -jar PSOATransRun.jar -i <kb> [-e] [-p] [-o <translated KB output>] [-q <query>] [-a] [-s] [-x <xsb folder>]");
359382
println("Options:");
360383
println(" -?,--help Print the help message");
361384
// println("");
362385
println(" -a,--allAns Retrieve all answers for each query at once");
363386
println(" -i,--input Input Knowledge Base (KB)");
387+
println(" -e,--echoInput Echo input KB to standard output");
364388
println(" -q,--query Query document for the KB. If the query document");
365389
println(" is not specified, the engine will read queries");
366390
println(" from the standard input.");
367-
println(" -p,--printTrans Print translated KB and queries to the standard output");
391+
println(" -p,--printTrans Print translated KB and queries to standard output");
368392
println(" -o,--outputTrans Save translated KB to the designated file");
369393
println(" -x,--xsbfolder Specifies XSB installation folder. The default path is ");
370394
println(" obtained from the environment variable XSB_DIR");
371-
println(" -s,--staticObj Apply static objectification instead of the default dymanic objectification");
395+
println(" -s,--staticOnly Apply static objectification only");
372396
} // End printUsage()
373397
}

ruleml-api/src/main/antlr3/org/ruleml/psoa/TreeWalkerTemplate.g

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ clause
5959

6060
head
6161
: atomic
62-
| ^(AND atomic+)
63-
| ^(EXISTS VAR_ID+ atomic)
62+
| ^(AND head+)
63+
| ^(EXISTS VAR_ID+ head)
6464
;
6565

6666
formula

ruleml-api/src/main/antlr3/org/ruleml/psoa/analyzer/KBInfoCollector.g

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ head
154154
$rule::isRuleHead = false;
155155
}
156156
: atomic
157-
| ^(AND atomic+)
158-
| ^(EXISTS VAR_ID+ atomic)
157+
| ^(AND head+)
158+
| ^(EXISTS VAR_ID+ head)
159159
;
160160

161161
formula

ruleml-api/src/main/antlr3/org/ruleml/psoa/normalizer/BodyExistentialRewriter.g

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,13 @@ scope
6464

6565
clause
6666
: ^(IMPLICATION head formula)
67-
| atomic
68-
| ^(AND atomic+)
67+
| head
6968
;
7069

7170
head
7271
: atomic
73-
| ^(AND atomic+)
74-
| ^(EXISTS VAR_ID+ atomic)
72+
| ^(AND head+)
73+
| ^(EXISTS VAR_ID+ head)
7574
;
7675

7776
formula

0 commit comments

Comments
 (0)