0

다른 조리법에 대한 조리법이 많고 주방에 다양한 재료가 포함 된 라디에이터가 있다고 가정 해보십시오. core.logic을 사용하여 모델을 구성하여 다음 질문에 답할 수있게 해드립니다. 주어진 재료 세트 (즉, 현재 내 라더에있는 재료)에 대해 어떤 요리법을 만들 수 있습니까?구속 조건 논리로 조리법 및 사용 가능한 재료 모델링

조리법은 다소 유연하며이를 모델링 할 수 있어야합니다. 나중에 수량을 추가하고 싶지만 시작하기 위해 잠시 남겨 두자.

나는 고깃간을 모델링하는 방법을 볼 수 있습니다

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

조리법은 이름과 선택 사양 일 또는 다양한 방법으로 결합 할 수 성분의 목록을 가지고있다. n 조리법이 있습니다. 예를 들어 조리법은 다음과 같이 (비공식적으로) 보일 수 있습니다 : 나는 core.logic이를 표현하는 방법에 고심하고

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

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

. (NB 텍스트는 위의 단지 예시 및 기계 판독 수 없습니다.) 내가 쿼리가 다음과 같이 보일 것입니다 상상

: 다음과 같은 결과를 반환

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

합니다 (고깃간 정의 제공 위의) :

(: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))) 

조리법 각 레시피 재료의 목록이 있습니다

답변

1

다음은이 문제를 모델링하는 하나의 방법입니다. 각 성분은 단일 성분 또는 복합 성분 일 수 있으며,이 경우 선택 성분 또는 필수 성분을 함유 할 수 있습니다.

우리는 그것이 작동하도록 좀 더 관계가 필요합니다

(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

+0

멋진 대답입니다 :

(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)])) 

이제 우리는 조리법을 얻기 위해 다른 larders로 조회 할 수 있습니다 ! 나는 조리법에서 불리언 표현을 임의적으로 복잡하게 만드는 방법을 생각해 내려고 노력했다. 당신이 해준 답은 깨끗한 표현 방식으로 깔끔하게 해결됩니다. –

관련 문제