Difference between revisions of "SCL Tutorial"

From Developer Documents
Jump to navigation Jump to search
(Created page with "== Getting started == The easiest way of getting started with SCL is to use SCL-console that is included in almost all Simantics-based products. You can open the console by pres...")
 
 
(36 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
[[Category: Simantics Constraint Language]]
 
== Getting started ==
 
== Getting started ==
  
The easiest way of getting started with SCL is to use SCL-console that is included in almost all Simantics-based products. You can open the console by pressing ALT-SHIFT-q and then q and choosing "SCL Console" from the list of views.
+
The easiest way of getting started with SCL is to use SCL console that is included in almost all Simantics-based products. You can open the console by pressing ALT-SHIFT-q and then q and choosing "SCL Console" from the list of views.
  
SCL Console works by executing commands you write into the input box in the bottom of the view. After the command has been written, it can be executed by pressing ENTER. However, this works only if the command contains no syntactic errors. Possible errors are highlighted in the input box and an error description is shown if you move mouse on top of problematic part of the command.
+
SCL console works by executing commands you write into the input box in the bottom of the view. After the command has been written, it can be executed by pressing ENTER. However, this works only if the command contains no syntactic errors. Possible errors are highlighted in the input box and a description of the error is shown when you move mouse on top of the highlighted text.
 +
 
 +
Multiline commands can be written by pressing CTRL-ENTER (or just ENTER when the current command text contains errors). The command history can be browsed with CTRL-UP and CTRL-DOWN.
 +
 
 +
If the command you write into console results as an ordinary value, it is just printed to the console. Here are couple of examples you can try:
 +
<pre>
 +
> 13
 +
13
 +
> 1+2
 +
3
 +
> sin 1
 +
0.8414709848078965
 +
> "Hello " + "world!"
 +
Hello world!
 +
> [1,3,5]
 +
[1, 3, 5]
 +
</pre>
 +
 
 +
You can also declare local variables to be used in the commands:
 +
<pre>
 +
> x = 35
 +
> y = 40
 +
> x + y
 +
75
 +
> x * y
 +
1400
 +
</pre>
 +
 
 +
Also new functions can be defined:
 +
<pre>
 +
> f x = x * x
 +
> f 123
 +
15129
 +
</pre>
 +
 
 +
If you write a command that has side-effects, it is executed in the console:
 +
<pre>
 +
> print "Hello" ; print "world!"
 +
Hello
 +
world!
 +
</pre>
 +
 
 +
SCL is a dialect of Haskell and tutorials written for Haskell can be used for learning the details of the language. The main differences between the languages are the strict evaluation strategy used in SCL and somewhat different standard library. Some Haskell tutorials can be found at [http://www.haskell.org/haskellwiki/Learning_Haskell http://www.haskell.org/haskellwiki/Learning_Haskell].
 +
 
 +
== Extending SCL environment ==
 +
 
 +
The SCL values, data types etc. that are available in expressions and commands are defined in SCL modules. Currently all SCL modules must be part of the product plugins (in the future, you can also write modules inside the models). Each module is identified by a URI.
 +
 
 +
SCL module is a text file ending with extension ".scl". The recommended place for modules is scl/ folder under plugin root, but also other directories can be used:
 +
 
 +
<b>scl/Test1.scl:</b>
 +
<pre>
 +
fib :: Integer -> Integer
 +
fib x | x <= 1    = 1
 +
      | otherwise = fib (x-1) + fib (x-2)
 +
</pre>
 +
 
 +
 
 +
A directory is declared as a SCL package with the following kind of extension points defined in org.simantics.scl.runtime:
 +
<pre>
 +
  <extension point="org.simantics.scl.runtime.package">
 +
      <package URI="http://www.simantics.org/Tests"
 +
              directory="scl"/>
 +
  </extension>
 +
</pre>
 +
 
 +
The module is not automatically available in the console, but you must run an import declaration:
 +
<pre>
 +
> import "http://www.simantics.org/Tests/Test1" as Test1
 +
> Test1.fib 13
 +
377
 +
</pre>
 +
 
 +
Import declaration can also be used in modules to refer other modules. Cyclic module dependencies are not allowed.
 +
 
 +
== Importing functionality from Java ==
 +
 
 +
Java interfaces and classes can be imported from Java by declaring them inside importJava block:
 +
<pre>
 +
importJava "java.util.regex.Pattern" where
 +
    data Pattern
 +
 
 +
importJava "java.util.List" where
 +
    data List a
 +
</pre>
 +
 
 +
Java methods, constructors and fields can be similarly imported by giving
 +
their type annotations in importJava block:
 +
<pre>
 +
importJava "java.util.regex.Pattern" where
 +
    @JavaName compile
 +
    compilePattern :: String -> Pattern
 +
 
 +
    @JavaName matcher
 +
    createMatcher :: Pattern -> String -> <Proc> Matcher
 +
 
 +
importJava "java.util.regex.Matcher" where
 +
    data Matcher
 +
 
 +
    @JavaName matches
 +
    matcherMatches :: Matcher -> <Proc> Boolean
 +
 
 +
matches : Pattern -> String -> <Proc> Boolean
 +
matches pattern text = do
 +
    matcherMatches (createMatcher pattern text)
 +
</pre>
 +
 
 +
Another example:
 +
<pre>
 +
importJava "java.util.ArrayList" where
 +
    @JavaName "<init>"
 +
    createArrayList :: () -> <Proc> List a
 +
 
 +
    @JavaName "<init>"
 +
    createArrayListWithCapacity :: Integer -> <Proc> List a
 +
 
 +
    @JavaName size
 +
    sizeList :: List a -> <Proc> Integer
 +
 
 +
    @JavaName get
 +
    getList :: List a -> Integer -> <Proc> a
 +
 
 +
    @JavaName set
 +
    setList :: List a -> Integer -> a -> <Proc> ()
 +
 
 +
    @JavaName add
 +
    addList :: List a -> a -> <Proc> Boolean
 +
</pre>
 +
 
 +
Java constructor is referred with "<init>". If Java method name and SCL name matches the annotation @JavaName can be left out. Java import mechanism tries to be quite flexible. It provides some arguments based on the effects the function has. It also ignores the return value of the Java method if return type is () in SCL.
 +
 
 +
A major functionality currently still missing is the ability to create new implementations of existing Java interfaces in SCL code or extend an existing class. This can be worked around currently by writing new implementations in Java.

Latest revision as of 15:21, 22 November 2016

Getting started

The easiest way of getting started with SCL is to use SCL console that is included in almost all Simantics-based products. You can open the console by pressing ALT-SHIFT-q and then q and choosing "SCL Console" from the list of views.

SCL console works by executing commands you write into the input box in the bottom of the view. After the command has been written, it can be executed by pressing ENTER. However, this works only if the command contains no syntactic errors. Possible errors are highlighted in the input box and a description of the error is shown when you move mouse on top of the highlighted text.

Multiline commands can be written by pressing CTRL-ENTER (or just ENTER when the current command text contains errors). The command history can be browsed with CTRL-UP and CTRL-DOWN.

If the command you write into console results as an ordinary value, it is just printed to the console. Here are couple of examples you can try:

> 13
13
> 1+2
3
> sin 1
0.8414709848078965
> "Hello " + "world!"
Hello world!
> [1,3,5]
[1, 3, 5]

You can also declare local variables to be used in the commands:

> x = 35
> y = 40
> x + y
75
> x * y
1400

Also new functions can be defined:

> f x = x * x
> f 123
15129

If you write a command that has side-effects, it is executed in the console:

> print "Hello" ; print "world!"
Hello
world!

SCL is a dialect of Haskell and tutorials written for Haskell can be used for learning the details of the language. The main differences between the languages are the strict evaluation strategy used in SCL and somewhat different standard library. Some Haskell tutorials can be found at http://www.haskell.org/haskellwiki/Learning_Haskell.

Extending SCL environment

The SCL values, data types etc. that are available in expressions and commands are defined in SCL modules. Currently all SCL modules must be part of the product plugins (in the future, you can also write modules inside the models). Each module is identified by a URI.

SCL module is a text file ending with extension ".scl". The recommended place for modules is scl/ folder under plugin root, but also other directories can be used:

scl/Test1.scl:

fib :: Integer -> Integer
fib x | x <= 1    = 1
      | otherwise = fib (x-1) + fib (x-2)


A directory is declared as a SCL package with the following kind of extension points defined in org.simantics.scl.runtime:

   <extension point="org.simantics.scl.runtime.package">
      <package URI="http://www.simantics.org/Tests"
               directory="scl"/>
   </extension> 

The module is not automatically available in the console, but you must run an import declaration:

> import "http://www.simantics.org/Tests/Test1" as Test1
> Test1.fib 13
377

Import declaration can also be used in modules to refer other modules. Cyclic module dependencies are not allowed.

Importing functionality from Java

Java interfaces and classes can be imported from Java by declaring them inside importJava block:

importJava "java.util.regex.Pattern" where
    data Pattern

importJava "java.util.List" where
    data List a

Java methods, constructors and fields can be similarly imported by giving their type annotations in importJava block:

importJava "java.util.regex.Pattern" where
    @JavaName compile
    compilePattern :: String -> Pattern

    @JavaName matcher
    createMatcher :: Pattern -> String -> <Proc> Matcher

importJava "java.util.regex.Matcher" where
    data Matcher

    @JavaName matches
    matcherMatches :: Matcher -> <Proc> Boolean

matches : Pattern -> String -> <Proc> Boolean
matches pattern text = do
    matcherMatches (createMatcher pattern text)

Another example:

importJava "java.util.ArrayList" where
    @JavaName "<init>"
    createArrayList :: () -> <Proc> List a

    @JavaName "<init>"
    createArrayListWithCapacity :: Integer -> <Proc> List a

    @JavaName size
    sizeList :: List a -> <Proc> Integer

    @JavaName get
    getList :: List a -> Integer -> <Proc> a

    @JavaName set
    setList :: List a -> Integer -> a -> <Proc> ()

    @JavaName add
    addList :: List a -> a -> <Proc> Boolean

Java constructor is referred with "<init>". If Java method name and SCL name matches the annotation @JavaName can be left out. Java import mechanism tries to be quite flexible. It provides some arguments based on the effects the function has. It also ignores the return value of the Java method if return type is () in SCL.

A major functionality currently still missing is the ability to create new implementations of existing Java interfaces in SCL code or extend an existing class. This can be worked around currently by writing new implementations in Java.