Skip to content

Commit

Permalink
Added new tests, support for additional belief category (generalised)…
Browse files Browse the repository at this point in the history
… in event store, and added ui support for the latter
  • Loading branch information
TonyLo1 committed Feb 14, 2019
1 parent 4485aa6 commit d6da425
Show file tree
Hide file tree
Showing 11 changed files with 2,320 additions and 1,881 deletions.
4 changes: 3 additions & 1 deletion ALANNStreams/ALANNStreams.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@
<Compile Include="System\Controller.fs" />
<Compile Include="Tests\TermFormatters\TermFormattersTests.fs" />
<Compile Include="Tests\Unify\UniifyTests.fs" />
<Compile Include="Tests\Inference\InferenceTests.fs" />
<Compile Include="Tests\Inference\FirstOrderInfernceTests.fs" />
<Compile Include="Tests\Inference\HigherOrderInferenceTests.fs" />
<Compile Include="Tests\Evidence\EvidenceTests.fs" />
<Compile Include="Tests\TermUtils\TermUtilsTests.fs" />
<Compile Include="Tests\Parser\ParserTests.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions ALANNStreams/Commands/CommandProcessor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ let showSuperBeliefs term =
showBeliefs (List.sortBy (fun b -> -exp(b.TV)) [for b in node.Beliefs.GetSuperBeliefs() -> b])
| _ -> printCommand "ERROR *** TERM DOES NOT EXIST ***"

let showVariableBeliefs term =
match getNodeFromTerm term with
| (true, node) ->
printCommandWithString "SHOW_VARIABLE_BELIEFS FOR TERM" (ft term)
showBeliefs (List.sortBy (fun b -> -exp(b.TV)) [for b in node.Beliefs.GetVariableBeliefs() -> b])
| _ -> printCommand "ERROR *** TERM DOES NOT EXIST ***"

let showNode term =
match getNodeFromTerm term with
| (true, node) ->
Expand Down Expand Up @@ -141,6 +148,7 @@ let processCommand (cmd : string) =
| Show_General_Beliefs(term) -> showGeneralBeliefs term
| Show_Temporal_Beliefs(term) -> showTemporalBeliefs term
| Show_Super_Beliefs(term) -> showSuperBeliefs term
| Show_Variable_Beliefs(term) -> showVariableBeliefs term
| Show_Node(term) -> showNode(term)
| Node_Count -> nodeCount()
| Enable_Trace(term) -> enableTrace term
Expand Down
16 changes: 15 additions & 1 deletion ALANNStreams/Containers/Store.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ type Store(hostTerm, generalCapacity, temporalCapacity, superCapacity ) =
let temporalStore = new SubStore(temporalCapacity) :> ISubStore
let GeneralStore = new SubStore(generalCapacity) :> ISubStore
let superStore = new SubStore(superCapacity) :> ISubStore
let variableStore = new SubStore(superCapacity) :> ISubStore

interface IStore with
member x.Contains(key) =
if key |> isSuperTerm hostTerm then
superStore.Contains key
else if key |> containsVars then
variableStore.Contains key
else if key |> isTemporal then
temporalStore.Contains key
else
Expand All @@ -46,6 +49,8 @@ type Store(hostTerm, generalCapacity, temporalCapacity, superCapacity ) =
member x.Insert(key, belief) =
if key |> isSuperTerm hostTerm then
superStore.Insert(key, belief)
else if key |> containsVars then
variableStore.Insert(key, belief)
else if key |> isTemporal then
temporalStore.Insert(key, belief)
else
Expand All @@ -54,6 +59,8 @@ type Store(hostTerm, generalCapacity, temporalCapacity, superCapacity ) =
member x.Update(key, belief) =
if key |> isSuperTerm hostTerm then
superStore.Update(key, belief)
else if key |> containsVars then
variableStore.Update(key, belief)
else if key |> isTemporal then
temporalStore.Update(key, belief)
else
Expand All @@ -62,6 +69,8 @@ type Store(hostTerm, generalCapacity, temporalCapacity, superCapacity ) =
member x.TryGetValue key =
if key |> isSuperTerm hostTerm then
superStore.TryGetValue key
else if key |> containsVars then
variableStore.TryGetValue key
else if key |> isTemporal then
temporalStore.TryGetValue key
else
Expand All @@ -71,19 +80,24 @@ type Store(hostTerm, generalCapacity, temporalCapacity, superCapacity ) =
superStore.Clear()
temporalStore.Clear()
GeneralStore.Clear()
variableStore.Clear()

member x.Count = temporalStore.Count + GeneralStore.Count
member x.Count = temporalStore.Count + GeneralStore.Count + superStore.Count + variableStore.Count

member x.GetBeliefs() =
Seq.append
(superStore.GetBeliefs())
(temporalStore.GetBeliefs())
|> Seq.append
(GeneralStore.GetBeliefs())
|> Seq.append
(variableStore.GetBeliefs())

member x.GetSuperBeliefs() = superStore.GetBeliefs()

member x.GetTemporalBeliefs() = temporalStore.GetBeliefs()

member x.GetGeneralBeliefs() = GeneralStore.GetBeliefs()

member x.GetVariableBeliefs() = variableStore.GetBeliefs()

74 changes: 37 additions & 37 deletions ALANNStreams/Inference/HigherOrderInference.fs
Original file line number Diff line number Diff line change
Expand Up @@ -339,64 +339,64 @@ let nal5_multi_conditional_syllogism : InferenceFunction = function
| _ -> []

let nal6_variable_introduction = function
| Inh(s, m1), Inh(p, m2) when m1 = m2 && s <> p -> [(Term(Imp, [Term(Inh, [p; Var(IVar, "X")]); Term(Inh, [s; Var(IVar, "X")])]), abd, None, [BeliefOnly])
(Term(Imp, [Term(Inh, [s; Var(IVar, "X")]); Term(Inh, [p; Var(IVar, "X")])]), ind, None, [BeliefOnly])
(Term(Equ, [Term(Inh, [p; Var(IVar, "X")]); Term(Inh, [s; Var(IVar, "X")])]), com, None, [BeliefOnly])
(Term(And, [Term(Inh, [p; Var(DVar, "Y")]); Term(Inh, [s; Var(DVar, "Y")])]), int, None, [BeliefOnly])
| Inh(s, m1), Inh(p, m2) when m1 = m2 && s <> p -> [(Term(Imp, [Term(Inh, [p; Var(IVar, "1")]); Term(Inh, [s; Var(IVar, "1")])]), abd, None, [BeliefOnly])
(Term(Imp, [Term(Inh, [s; Var(IVar, "1")]); Term(Inh, [p; Var(IVar, "1")])]), ind, None, [BeliefOnly])
(Term(Equ, [Term(Inh, [p; Var(IVar, "1")]); Term(Inh, [s; Var(IVar, "1")])]), com, None, [BeliefOnly])
(Term(And, [Term(Inh, [p; Var(DVar, "1")]); Term(Inh, [s; Var(DVar, "1")])]), int, None, [BeliefOnly])

(Term(ConImp, [Term(Inh, [p; Var(IVar, "X")]); Term(Inh, [s; Var(IVar, "X")])]), abd, None, [BeliefOnly; IsConcurrent])
(Term(ConImp, [Term(Inh, [s; Var(IVar, "X")]); Term(Inh, [p; Var(IVar, "X")])]), ind, None, [BeliefOnly; IsConcurrent])
(Term(ConImp, [Term(Inh, [p; Var(IVar, "X")]); Term(Inh, [s; Var(IVar, "X")])]), ind, None, [BeliefOnly; IsConcurrent])
(Term(Par, [Term(Inh, [p; Var(DVar, "Y")]); Term(Inh, [s; Var(DVar, "Y")])]), int, None, [BeliefOnly; IsConcurrent])
(Term(ConImp, [Term(Inh, [p; Var(IVar, "1")]); Term(Inh, [s; Var(IVar, "1")])]), abd, None, [BeliefOnly; IsConcurrent])
(Term(ConImp, [Term(Inh, [s; Var(IVar, "1")]); Term(Inh, [p; Var(IVar, "1")])]), ind, None, [BeliefOnly; IsConcurrent])
(Term(ConImp, [Term(Inh, [p; Var(IVar, "1")]); Term(Inh, [s; Var(IVar, "1")])]), ind, None, [BeliefOnly; IsConcurrent])
(Term(Par, [Term(Inh, [p; Var(DVar, "1")]); Term(Inh, [s; Var(DVar, "")])]), int, None, [BeliefOnly; IsConcurrent])

(Term(PreImp, [Term(Inh, [s; Var(IVar, "X")]); Term(Inh, [p; Var(IVar, "X")])]), abd, None, [BeliefOnly; IsBefore])
(Term(RetImp, [Term(Inh, [p; Var(IVar, "X")]); Term(Inh, [s; Var(IVar, "X")])]), ind, None, [BeliefOnly; IsBefore])
(Term(Seq, [Term(Inh, [s; Var(DVar, "Y")]); Term(Inh, [p; Var(DVar, "Y")])]), int, None, [BeliefOnly; IsBefore])
(Term(PreImp, [Term(Inh, [s; Var(IVar, "1")]); Term(Inh, [p; Var(IVar, "1")])]), abd, None, [BeliefOnly; IsBefore])
(Term(RetImp, [Term(Inh, [p; Var(IVar, "1")]); Term(Inh, [s; Var(IVar, "1")])]), ind, None, [BeliefOnly; IsBefore])
(Term(Seq, [Term(Inh, [s; Var(DVar, "1")]); Term(Inh, [p; Var(DVar, "1")])]), int, None, [BeliefOnly; IsBefore])

(Term(PreImp, [Term(Inh, [s; Var(IVar, "X")]); Term(Inh, [p; Var(IVar, "X")])]), abd, None, [BeliefOnly; IsAfter])
(Term(RetImp, [Term(Inh, [p; Var(IVar, "X")]); Term(Inh, [s; Var(IVar, "X")])]), ind, None, [BeliefOnly; IsAfter])
(Term(Seq, [Term(Inh, [s; Var(DVar, "Y")]); Term(Inh, [p; Var(DVar, "Y")])]), int, None, [BeliefOnly; IsAfter])]
(Term(PreImp, [Term(Inh, [s; Var(IVar, "1")]); Term(Inh, [p; Var(IVar, "1")])]), abd, None, [BeliefOnly; IsAfter])
(Term(RetImp, [Term(Inh, [p; Var(IVar, "1")]); Term(Inh, [s; Var(IVar, "1")])]), ind, None, [BeliefOnly; IsAfter])
(Term(Seq, [Term(Inh, [s; Var(DVar, "1")]); Term(Inh, [p; Var(DVar, "1")])]), int, None, [BeliefOnly; IsAfter])]



// interval variants ommitted here as not required

| Inh(m1, s), Inh(m2, p) when m1 = m2 && s <> p -> [(Term(Imp, [Term(Inh, [Var(IVar, "X"); s]); Term(Inh, [Var(IVar, "X"); p])]), ind, None, [BeliefOnly])
(Term(Imp, [Term(Inh, [Var(IVar, "X"); p]); Term(Inh, [Var(IVar, "X"); s])]), abd, None, [BeliefOnly])
(Term(Equ, [Term(Inh, [Var(IVar, "X"); s]); Term(Inh, [Var(IVar, "X"); p])]), com, None, [BeliefOnly])
(Term(And, [Term(Inh, [Var(DVar, "Y"); s]); Term(Inh, [Var(DVar, "Y"); p])]), int, None, [BeliefOnly])
| Inh(m1, s), Inh(m2, p) when m1 = m2 && s <> p -> [(Term(Imp, [Term(Inh, [Var(IVar, "1"); s]); Term(Inh, [Var(IVar, "1"); p])]), ind, None, [BeliefOnly])
(Term(Imp, [Term(Inh, [Var(IVar, "1"); p]); Term(Inh, [Var(IVar, "1"); s])]), abd, None, [BeliefOnly])
(Term(Equ, [Term(Inh, [Var(IVar, "1"); s]); Term(Inh, [Var(IVar, "1"); p])]), com, None, [BeliefOnly])
(Term(And, [Term(Inh, [Var(DVar, "1"); s]); Term(Inh, [Var(DVar, "1"); p])]), int, None, [BeliefOnly])

(Term(ConImp, [Term(Inh, [Var(IVar, "X"); s]); Term(Inh, [Var(IVar, "X"); p])]), ind, None, [BeliefOnly; IsConcurrent])
(Term(ConImp, [Term(Inh, [Var(IVar, "X"); p]); Term(Inh, [Var(IVar, "X"); s])]), abd, None, [BeliefOnly; IsConcurrent])
(Term(ConImp, [Term(Inh, [Var(IVar, "X"); s]); Term(Inh, [Var(IVar, "X"); p])]), ind, None, [BeliefOnly; IsConcurrent])
(Term(ConEqu, [Term(Inh, [Var(IVar, "X"); s]); Term(Inh, [Var(IVar, "X"); p])]), com, None, [BeliefOnly; IsConcurrent])
(Term(Par, [Term(Inh, [Var(DVar, "Y"); s]); Term(Inh, [Var(DVar, "Y"); p])]), int, None, [BeliefOnly; IsConcurrent])
(Term(ConImp, [Term(Inh, [Var(IVar, "1"); s]); Term(Inh, [Var(IVar, "1"); p])]), ind, None, [BeliefOnly; IsConcurrent])
(Term(ConImp, [Term(Inh, [Var(IVar, "1"); p]); Term(Inh, [Var(IVar, "1"); s])]), abd, None, [BeliefOnly; IsConcurrent])
(Term(ConImp, [Term(Inh, [Var(IVar, "1"); s]); Term(Inh, [Var(IVar, "1"); p])]), ind, None, [BeliefOnly; IsConcurrent])
(Term(ConEqu, [Term(Inh, [Var(IVar, "1"); s]); Term(Inh, [Var(IVar, "1"); p])]), com, None, [BeliefOnly; IsConcurrent])
(Term(Par, [Term(Inh, [Var(DVar, "1"); s]); Term(Inh, [Var(DVar, "1"); p])]), int, None, [BeliefOnly; IsConcurrent])

(Term(PreImp, [Term(Inh, [Var(IVar, "X"); s]); Term(Inh, [Var(IVar, "X"); p])]), ind, None, [BeliefOnly; IsBefore])
(Term(RetImp, [Term(Inh, [Var(IVar, "X"); p]); Term(Inh, [Var(IVar, "X"); s])]), abd, None, [BeliefOnly; IsBefore])
(Term(Seq, [Term(Inh, [Var(IVar, "X"); s]); Term(Inh, [Var(IVar, "X"); p])]), ind, None, [BeliefOnly; IsBefore])
(Term(PreImp, [Term(Inh, [Var(IVar, "1"); s]); Term(Inh, [Var(IVar, "1"); p])]), ind, None, [BeliefOnly; IsBefore])
(Term(RetImp, [Term(Inh, [Var(IVar, "1"); p]); Term(Inh, [Var(IVar, "1"); s])]), abd, None, [BeliefOnly; IsBefore])
(Term(Seq, [Term(Inh, [Var(IVar, "1"); s]); Term(Inh, [Var(IVar, "1"); p])]), ind, None, [BeliefOnly; IsBefore])

(Term(PreImp, [Term(Inh, [Var(IVar, "X"); s]); Term(Inh, [Var(IVar, "X"); p])]), ind, None, [BeliefOnly; IsAfter])
(Term(RetImp, [Term(Inh, [Var(IVar, "X"); p]); Term(Inh, [Var(IVar, "X"); s])]), abd, None, [BeliefOnly; IsAfter])
(Term(Seq, [Term(Inh, [Var(IVar, "X"); s]); Term(Inh, [Var(IVar, "X"); p])]), ind, None, [BeliefOnly; IsAfter])]
(Term(PreImp, [Term(Inh, [Var(IVar, "1"); s]); Term(Inh, [Var(IVar, "1"); p])]), ind, None, [BeliefOnly; IsAfter])
(Term(RetImp, [Term(Inh, [Var(IVar, "1"); p]); Term(Inh, [Var(IVar, "1"); s])]), abd, None, [BeliefOnly; IsAfter])
(Term(Seq, [Term(Inh, [Var(IVar, "1"); s]); Term(Inh, [Var(IVar, "1"); p])]), ind, None, [BeliefOnly; IsAfter])]


| _ -> []

let nal6_variable_syllogisms = function
| Imp(Inh(a, r1), z1), Imp(And([Inh(Var(DVar, "Y"), b); Inh(Var(DVar, "Y"), r2)]), z2) when r1 = r2 && z1 = z2 && a <> b -> [Term(Inh, [a; b]), abd, None, []]
| Imp(Inh(a, r1), z1), Imp(And([Inh(Var(DVar, "Y"), r2); Inh(Var(DVar, "Y"), b)]), z2) when r1 = r2 && z1 = z2 && a <> b -> [Term(Inh, [a; b]), abd, None, []]
| Imp(Inh(a, r1), z1), Imp(And([Inh(Var(DVar, "1"), b); Inh(Var(DVar, "1"), r2)]), z2) when r1 = r2 && z1 = z2 && a <> b -> [Term(Inh, [a; b]), abd, None, []]
| Imp(Inh(a, r1), z1), Imp(And([Inh(Var(DVar, "1"), r2); Inh(Var(DVar, "1"), b)]), z2) when r1 = r2 && z1 = z2 && a <> b -> [Term(Inh, [a; b]), abd, None, []]

| Inh(u, l1), Imp(And([Inh(Var(DVar, "Y"), l2); Inh(Var(DVar, "Y"), r)]), z) when l1 = l2 && u <> z && z <> l1 -> [(Term(Imp, [Term(Inh, [u; r]); z]), ded, None, [])]
| Inh(u, l1), Imp(And([Inh(Var(DVar, "Y"), r); Inh(Var(DVar, "Y"), l2)]), z) when l1 = l2 && u <> z && z <> l1 -> [(Term(Imp, [Term(Inh, [u; r]); z]), ded, None, [])]
| Inh(u, l1), Imp(And([Inh(Var(DVar, "1"), l2); Inh(Var(DVar, "1"), r)]), z) when l1 = l2 && u <> z && z <> l1 -> [(Term(Imp, [Term(Inh, [u; r]); z]), ded, None, [])]
| Inh(u, l1), Imp(And([Inh(Var(DVar, "1"), r); Inh(Var(DVar, "1"), l2)]), z) when l1 = l2 && u <> z && z <> l1 -> [(Term(Imp, [Term(Inh, [u; r]); z]), ded, None, [])]

| _ -> []

let nal6_multiple_variable_introduction = function
| Imp(a, Inh(m1, p)), Inh(m2, s) when m1 = m2 && s <> p && a <> Term(Inh, [m1; s]) -> [(Term(Imp, [Term(And, [a; Term(Inh, [Var(IVar, "X"); s])]); Term(Inh, [Var(IVar, "X"); p])]), ind, None, [])
(Term(And, [Term(Imp, [a; Term(Inh, [Var(DVar, "Y"); p])]); Term(Inh, [Var(DVar, "Y"); s])]), int, None, [])]
| And(Inh(m1, p)::lst), Inh(m2, s) when m1 = m2 && s <> p -> [(Term(Imp, [Term(Inh, [Var(IVar, "Y"); s]); Term(And, Term(Inh, [Var(IVar, "Y"); p])::lst)]), ind, None, [])
(Term(And, Term(Inh, [Var(DVar, "Y"); s])::Term(Inh, [Var(DVar, "Y"); p])::lst), ind, None, [])]
| Imp(a, Inh(m1, p)), Inh(m2, s) when m1 = m2 && s <> p && a <> Term(Inh, [m1; s]) -> [(Term(Imp, [Term(And, [a; Term(Inh, [Var(IVar, "1"); s])]); Term(Inh, [Var(IVar, "1"); p])]), ind, None, [])
(Term(And, [Term(Imp, [a; Term(Inh, [Var(DVar, "1"); p])]); Term(Inh, [Var(DVar, "1"); s])]), int, None, [])]
| And(Inh(m1, p)::lst), Inh(m2, s) when m1 = m2 && s <> p -> [(Term(Imp, [Term(Inh, [Var(IVar, "1"); s]); Term(And, Term(Inh, [Var(IVar, "1"); p])::lst)]), ind, None, [])
(Term(And, Term(Inh, [Var(DVar, "1"); s])::Term(Inh, [Var(DVar, "1"); p])::lst), ind, None, [])]
| _ -> []

let nal6_variable_elimination = function
Expand Down
3 changes: 3 additions & 0 deletions ALANNStreams/Parser/CommandParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ let term_ws = temporal_term <|> pterm
let show_general_beliefs = (str_ws "SHOW_GENERAL_BELIEFS" <|> str_ws "SGB") >>. term_ws |>> fun t -> Show_General_Beliefs(t)
let show_temporal_beliefs = (str_ws "SHOW_TEMPORAL_BELIEFS" <|> str_ws "STB") >>. term_ws |>> fun t -> Show_Temporal_Beliefs(t)
let show_super_beliefs = (str_ws "SHOW_SUPER_BELIEFS" <|> str_ws "SSB") >>. term_ws |>> fun t -> Show_Super_Beliefs(t)
let show_variable_beliefs = (str_ws "SHOW_VARIABLE_BELIEFS" <|> str_ws "SVB") >>. term_ws |>> fun t -> Show_Variable_Beliefs(t)
let show_node = (str_ws "SHOW_NODE" <|> str_ws "SN") >>. term_ws |>> fun t -> Show_Node(t)


let node_count = (str_ws "NODE_COUNT" <|> str_ws "NC") |>> fun _ -> Node_Count
let enable_trace = (str_ws "ENABLE_TRACE" <|> str_ws "ET") >>. term_ws |>> fun t -> Enable_Trace(t)
let disable_trace = (str_ws "DISABLE_TRACE" <|> str_ws "DT") >>. term_ws |>> fun t -> Disable_Trace(t)
Expand All @@ -50,6 +52,7 @@ let reset = (str_ws "RESET" <|> str_ws "R") |>> fun _ -> Reset
let pcmd = show_general_beliefs
<|> show_temporal_beliefs
<|> show_super_beliefs
<|> show_variable_beliefs
<|> show_node
<|> node_count
<|> enable_trace
Expand Down
Loading

0 comments on commit d6da425

Please sign in to comment.