我正在尝试使用Haskell的hxt包解析COLLADA文件。
我一直很好,但我遇到了一个奇怪的错误(或者更可能是我的错误)。
我有一个看起来像这样的箭头:
processGeometry = proc x -> do
geometry <- atTag "geometry" -< x
meshID <- getAttrValue "id" -< geometry
meshName <- getAttrValue "name" -< geometry
mesh <- atTag "mesh" -< geometry
sources <- hasName "source" <<< getChildren -< mesh
positionSource <- hasAttrValue "id" ("-positions" `isSuffixOf`) -< sources
positionArray <- processFloatSource -< positionSource
returnA -< positionArray
添加行
normalSource <- hasAttrValue "id" ("-normals" `isSuffixOf`) -< sources
但是,靠近底部的会使整个箭头失败。
无论我返回什么,即使我返回原始x
,也会发生这种情况。
这是我的atTag
功能:
atTag tag = deep (isElem >>> hasName tag)
这是我试图解析的示例COLLADA文件: https://pastebin.com/mDSTH2TW
为什么添加一条线会完全改变箭头的结果,什么时候根本不应该做什么?
答案 0 :(得分:2)
TL; DR:如果您要查找两个单独的子元素,请使用单独的getChildren
调用。
您的变量sources
不代表所有源元素的列表。相反,它是单个源。如果您检查sources
的类型,则会看到XMLTree
。因此,当你对它使用hasAttrValue
两次时,你正在寻找一个匹配两种情况的单个源元素。
至于为什么返回什么并不重要:即使不使用其值,也会执行每一行。实际上,除非您使用输出,否则您甚至不必为其指定名称:仅hasAttrValue "id" (isSuffixOf "-normals") <- sources
行(删除normalSource <-
)的工作方式相同。因此,如果您返回x
,只有在找到不可能的源元素时,它才会返回x
。
您可以通过对getChildren
进行单独的两次调用来查找两个单独的源元素 - 每个单独的元素一个 - 并分别检查每个元素的“id”属性。
如果上述情况不明确,这是一个独立的例子。
data Tree a = Tree a [Tree a]
exampleTree :: Tree String
exampleTree = Tree "root" [Tree "childA" [], Tree "childB" []]
newtype ListArrow a b = ListArrow { runListArrow :: a -> [b] }
instance Category ListArrow where
id = ListArrow (\x -> [x])
(ListArrow g) . (ListArrow f) = ListArrow (\x -> concatMap g (f x))
instance Arrow ListArrow where
arr f = ListArrow (\x -> [f x])
first (ListArrow f) = ListArrow (\(a, b) -> [ (a', b) | a' <- f a ])
getChildren :: ListArrow (Tree a) (Tree a)
getChildren = ListArrow gc where
gc (Tree _ children) = children
hasContent :: Eq a => a -> ListArrow (Tree a) (Tree a)
hasContent content = ListArrow hc where
hc cur@(Tree c _) = if content == c then [cur] else []
getContent :: ListArrow (Tree a) a
getContent = ListArrow gc where
gc (Tree c _) = [c]
-- this has the same problem as the code in the question
findBothChildrenBad :: ListArrow (Tree String) (String, String)
findBothChildrenBad = proc root -> do
-- child is a (single) child of the root
child <- getChildren -< root
-- childA == child, and filter to only cases where its content is "childA"
childA <- hasContent "childA" -< child
-- childB == child, and filter to only cases where its content is "childB"
childB <- hasContent "childB" -< child
-- now the content has to be both "childA" and "childB" -- so we're stuck
childAContent <- getContent -< childA
childBContent <- getContent -< childB
returnA -< (childAContent, childBContent)
-- this is the fixed version
findBothChildren :: ListArrow (Tree String) (String, String)
findBothChildren = proc root -> do
-- childA is a (single) child of the root
childA <- getChildren -< root
-- filter to only cases where its content is "childA"
hasContent "childA" -< childA
-- childB is a (potentially different) child of the root
childB <- getChildren -< root
-- filter to only cases where its content is "childB"
hasContent "childB" -< childB
-- we're not stuck here
childAContent <- getContent -< childA
childBContent <- getContent -< childB
returnA -< (childAContent, childBContent)