Skip to content

Commit b1aa84e

Browse files
committed
Further additions to proof
1 parent 5b96b19 commit b1aa84e

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed

aima-csharp/aima-csharp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,12 @@
123123
<Compile Include="logic\fol\inference\LightestClauseHeuristic.cs" />
124124
<Compile Include="logic\fol\inference\proof\AbstractProofStep.cs" />
125125
<Compile Include="logic\fol\inference\proof\Proof.cs" />
126+
<Compile Include="logic\fol\inference\proof\ProofFinal.cs" />
126127
<Compile Include="logic\fol\inference\proof\ProofPrinter.cs" />
127128
<Compile Include="logic\fol\inference\proof\ProofStep.cs" />
128129
<Compile Include="logic\fol\inference\proof\ProofStepGoal.cs" />
130+
<Compile Include="logic\fol\inference\proof\ProofStepPremise.cs" />
131+
<Compile Include="logic\fol\inference\proof\ProofStepRenaming.cs" />
129132
<Compile Include="logic\fol\kb\data\Chain.cs" />
130133
<Compile Include="logic\fol\kb\data\Clause.cs" />
131134
<Compile Include="logic\fol\kb\data\CNF.cs" />
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using aima.core.logic.fol.parsing.ast;
4+
5+
namespace aima.core.logic.fol.inference.proof
6+
{
7+
/**
8+
* @author Ciaran O'Reilly
9+
*
10+
*/
11+
public class ProofFinal : Proof
12+
{
13+
private Dictionary<Variable, Term> answerBindings = new Dictionary<Variable, Term>();
14+
private ProofStep finalStep = null;
15+
private List<ProofStep> proofSteps = null;
16+
17+
public ProofFinal(ProofStep finalStep, Dictionary<Variable, Term> answerBindings)
18+
{
19+
this.finalStep = finalStep;
20+
this.answerBindings = answerBindings;
21+
}
22+
23+
// START-Proof
24+
25+
public List<ProofStep> getSteps()
26+
{
27+
// Only calculate if the proof steps are actually requested.
28+
if (null == proofSteps)
29+
{
30+
calculateProofSteps();
31+
}
32+
return proofSteps;
33+
}
34+
35+
public Dictionary<Variable, Term> getAnswerBindings()
36+
{
37+
return answerBindings;
38+
}
39+
40+
public void replaceAnswerBindings(Dictionary<Variable, Term> updatedBindings)
41+
{
42+
answerBindings.Clear();
43+
answerBindings = updatedBindings;
44+
}
45+
46+
// END-Proof
47+
48+
public override String ToString()
49+
{
50+
return answerBindings.ToString();
51+
}
52+
53+
54+
// PRIVATE METHODS
55+
56+
private void calculateProofSteps()
57+
{
58+
proofSteps = new List<ProofStep>();
59+
addToProofSteps(finalStep);
60+
61+
// Move all premises to the front of the
62+
// list of steps
63+
int to = 0;
64+
for (int i = 0; i < proofSteps.Count; i++)
65+
{
66+
if (proofSteps[i] is ProofStepPremise)
67+
{
68+
ProofStep m = proofSteps[i];
69+
proofSteps.RemoveAt(i);
70+
proofSteps.Insert(to, m);
71+
to++;
72+
}
73+
}
74+
75+
// Move the Goals after the premises
76+
for (int i = 0; i < proofSteps.Count; i++)
77+
{
78+
if (proofSteps[i] is ProofStepGoal)
79+
{
80+
ProofStep m = proofSteps[i];
81+
proofSteps.RemoveAt(i);
82+
proofSteps.Insert(to, m);
83+
to++;
84+
}
85+
}
86+
87+
// Assign the step #s now that all the proof
88+
// steps have been unwound
89+
for (int i = 0; i < proofSteps.Count; i++)
90+
{
91+
proofSteps[i].setStepNumber(i + 1);
92+
}
93+
}
94+
95+
private void addToProofSteps(ProofStep step)
96+
{
97+
if (!proofSteps.Contains(step))
98+
{
99+
proofSteps.Insert(0, step);
100+
}
101+
else
102+
{
103+
proofSteps.Remove(step);
104+
proofSteps.Insert(0, step);
105+
}
106+
List<ProofStep> predecessors = step.getPredecessorSteps();
107+
for (int i = predecessors.Count - 1; i >= 0; i--)
108+
{
109+
addToProofSteps(predecessors[i]);
110+
}
111+
}
112+
}
113+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Linq;
5+
6+
namespace aima.core.logic.fol.inference.proof
7+
{
8+
/**
9+
* @author Ciaran O'Reilly
10+
*
11+
*/
12+
public class ProofStepPremise : AbstractProofStep
13+
{
14+
private static readonly List<ProofStep> _noPredecessors = new List<ProofStep>();
15+
16+
private Object proof = "";
17+
18+
public ProofStepPremise(Object proof)
19+
{
20+
this.proof = proof;
21+
}
22+
23+
// START-ProofStep
24+
25+
public override List<ProofStep> getPredecessorSteps()
26+
{
27+
return new ReadOnlyCollection<ProofStep>(_noPredecessors).ToList<ProofStep>();
28+
}
29+
30+
public override String getProof()
31+
{
32+
return proof.ToString();
33+
}
34+
35+
public override String getJustification()
36+
{
37+
return "Premise";
38+
}
39+
40+
// END-ProofStep
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Linq;
5+
6+
namespace aima.core.logic.fol.inference.proof
7+
{
8+
/**
9+
* @author Ciaran O'Reilly
10+
*
11+
*/
12+
public class ProofStepRenaming : AbstractProofStep
13+
{
14+
private List<ProofStep> predecessors = new List<ProofStep>();
15+
private Object proof = "";
16+
17+
public ProofStepRenaming(Object proof, ProofStep predecessor)
18+
{
19+
this.proof = proof;
20+
this.predecessors.Add(predecessor);
21+
}
22+
23+
// START-ProofStep
24+
25+
public override List<ProofStep> getPredecessorSteps()
26+
{
27+
return new ReadOnlyCollection<ProofStep>(predecessors).ToList<ProofStep>();
28+
}
29+
30+
public override String getProof()
31+
{
32+
return proof.ToString();
33+
}
34+
35+
public override String getJustification()
36+
{
37+
return "Renaming of " + predecessors[0].getStepNumber();
38+
}
39+
40+
// END-ProofStep
41+
}
42+
}

0 commit comments

Comments
 (0)