使用约束逻辑对食谱和可用成分进行建模

Modelling recipes and available ingredients with constraint logic

想象一下,我的厨房里有许多不同菜肴的食谱和一个装有各种食材的储藏室。我想使用 core.logic 构建一个模型,这将使​​我能够回答以下问题:对于一组给定的成分(即现在我储藏室中的成分),我可以制作哪些食谱?

食谱有些灵活,我需要能够对此进行建模。稍后我想给它们添加数量,但为了开始,让我们暂时忽略它。

我可以看到如何为储藏室建模:

(db-rel in-larder x)
(def larder (db
             [in-larder :carrots]
             [in-larder :rice]
             [in-larder :garlic]))

食谱有一个名称和一个成分列表,这些成分可以是可选的,也可以以各种方式组合。有 n 个食谱。例如,食谱可能看起来(非正式地)像这样:

Risotto A
=========
(carrots OR peas)
rice
(onions OR garlic)

Risotto B
=========
((carrots AND onions)) OR (rice AND peas))
garlic

我正在考虑如何在 core.logic 中表达这一点。 (N.B。上面的文字只是说明性的,并不是机器可读的。)

我想查询看起来像这样:

(with-dbs [larder recipes] (run* [q] (possible-recipe q)))

这将 return 以下结果(给定上面的 larder 定义):

(:risotto-a :risotto-b)

我的问题是:如何为这些食谱建模,以便我可以针对食谱和储藏室编写查询,以根据储藏室的当前内容列出可能的食谱名称?

这是对此问题建模的一种方法:

(db-rel in-larder i)
(db-rel recipe r)
(db-rel in-recipe r i)
(db-rel compound-ingredient i is)

(def recipes (db
               [compound-ingredient :carrots-or-peas [:or :carrots :peas]]
               [compound-ingredient :onions-or-garlic [:or :onions :garlic]]
               [compound-ingredient :carrots-and-onions [:and :carrots :onions]]
               [compound-ingredient :rice-and-peas [:and :rice :peas]]
               [compound-ingredient :carrots-onions-or-rice-peas [:or :carrots-and-onions :rice-and-peas]]  
               [recipe :risotto-a]
               [recipe :risotto-b]
               [in-recipe :risotto-a [:carrots-or-peas :rice :onions-or-garlic]]
               [in-recipe :risotto-b [:garlic :carrots-onions-or-rice-peas]]))

(defn possible-recipe [r]
  (recipe r)
  (fresh [ingredients]
         (in-recipe r ingredients)
         (all-ingredients-in-lardero ingredients)))

每个食谱都有食谱和配料表。每种成分可以是单一成分或复合成分,在这种情况下,它可以具有可选成分或强制成分。

我们需要更多的关系来让它工作:

(defne any-ingredient-in-lardero [ingredients]
  ([[?i . ?morei]] (conda [(ingredient-in-lardero ?i)]
                          [(emptyo ?morei) fail]
                          [(any-ingredient-in-lardero ?morei)])))

(defne all-ingredients-in-lardero [ingredients]
  ([[?i . ?morei]]
   (ingredient-in-lardero ?i)
   (conda [(emptyo ?morei)]
          [(all-ingredients-in-lardero ?morei)])))

(defn ingredient-in-lardero [i]
  (conde        
    [(fresh [composition op sub-ingredients]
            (compound-ingredient i composition)
            (conso op sub-ingredients composition)

            (conde
              [(== :or op) (any-ingredient-in-lardero sub-ingredients)]
              [(== :and op) (all-ingredients-in-lardero sub-ingredients)]))]
    [(in-larder i)]))

现在我们可以查询不同的储藏室来获取食谱:

(def larder-1 (db [in-larder :carrots] [in-larder :rice] [in-larder :garlic])) 
(def larder-2 (db [in-larder :peas]    [in-larder :rice] [in-larder :garlic]))

(with-dbs [recipes larder-1]
  (run* [q]
        (possible-recipe q)))
;=> (:risotto-a)

(with-dbs [recipes larder-2]
  (run* [q]
        (possible-recipe q)))
;=> (:risotto-a :risotto-b)

完整代码在this gist