creating a list of head variables for a rule
This commit is contained in:
parent
2102767367
commit
210cae7ca6
@ -195,11 +195,11 @@ withFactsAndRules facts rules =
|
||||
toConstraintElement :: RuleElement -> ConstraintElement
|
||||
toConstraintElement (RuleElementConstant constant) = ConstraintElementConstant constant
|
||||
toConstraintElement (RuleElementVariable varName) = ConstraintElementIndex (varIndex varName)
|
||||
extractVarName :: RuleElement -> Text
|
||||
extractVarName (RuleElementVariable varName) = varName
|
||||
extractVarName (RuleElementConstant constant) = throw $ UnexpectedConstantException constant
|
||||
extractVarName :: RuleElement -> Maybe Text
|
||||
extractVarName (RuleElementVariable varName) = Just varName
|
||||
extractVarName (RuleElementConstant constant) = Nothing
|
||||
newRule = RelationRule {
|
||||
headVariables = extractVarName <$> variables',
|
||||
headVariables = catMaybes $ extractVarName <$> variables',
|
||||
bodyElements = fromBodyConstraint <$> (_bodyConstraints context'')
|
||||
} where
|
||||
fromBodyConstraint :: BodyConstraint -> RuleBodyElement
|
||||
|
||||
@ -22,8 +22,6 @@ import qualified Data.Map as Map
|
||||
spec :: Spec
|
||||
spec = do
|
||||
describe "NaiveDatabase operations" $ do
|
||||
it "..." $ do
|
||||
1 `shouldBe` (1 :: Int)
|
||||
it "can ingest facts into relations & a universe" $ do
|
||||
let db = NaiveDatabase.withFacts
|
||||
[ "parent(\"alice\", \"bob\")."
|
||||
@ -79,6 +77,28 @@ spec = do
|
||||
("parent", parentRelation )
|
||||
]
|
||||
|
||||
it "can ingest facts and rules with constants" $ do
|
||||
let db = NaiveDatabase.withFactsAndRules
|
||||
[]
|
||||
[ "ancestor(X,\"patriarch\") :- ." ]
|
||||
let ancestorRule = RelationRule {
|
||||
headVariables = [ "X" ],
|
||||
bodyElements = []
|
||||
}
|
||||
let ancestorRelation = Relation {
|
||||
_arity = 2,
|
||||
_name = "ancestor",
|
||||
_tuples = Set.empty,
|
||||
_rules = [ ancestorRule ]
|
||||
}
|
||||
relations db `shouldBe`
|
||||
Map.fromList [
|
||||
("ancestor", ancestorRelation)
|
||||
]
|
||||
|
||||
constants db `shouldBe`
|
||||
(Set.fromList $ Sym <$> ["patriarch"])
|
||||
|
||||
it "can do basic queries" $ do
|
||||
let db = NaiveDatabase.withFacts
|
||||
[ "parent(\"alice\", \"bob\")."
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user