Dustin
Dustin

Reputation: 143

Is it possible to have an instance of multiple classes in CLIPS?

I have a class structure from an OWL ontology that I'm looking to convert into CLIPS knowledge to perform closed-world reasoning. My existing class hierarchy has instances that are types of multiple classes. For example, I'd like to do the following in CLIPS:

CLIPS> (defclass A (is-a USER))
CLIPS> (defclass B (is-a USER))
CLIPS> (bind ?x (make-instance fact1 of A B))
[fact1]
CLIPS> (type ?x)
A B

I know this isn't possible (see next example).

CLIPS> (make-instance fact1 of A B)

[PRNTUTIL2] Syntax Error:  Check appropriate syntax for slot-override.

Is it possible to make an instance have multiple types, where one type is unrelated from another type (i.e. not a parent or child type of the other types). If not, any advice would be appreciated. I'm guessing if this isn't possible, it could be done with new defclass that has parents of type A and type B. If taking this route, would making a rule that automatically creates an anonymous type that has both of these as parents be considered good style? Or should I just fundamentally alter the class structure?

Hope that was clear. Any help/advice is appreciated.

Upvotes: 1

Views: 443

Answers (1)

noxdafox
noxdafox

Reputation: 15060

In OOP, you have two ways to "combine" objects: multiple inheritance or composition. In the first, you use a class C which inherits from A and B. In the latter, you create a generic object C, which contains references from objects generated from A and from B.

For multiple inheritance, you can check the chapter 9.3.1 Multiple Inheritance of the Basic Programming Guide to see how to implement it in CLIPS.

In practice, you need to specify the multiple types within the defclass construct.

(defclass A (is-a USER)) 
(defclass B (is-a USER))
(defclass C (is-a A B))

(make-instance c of C)

For composition, you can store in the C class either the instance addresses of A and B or their name. In other words, you make an instance the result of the composition of other two.

(defclass A (is-a USER))                                                                                                                                                                                    
(defclass B (is-a USER))                                                                                                                                                                                    
(defclass C (is-a USER) 
  (slot a) 
  (slot b))     

(make-instance c of C 
  (a (make-instance a of A)) 
  (b (make-instance b of B)))

The type of an object is always inferred by its class. You can't create objects without a class definition.

Upvotes: 2

Related Questions