Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Commit

Permalink
Add permissibility function
Browse files Browse the repository at this point in the history
Finding the possible calls seems broken though
  • Loading branch information
RamonMeffert committed Nov 22, 2020
1 parent c0c3c9c commit 7b9344d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/elm/CallSequence/CallSequence.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ module CallSequence.CallSequence exposing (..)
import List exposing (head)
import GossipGraph.Agent exposing (AgentId)
import GossipGraph.Call exposing (Call, includes)
import GossipGraph.Relation exposing (Kind(..))
import GossipGraph.Relation as Relation exposing (Kind(..))
import Graph
import IntDict
import List.Extra exposing (mapAccumr)
import GossipGraph.Call as Call


{-| A list of consecutive calls. Ordered latest to first call to improve lookup speed.
Expand All @@ -25,4 +29,4 @@ containing sequence agent =
call :: containing calls agent

else
containing calls agent
containing calls agent
59 changes: 51 additions & 8 deletions src/elm/GossipProtocol/GossipProtocol.elm
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module GossipProtocol.GossipProtocol exposing (..)

import Graph exposing (Graph, NodeContext, fold)
import GossipGraph.Agent exposing (Agent, AgentId)
import GossipGraph.Call exposing (Call)
import CallSequence.CallSequence exposing (CallSequence)
import GossipGraph.Relation as Relation exposing (Kind(..), Relation)
import GossipGraph.Agent exposing (Agent, AgentId)
import GossipGraph.Call as Call exposing (Call)
import GossipGraph.Relation as Relation exposing (Kind(..), Relation, knows)
import Graph exposing (Graph, NodeContext)
import IntDict
import List.Extra exposing (mapAccumr)


{-| Protocol conditions
Expand All @@ -18,8 +20,8 @@ type alias ProtocolCondition =
Select x, y ∈ A, such that x ≠ y, Nxy, and π(x, y)
-}
select : Graph Agent Relation -> ProtocolCondition -> CallSequence -> List Call
select graph condition sequence =
selectCalls : Graph Agent Relation -> ProtocolCondition -> CallSequence -> List Call
selectCalls graph condition sequence =
let
calls : NodeContext Agent Relation -> List Call -> List Call
calls context acc =
Expand All @@ -28,7 +30,7 @@ select graph condition sequence =
localRelations =
-- select x, y ∈ A, such that x /= y, Nxy
Relation.fromNodeContext context
|> List.filter (\{ from, to } -> from /= to )
|> List.filter (\{ from, to } -> from /= to)

-- the protocol condition needs a pair (x,y)
relationPairs =
Expand All @@ -38,9 +40,50 @@ select graph condition sequence =
-- check π(x, y)
-- the resulting list of calls is the list of calls that can be executed on G given the call history
List.filter (\x -> condition x localRelations sequence) relationPairs
|> List.map GossipGraph.Call.fromTuple
|> List.map Call.fromTuple
|> (++) acc
in
Graph.fold calls [] graph


{-| Given a call sequence, a graph and a protocol condition, figure out whether
the call sequence is permitted.
Call sequence σ;xy is P-permitted on G iff σ is P-permitted on G and xy is P-permitted on Gσ
-}
sequencePermittedOn : ProtocolCondition -> Graph Agent Relation -> CallSequence -> Bool
sequencePermittedOn condition graph sequence =
let
relations : Graph Agent Relation -> List Relation
relations g =
Graph.edges g
|> List.map .label

isCallPermitted : Call -> Graph Agent Relation -> CallSequence -> Bool
isCallPermitted { from, to } currentGraph callHistory =
let
rels =
relations currentGraph
in
from
/= to
-- N^σ xy
&& List.any (\r -> knows from to Number r) rels
-- π(x, y)
&& condition ( from, to ) rels callHistory
in
-- results in a tuple of the form ((Bool, (CallSequence, Graph Agent Relation)), List Graph Agent Relation)
-- Since the empty call sequence is allowed, start with true
-- while traversing the call sequence, we need to keep track of
-- - the calls that have occured so far
-- - the current state of the graph
-- - whether the current call was permitted
List.foldr (\call (history, state, permitted) ->
(call :: history
, Call.execute state call
, permitted && isCallPermitted call state history
)
) ([], graph, True) sequence
|> Debug.log "result idk"
|> \(_, _, isPermitted) -> isPermitted
11 changes: 10 additions & 1 deletion src/elm/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Json.Decode as Json
import GossipGraph.Parser
import CallSequence.CallSequence



Expand Down Expand Up @@ -232,6 +233,14 @@ callSequenceView model =
model.callSequence
model.agents
)
, div [] [
case (model.graph, model.callSequence) of
(Ok graph, Ok sequence) ->
GossipProtocol.sequencePermittedOn model.protocolCondition graph sequence
|> (\b -> text <| "This sequence is " ++ (if b then "" else "not") ++ " permitted in protocol " ++ model.protocolName)
_ ->
text <| "This sequence is not permitted in protocol " ++ model.protocolName
]
]


Expand Down Expand Up @@ -263,7 +272,7 @@ protocolView model =
(Ok sequence, Ok agents, Ok graph) ->
let
calls =
GossipProtocol.select graph model.protocolCondition sequence
GossipProtocol.selectCalls graph model.protocolCondition sequence
in
if List.isEmpty calls then
[ text "No more calls are possible." ]
Expand Down

2 comments on commit 7b9344d

@RamonMeffert
Copy link
Owner Author

@RamonMeffert RamonMeffert commented on 7b9344d Nov 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finding the possible calls seems broken though

Actually it isn't, it's just that no calls are possible on the graph A B C. I must've been tired when I was testing this

@RamonMeffert
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, permissibility actually doesn't work fully yet:
Screenshot_2020-11-23 Tools for Gossip

Obviously, on ANY, this call sequence should be allowed. I'm looking into why the algorithm doesn't get that

Please sign in to comment.