标签:
https://github.com/bajdcc/jProlog===========================
jProlog is a language describing simple logical problems, using exhaustion to find solutions. Developed by bajdcc.
jProlog 是一个简易的逻辑问题解答程序,主要运用穷举法搜寻解空间,因而时间复杂度受变量数量和长度影响,因而有待优化。
QUICK: See ANTLR Grammar File(.g4) at Prolog.g4
PS. Supporting Chinese.
COLLECTION_NAME{} = {val1,val2,...};
or
集合名称{} = {值1,值2,...};
COLLECTION_NAME, alias Type
example:
所有人{} = {"埃夫里","布莱克","克朗","戴维斯","其他人"};
Digit{} = {0..9};
COLLECTION_NAME var1,var2,...;
or
集合名称 变量名1,变量名2,...;
example:
时间 死亡时刻;
Digit A,B,C;
Note: System will searching in the SPACE of O(n), where n is Elements of a Type or Math Function card(type);
COLLECTION_NAME var1[],var2[],...;
or
集合名称 变量名1[],变量名2[],...;
example:
时间 序列[];
Digit A[],B[],C[];
Note: System will searching in the SPACE of O(n!), where n is card(type);
COLLECTION_NAME var1(Type1,Type2,...),var2(Type1,Type2,...),...;
or
集合名称 变量名1(参数类型1,参数类型2,...),变量名2(参数类型1,参数类型2,...),...;
example:
时间 在寓所(所有人);
Digit Score(Everyone);
Note: System will searching in the SPACE of O(pow(n,t)), where n is card(type) and t is The Product of card(type1), card(type2), ...;
usage:
设置(多解);
设置(定时,1000);
设置(版权,"Copyright");
or (DIY)
settings1(...);
settings2(...);
Note: If you want to DIY them, modify RtSettingFunc.java. Edit attribute ‘desc‘ of SettingType. Extend method ‘dealWithSettingId‘ if you want to add new features. The setting function will apply settings in RtSettings.java.
usage:
输出("foo", bar);
输出("foo", func((bar == 4) + 7, val3 9);
or (DIY)
print(...);
Note: A Print Func will call println in the end.
usage:
存在 x 属于 集合1, 任意 y 属于 集合2, ..., 逻辑表达式;
or (DIY)
exists x in type1, any y in type2, ..., logical expression;
Note: Use of quantity will increase the complexity of solutions, leading to waiting for long time to see the results.
usage:
参变量(参数...) = 字串 或 数值;
or
Argument_Variable(param1,...) = String or Integer;
Note: Use of fixed will decrease the complexity of solutions.
usage:
互斥(逻辑表达式1.逻辑表达式2,...);
or (DIY)
Mutex(Logical_exp1,Logical_exp2,...);
Note: Use of Mutex will increase the complexity of solutions. Use Sequence Variable instead if possible.
usage:
函数(逻辑表达式1.逻辑表达式2,...);
or
Func(Logical_exp1,Logical_exp2,...);
Note: See Enum ‘FuncType‘ in RtFuncFactory.java.
The Grammar of jProlog is referred to 智能通用逻辑问题解题器 ZnlogWin. You can find it by Google or Baidu. Znlog is compiled by VC6.0. I have read its grammar.
I use ANTLR to analyze the input and generate a simple parse tree. Then, I convert it to an AST. The difference between Original Tree and AST is String Factory and Semantic Analysis.
I generate OPT from PrologBaseVisitorImpl.java. You can serialize OPT by calling toString method.
I made a String Map from OPT. The String Factory is used to store any infomation of Variable Name and Literal String and then return an id. It made Semantic Analysis possible. Besides, it will be used in Code Generation, which I haven‘t done yet.
ANTLR does not do semantic analysis. So I need to complete it by myself.
The exception of semantic problem is SemanticException.java.
The most complex work is to convert OPT to AST. I use Visitor Pattern and Stack to do semantic analysis, tree generation and maintenance of string factory at the same time. The AST is too abstract to be implemented carefully on toString method.
The converting work is in RtBlock.java.
The underlying base of system running successfully is Sequence Generator.
Generator 1 and 2 is made by exhaustion. I implemented Fixed feature by using a HashSet and skipping if its position is to be changed in a Foreach loop.
If one wants to generate a non-repetitive sequence, he should see std::next_permutation written in C++. I implemented it with Java.
The Sequence Generator can be clone for storage of results, because system may find multiple solutions.
The Environment object is passed to all of RtExp/RtToken/RtFunc, because the Env include the most infomation of runtime. If system find one solutions, it will clone the env and save it.
In Swing, system is running in a background thread instead of main Gui thread. I used SwingWork<t, v="">. Besides, PrologDialog needs support of JRE8.
Usage:
IRtQueryAnswer query = PrologExecutor.getInstance().run(input_context);
query.queryValue(id, var, params);
Exception: SemanticException
See PrologExecutor.java.
If one wants to redirect the output stream to JTextArea. He can use SwingWorker to set the output stream to a new stream whose ‘write‘ method is overridden to print the messages on JTextArea.
The future is Future, used for interruption where current thread is not Gui thread.
Screenshot 1 - Lie1
Screenshot 2 - Gougu
标签:
原文地址:http://www.cnblogs.com/bajdcc/p/5076032.html