CGLIB
A
Constraint-based Graphics Library for B-Prolog
(Version 6.5)
By Neng-Fa Zhou
Copyright © Afany Software,
2000-2003
The widespread use of window systems has made a graphics package
indispensable for any programming languages. Prolog is not an exception,
although graphics is not part of the ISO standard. Some efforts have been made
to introduce graphics programming into Prolog. One widely used approach is to
adopt an external language interface with a language that supports graphics
programming. For example, JIPL is an interface developed by KLS Inc. that
bridges Prolog and Java bi-directionally. A Prolog system that adopts an interface like this will be able to access the graphics library of Java. This approach is
not satisfactory for the following two reasons. First, you have to write code in two languages. This is especially daunting when there
are interactions involved. Second, the graphics library in the adopted
language, whether it is C, C++, Java, VB, or Tcl/Tk, is at such a low level
that it does not match well with a high-level language like Prolog. Prolog, and
CLP (Constraint Logic Programming) languages in general, should have a graphics
library that provides high-level abstraction for specifying the states,
layouts, and behaviors of graphical objects.
Motivated by the observations, we designed and implemented a high-level
and constraint-based graphics library, called CGLIB, for B-Prolog. The
library provides primitives on graphical objects and provides various kinds of
constraints that facilitate the specification of layouts of graphical objects.
The constraint solver of B-Prolog serves as a general-purpose layout manager
that is significantly more flexible than the special-purpose layout managers
used in Java and Tck/Tk. The library adopts a construct called action rules available in B-Prolog for
programming interactions. An action rule consists of a pattern for agents, a
pattern for events that can activate the agents, and an action the agents carry
out when activated. Agents can communicate with each other through logical
variables or global variables. Agents can behave concurrently and sequentially
as well.
CGLIB
enables you to use the Java graphics package without the need to write any code
in Java. CGLIB, however, is not just another syntactical sugar for Java’s
graphics package. It has a significantly higher abstraction level and is much
easier to learn and use than Java’s graphics package. One of prominent features
of CGLIB is the use of constraints in the specification of layouts. In graphics languages such as PostScript and
Tex, you have to
specify every single detail about the graphical objects, including their sizes
and positions. In languages such as Java and Tcl/Tk, the layout managers help
determine the layouts of objects for certain applications, but the layout
algorithms lack flexibility and are difficult to learn and use. The use of
action rules to describe event handling is another feature of CGLIB. Action
rules can be used to describe sophisticated and dynamic behaviors of graphical
objects in a simple and straightforward manner.
CGLIB
can be used in many areas such as drawing editors, interactive user interfaces,
animation, information visualization, intelligent agents, and games. This manual describes the library and its usage. More
example
programs are available at www.probp.com.