l13: scripting

32
1 i-views University Part 13: Scripting

Upload: medialeg-gmbh

Post on 23-Jan-2018

47 views

Category:

Data & Analytics


1 download

TRANSCRIPT

Page 1: L13: Scripting

1

i-viewsUniversityPart13:Scripting

Page 2: L13: Scripting

22

Scripting

Your speakers today

PatrickCloshen RalphHerold

SoftwareEngineer KnowledgeEngineer

intelligentviews gmbh intelligentviews gmbh

Page 3: L13: Scripting

33

Scripting

Agenda

Javascript-API1. Use-cases

2. Structure

3. Editinganddebuggingscripts

4. Limitations

5. Examples and caveats

6. Homework

Page 4: L13: Scripting

44

Scripting

1.Use-cases

Scriptingis needed inthe following technologies ini-views

• Triggers

• Reports

• REST-API

• Datamappings

• Object lists

• Scriptsas attributes

Page 5: L13: Scripting

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:

Page 6: L13: Scripting

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

Page 7: L13: Scripting

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:

Page 8: L13: Scripting

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.)

Page 9: L13: Scripting

99

Scripting

Use-cases data mapping and object lists

Scriptsenable computation of special values for data mappings (exports)and object lists

Page 10: L13: Scripting

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

Page 11: L13: Scripting

1111

Scripting

Use-case scripts as attributes

Page 12: L13: Scripting

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)

Page 13: L13: Scripting

1313

Scripting

Structure

Furthermore there are classesfor other objects suchas

• Queries

• Datamappings

• HttpRequestand HttpResponse

• Hitsand hit causes

Page 14: L13: Scripting

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);

Page 15: L13: Scripting

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:

Page 16: L13: Scripting

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

Page 17: L13: Scripting

1717

Scripting

3.EditingandDebuggingScripts

Scriptscan be handled with the integrated editor that alsoprovides debugging and testing capabilities

Page 18: L13: Scripting

1818

Scripting

Editing and DebuggingScripts

• Execution pane:

• Variablescan be set

• Transactionconfiguration

• Additionalpreparatory script canbe created

Page 19: L13: Scripting

1919

Scripting

Editing and DebuggingScripts

Debugger:

• Breakpointscan be set with mouseclicks onleft side of code

• Buttonscontrol execution

• Current context is displayed ontheright

Page 20: L13: Scripting

2020

Scripting

4.Limitations

• No direct file operations onthe system are allowed.

• Theextent inwhich structured queries can be constructed through scripts is limited.

Page 21: L13: Scripting

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

Page 22: L13: Scripting

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);}

}

Page 23: L13: Scripting

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

Page 24: L13: Scripting

2424

Scripting

Intervals

var interval = refData.attributeValue("validFromTo");

if (interval) { interval.setStop(new Date()); }

else { interval = new $k.Interval(undefined,new Date()); };

Page 25: L13: Scripting

2525

Scripting

Examples and caveats

Using astructured query with parameters

Var query = $k.Registry.query(„myQuery");

var result = query.findElements({ id: this.idString() });

Page 26: L13: Scripting

2626

Scripting

Examples and caveats

Locators

• IDlocator:ID12_23456

• Internalname locator:IN~internalName

• Attributevalue locator:AV~attrTypeInternalName~value

Page 27: L13: Scripting

2727

Scripting

Examples and caveats

JSONSupport

• Asit is embedded inJavascript the JSONobject allows handling of objects• E.g.:JSON.parse()and JSON.stringify()

Page 28: L13: Scripting

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:

Page 29: L13: Scripting

2929

Scripting

Examples and caveats

Messagesfrom script especially debug-messagescan be found here:

Mainmenu:Analyse->ScriptMessages

Page 30: L13: Scripting

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/

Page 31: L13: Scripting

3131

Scripting-Tool

Sendyour questions to:[email protected]

Consultationhours:EveryWednesday

Thank you for visitingi-viewsUniversity

Page 32: L13: Scripting

3232

Scripting-Tool

UnsereneuenIcons