Skip to content

Commit 2d63fff

Browse files
authored
Merge pull request #1 from manu4rhyme/temp_test
Added aima.core.logic.propositional.parsing.ast
2 parents 204b7e5 + 0a70038 commit 2d63fff

File tree

6 files changed

+839
-0
lines changed

6 files changed

+839
-0
lines changed

aima-csharp/aima-csharp.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@
191191
<Compile Include="logic\fol\Unifier.cs" />
192192
<Compile Include="logic\fol\VariableCollector.cs" />
193193
<Compile Include="logic\propositional\agent\KBAgent.cs" />
194+
<Compile Include="logic\propositional\parsing\ast\AtomicSentence.cs" />
195+
<Compile Include="logic\propositional\parsing\ast\ComplexSentence.cs" />
196+
<Compile Include="logic\propositional\parsing\ast\Connective.cs" />
197+
<Compile Include="logic\propositional\parsing\ast\PropositionSymbol.cs" />
198+
<Compile Include="logic\propositional\parsing\ast\Sentence.cs" />
199+
<Compile Include="logic\propositional\parsing\PLVisitor.cs" />
194200
<Compile Include="obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs" />
195201
<Compile Include="obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs" />
196202
<Compile Include="obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs" />
@@ -263,6 +269,7 @@
263269
<ItemGroup>
264270
<Folder Include="bin\Release\" />
265271
<Folder Include="environment\wumpusworld\action\" />
272+
<Folder Include="logic\propositional\kb\" />
266273
<Folder Include="obj\Debug\TempPE\" />
267274
</ItemGroup>
268275
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
using aima.core.logic.propositional.parsing.ast;
2+
using System;
3+
4+
namespace aima.core.logic.propositional.parsing.ast
5+
{
6+
/**
7+
* Artificial Intelligence A Modern Approach (3rd Edition): page 244.<br>
8+
* <br>
9+
* <b>Complex Sentence:</b> are constructed from simpler sentences, using
10+
* parentheses (and square brackets) and logical connectives.
11+
*
12+
* @author Ciaran O'Reilly
13+
* @author Ravi Mohan
14+
*/
15+
public class ComplexSentence : Sentence
16+
{
17+
18+
19+
private Connective connective;
20+
private Sentence[] simplerSentences;
21+
// Lazy initialize these values.
22+
private int cachedHashCode = -1;
23+
private String cachedConcreteSyntax = null;
24+
25+
/**
26+
* Constructor.
27+
*
28+
* @param connective
29+
* the complex sentence's connective.
30+
* @param sentences
31+
* the simpler sentences making up the complex sentence.
32+
*/
33+
public ComplexSentence(Connective connective, params Sentence[] sentences)
34+
{
35+
// Assertion checks
36+
assertLegalArguments(connective, sentences);
37+
38+
this.connective = connective;
39+
simplerSentences = new Sentence[sentences.Length];
40+
for (int i = 0; i < sentences.Length; i++)
41+
{
42+
simplerSentences[i] = sentences[i];
43+
}
44+
}
45+
46+
/**
47+
* Convenience constructor for binary sentences.
48+
*
49+
* @param sentenceL
50+
* the left hand sentence.
51+
* @param binaryConnective
52+
* the binary connective.0
53+
12 * @param sentenceR
54+
* the right hand sentence.
55+
*/
56+
public ComplexSentence(Sentence sentenceL, Connective binaryConnective, Sentence sentenceR) : this(binaryConnective, sentenceL, sentenceR)
57+
{
58+
59+
}
60+
61+
public override Connective getConnective()
62+
{
63+
return connective;
64+
}
65+
66+
public override int getNumberSimplerSentences()
67+
{
68+
return simplerSentences.Length;
69+
}
70+
71+
public override Sentence getSimplerSentence(int offset)
72+
{
73+
return simplerSentences[offset];
74+
}
75+
76+
public override bool Equals(Object o)
77+
{
78+
if (this == o)
79+
{
80+
return true;
81+
}
82+
if ((o == null) || (this.GetType() != o.GetType()))
83+
{
84+
return false;
85+
}
86+
87+
bool result = false;
88+
ComplexSentence other = (ComplexSentence)o;
89+
if (other.GetHashCode() == this.GetHashCode())
90+
{
91+
if (other.getConnective().Equals(this.getConnective())
92+
&& other.getNumberSimplerSentences() == this
93+
.getNumberSimplerSentences())
94+
{
95+
// connective and # of simpler sentences match
96+
// assume match and then test each simpler sentence
97+
result = true;
98+
for (int i = 0; i < this.getNumberSimplerSentences(); i++)
99+
{
100+
if (!other.getSimplerSentence(i).Equals(
101+
this.getSimplerSentence(i)))
102+
{
103+
result = false;
104+
break;
105+
}
106+
}
107+
}
108+
}
109+
110+
return result;
111+
}
112+
113+
public override int GetHashCode()
114+
{
115+
if (cachedHashCode == -1)
116+
{
117+
cachedHashCode = 17 * getConnective().GetHashCode();
118+
foreach(Sentence s in simplerSentences)
119+
{
120+
cachedHashCode = (cachedHashCode * 37) + s.GetHashCode();
121+
}
122+
}
123+
124+
return cachedHashCode;
125+
}
126+
127+
public override String ToString()
128+
{
129+
if (cachedConcreteSyntax == null)
130+
{
131+
if (isUnarySentence())
132+
{
133+
cachedConcreteSyntax = getConnective()
134+
+ bracketSentenceIfNecessary(getConnective(), getSimplerSentence(0));
135+
}
136+
else if (isBinarySentence())
137+
{
138+
cachedConcreteSyntax = bracketSentenceIfNecessary(getConnective(), getSimplerSentence(0))
139+
+ " "
140+
+ getConnective()
141+
+ " "
142+
+ bracketSentenceIfNecessary(getConnective(), getSimplerSentence(1));
143+
}
144+
}
145+
return cachedConcreteSyntax;
146+
}
147+
148+
//
149+
// PRIVATE
150+
//
151+
private void assertLegalArguments(Connective connective, params Sentence[] sentences)
152+
{
153+
if (connective == null)
154+
{
155+
throw new ArgumentException("Connective must be specified.");
156+
}
157+
if (sentences == null)
158+
{
159+
throw new ArgumentException("> 0 simpler sentences must be specified.");
160+
}
161+
if (connective == Connective.NOT)
162+
{
163+
if (sentences.Length != 1)
164+
{
165+
throw new ArgumentException("A not (~) complex sentence only take 1 simpler sentence not " + sentences.Length);
166+
}
167+
}
168+
else
169+
{
170+
if (sentences.Length != 2)
171+
{
172+
throw new ArgumentException("Connective is binary (" + connective + ") but only " + sentences.Length + " simpler sentences provided");
173+
}
174+
}
175+
}
176+
}
177+
}
178+
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
using System;
2+
using aima.core.util;
3+
using System.Collections.Generic;
4+
5+
namespace aima.core.logic.propositional.parsing.ast
6+
{
7+
/**
8+
* Artificial Intelligence A Modern Approach (3rd Edition): page 244.<br>
9+
* <br>
10+
*
11+
* <pre>
12+
* <b>Logical Connectives:</b> There are five connectives in common use:
13+
* 1. ~ (not).
14+
* 2. & (and).
15+
* 3. | (or).
16+
* 4. => (implication).
17+
* 5. <=> (biconditional).
18+
*
19+
* Note: We use ASCII characters that commonly have the same meaning to those
20+
* symbols used in the book.
21+
*
22+
* OPERATOR PRECEDENCE: ~, &, |, =>, <=>
23+
* </pre>
24+
*
25+
* @author Avinash Agarwal
26+
*
27+
*/
28+
public class Connective
29+
{
30+
public static readonly Connective NOT = new Connective("~", 10);
31+
public static readonly Connective AND = new Connective("&", 8);
32+
public static readonly Connective OR = new Connective("|", 6);
33+
public static readonly Connective IMPLICATION = new Connective("=>", 4);
34+
public static readonly Connective BICONDITIONAL = new Connective("<=>", 2);
35+
36+
public static IEnumerable<Connective> Values
37+
{
38+
get
39+
{
40+
yield return NOT;
41+
yield return AND;
42+
yield return OR;
43+
yield return IMPLICATION;
44+
yield return BICONDITIONAL;
45+
}
46+
}
47+
48+
/**
49+
*
50+
* @return the symbol for this connective.
51+
*/
52+
public String getSymbol()
53+
{
54+
return symbol;
55+
}
56+
57+
/**
58+
*
59+
* @return the precedence associated with this connective.
60+
*/
61+
public int getPrecedence()
62+
{
63+
return precedence;
64+
}
65+
66+
public String toString()
67+
{
68+
return getSymbol();
69+
}
70+
71+
/**
72+
* Determine if a given symbol is representative of a connective.
73+
*
74+
* @param symbol
75+
* a symbol to be tested whether or not is represents a
76+
* connective.
77+
* @return true if the symbol passed in is representative of a connective.
78+
*/
79+
public static bool isConnective(String symbol)
80+
{
81+
if (NOT.getSymbol().Equals(symbol))
82+
{
83+
return true;
84+
}
85+
else if (AND.getSymbol().Equals(symbol))
86+
{
87+
return true;
88+
}
89+
else if (OR.getSymbol().Equals(symbol))
90+
{
91+
return true;
92+
}
93+
else if (IMPLICATION.getSymbol().Equals(symbol))
94+
{
95+
return true;
96+
}
97+
else if (BICONDITIONAL.getSymbol().Equals(symbol))
98+
{
99+
return true;
100+
}
101+
return false;
102+
}
103+
104+
/**
105+
* Get the connective associated with the given symbolic representation.
106+
*
107+
* @param symbol
108+
* a symbol for which a corresponding connective is wanted.
109+
* @return the connective associated with a given symbol.
110+
* @throws IllegalArgumentException
111+
* if a connective cannot be found that matches the given
112+
* symbol.
113+
*/
114+
public static Connective get(String symbol)
115+
{
116+
if (NOT.getSymbol().Equals(symbol))
117+
{
118+
return NOT;
119+
}
120+
else if (AND.getSymbol().Equals(symbol))
121+
{
122+
return AND;
123+
}
124+
else if (OR.getSymbol().Equals(symbol))
125+
{
126+
return OR;
127+
}
128+
else if (IMPLICATION.getSymbol().Equals(symbol))
129+
{
130+
return IMPLICATION;
131+
}
132+
else if (BICONDITIONAL.getSymbol().Equals(symbol))
133+
{
134+
return BICONDITIONAL;
135+
}
136+
137+
throw new ArgumentException(
138+
"Not a valid symbol for a connective: " + symbol);
139+
}
140+
141+
/**
142+
* Determine if the given character is at the beginning of a connective.
143+
*
144+
* @param ch
145+
* a character.
146+
* @return true if the given character is at the beginning of a connective's
147+
* symbolic representation, false otherwise.
148+
*/
149+
public static bool isConnectiveIdentifierStart(char ch)
150+
{
151+
return _connectiveLeadingChars.Contains(ch);
152+
}
153+
154+
/**
155+
* Determine if the given character is part of a connective.
156+
*
157+
* @param ch
158+
* a character.
159+
* @return true if the given character is part of a connective's symbolic
160+
* representation, false otherwise.
161+
*/
162+
public static bool isConnectiveIdentifierPart(char ch)
163+
{
164+
return _connectiveChars.Contains(ch);
165+
}
166+
167+
//
168+
// PRIVATE
169+
//
170+
private static readonly HashSet<char> _connectiveLeadingChars = Util.createSet('~', '&', '|', '=', '<');
171+
private static readonly HashSet<char> _connectiveChars = Util.createSet('~', '&', '|', '=', '<', '>');
172+
173+
private readonly int precedence;
174+
private readonly String symbol;
175+
176+
private Connective(String symbol, int precedence)
177+
{
178+
this.precedence = precedence;
179+
this.symbol = symbol;
180+
}
181+
182+
}
183+
184+
}

0 commit comments

Comments
 (0)