l13: scripting
TRANSCRIPT
1
i-viewsUniversityPart13:Scripting
22
Scripting
Your speakers today
PatrickCloshen RalphHerold
SoftwareEngineer KnowledgeEngineer
intelligentviews gmbh intelligentviews gmbh
33
Scripting
Agenda
Javascript-API1. Use-cases
2. Structure
3. Editinganddebuggingscripts
4. Limitations
5. Examples and caveats
6. Homework
44
Scripting
1.Use-cases
Scriptingis needed inthe following technologies ini-views
• Triggers
• Reports
• REST-API
• Datamappings
• Object lists
• Scriptsas attributes
55
Scripting
Use-case Triggers
• React to changes
• Implement work flows
Needed:
• Accesscomposed of operation and objectthat is to be changed (to allow filteringand to gain access to modification values)
• Scriptthat expresses how to react to thedetected change
Example:
66
Scripting
Use-case Reports
Typical scenario:
• Locate the objects that need manipulation
• Implement said manipulation
• Logchanges or effects or errors of desired operations
Example:
Executeaquery and iterate over allhits with the samefunction that sets acertain property,returnnumber of objects changed
77
Scripting
Use-caseReports
Registering Scripts:
Scriptscan be registeredinthe semantic graph database to make them accessible.Registrationkeys arestructured into ahierarchy based ontheir values which are tokenized by the dot character
Example:
88
Scripting
REST-API
• Scriptsexpresswhat resources available viaRESTareexposedto the outside
• Highdegree of freedom thanks to Javascript-framework
• Readand/or write operations
• Allows access to REST-servicecontext (user,authentication,parameters,request and response,etc.)
99
Scripting
Use-cases data mapping and object lists
Scriptsenable computation of special values for data mappings (exports)and object lists
1010
Scripting
Use-case scripts as attributes
Originally implemented to store scripts inactions as part of view configurations
Objectscan carryfunctionality specific to them inattributes designed to store scripts
Thisis done to allow for polymorphism inthe designof the semantic database
1111
Scripting
Use-case scripts as attributes
1212
Scripting
2.Structure
TheJavascript APIhas representation classes for the standard building blocks of our semantic graphdatabase:
• Types
• Objects
• Properties(Attributesand Relations)
• Extensions
Thesecan be accessed and manipulated inallways that are available inthe UI,except for certainadministrativeactions (e.g.creating and associating indexers to properties)
1313
Scripting
Structure
Furthermore there are classesfor other objects suchas
• Queries
• Datamappings
• HttpRequestand HttpResponse
• Hitsand hit causes
1414
Scripting
Structure
• Aspecial namespace wascreated to access specific objects:$k
• Registry$k.RegistryServes as access point for allobjects that are registeredor are supplied with aninternalname
• Locating objects:$k.Registry.type(„<internalNameOfType>“);
$k.Registry.elementAtValue(internalNameOfAttrType,value,language);
$k.Registry.elementByLocator(locator);
$k.Registry.elementWithID(id);
1515
Scripting
Structure
Modules
• Functions that are to be re-used can be accumulated inmodule-scripts
• Thesemodules need to be registered
• Accessviathe „require“-function or „module“-function:
1616
Scripting
Structure
Transactions
• Asi-views system is collaborative,allaccesses are governed within atransaction system
• Thismeans that writing willneed awrite transaction to function properly
• Therefore you need to adjust the settings of the script/method to make sure that you can edit things,i.e.modify them
1717
Scripting
3.EditingandDebuggingScripts
Scriptscan be handled with the integrated editor that alsoprovides debugging and testing capabilities
1818
Scripting
Editing and DebuggingScripts
• Execution pane:
• Variablescan be set
• Transactionconfiguration
• Additionalpreparatory script canbe created
1919
Scripting
Editing and DebuggingScripts
Debugger:
• Breakpointscan be set with mouseclicks onleft side of code
• Buttonscontrol execution
• Current context is displayed ontheright
2020
Scripting
4.Limitations
• No direct file operations onthe system are allowed.
• Theextent inwhich structured queries can be constructed through scripts is limited.
2121
Scripting
5.Examples and caveats
Choiceattributes:
Thevalues for Choiceattributes are of class $k.Choice,these have no set-method!
Available values for choices can be determined atthe attribute typewith valueRange(),this returns anobject of type$k.ChoiceRange
2222
Scripting
Examples and caveats
Choices
var attrType = $k.Registry.type("gender");
var attrRange = attrType.valueRange();
var value = attrRange.choiceInternalNamed(person.gender);
if ( value ) {
var attr = associateTopic.attribute("gender");
if (!attr) { attr = associateTopic.createAttribute("gender",value); }
else { attr.setValue(value);}
}
2323
Scripting
Examples and caveats
Intervals for interval attributes:
You have to create anobject of class $k.Interval and set its start and endvalues to get apropervalue forattributes of typeinterval
2424
Scripting
Intervals
var interval = refData.attributeValue("validFromTo");
if (interval) { interval.setStop(new Date()); }
else { interval = new $k.Interval(undefined,new Date()); };
2525
Scripting
Examples and caveats
Using astructured query with parameters
Var query = $k.Registry.query(„myQuery");
var result = query.findElements({ id: this.idString() });
2626
Scripting
Examples and caveats
Locators
• IDlocator:ID12_23456
• Internalname locator:IN~internalName
• Attributevalue locator:AV~attrTypeInternalName~value
2727
Scripting
Examples and caveats
JSONSupport
• Asit is embedded inJavascript the JSONobject allows handling of objects• E.g.:JSON.parse()and JSON.stringify()
2828
Scripting
Examples and caveats
Inthe KnowledgeBuilder the context menu of each object contains the item„Script->EvaluateJavascript“
Thiscan be useful to test code snippets or access certain things
Theoutput of scripts can be directed into two destinations:• $k.out is atext document where you can print stuff
• Function $k.log(object,level,channel)allows sending stuff to the configured logfile
Messagesfrom script especially debug-messagescan be found here:
2929
Scripting
Examples and caveats
Messagesfrom script especially debug-messagescan be found here:
Mainmenu:Analyse->ScriptMessages
3030
Scripting
6.Homework
1. DefineaRESTscriptresourcewhichcreatesanewpersonwheneveraspecificRESTrequestisreceivedandextractsthenameofthispersonoutoftheRESTrequest.
2. Createastructuredquerywhichfindsallmusicianswhoplayaguitarandwhosenamecontainsastringwhichispassedbyaparameter.Thendefineascriptwhichpassesthestring„Daniel“tothequeryandcreatesarelationofthetype„hasMember“foreachreturnedmusiciantoabandofyourchoice.
3. Define ascript that iterates allcomposers and then outputs first the name of the composer and thenalist of their compositions.
Documentation
http://documentation.i-views.com/5.0/javascript-api/
3131
Scripting-Tool
Sendyour questions to:[email protected]
Consultationhours:EveryWednesday
Thank you for visitingi-viewsUniversity
3232
Scripting-Tool
UnsereneuenIcons