Logic problem solving program and logic Solution Program
Https://github.com/bajdcc/jPrologJProlog-A Simple Solver (Java)
======================================
0x00 Introduction/Introduction
JPrologIs a language describing simple logical problems, using exhaustion to find solutions. DevelopedBajdcc.
JPrologIt is a simple logic-based solution that uses the exhaustive method to search for space. Therefore, the time complexity is affected by the number and length of variables, so it needs to be optimized.
0x01 Grammar/syntax
QUICK: See anlr Grammar File (. g4) at Prolog. g4
PS. Supporting Chinese.
I. Define Values of a Collection/defines the value of the Set
COLLECTION_NAME {}= {val1, val2 ,...};
Or
Set Name {}={ value 1, value 2 ,...};
COLLECTION_NAME, aliasType
Example:
All people {}= {"evri", "black", "krona", "Davis", "Others "};
Digit {}= {0 .. 9 };
II. Define Single Variable/defines a Single Variable
COLLECTION_NAME var1, var2 ,...;
Or
Set name variable name 1, variable name 2 ,...;
Example:
Time of death;
Digit A, B, C;
Note:System will searching in the SPACE of O (n), where n isElements of a TypeOr Math FunctionCard (type);
III. Define Sequence Variable/Define the Sequence Variable
COLLECTION_NAME var1 [], var2 [],...;
Or
Set name variable name 1 [], variable name 2 [],...;
Example:
Time Series [];
Digit A [], B [], C [];
Note:System will searching in the SPACE of O (n !), Where n is card (type );
IV. Define Argument Variable/Define the Parameter Variable
COLLECTION_NAME var1 (Type1, Type2,...), var2 (Type1, Type2 ,...),...;
Or
Set name variable name 1 (parameter Type 1, parameter type 2,...), variable name 2 (parameter Type 1, parameter type 2 ,...),...;
Example:
Time in apartment (all people );
Digit Score (Everyone );
Note:System will searching in the SPACE of O (pow (n, t), where n is card (type) and t isThe Product of card (type1), card (type2 ),...;
V. Settings/Settings
Usage:
Set (Multiple Solutions );
Set (timing, 1000 );
Set (Copyright, "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.
VI. Print/Output
Usage:
Output ("foo", bar );
Output ("foo", func (bar = 4) + 7, val3 9 );
Or (DIY)
print(...);
Note:A Print Func will call println in the end.
VII. Quantity/Quantizer
Usage:
The existence of x belongs to set 1, and any y belongs to set 2,..., logical expression;
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.
VIII. Fixed, Pruning/Fixed value, Pruning
Usage:
Variable (parameter...) = string or value;
Or
Argument_Variable(param1,...) = String or Integer;
Note:Use of fixed will decrease the complexity of solutions.
IX. Mutex/Mutex
Usage:
Mutual Exclusion (logical expression 1. logical expression 2 ,...);
Or (DIY)
Mutex(Logical_exp1,Logical_exp2,...);
Note:Use of Mutex will increase the complexity of solutions. Use Sequence Variable instead if possible.
X. Customize/custom function
Usage:
Function (logical expression 1. logical expression 2 ,...);
Or
Func(Logical_exp1,Logical_exp2,...);
Note:See Enum'FuncType'In RtFuncFactory. java.
0x02 Design/Design I. Designing DSL/specific language in the Design field
The GrammarJPrologIs referredIntelligent universal logic problem solving tool ZnlogWin. You can find it by Google or Baidu. Znlog is compiled by VC6.0. I have read its grammar.
I use anlr to analyze the input and generate a simple parse tree. Then, I convert it to an AST. The difference between Original Tree and AST isString FactoryAndSemantic Analysis.
II. Original Parse Tree/Original syntax Tree
I generate OPT from PrologBaseVisitorImpl. java. You can serialize OPT by callingToStringMethod.
III. String Factory/String Factory
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'tdone yet.
IV. Semantic Analysis/Semantic Analysis
Anlr does not do semantic analysis. So I need to complete it by myself.
The exception of semantic problem is SemanticException. java.
V. AST/abstract syntax tree
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 onToStringMethod.
The converting work is in RtBlock. java.
VI. Sequence Generator/Sequence Generator
The underlying base of system running successfully is Sequence Generator.
Generator 1 and 2 is made by exhaustion. I implementedFixedFeature 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 shocould 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.
VII. Runtime Environment/Runtime Environment
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.
VIII. Gui Application/interface
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.
0x03 Interface/Interface I. Query/Query
Usage:
IRtQueryAnswer query = PrologExecutor.getInstance().run(input_context);
query.queryValue(id, var, params);
- Id: The ordinal of results, default is 0. If multi-answer is turned on, the id is valid.
- Var: Name of variable.
- Params: Array index, or parameters of argument variable. The type of param is eithor String or Integer.
Exception:SemanticException
II. Global Settings/Global Settings
See PrologExecutor. java.
- Out: Set output stream, whose type is PrintStream.
- Future: For interruption, e.g., Stop Action of Gui.
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.
0x04 Example/Example Screenshot
Screenshot 1-Lie1
Screenshot 2-Gougu