]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/src-isv/ExtendingSCLEnvironment.mediawiki
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scl.runtime / src-isv / ExtendingSCLEnvironment.mediawiki
1 == Extending SCL environment ==
2
3 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.
4
5 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:
6
7 <b>scl/Test1.scl:</b>
8 <pre>
9 fib :: Integer -> Integer
10 fib x | x <= 1    = 1
11       | otherwise = fib (x-1) + fib (x-2)
12 </pre>
13
14
15 A directory is declared as a SCL package with the following kind of extension points defined in org.simantics.scl.runtime:
16 <pre>
17    <extension point="org.simantics.scl.runtime.package">
18       <package URI="http://www.simantics.org/Tests"
19                directory="scl"/>
20    </extension> 
21 </pre>
22
23 The module is not automatically available in the console, but you must run an import declaration:
24 <pre>
25 > import "http://www.simantics.org/Tests/Test1" as Test1
26 > Test1.fib 13
27 377
28 </pre>
29
30 Import declaration can also be used in modules to refer other modules. Cyclic module dependencies are not allowed.
31
32 == Importing functionality from Java ==
33
34 Java interfaces and classes can be imported from Java by declaring them inside importJava block:
35 <pre>
36 importJava "java.util.regex.Pattern" where
37     data Pattern
38
39 importJava "java.util.List" where
40     data List a
41 </pre>
42
43 Java methods, constructors and fields can be similarly imported by giving
44 their type annotations in importJava block:
45 <pre>
46 importJava "java.util.regex.Pattern.compile" where
47     @JavaName compile
48     compilePattern :: String -> Pattern
49
50     @JavaName matcher
51     createMatcher :: Pattern -> String -> <Proc> Matcher
52
53 importJava "java.util.regex.Matcher" where
54     data Matcher
55
56     @JavaName matches
57     matcherMatches :: Matcher -> <Proc> Boolean
58
59 matches : Pattern -> String -> <Proc> Boolean
60 matches pattern text = do
61     matcherMatches (createMatcher pattern text)
62 </pre>
63
64 Another example:
65 <pre>
66 importJava "java.util.ArrayList" where
67     @JavaName "<init>"
68     createArrayList :: () -> <Proc> List a
69
70     @JavaName "<init>"
71     createArrayListWithCapacity :: Integer -> <Proc> List a
72
73     @JavaName size
74     sizeList :: List a -> <Proc> Integer
75
76     @JavaName get
77     getList :: List a -> Integer -> <Proc> a
78
79     @JavaName set
80     setList :: List a -> Integer -> a -> <Proc> ()
81
82     @JavaName add
83     addList :: List a -> a -> <Proc> Boolean
84 </pre>
85
86 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. 
87
88 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.