Oxford
Oxford

Reputation: 35

How to construct components from build of materials

I have the following predicates that models a PC. What I want is to print all pc compoments and sub-compoments.

I want to do something like this

find_set_of_attributes(X):-
 print(X)
 print(X[power)
 print(X[power][usb])

pc([power).
power([usb, voltageregulator]).
usb([container, usbhead, pins]).
voltageregulator([barreljack, capacitor, regulator, leds]).

Upvotes: 1

Views: 54

Answers (1)

Paulo Moura
Paulo Moura

Reputation: 18663

One way to describe the computer components and sub-components is to use Definite Clause Grammars (DCGs). For example:

computer --> monitor, cpu, keyboard, mouse.
...

power --> usb, voltageregulator.

usb --> container, usbhead, pins.

voltageregulator --> barreljack, capacitor, regulator, leds.

Components without sub-components can be defined as:

capacitor --> [capacitor].
leds --> [leds].

To list all the parts (e.g for printing), you can then simply use the de facto standard phrase/2 predicate. For example:

| ?- phrase(computer, Parts).

Upvotes: 1

Related Questions