## Logic language

For a broader introduction to the logic system see Declarative logic.

## Logic expressions syntax in lazy stream approach

To change the categorizers used for particle analyzer or letter matcher you can override a class: DigitStreamVectorizer is an example showing what is needed to define a match of the digits working as a plugin from ImageJ. Starting in ShapeLogic 1.6 this could be done by using a categorizer setup file: shapelogic/src/test/resources/data/neuralnetwork/polygon_digit_recognizer_with_rules_print.txt. This has exactly the same effect.

### Define a rule for matching the letter A using categorizer setup file

```========== def A
pointCount == 5
holeCount == 1
tJunctionLeftCount == 1
tJunctionRightCount == 1
endpointBottomPointCount == 2
horizontalLineCount == 1
verticalLineCount == 0
endPointCount == 2
softPointCount == 0```

The "def A" is saying apply this rule to the OH, object hypothesis, the letter A.

Each line has 3 elements:

• Names of streams that generate the point count and hole count for the polygons
• Then there is the predicate <, > and ==
• The comparison value

#### Define a rule for matching the letter A using programming

```rule("A", POINT_COUNT, "==", 5.);
rule("A", HOLE_COUNT, "==", 1.);
rule("A", T_JUNCTION_LEFT_POINT_COUNT,"==", 1.);
rule("A", T_JUNCTION_RIGHT_POINT_COUNT,"==", 1.);
rule("A", END_POINT_BOTTOM_POINT_COUNT, "==", 2.);
rule("A", HORIZONTAL_LINE_COUNT, "==", 1.);
rule("A", VERTICAL_LINE_COUNT, "==", 0.);
rule("A", END_POINT_COUNT, "==", 2.);
rule("A", SOFT_POINT_COUNT, "==", 0.);```

### Idea behind lazy streams

Lazy streams have the following features

• You can define a lazy stream based on other lazy streams
• They works as a kind of UNIX pipes or Legos
• They serves as your query construct, you can directly query them

## Different types of declarative programming in ShapeLogic

1. Lazy calculations
2. Lazy streams
3. IoC, Inversion of Control using Google Guise, not been started yet

### Declarative programming concept

• Lazy computations was expanded
• Lazy streams was introduced
• Lazy streams can use the Java 6 Scripting interface
• Tested with Groovy, JRuby and JavaScript, but this is supporting 25 languages
• Lazy streams work with matching several polygons at once
• Named lazy stream turned out to be very easy for user defined rules
• Using a scripting language is less essential for rule definition is less essential than with goal driven tasks

IoC hold potential but has not been used yet.

### Future plan for declarative programming in ShapeLogic

Make menu items in ImageJ's menu for opening and running a user defined rule database, without user defined Java code. Since it was so easy to define rules in Java this have been given lower priority than originally planned.

#### Problem with Java 6 Scripting for rule definition in ImageJ

One possible problem with moving rules to Java 6 Scripting, is that this requires that the user downloads engine interface files from Sun. This would make the installation of ShapeLogic more complicated now it is just unpacking a zip and moving it into ImageJ's plugin dir.

The only scripting language that works out of the box is JavaScript. While that is a reasonable language the integration with Java is not so great especially not Java 5.

It is also possible to connect Groovy and Java outside Java 6 Scripting, but then it would not be uniform.