├── .editorconfig
├── .gitignore
├── CSProlog.Core.Test
├── CSProlog.Core.Test.csproj
└── PrologEngine.cs
├── CSProlog.sln
├── CSProlog
├── BaseTerm.cs
├── Bootstrap.cs
├── Builtins.cs
├── CSProlog.csproj
├── CSProlog.exe.config
├── CrossRefTable.cs
├── CsPrologHelp.resx
├── DerivedTerms.cs
├── DerivedTermsExotic.cs
├── Engine.cs
├── ExternalUsage.cs
├── IO.cs
├── Internet.cs
├── ListPatternTerm.cs
├── Miscellaneous.cs
├── Operator.cs
├── PG
│ ├── BaseParser.cs
│ ├── JSON.cs
│ ├── JSON.grm
│ ├── JsonParser.cs
│ ├── JsonParserEx.cs
│ ├── PL.cs
│ ├── PL.grm
│ ├── Parser.tpl
│ ├── PrologParser.cs
│ ├── PrologParserEx.cs
│ ├── go.bat
│ ├── jgo.bat
│ └── pg4main.exe
├── PredDescr.cs
├── PredStorage.cs
├── SimpleDOMParser.cs
├── TermArithmetic.cs
├── TermArray.cs
├── TermNodeList.cs
├── TermSet.cs
└── TokenSeqToTerm.cs
├── Docu
├── Covington-NL-book.zip
├── Edinburgh-style IO.doc
├── HistoryOfProlog.pdf
├── KdeBosschere-parser.zip
├── KdeBosschere-prolog.pdf
├── Standard Operators.doc
└── prolog-digital.pdf
├── LICENSE
├── PL.NETcore
├── Main.cs
└── PL.NETcore.csproj
├── PLd
├── Main.cs
├── PLd.csproj
└── Properties
│ └── AssemblyInfo.cs
├── PLw
├── MainForm.Designer.cs
├── MainForm.cs
├── MainForm.resx
├── PLw.csproj
├── PLw.sln
├── Program.cs
└── Properties
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ ├── Resources.resx
│ ├── Settings.Designer.cs
│ ├── Settings.settings
│ └── app.manifest
├── PLx
├── PLx.csproj
├── Program.cs
└── Properties
│ └── AssemblyInfo.cs
├── README (2007-2014).doc
├── README (2007-2014).pdf
├── README.md
├── README.txt
└── appveyor.yml
/.editorconfig:
--------------------------------------------------------------------------------
1 | # Remove the line below if you want to inherit .editorconfig settings from higher directories
2 | root = true
3 |
4 | # C# files
5 | [*.cs]
6 |
7 | #### Core EditorConfig Options ####
8 |
9 | # Indentation and spacing
10 | indent_size = 4
11 | indent_style = space
12 | tab_width = 4
13 |
14 | # New line preferences
15 | end_of_line = crlf
16 | insert_final_newline = false
17 |
18 | #### .NET Coding Conventions ####
19 |
20 | # Organize usings
21 | dotnet_separate_import_directive_groups = false
22 | dotnet_sort_system_directives_first = true
23 | file_header_template = unset
24 |
25 | # this. and Me. preferences
26 | dotnet_style_qualification_for_event = false:silent
27 | dotnet_style_qualification_for_field = false:silent
28 | dotnet_style_qualification_for_method = false:silent
29 | dotnet_style_qualification_for_property = false:silent
30 |
31 | # Language keywords vs BCL types preferences
32 | dotnet_style_predefined_type_for_locals_parameters_members = true:silent
33 | dotnet_style_predefined_type_for_member_access = true:silent
34 |
35 | # Parentheses preferences
36 | dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent
37 | dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent
38 | dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent
39 | dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent
40 |
41 | # Modifier preferences
42 | dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
43 |
44 | # Expression-level preferences
45 | dotnet_style_coalesce_expression = true:suggestion
46 | dotnet_style_collection_initializer = true:suggestion
47 | dotnet_style_explicit_tuple_names = true:suggestion
48 | dotnet_style_null_propagation = true:suggestion
49 | dotnet_style_object_initializer = true:suggestion
50 | dotnet_style_operator_placement_when_wrapping = beginning_of_line
51 | dotnet_style_prefer_auto_properties = true:silent
52 | dotnet_style_prefer_compound_assignment = true:suggestion
53 | dotnet_style_prefer_conditional_expression_over_assignment = true:silent
54 | dotnet_style_prefer_conditional_expression_over_return = true:silent
55 | dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
56 | dotnet_style_prefer_inferred_tuple_names = true:suggestion
57 | dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
58 | dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
59 | dotnet_style_prefer_simplified_interpolation = true:suggestion
60 |
61 | # Field preferences
62 | dotnet_style_readonly_field = true:suggestion
63 |
64 | # Parameter preferences
65 | dotnet_code_quality_unused_parameters = all:suggestion
66 |
67 | #### C# Coding Conventions ####
68 |
69 | # var preferences
70 | csharp_style_var_elsewhere = false:silent
71 | csharp_style_var_for_built_in_types = false:silent
72 | csharp_style_var_when_type_is_apparent = false:silent
73 |
74 | # Expression-bodied members
75 | csharp_style_expression_bodied_accessors = true:silent
76 | csharp_style_expression_bodied_constructors = false:silent
77 | csharp_style_expression_bodied_indexers = true:silent
78 | csharp_style_expression_bodied_lambdas = true:silent
79 | csharp_style_expression_bodied_local_functions = false:silent
80 | csharp_style_expression_bodied_methods = false:silent
81 | csharp_style_expression_bodied_operators = false:silent
82 | csharp_style_expression_bodied_properties = true:silent
83 |
84 | # Pattern matching preferences
85 | csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
86 | csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
87 | csharp_style_prefer_switch_expression = true:suggestion
88 |
89 | # Null-checking preferences
90 | csharp_style_conditional_delegate_call = true:suggestion
91 |
92 | # Modifier preferences
93 | csharp_prefer_static_local_function = true:suggestion
94 | csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:silent
95 |
96 | # Code-block preferences
97 | csharp_prefer_braces = true:silent
98 | csharp_prefer_simple_using_statement = true:suggestion
99 |
100 | # Expression-level preferences
101 | csharp_prefer_simple_default_expression = true:suggestion
102 | csharp_style_deconstructed_variable_declaration = true:suggestion
103 | csharp_style_inlined_variable_declaration = true:suggestion
104 | csharp_style_pattern_local_over_anonymous_function = true:suggestion
105 | csharp_style_prefer_index_operator = true:suggestion
106 | csharp_style_prefer_range_operator = true:suggestion
107 | csharp_style_throw_expression = true:suggestion
108 | csharp_style_unused_value_assignment_preference = discard_variable:suggestion
109 | csharp_style_unused_value_expression_statement_preference = discard_variable:silent
110 |
111 | # 'using' directive preferences
112 | csharp_using_directive_placement = outside_namespace:silent
113 |
114 | #### C# Formatting Rules ####
115 |
116 | # New line preferences
117 | csharp_new_line_before_catch = true
118 | csharp_new_line_before_else = true
119 | csharp_new_line_before_finally = true
120 | csharp_new_line_before_members_in_anonymous_types = true
121 | csharp_new_line_before_members_in_object_initializers = true
122 | csharp_new_line_before_open_brace = all
123 | csharp_new_line_between_query_expression_clauses = true
124 |
125 | # Indentation preferences
126 | csharp_indent_block_contents = true
127 | csharp_indent_braces = false
128 | csharp_indent_case_contents = true
129 | csharp_indent_case_contents_when_block = true
130 | csharp_indent_labels = no_change
131 | csharp_indent_switch_labels = true
132 |
133 | # Space preferences
134 | csharp_space_after_cast = false
135 | csharp_space_after_colon_in_inheritance_clause = true
136 | csharp_space_after_comma = true
137 | csharp_space_after_dot = false
138 | csharp_space_after_keywords_in_control_flow_statements = true
139 | csharp_space_after_semicolon_in_for_statement = true
140 | csharp_space_around_binary_operators = before_and_after
141 | csharp_space_around_declaration_statements = false
142 | csharp_space_before_colon_in_inheritance_clause = true
143 | csharp_space_before_comma = false
144 | csharp_space_before_dot = false
145 | csharp_space_before_open_square_brackets = false
146 | csharp_space_before_semicolon_in_for_statement = false
147 | csharp_space_between_empty_square_brackets = false
148 | csharp_space_between_method_call_empty_parameter_list_parentheses = false
149 | csharp_space_between_method_call_name_and_opening_parenthesis = false
150 | csharp_space_between_method_call_parameter_list_parentheses = false
151 | csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
152 | csharp_space_between_method_declaration_name_and_open_parenthesis = false
153 | csharp_space_between_method_declaration_parameter_list_parentheses = false
154 | csharp_space_between_parentheses = false
155 | csharp_space_between_square_brackets = false
156 |
157 | # Wrapping preferences
158 | csharp_preserve_single_line_blocks = true
159 | csharp_preserve_single_line_statements = true
160 |
161 | #### Naming styles ####
162 |
163 | # Naming rules
164 |
165 | dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
166 | dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
167 | dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i
168 |
169 | dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
170 | dotnet_naming_rule.types_should_be_pascal_case.symbols = types
171 | dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case
172 |
173 | dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
174 | dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
175 | dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case
176 |
177 | # Symbol specifications
178 |
179 | dotnet_naming_symbols.interface.applicable_kinds = interface
180 | dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
181 | dotnet_naming_symbols.interface.required_modifiers =
182 |
183 | dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
184 | dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
185 | dotnet_naming_symbols.types.required_modifiers =
186 |
187 | dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
188 | dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
189 | dotnet_naming_symbols.non_field_members.required_modifiers =
190 |
191 | # Naming styles
192 |
193 | dotnet_naming_style.pascal_case.required_prefix =
194 | dotnet_naming_style.pascal_case.required_suffix =
195 | dotnet_naming_style.pascal_case.word_separator =
196 | dotnet_naming_style.pascal_case.capitalization = pascal_case
197 |
198 | dotnet_naming_style.begins_with_i.required_prefix = I
199 | dotnet_naming_style.begins_with_i.required_suffix =
200 | dotnet_naming_style.begins_with_i.word_separator =
201 | dotnet_naming_style.begins_with_i.capitalization = pascal_case
202 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.user
2 | *.suo
3 | bin/
4 | obj/
5 | packages/
6 | .vs/
7 | Dist/
--------------------------------------------------------------------------------
/CSProlog.Core.Test/CSProlog.Core.Test.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 | false
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/CSProlog.Core.Test/PrologEngine.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Xunit;
3 | using Prolog;
4 |
5 | namespace CSProlog.Core.Test
6 | {
7 | public class PrologEngineTest
8 | {
9 | [Fact]
10 | public void ConsultFromString_GetOneSolution()
11 | {
12 | var prolog = new PrologEngine(persistentCommandHistory: false);
13 |
14 | // 'socrates' is human.
15 | prolog.ConsultFromString("human(socrates).");
16 | // 'R2-D2' is droid.
17 | prolog.ConsultFromString("droid(r2d2).");
18 | // human is bound to die.
19 | prolog.ConsultFromString("mortal(X) :- human(X).");
20 |
21 | // Question: Shall 'socrates' die?
22 | var solution1 = prolog.GetFirstSolution(query: "mortal(socrates).");
23 | Assert.True(solution1.Solved); // = "True" (Yes)
24 |
25 | // Question: Shall 'R2-D2' die?
26 | var solution2 = prolog.GetFirstSolution(query: "mortal(r2d2).");
27 | Assert.False(solution2.Solved); // = "False" (No)
28 | }
29 |
30 | [Fact]
31 | public void ConsultFromString_GetAllSolutions_Adhoc()
32 | {
33 | var prolog = new PrologEngine(persistentCommandHistory: false);
34 |
35 | // 'socrates' is human.
36 | prolog.ConsultFromString("human(socrates).");
37 | // 'R2-D2' is droid.
38 | prolog.ConsultFromString("droid(r2d2).");
39 | // human is bound to die.
40 | prolog.ConsultFromString("mortal(X) :- human(X).");
41 |
42 | prolog.GetFirstSolution(query: "listing.");
43 |
44 | SolutionSet solutionset1 = prolog.GetAllSolutions(null, "human(H)");
45 | Console.WriteLine("=====================================");
46 | Console.WriteLine(solutionset1.ErrMsg);
47 | Console.WriteLine("=====================================");
48 | Assert.True(solutionset1.Success);
49 | if (solutionset1.Success)
50 | {
51 | var s = solutionset1[0];
52 | foreach (Variable v in s.NextVariable)
53 | Console.WriteLine(string.Format("{0} ({1}) = {2}", v.Name, v.Type, v.Value));
54 |
55 | }
56 |
57 | }
58 |
59 | [Fact]
60 | public void HelpTest()
61 | {
62 | var prolog = new PrologEngine(persistentCommandHistory: false);
63 | var s1 = prolog.GetFirstSolution("help.");
64 | Assert.True(s1.Solved); // = "True" (Yes) [help atleast ran]
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/CSProlog.sln:
--------------------------------------------------------------------------------
1 | Microsoft Visual Studio Solution File, Format Version 12.00
2 | # Visual Studio Version 16
3 | VisualStudioVersion = 16.0.29924.181
4 | MinimumVisualStudioVersion = 10.0.40219.1
5 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{435BB1A6-EBA7-4F55-BF99-AF1C65FC43E2}"
6 | ProjectSection(SolutionItems) = preProject
7 | .editorconfig = .editorconfig
8 | appveyor.yml = appveyor.yml
9 | EndProjectSection
10 | EndProject
11 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PLd", "PLd\PLd.csproj", "{B7438FBF-6E46-4065-A60B-1B1CA2AC9875}"
12 | EndProject
13 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PLw", "PLw\PLw.csproj", "{BB06ADF9-92C3-4F92-8397-5ACC92208528}"
14 | EndProject
15 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PLx", "PLx\PLx.csproj", "{E84A59D2-E868-4108-BE9A-55CBB7E85E36}"
16 | EndProject
17 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSProlog", "CSProlog\CSProlog.csproj", "{6A947B78-96D1-45F2-BB9F-036586BAB544}"
18 | EndProject
19 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSProlog.Core.Test", "CSProlog.Core.Test\CSProlog.Core.Test.csproj", "{927B2CA6-BB69-422F-8107-B2E5EE9A6C4C}"
20 | EndProject
21 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PL.NETcore", "PL.NETcore\PL.NETcore.csproj", "{191FF3C7-503C-4C3E-AC49-46B9BCB4B396}"
22 | EndProject
23 | Global
24 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
25 | Debug|Any CPU = Debug|Any CPU
26 | LibraryRelease|Any CPU = LibraryRelease|Any CPU
27 | Release|Any CPU = Release|Any CPU
28 | EndGlobalSection
29 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
30 | {B7438FBF-6E46-4065-A60B-1B1CA2AC9875}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31 | {B7438FBF-6E46-4065-A60B-1B1CA2AC9875}.Debug|Any CPU.Build.0 = Debug|Any CPU
32 | {B7438FBF-6E46-4065-A60B-1B1CA2AC9875}.LibraryRelease|Any CPU.ActiveCfg = Release|Any CPU
33 | {B7438FBF-6E46-4065-A60B-1B1CA2AC9875}.Release|Any CPU.ActiveCfg = Release|Any CPU
34 | {B7438FBF-6E46-4065-A60B-1B1CA2AC9875}.Release|Any CPU.Build.0 = Release|Any CPU
35 | {BB06ADF9-92C3-4F92-8397-5ACC92208528}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36 | {BB06ADF9-92C3-4F92-8397-5ACC92208528}.Debug|Any CPU.Build.0 = Debug|Any CPU
37 | {BB06ADF9-92C3-4F92-8397-5ACC92208528}.LibraryRelease|Any CPU.ActiveCfg = Release|Any CPU
38 | {BB06ADF9-92C3-4F92-8397-5ACC92208528}.Release|Any CPU.ActiveCfg = Release|Any CPU
39 | {BB06ADF9-92C3-4F92-8397-5ACC92208528}.Release|Any CPU.Build.0 = Release|Any CPU
40 | {E84A59D2-E868-4108-BE9A-55CBB7E85E36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41 | {E84A59D2-E868-4108-BE9A-55CBB7E85E36}.Debug|Any CPU.Build.0 = Debug|Any CPU
42 | {E84A59D2-E868-4108-BE9A-55CBB7E85E36}.LibraryRelease|Any CPU.ActiveCfg = Release|Any CPU
43 | {E84A59D2-E868-4108-BE9A-55CBB7E85E36}.Release|Any CPU.ActiveCfg = Release|Any CPU
44 | {E84A59D2-E868-4108-BE9A-55CBB7E85E36}.Release|Any CPU.Build.0 = Release|Any CPU
45 | {6A947B78-96D1-45F2-BB9F-036586BAB544}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
46 | {6A947B78-96D1-45F2-BB9F-036586BAB544}.Debug|Any CPU.Build.0 = Debug|Any CPU
47 | {6A947B78-96D1-45F2-BB9F-036586BAB544}.LibraryRelease|Any CPU.ActiveCfg = Release|Any CPU
48 | {6A947B78-96D1-45F2-BB9F-036586BAB544}.LibraryRelease|Any CPU.Build.0 = Release|Any CPU
49 | {6A947B78-96D1-45F2-BB9F-036586BAB544}.Release|Any CPU.ActiveCfg = Release|Any CPU
50 | {6A947B78-96D1-45F2-BB9F-036586BAB544}.Release|Any CPU.Build.0 = Release|Any CPU
51 | {927B2CA6-BB69-422F-8107-B2E5EE9A6C4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
52 | {927B2CA6-BB69-422F-8107-B2E5EE9A6C4C}.Debug|Any CPU.Build.0 = Debug|Any CPU
53 | {927B2CA6-BB69-422F-8107-B2E5EE9A6C4C}.LibraryRelease|Any CPU.ActiveCfg = Release|Any CPU
54 | {927B2CA6-BB69-422F-8107-B2E5EE9A6C4C}.LibraryRelease|Any CPU.Build.0 = Release|Any CPU
55 | {927B2CA6-BB69-422F-8107-B2E5EE9A6C4C}.Release|Any CPU.ActiveCfg = Release|Any CPU
56 | {927B2CA6-BB69-422F-8107-B2E5EE9A6C4C}.Release|Any CPU.Build.0 = Release|Any CPU
57 | {191FF3C7-503C-4C3E-AC49-46B9BCB4B396}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
58 | {191FF3C7-503C-4C3E-AC49-46B9BCB4B396}.Debug|Any CPU.Build.0 = Debug|Any CPU
59 | {191FF3C7-503C-4C3E-AC49-46B9BCB4B396}.LibraryRelease|Any CPU.ActiveCfg = Release|Any CPU
60 | {191FF3C7-503C-4C3E-AC49-46B9BCB4B396}.Release|Any CPU.ActiveCfg = Release|Any CPU
61 | {191FF3C7-503C-4C3E-AC49-46B9BCB4B396}.Release|Any CPU.Build.0 = Release|Any CPU
62 | EndGlobalSection
63 | GlobalSection(SolutionProperties) = preSolution
64 | HideSolutionNode = FALSE
65 | EndGlobalSection
66 | GlobalSection(ExtensibilityGlobals) = postSolution
67 | SolutionGuid = {D1917349-2AD7-41E1-B8E7-E1BE1AABA632}
68 | EndGlobalSection
69 | EndGlobal
70 |
--------------------------------------------------------------------------------
/CSProlog/BaseTerm.cs:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jsakamoto/CSharpProlog/d30f1bb334b44413f5e338672feffb715bc1ab37/CSProlog/BaseTerm.cs
--------------------------------------------------------------------------------
/CSProlog/Builtins.cs:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jsakamoto/CSharpProlog/d30f1bb334b44413f5e338672feffb715bc1ab37/CSProlog/Builtins.cs
--------------------------------------------------------------------------------
/CSProlog/CSProlog.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard1.4;net35
5 | $(NoWarn);1591
6 | true
7 | CSProlog
8 | C#Prolog (CSProlog)
9 | $(AssemblyTitle)
10 | CSProlog
11 | 1.6.0
12 | $(PackageTargetFallback);dnxcore50
13 | CSProlog
14 | 6.0.0.0
15 | John Pool,Amersfoort,Netherlands,J.Sakamoto,Josh Ahlstrom
16 | N/A
17 | C#Prolog -- A Prolog interpreter written in C#.
18 | Can easily be integrated in C# programs.
19 | Characteristics: reliable and fairly fast interpreter, command line interface, builtin DCG, XML- and JSON-predicates, SQL-predicates, extendible.
20 | Copyright (C) 2007-2015 John Pool -- j.pool@ision.nl
21 | https://github.com/jsakamoto/CSharpProlog/blob/vnext/master/LICENSE
22 | https://github.com/jsakamoto/CSharpProlog/
23 | Prolog interpreter
24 | v.6.0.0
25 | - BREAKING CHANGE: Remove "SAMPLES, TESTING & EXPERIMENTAL" predefined predicates. (including CHAT-80 support)
26 | - Fix: "help" predefined predicate dose not work.
27 | - Enhance: GetAllSolutions can work with null file name.
28 | v.5.0.0.1
29 | - Support: .NET Standard 1.4 (.NET Core) and UWP
30 | v.5.0.0
31 | - BREAKING CHANGE: Remove dependency of "System.Windows.Forms".
32 | - NuGet package release
33 | true
34 |
35 |
36 |
37 | TRACE;NETSTANDARD;RELEASE;NETSTANDARD1_4;RELEASE;NETSTANDARD1_4
38 |
39 |
40 | TRACE;DEBUG;NETSTANDARD
41 |
42 |
43 | TRACE;RELEASE;mswindows;net35
44 |
45 |
46 | TRACE;DEBUG;mswindows;net35
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/CSProlog/CSProlog.exe.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
80 |
81 |
82 |
--------------------------------------------------------------------------------
/CSProlog/CrossRefTable.cs:
--------------------------------------------------------------------------------
1 | namespace Prolog
2 | {
3 | using System;
4 | using System.Text;
5 | using System.Collections;
6 | using System.Collections.Generic;
7 | using System.IO;
8 |
9 | /* _______________________________________________________________________________________________
10 | | |
11 | | C#Prolog -- Copyright (C) 2007-2014 John Pool -- j.pool@ision.nl |
12 | | |
13 | | This library is free software; you can redistribute it and/or modify it under the terms of |
14 | | the GNU General Public License as published by the Free Software Foundation; either version |
15 | | 2 of the License, or any later version. |
16 | | |
17 | | This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
18 | | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | | See the GNU General Public License for details, or enter 'license.' at the command prompt. |
20 | |_______________________________________________________________________________________________|
21 | */
22 |
23 | // PrologParser Generator version 4.0 -- Date/Time: 22-12-2010 8:42:54
24 |
25 | public partial class PrologEngine
26 | {
27 | // CrossRefTable implements a cross reference table for the predicates making up the 'program'.
28 | // Each predicate corresponds to a row. The entries in the row are the predicates that are called.
29 | // CalculateClosure () calculates the indirect calls (i.e. if A calls B and B calls C directly,
30 | // then the closure would calculate the indirect call A -> C.
31 | // Direct calls have an entry value 'false', indirect calls an entry value 'true'.
32 | #region CrossRefTable
33 | public class CrossRefTable : Dictionary
34 | {
35 | List axis; // predicate names sorted alphabetically
36 | int dimension { get { return axis.Count; } }
37 | bool findAllCalls; // i.e. both direct and indirect (result of closure)
38 | bool? result;
39 | public bool FindAllCalls { get { return findAllCalls; } set { findAllCalls = value; } }
40 |
41 | public CrossRefTable()
42 | {
43 | axis = new List();
44 | findAllCalls = false;
45 | }
46 |
47 |
48 | public void Reset()
49 | {
50 | Clear();
51 | axis.Clear();
52 | findAllCalls = false;
53 | }
54 |
55 |
56 | public void AddPredicate(PredicateDescr pd)
57 | {
58 | // add predicate only if not already present
59 | int i = axis.BinarySearch(pd);
60 |
61 | if (i < 0) axis.Insert(~i, pd); // keep range sorted
62 | }
63 |
64 |
65 | #region indices
66 | // used only when registering the direct calls, not for the closure (indirect calls)
67 | public bool? this[PredicateDescr row, PredicateDescr col]
68 | {
69 | set
70 | {
71 | this[CompoundKey(row, col)] = false; // i.e. a direct call
72 | }
73 |
74 | get
75 | {
76 | if (TryGetValue(CompoundKey(row, col), out result)) return result;
77 |
78 | return null;
79 | }
80 | }
81 |
82 | // used only when calculating the indirect calls (CalculateClosure)
83 | public bool? this[int i, int j]
84 | {
85 | set
86 | {
87 | string key = CompoundKey(axis[i], axis[j]);
88 | bool? result;
89 |
90 | // do not overwrite 'false', as this would hide the fact that a direct call exists
91 | if (!TryGetValue(key, out result))
92 | this[key] = true;
93 | }
94 |
95 | get
96 | {
97 | if (TryGetValue(CompoundKey(axis[i], axis[j]), out result)) return result;
98 |
99 | return null;
100 | }
101 | }
102 | #endregion indices
103 |
104 | // find all predicates called by 'row'
105 | public IEnumerable Row(PredicateDescr row)
106 | {
107 | foreach (PredicateDescr col in axis)
108 | if ((result = this[row, col]) != null)
109 | if (findAllCalls || result == false)
110 | yield return col;
111 | }
112 |
113 | // find all predicates that call 'col'
114 | public IEnumerable Col(PredicateDescr col)
115 | {
116 | foreach (PredicateDescr row in axis)
117 | if ((result = this[row, col]) != null)
118 | if (findAllCalls || result == false)
119 | yield return col;
120 | }
121 |
122 |
123 | // Warshall algorithm -- Journal of the ACM, Jan. 1962, pp. 11-12
124 | // This algorithm calculates the indirect calls.
125 | public void CalculateClosure()
126 | {
127 | int i, j, k;
128 |
129 | for (i = 0; i < dimension; i++)
130 | for (j = 0; j < dimension; j++)
131 | if (this[j, i] != null)
132 | for (k = 0; k < dimension; k++)
133 | if (this[i, k] != null)
134 | this[j, k] = true; // 'true' to indicate entry is indirect call (result of closure)
135 | }
136 |
137 |
138 | public void GenerateCsvFile(string fileName)
139 | {
140 | bool? value;
141 | StreamWriter sr = null;
142 | int rowTotal;
143 | int[] colTotal = new int[axis.Count];
144 |
145 | try
146 | {
147 | sr = new StreamWriter(File.OpenWrite(fileName));
148 |
149 | sr.WriteLine(Enquote("Call table. Row calls column entries. 'D' stands for direct call, 'I' for indirect call"));
150 | sr.WriteLine();
151 |
152 | // column titles
153 | for (int j = 0; j < dimension; j++)
154 | {
155 | sr.Write(";{0}", Enquote(axis[j].Name));
156 | colTotal[j] = 0;
157 | }
158 |
159 | sr.WriteLine(";TOTAL");
160 |
161 | // rows
162 | for (int i = 0; i < dimension; i++)
163 | {
164 | sr.Write(Enquote(axis[i].Name)); // row title
165 | rowTotal = 0;
166 |
167 | for (int j = 0; j < dimension; j++)
168 | {
169 | sr.Write(';');
170 |
171 | if ((value = this[i, j]) != null)
172 | {
173 | sr.Write((value == false) ? "D" : "I");
174 | rowTotal++;
175 | colTotal[j]++;
176 | }
177 | }
178 |
179 | sr.WriteLine(";{0}", rowTotal);
180 | }
181 |
182 | // column totals
183 | sr.Write("TOTAL");
184 |
185 | for (int j = 0; j < dimension; j++) sr.Write(";{0}", colTotal[j]);
186 |
187 | sr.WriteLine();
188 | }
189 | catch (Exception e)
190 | {
191 | IO.Error("Error while trying to save Excel spreadsheet '{0}'. Message was:\r\n{1}" + e.Message);
192 | }
193 | finally
194 | {
195 | if (sr != null) sr.Dispose();
196 | }
197 | }
198 |
199 | string Enquote(string s)
200 | {
201 | return '"' + s.Replace("\"", "\"\"") + '"';
202 | }
203 |
204 | string CompoundKey(PredicateDescr row, PredicateDescr col)
205 | {
206 | return string.Format("{0} {1}", row.Name, col.Name);
207 | }
208 | }
209 | #endregion CrossRefTable
210 |
211 | #region PredicateDescr CompareTo ()
212 | public partial class PredicateDescr : IComparable
213 | {
214 | public int CompareTo(PredicateDescr pd)
215 | {
216 | int result = Functor.CompareTo(pd.Functor);
217 |
218 | if (result == 0) return Arity.CompareTo(pd.Arity);
219 |
220 | return result;
221 | }
222 | }
223 | #endregion
224 | }
225 | }
226 |
--------------------------------------------------------------------------------
/CSProlog/ExternalUsage.cs:
--------------------------------------------------------------------------------
1 | /*-----------------------------------------------------------------------------------------
2 |
3 | C#Prolog -- Copyright (C) 2007-2015 John Pool -- j.pool@ision.nl
4 |
5 | This library is free software; you can redistribute it and/or modify it under the terms of
6 | the GNU Lesser General Public License as published by the Free Software Foundation; either
7 | version 3.0 of the License, or any later version.
8 |
9 | This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 | See the GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl-3.0.html), or
12 | enter 'license' at the command prompt.
13 |
14 | -------------------------------------------------------------------------------------------*/
15 |
16 | using System;
17 | using System.Collections.Generic;
18 | using System.IO;
19 | using System.Text;
20 | using System.Xml;
21 | #if mswindows
22 | using System.Windows.Forms;
23 | #endif
24 |
25 | namespace Prolog
26 | {
27 | #region SolutionSet
28 | public class SolutionSet
29 | {
30 | string query;
31 | public string Query { get { return query; } internal set { query = value; } }
32 | bool success;
33 | public bool Success { get { return success; } internal set { success = value; } }
34 | string errorMsg;
35 | public string ErrMsg { get { return errorMsg; } internal set { errorMsg = value; } }
36 | public bool HasError { get { return errorMsg != null; } }
37 | List solutionSet;
38 | public int Count { get { return solutionSet.Count; } }
39 | Solution currVarSet;
40 |
41 | public SolutionSet()
42 | {
43 | solutionSet = new List();
44 | success = false;
45 | errorMsg = null;
46 | }
47 |
48 | internal void CreateVarSet()
49 | {
50 | solutionSet.Add(currVarSet = new Solution());
51 | }
52 |
53 | internal void AddToVarSet(string name, string type, string value)
54 | {
55 | currVarSet.Add(name, type, value);
56 | success = true;
57 | }
58 |
59 | public IEnumerable NextSolution
60 | {
61 | get
62 | {
63 | foreach (Solution s in solutionSet)
64 | yield return s;
65 | }
66 | }
67 |
68 | public Solution this[int i]
69 | {
70 | get
71 | {
72 | return solutionSet[i];
73 | }
74 | }
75 |
76 | public override string ToString()
77 | {
78 | if (errorMsg != null)
79 | return errorMsg;
80 |
81 | if (success)
82 | {
83 | if (solutionSet.Count == 0)
84 | return "yes";
85 | else
86 | {
87 | StringBuilder sb = new StringBuilder();
88 | int i = 0;
89 | foreach (Solution s in solutionSet)
90 | sb.AppendLine("Solution {0}\r\n{1}", ++i, s.ToString());
91 |
92 | return sb.ToString();
93 | }
94 | }
95 | else
96 | return "no";
97 | }
98 | }
99 | #endregion SolutionSet
100 |
101 | #region Solution
102 | public class Solution // a solution is a set of variables
103 | {
104 | List variables;
105 | int Count { get { return variables.Count; } }
106 |
107 | public Solution()
108 | {
109 | variables = new List();
110 | }
111 |
112 | internal void Add(string name, string type, string value)
113 | {
114 | variables.Add(new Variable(name, type, value));
115 | }
116 |
117 | public IEnumerable NextVariable
118 | {
119 | get
120 | {
121 | foreach (Variable v in variables)
122 | yield return v;
123 | }
124 | }
125 |
126 | public Variable this[int i]
127 | {
128 | get
129 | {
130 | return variables[i];
131 | }
132 | }
133 |
134 | public override string ToString()
135 | {
136 | StringBuilder sb = new StringBuilder();
137 |
138 | foreach (Variable v in variables)
139 | sb.AppendLine(v.ToString());
140 |
141 | return sb.ToString();
142 | }
143 | }
144 | #endregion Solution
145 |
146 | #region Variable
147 | public class Variable
148 | {
149 | string name;
150 | public string Name { get { return name; } }
151 | string type;
152 | public string Type { get { return type; } }
153 | string value;
154 | public string Value { get { return value; } }
155 |
156 | public Variable(string name, string type, string value)
157 | {
158 | this.name = name;
159 | this.type = type;
160 | this.value = value;
161 | }
162 |
163 | public override string ToString()
164 | {
165 | return string.Format("{0} ({1}) = {2}", name, type, value);
166 | }
167 | }
168 | #endregion Variable
169 |
170 | public partial class PrologEngine
171 | {
172 | #if mswindows
173 | #region Batch Processing
174 | public bool ProcessArgs(string[] args, bool windowsMode)
175 | {
176 | if (args.Length == 0) return false;
177 |
178 | // Process command line arguments. First is a query, optional second
179 | // arg is number of backtrack attempts (default is 0, 'infinite' = *)
180 | // If the first argument contains spaces, it must be enclosed in double quotes.
181 | // Example: pld "['path.pl'], solve( p(2,2), L)" 4
182 | string command = args[0].Dequoted().Trim();
183 | Query = command + (command.EndsWith(".") ? null : "."); // append a dot if necessary
184 | int solutionCount = 0; // i.e. find all solutions (backtrack until failure; value corresponds to '*')
185 | string msg = null;
186 |
187 | if (args.Length > 2)
188 | msg = string.Format("Superfluous argument '{0}'", args[2]);
189 | else if (args.Length == 2 && !(int.TryParse(args[1], out solutionCount) || args[1] == "*"))
190 | msg = string.Format("Illegal value '{0}' for maximum number of solutions", args[1]);
191 | else
192 | solutionCount = 1;
193 |
194 | if (msg == null)
195 | {
196 | int i = 0;
197 | bool found = false; // true if at least one solution found
198 |
199 | foreach (PrologEngine.ISolution s in SolutionIterator)
200 | {
201 | if (Error || (!found && !s.Solved)) // only an immediate 'no' give rise to an error
202 | {
203 | msg = s.ToString();
204 |
205 | break;
206 | }
207 |
208 | if (++i == solutionCount) break;
209 |
210 | found = true;
211 | }
212 | }
213 |
214 | if (msg != null)
215 | {
216 | if (windowsMode)
217 | MessageBox.Show(msg);
218 | else
219 | Console.WriteLine(msg);
220 |
221 | Environment.ExitCode = 1; // sets DOS ERRORLEVEL to 1
222 | }
223 |
224 | return true;
225 | }
226 | #endregion Batch Processing
227 | #endif
228 |
229 | #region GetAllSolutionsXml
230 | // Store solutions in an xml structure
231 | #if !NETSTANDARD
232 | public string GetAllSolutionsXml(string sourceFileName, string destinFileName, string query)
233 | {
234 | return GetAllSolutionsXml(sourceFileName, destinFileName, query, 0);
235 | }
236 |
237 | public string GetAllSolutionsXml(string sourceFileName, string destinFileName, string query, int maxSolutionCount)
238 | {
239 | XmlTextWriter xtw = null;
240 | StreamWriter sw = null;
241 |
242 | if (destinFileName == null)
243 | xtw = new XmlTextWriter(new MemoryStream(), System.Text.Encoding.GetEncoding(1252));
244 | else
245 | xtw = new XmlTextWriter(destinFileName, null);
246 |
247 | xtw.Formatting = Formatting.Indented;
248 | xtw.WriteStartDocument();
249 |
250 | try
251 | {
252 | if (sourceFileName != null)
253 | Reset();
254 |
255 | if (sourceFileName != null)
256 | Consult(sourceFileName);
257 |
258 | Query = query + (query.EndsWith(".") ? null : "."); // append a dot if necessary
259 | int i = 0;
260 | bool found = false; // true if at least one solution found
261 | bool firstSol = true;
262 |
263 | foreach (PrologEngine.ISolution s in SolutionIterator)
264 | {
265 | if (Error)
266 | {
267 | xtw.WriteStartElement("error");
268 | xtw.WriteCData(s.ToString());
269 | xtw.WriteEndElement();
270 |
271 | break;
272 | }
273 | else if (!found && !s.Solved)
274 | {
275 | xtw.WriteStartElement("solutions");
276 | xtw.WriteAttributeString("success", "false");
277 | xtw.WriteStartElement("query");
278 | xtw.WriteString(query);
279 | xtw.WriteEndElement();
280 |
281 | break;
282 | }
283 |
284 | if (firstSol)
285 | {
286 | firstSol = false;
287 | xtw.WriteStartElement("solutions");
288 | xtw.WriteAttributeString("success", "true");
289 | xtw.WriteStartElement("query");
290 | xtw.WriteString(query);
291 | xtw.WriteEndElement();
292 | }
293 |
294 | bool firstVar = true;
295 |
296 | foreach (PrologEngine.IVarValue varValue in s.VarValuesIterator)
297 | {
298 | if (varValue.DataType == "none") break;
299 |
300 | if (firstVar)
301 | {
302 | firstVar = false;
303 | xtw.WriteStartElement("solution");
304 | }
305 |
306 | xtw.WriteStartElement("variable");
307 | xtw.WriteAttributeString("name", varValue.Name);
308 | xtw.WriteAttributeString("type", varValue.DataType);
309 | xtw.WriteString(varValue.Value.ToString());
310 | xtw.WriteEndElement(); // variable
311 | }
312 |
313 | if (!firstVar) xtw.WriteEndElement(); // solution
314 |
315 | if (++i == maxSolutionCount) break;
316 |
317 | found = true;
318 | }
319 |
320 | if (!Error) xtw.WriteEndElement(); // solutions
321 |
322 | xtw.WriteEndDocument();
323 | xtw.Flush();
324 |
325 | if (destinFileName == null)
326 | {
327 | if (xtw.BaseStream == null) return null;
328 |
329 | return new ASCIIEncoding().GetString(((MemoryStream)xtw.BaseStream).ToArray());
330 | }
331 | else
332 | return null;
333 | }
334 | finally
335 | {
336 | if (xtw != null) xtw.Close();
337 | if (sw != null) sw.Close();
338 | }
339 | }
340 | #endif
341 | #endregion GetAllSolutionsXml
342 |
343 |
344 | #region GetAllSolutions
345 | // Store solutions in an GetAllSolutions class
346 | public SolutionSet GetAllSolutions(string sourceFileName, string query)
347 | {
348 | return GetAllSolutions(sourceFileName, query, 0);
349 | }
350 |
351 | public SolutionSet GetAllSolutions(string sourceFileName, string query, int maxSolutionCount)
352 | {
353 |
354 | SolutionSet solutions = new SolutionSet();
355 |
356 | try
357 | {
358 | if (sourceFileName != null) Reset();
359 |
360 | if (sourceFileName != null) Consult(sourceFileName);
361 |
362 | Query = solutions.Query = query + (query.EndsWith(".") ? null : "."); // append a dot if necessary
363 | int i = 0;
364 | bool found = false;
365 | bool varFound = false;
366 |
367 | foreach (PrologEngine.ISolution s in SolutionIterator)
368 | {
369 | if (Error)
370 | {
371 | solutions.ErrMsg = s.ToString();
372 |
373 | break;
374 | }
375 | else if (!found && !s.Solved)
376 | break;
377 |
378 | solutions.Success = true;
379 | bool firstVar = true;
380 |
381 | foreach (PrologEngine.IVarValue varValue in s.VarValuesIterator)
382 | {
383 | if (varValue.DataType == "none") break;
384 |
385 | if (firstVar)
386 | {
387 | firstVar = false;
388 | solutions.CreateVarSet();
389 | }
390 |
391 | solutions.AddToVarSet(varValue.Name, varValue.DataType, varValue.Value.ToString());
392 | varFound = true;
393 | }
394 |
395 | if (++i == maxSolutionCount || !varFound) break;
396 |
397 | found = true;
398 | }
399 | }
400 | catch (Exception e)
401 | {
402 | solutions.ErrMsg = e.Message;
403 | }
404 |
405 | return solutions;
406 | }
407 | #endregion GetAllSolutions
408 | }
409 | }
410 |
--------------------------------------------------------------------------------
/CSProlog/IO.cs:
--------------------------------------------------------------------------------
1 | /*-----------------------------------------------------------------------------------------
2 |
3 | C#Prolog -- Copyright (C) 2007-2015 John Pool -- j.pool@ision.nl
4 |
5 | This library is free software; you can redistribute it and/or modify it under the terms of
6 | the GNU Lesser General Public License as published by the Free Software Foundation; either
7 | version 3.0 of the License, or any later version.
8 |
9 | This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 | See the GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl-3.0.html), or
12 | enter 'license' at the command prompt.
13 |
14 | -------------------------------------------------------------------------------------------*/
15 |
16 | using System;
17 | using System.IO;
18 | using System.Text;
19 |
20 | namespace Prolog
21 | {
22 | #if NETSTANDARD
23 | using ApplicationException = System.Exception;
24 | #endif
25 |
26 | #region BasicIo
27 | public abstract class BasicIo
28 | {
29 | #region abstract methods
30 | public abstract string ReadLine();
31 |
32 | public abstract int ReadChar();
33 |
34 | public abstract void Write(string s);
35 |
36 | public abstract void WriteLine(string s);
37 |
38 | public abstract void WriteLine();
39 |
40 | public abstract void Clear();
41 |
42 | public abstract void Reset();
43 | #endregion abstract methods
44 |
45 | public void Write(string s, params object[] o)
46 | {
47 | Write(string.Format(s, o));
48 | }
49 |
50 | public void WriteLine(string s, params object[] o)
51 | {
52 | WriteLine(string.Format(s, o));
53 | }
54 | }
55 | #endregion BasicIo
56 |
57 | #region DosIO. Base class for DOS IO
58 | public class DosIO : BasicIo
59 | {
60 | public override string ReadLine()
61 | {
62 | return Console.ReadLine();
63 | }
64 |
65 |
66 | public override int ReadChar()
67 | {
68 | return Console.ReadKey().KeyChar;
69 | }
70 |
71 |
72 | public override void Write(string s)
73 | {
74 | Console.Write(s);
75 | }
76 |
77 |
78 | public override void WriteLine(string s)
79 | {
80 | Console.WriteLine(s);
81 | }
82 |
83 |
84 | public override void WriteLine()
85 | {
86 | Console.WriteLine();
87 | }
88 |
89 |
90 | public override void Clear()
91 | {
92 | Console.Clear();
93 | }
94 |
95 |
96 | public override void Reset()
97 | {
98 | }
99 | }
100 | #endregion DosIO
101 |
102 | #region FileIO. Base class for text File IO
103 | public class FileIO : BasicIo
104 | {
105 | StreamReader inFile;
106 | StreamWriter outFile;
107 |
108 | public FileIO(string inFileName, string outFileName)
109 | {
110 | // no try/catch, as I would not know how to handle the exception caught
111 | inFile = new StreamReader(File.OpenRead(inFileName));
112 | outFile = new StreamWriter(File.OpenWrite(outFileName));
113 | outFile.AutoFlush = true; // file will contain all output even if not closed explicitly
114 | }
115 |
116 | public override string ReadLine()
117 | {
118 | if (inFile == null)
119 | throw new ApplicationException("FileIO class: input file is not defined");
120 |
121 | return inFile.ReadLine();
122 | }
123 |
124 |
125 | public override int ReadChar()
126 | {
127 | if (inFile == null)
128 | throw new ApplicationException("FileIO class: input file is not defined");
129 |
130 | return inFile.Read();
131 | }
132 |
133 |
134 | public override void Write(string s)
135 | {
136 | if (outFile == null)
137 | throw new ApplicationException("FileIO class: output file is not defined");
138 |
139 | outFile.Write(s);
140 | }
141 |
142 |
143 | public override void WriteLine(string s)
144 | {
145 | if (outFile == null)
146 | throw new ApplicationException("FileIO class: output file is not defined");
147 |
148 | outFile.WriteLine(s);
149 | }
150 |
151 |
152 | public override void WriteLine()
153 | {
154 | if (outFile == null)
155 | throw new ApplicationException("FileIO class: output file is not defined");
156 |
157 | outFile.WriteLine();
158 | }
159 |
160 |
161 | public override void Clear()
162 | {
163 | }
164 |
165 |
166 | public override void Reset()
167 | {
168 | }
169 |
170 |
171 | public void Close()
172 | {
173 | if (inFile != null) inFile.Dispose();
174 |
175 | if (outFile != null) outFile.Dispose();
176 | }
177 | }
178 | #endregion FileIO
179 |
180 | public partial class PrologEngine
181 | {
182 | FileReaderTerm currentFileReader;
183 | FileWriterTerm currentFileWriter;
184 | public BasicIo BasicIO { set { IO.BasicIO = value; } }
185 |
186 | #region BaseRead(Line/Term/Char)CurrentInput and BaseWriteCurrentOutput
187 | // BaseReadCurrentInput. Input is read from StandardInput.
188 | // StandardInput is the file set by the see command, or Console if no such file exists.
189 | string BaseReadLineCurrentInput() // returns null at end of file
190 | {
191 | return (currentFileReader == null) ? IO.ReadLine() : currentFileReader.ReadLine();
192 | }
193 |
194 |
195 | BaseTerm BaseReadTermCurrentInput()
196 | {
197 | if (currentFileReader == null)
198 | {
199 | StringBuilder query = new StringBuilder();
200 | string line;
201 | PrologParser p = new PrologParser(this);
202 |
203 | bool first = true;
204 |
205 | while (true)
206 | {
207 | IO.Write("|: ");
208 |
209 | if (first) first = false; else query.AppendLine();
210 |
211 | if ((line = IO.ReadLine()) == null) return FileTerm.END_OF_FILE;
212 |
213 | query.Append(line = line.Trim());
214 |
215 | if (line.EndsWith(".")) break;
216 | }
217 |
218 | p.StreamIn = "&reading\r\n" + query.ToString(); // equal to parser ReadingSym
219 | BaseTerm result = p.ReadTerm;
220 |
221 | return (result == null) ? FileTerm.END_OF_FILE : result;
222 | }
223 |
224 | return currentFileReader.ReadTerm();
225 | }
226 |
227 |
228 | int BaseReadCharCurrentInput() // returns -1 at end of file
229 | {
230 | return (currentFileReader == null) ? IO.ReadChar() : currentFileReader.ReadChar();
231 | }
232 |
233 |
234 | // BaseWriteCurrentOutput
235 | // Output from print, display, tab, put etc. is written to StandardOutput.
236 | // StandardOutput is the file set by the tell command, or Console if
237 | // no such file exists.
238 | void BaseWriteCurrentOutput(string s)
239 | {
240 | if (currentFileWriter == null)
241 | IO.Write(s);
242 | else
243 | currentFileWriter.Write(s);
244 | }
245 |
246 |
247 | void BaseWriteCurrentOutput(string s, object[] args)
248 | {
249 | BaseWriteCurrentOutput(string.Format(s, args));
250 | }
251 | #endregion BaseRead(Line/Term/Char)CurrentInput and BaseWriteCurrentOutput
252 |
253 |
254 | #region Read(Line/Char) and Write(Line)
255 | BaseTerm ReadTerm()
256 | {
257 | return BaseReadTermCurrentInput();
258 | }
259 |
260 |
261 | string ReadLine()
262 | {
263 | return BaseReadLineCurrentInput();
264 | }
265 |
266 |
267 | int ReadChar()
268 | {
269 | return BaseReadCharCurrentInput();
270 | }
271 |
272 |
273 | void Write(BaseTerm t, bool dequote)
274 | {
275 | if (t.IsString)
276 | BaseWriteCurrentOutput(dequote ? t.FunctorToString : '"' + t.FunctorToString + '"');
277 | else if (t.IsAtom)
278 | BaseWriteCurrentOutput(dequote ? t.FunctorToString.Dequoted("'") : t.FunctorToString);
279 | else
280 | BaseWriteCurrentOutput(t.ToString());
281 | }
282 |
283 |
284 | public void Write(string s)
285 | {
286 | BaseWriteCurrentOutput(s);
287 | }
288 |
289 |
290 | public void Write(string s, params object[] args)
291 | {
292 | BaseWriteCurrentOutput(s, args);
293 | }
294 |
295 |
296 | public void WriteLine(string s)
297 | {
298 | BaseWriteCurrentOutput(s + Environment.NewLine);
299 | }
300 |
301 |
302 | public void WriteLine(string s, params object[] args)
303 | {
304 | BaseWriteCurrentOutput(s + Environment.NewLine, args);
305 | }
306 |
307 |
308 | public void NewLine()
309 | {
310 | BaseWriteCurrentOutput(Environment.NewLine);
311 | }
312 | #endregion Read(Line/Char) and Write(Line)
313 |
314 |
315 | // for IO *not* generated by Prolog predicates and not subject to
316 | // current input and current output (i.e. error messages etc.)
317 | public static class IO
318 | {
319 | static BasicIo basicIO;
320 | public static BasicIo BasicIO { get { return basicIO; } set { basicIO = value; } }
321 | public static bool Verbose = true; // message display
322 | static int debugLevel = 0;
323 |
324 | public static void SetDebugLevel(int level)
325 | {
326 | debugLevel = level;
327 | }
328 |
329 | public static bool Error(string msg, params object[] o)
330 | {
331 | Error(String.Format(msg, o));
332 |
333 | return false;
334 | }
335 |
336 | public static bool Error(string msg)
337 | {
338 | if (Globals.LineNo == -1) // interactive
339 | throw new ParserException("\r\n*** error: " + msg);
340 | else
341 | throw new ParserException(String.Format("\r\n*** error in line {0} at position {1}: {2}",
342 | Globals.LineNo, Globals.ColNo, msg));
343 | }
344 |
345 |
346 | public static void Warning(string msg, params object[] o)
347 | {
348 | BasicIO.WriteLine(string.Format("\r\n*** warning: " + msg, o));
349 | }
350 |
351 |
352 | public static void Warning(string msg)
353 | {
354 | BasicIO.WriteLine("\r\n*** warning: " + msg);
355 | }
356 |
357 |
358 | public static void Message(string msg, params object[] o)
359 | {
360 | BasicIO.WriteLine(string.Format("\r\n--- " + msg, o));
361 | }
362 |
363 |
364 | public static void Message(string msg)
365 | {
366 | BasicIO.WriteLine("\r\n--- " + msg);
367 | }
368 |
369 |
370 | public static void Fatal(string msg, params object[] o)
371 | {
372 | throw new Exception("\r\n*** fatal: " + String.Format(msg, o));
373 | }
374 |
375 |
376 | public static void Fatal(string msg)
377 | {
378 | throw new Exception("\r\n*** fatal: " + msg);
379 | }
380 |
381 |
382 | public static string ReadLine()
383 | {
384 | return BasicIO.ReadLine();
385 | }
386 |
387 |
388 | public static int ReadChar()
389 | {
390 | return BasicIO.ReadChar();
391 | }
392 |
393 |
394 | public static void Write(string s, params object[] o)
395 | {
396 | BasicIO.Write(string.Format(s, o));
397 | }
398 |
399 |
400 | public static void Write(string s)
401 | {
402 | BasicIO.Write(s);
403 | }
404 |
405 |
406 | public static void WriteLine(string s, params object[] o)
407 | {
408 | BasicIO.WriteLine(string.Format(s, o));
409 | }
410 |
411 |
412 | public static void WriteLine(string s)
413 | {
414 | BasicIO.WriteLine(s);
415 | }
416 |
417 |
418 | public static void WriteLine()
419 | {
420 | BasicIO.WriteLine();
421 | }
422 |
423 |
424 | public static void ClearScreen()
425 | {
426 | BasicIO.Clear();
427 | }
428 | }
429 | }
430 | }
431 |
--------------------------------------------------------------------------------
/CSProlog/Internet.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Net;
3 | using System.Net.Mail;
4 | using System.Security.Cryptography.X509Certificates;
5 |
6 | namespace Prolog
7 | {
8 | public partial class PrologEngine
9 | {
10 | #region engine-mail
11 | static bool SendMail(string smtpHost, int port, string toAddr, string subject, string body)
12 | {
13 | try
14 | {
15 | SmtpClient client = new SmtpClient(smtpHost, port);
16 | MailAddress from = new MailAddress("xxxxxx@xxxxxx.xx");
17 | MailAddress to = new MailAddress(toAddr);
18 | MailMessage msg = new MailMessage();
19 | msg.From = from;
20 | msg.To.Add(to);
21 | msg.Subject = subject;
22 | msg.Body = body;
23 | //client.EnableSsl = true;
24 | //ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy ();
25 | client.Send(msg);
26 | //client.SendAsync (msg, userState);
27 |
28 | //Attachment attachment = new Attachment ("you attachment file");
29 | //msg.Attachments.Add (attachment);
30 |
31 | return true;
32 | }
33 | catch (Exception x)
34 | {
35 | IO.Warning("Unable to send message. Reason was:\r\n{0}", x.Message);
36 |
37 | return false;
38 | }
39 | }
40 |
41 |
42 | class AcceptAllCertificatePolicy : ICertificatePolicy
43 | {
44 | public AcceptAllCertificatePolicy()
45 | {
46 | }
47 |
48 | public bool CheckValidationResult(ServicePoint sPoint,
49 | X509Certificate cert, WebRequest wRequest, int certProb)
50 | {
51 | // Always accept
52 | return true;
53 | }
54 | }
55 | #endregion engine-mail
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/CSProlog/Miscellaneous.cs:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jsakamoto/CSharpProlog/d30f1bb334b44413f5e338672feffb715bc1ab37/CSProlog/Miscellaneous.cs
--------------------------------------------------------------------------------
/CSProlog/PG/JSON.cs:
--------------------------------------------------------------------------------
1 | //#define LL1_tracing
2 | namespace Prolog
3 | {
4 | using System;
5 | using System.IO;
6 | using System.Text;
7 | using System.Xml;
8 | using System.Collections;
9 | using System.Collections.Generic;
10 | using System.Collections.Specialized;
11 | using System.Globalization;
12 | using System.Threading;
13 | using System.Diagnostics;
14 |
15 | /* _______________________________________________________________________________________________
16 | | |
17 | | C#Prolog -- Copyright (C) 2007 John Pool -- j.pool@ision.nl |
18 | | |
19 | | This library is free software; you can redistribute it and/or modify it under the terms of |
20 | | the GNU General Public License as published by the Free Software Foundation; either version |
21 | | 2 of the License, or any later version. |
22 | | |
23 | | This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
24 | | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
25 | | See the GNU General Public License for details, or enter 'license.' at the command prompt. |
26 | |_______________________________________________________________________________________________|
27 | */
28 |
29 | // Parser Generator version 4.0 -- Date/Time: 10-Feb-14 08:40:11
30 |
31 |
32 | public partial class PrologEngine
33 | {
34 | #region JsonParser
35 | public partial class JsonParser : BaseParser