gpt4 book ai didi

data-structures - Prolog - 使用术语来表示和访问复杂的嵌套数据

转载 作者:行者123 更新时间:2023-12-04 15:10:30 25 4
gpt4 key购买 nike

my_computer([
case([
motherboard([board(plastic),ports(metal),slots(plastic),capacitors(plastic)]),
power_supply_unit([casing(metal),cables(plastic),connectors(plastic),capacitors(plastic),fan(plastic),transformer(metal)]),
central_processing_unit([board(plastic),fan(plastic),heatsink(metal)]),
random_access_memory([board(plastic)]),
graphics_processing_unit([board(plastic),ports(metal),capacitors(plastic),fan(plastic),heatsink(metal)])
]),
monitor([
lcd_screen(plastic),inverter(plastic),frame(plastic)
]),
keyboard([
key(plastic),frame(plastic),cable(plastic)
]),
mouse([
key(plastic),wheel(plastic),casing(plastic),cable(plastic)
])
]).

我应该怎么做才能运行类似 monitor(X). 的问题或 motherboard(X)提供一层或所有层(子) Material (如 my_computer(X). 会做)?

下面的代码对于问这样的问题会更有用吗?通过这种方式可以轻松回答有关一层子 Material 的问题。
my_computer([case,monitor,keyboard,mouse]).
case([motherboard,power_supply_unit,central_processing_unit,random_access_memory,graphics_processing_unit]).
motherboard([board,ports,slots,capacitors]).
power_supply_unit([casing,cables,connectors,capacitors,fan,transformer]).
central_processing_unit([board,fan,heatsink]).
random_access_memory([board]).
graphics_processing_unit([board,ports,capacitors,fan,heatsink]).
monitor([lcd_screen,inverter,frame]).
keyboard(keys,frame,cable).
mouse([keys,wheel,casing,cable]).

最佳答案

对您的问题的简短回答是:

monitor(X) :-
my_computer([_, monitor(X), _, _]).

keyboard 类似或 mousemotherboard会更深一层:
motherboard(X) :-
my_computer([case([motherboard(X), _, _, _, _), _, _, _]).

这些谓词当然采用固定结构。如果您想要更通用一点,您可以对嵌入式仿函数( monitormotherboard 等)进行更精细的“搜索”。

根据您更广泛的应用程序目标,我不清楚这是数据的最佳表示。现在已经足够好了,但上下文可能希望将其带入不同的方向。

这是另一种方法,将数据视为暗示树关系的单个事实。基本上只是 has关系。将“重大”事实分开为 material(Item, Type) :
item(my_computer, case).
item(my_computer, monitor).
item(my_computer, keyboard).
item(my_computer, mouse).

item(case, motherboard).
item(case, power_supply_unit).
item(case, central_processing_unit).
item(case, random_access_memory).
item(case, graphics_processing_unit).

item(motherboard, board).
item(motherboard, ports).
item(motherboard, slots).
item(motherboard, capacitors).

item(power_supply_unit, casing).
item(power_supply_unit, cable).
item(power_supply_unit, connectors).
item(power_supply_unit, capacitors).
item(power_supply_unit, fan).
item(power_supply_unit, transformer).

item(central_processing_unit, board).
item(central_processing_unit, fan).
item(central_processing_unit, heatsink).

item(random_access_memory, board).

item(graphics_processing_unit, board).
item(graphics_processing_unit, ports).
item(graphics_processing_unit, capacitors).
item(graphics_processing_unit, fan).
item(graphics_processing_unit, heatsink).

item(monitor, lcd_screen).
item(monitor, inverter).
item(monitor, frame).

item(keyboard, key).
item(keyboard, frame).
item(keyboard, cable).

item(mouse, key).
item(mouse, wheel).
item(mouse, casing).
item(mouse, cable).

material(board, plastic).
material(slots, plastic).
material(capacitors, plastic).
material(ports, metal).
material(casing, metal).
material(cable, plastic).
material(connectors, plastic).
material(fan, plastic).
material(heatsink, metal).
material(lcd_screen, plastic).
material(inverter, plastic).
material(frame, plastic).
material(key, plastic).
material(cable, plastic).

然后你可以定义一个谓词来为你想要的任何级别生成树。这是一个以术语(不是列表)形式执行的示例:
structure(Item, Structure) :-
( item(Item, _)
-> findall(T, (item(Item, R), structure(R, T)), Rs),
Structure =.. [Item |Rs]
; Structure = Item
).

那么:
:- structure(case, S).
S = case(motherboard(board,ports,slots,capacitors),
power_supply_unit(casing,cable,connectors,capacitors,fan,transformer),
central_processing_unit(board,fan,heatsink),
random_access_memory(board),
graphics_processing_unit(board,ports,capacitors,fan,heatsink)
)

这可以很容易地更改为以列表形式提供结果。例如,这里有一个谓词,它采用上述事实并给出您最初在问题中提出的形式:
structure(Item, Tree) :-
( item(Item, _)
-> findall(T, (item(Item, R), structure(R, T)), Rs),
Tree =.. [Item, Rs]
; material(Item, Material),
Tree =.. [Item, Material]
).

item成为 where_used(Item, Parent) 的微不足道的结果谓词:
where_used(Item, Parent) :-
item(Parent, Item).

同样,这完全取决于您希望如何使用和管理数据。

关于data-structures - Prolog - 使用术语来表示和访问复杂的嵌套数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28700602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com