Https://github.com/bajdcc/jPrologJprolog-a Simple Solver (
Java)
===========================
0x00 Introduction/Introduction
Jprolog is a language describing simple logical problems, using exhaustion to find solutions. Developed by BAJDCC.
Jprolog is a simple logic problem solving program, which mainly uses the exhaustive method to search the solution space, so the time complexity is affected by the number and length of variables, so it needs to be optimized.
0x01 Grammar/syntax
QUICK: See ANTLR Grammar File (. g4) at Prolog.g4
PS. Supporting Chinese.
I. Define values of a Collection/definition collection
collection_name{} = {Val1,val2,...};
Or
Collection name {} = {value 1, value 2,...};
collection_name, alias Type
Example
Everyone {} = {"Evry", "Blake", "Krone", "Davis", "Other person"};
digit{} = {0..9};
Ii. Define Single Variable/defining univariate variables
collection_name var1,var2,...;
Or
Set name variable name 1, variable name 2,...;
Example
Time of death;
Digit A,b,c;
Note: System would searching in the SPACE of O (n), where n is Elements of a type or Math Function card (Type);
Iii. Define Sequence Variable/defining order variables
collection_name var1[],var2[],...;
Or
Set name variable name 1[], variable name 2[],...;
Example
Time series [];
Digit a[],b[],c[];
Note: System would searching in the SPACE of O (n!), where n is card (type);
Iv. Define Argument Variable/definition parametric
Collection_name var1 (type1,type2,...), var2 (type1,type2,...),...;
Or
The collection name Variable name 1 (parameter type 1, parameter Type 2,...), variable name 2 (parameter type 1, parameter Type 2,...),...;
Example
Time in the apartment (everyone);
Digit score (Everyone);
Note: System would searching in the SPACE of O (POW (n,t)), where n was card (type) and T is the Product of the card (type1), Card (Typ E2), ...;
V. Settings/settings
Usage
Set (multi-solution);
Set (timing, 1000);
Settings (Copyright, "copyrights");
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 would 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 would call println in the end.
Vii. Quantity/quantifiers
Usage
Existence x belongs to set 1, any y belongs to set 2, ..., logical expression;
or (DIY)
exists x in type1, any y in type2, ..., logical expression;
Note: Use of quantity would increase the complexity of solutions, leading to waiting for long time to see the results.
Viii. fixed, pruning/fixed value, pruning
Usage
Argument (parameter ...) = string or value;
Or
Argument_variable (param1,...) = String or Integer;
Note: Use of fixed would 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 would increase the complexity of solutions. Use Sequence Variable instead if possible.
X. Customize/Custom Functions
Usage
function (logical expression 1. Logical expression 2,...);
Or
Func (Logical_exp1,logical_exp2,...);
Note: See Enum 'functype' in Rtfuncfactory.java.
0x02 Design/Designs I. Designing DSL/Design Domain Specific language
The Grammar of Jprolog is referred to intelligent general logic problem Solver Znlogwin. You can find it by Google or Baidu. Znlog is compiled by VC6.0. I have the 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 a String Factory and Semanticanalysis.
Ii. Original Parse tree/Primitive syntax trees
I generate OPT from Prologbasevisitorimpl.java. You can serialize OPT by calling toString method.
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'll be used in Code Generation, which I haven ' t do yet.
Iv. Semantic analysis/Semantic analyses
ANTLR does not does semantic analysis. So I need the it by myself.
The exception of semantic problem is semanticexception.java.
V. AST/Abstract Syntax tree
The most complex are to convert OPT-to AST. I use Visitor-Pattern and Stack to does semantic analysis, tree generation and maintenance of string factory at the same Tim E. The AST is too an abstract to being implemented carefully on toString method.
The converting work was in Rtblock.java.
Vi. Sequence Generator/Sequence Generator
The underlying base of system running successfully is Sequence Generator.
- With repetition and 1D
- With repetition and ND, where n > 1
- Without repetition
Generator 1 and 2 are made by exhaustion. I implemented Fixed feature by using a HashSet and skipping if it position is to being 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.
VII. Runtime Environment/runtime environment
The environment object is passed to all of Rtexp/rttoken/rtfunc, because the ENV include the most infomation of the runtime. If system find one solutions, it would clone the Env and save it.
VIII. Gui application/interface
In Swing, system was 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/Enquiry
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/settings
See Prologexecutor.java.
- Out:set output stream, whose type is PrintStream.
- Future:for interruption, e.g., Stop Action of the 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 was overridden to print the M Essages on JTextArea.
The future was the future, and the used for interruption where current thread was not the GUI thread.
0x04 Example/Example screenshot
Screenshot 1-lie1
Screenshot 2-gougu
Logic Problem Solver