Commit 1405a181 authored by GROSS-AMBLARD David's avatar GROSS-AMBLARD David
Browse files

adds a sugarFree function that encapsulate many of the syntactic sugar for artifacts

parent 216a1f65
......@@ -2,6 +2,85 @@
require_once("wirk.php");
/**
* sugarFree
*
* Remove syntactic sugar from guards and actions in artifacts
*
* @param $str string: The string to unsugar
* @return stringbool string: sugar-free string
*/
function sugarFree($str){
if($str=="none")
$str="select true from dual";
if (preg_match("/^task ([0-9]+) is answered/",$str,$matches)){
$str="select true from Answered where artifact=CURRENT_ARTIFACT and id=".$matches[1];
}
if (preg_match("/^use '(.*)' as template/",$str,$matches)){
$str="select @TEMPLATE:=body from Template where id='".$matches[1]."'";
}
if (preg_match("/^use '(.*)' as title/",$str,$matches)){
$str="select @TITLE:='".$matches[1]."'";
}
if (preg_match("/^use '(.*)' as description/",$str,$matches)){
$str="select @DESCRIPTION:='".$matches[1]."'";
}
if (preg_match("/^use '(.*)' as checker message/",$str,$matches)){
$str="select @CHECKERMSG:='".$matches[1]."'";
}
if (preg_match("/^use '(.*)' as checker/",$str,$matches)){
$str="select @CHECKER:='".$matches[1]."'";
}
if (preg_match("/^prepare task ([0-9]+) as (basic|message|multi-line input)/",$str,$matches)){
$str="
select @TITLE:='default title';
select @DESCRIPTION:='default description';
select @TEMPLATE:='';
select @BODY:=@TEMPLATE;
select @CHECKER:=NULL;
select @CHECKERMSG:=NULL;
";
$str.="select @TASKID:=".$matches[1].";";
switch ($matches[2]){
case "basic":
$str.="select @TYPE:=NULL";
break;
case "message":
$str.="select @TYPE:=0";
break;
case "multi-line input":
$str.="select @TYPE:=3";
break;
// TODO continue with other sugar cubes :)
}
}
if (preg_match("/^install task/",$str,$matches)){
$str="insert into Task(id,artifact,title,description,body,checker,checkermsg,type) values (@TASKID,CURRENT_ARTIFACT,@TITLE,@DESCRIPTION,@TEMPLATE,@CHECKER,@CHECKERMSG,@TYPE)";
}
if (preg_match("/^forget profile for current artifact/",$str,$matches)){
$str="delete from Profile where idartifact=CURRENT_ARTIFACT";
}
if (preg_match("/^offer task to anyone/",$str,$matches)){
$str="insert into Profile values (@TASKID,CURRENT_ARTIFACT,1)";
}
if (preg_match("/^skill ([0-9]+) is relevant for the task/",$str,$matches)){
$str="insert into Profile values (@TASKID,CURRENT_ARTIFACT,".$matches[1].")";
}
return $str;
}
/**
* update
*
......@@ -41,25 +120,18 @@ function updateonce($conn, $artifact, $artifactid, $count)
debug("checking transition $node -> $dest");
debug("node data: ".json_encode($stateinfo[$dest]));
$guard=$artifact[$node][$dest]["guard"];
if($guard=="none")
$guard="select true from dual";
if (preg_match("/^task ([0-9]+) is answered/",$guard,$matches)){
$guard="select true from Answered where artifact=CURRENT_ARTIFACT and id=".$matches[1];
}
$guard=sugarFree($guard);
$guard=str_replace('SESSION_USER', $_SESSION['id'], $guard);
$guard=str_replace("CURRENT_ARTIFACT",$artifactid, $guard);
$actions=str_replace('SESSION_USER', $_SESSION['id'], $artifact[$node][$dest]["actions"]);
if (isset($_SESSION['skills'])) {
$actions=str_replace('SKILLS', $_SESSION['skills'], $actions);
// TODO but actions is an array?
// TODO what for?
}
$activated=step($conn, $artifactid, $node, $dest, $guard, $actions);
debug("result:".$activated);
if ($activated) {
return true;
}
}
......@@ -173,77 +245,7 @@ function step($conn, $artifactid, $from, $to, $guard, $actions)
$conn->query("update Artifact set node=$to where ID=$artifactid");
foreach ($actions as $action) {
debug("executing $action");
if (preg_match("/^use '(.*)' as template/",$action,$matches)){
debug("a match");
$action="select @TEMPLATE:=body from Template where id='".$matches[1]."'";
debug("action : $action");
}
if (preg_match("/^use '(.*)' as title/",$action,$matches)){
debug("a match");
$action="select @TITLE:='".$matches[1]."'";
debug("action : $action");
}
if (preg_match("/^use '(.*)' as description/",$action,$matches)){
debug("a match");
$action="select @DESCRIPTION:='".$matches[1]."'";
debug("action : $action");
}
if (preg_match("/^use '(.*)' as checker message/",$action,$matches)){
debug("a match");
$action="select @CHECKERMSG:='".$matches[1]."'";
debug("action : $action");
}
if (preg_match("/^use '(.*)' as checker/",$action,$matches)){
debug("a match");
$action="select @CHECKER:='".$matches[1]."'";
debug("action : $action");
}
if (preg_match("/^prepare task ([0-9]+) as (basic|message|multi-line input)/",$action,$matches)){
debug("a match");
$action="
select @TITLE:='default title';
select @DESCRIPTION:='default description';
select @TEMPLATE:='';
select @BODY:=@TEMPLATE;
select @CHECKER:=NULL;
select @CHECKERMSG:=NULL;
";
$action.="select @TASKID:=".$matches[1].";";
switch ($matches[2]){
case "basic":
$action.="select @TYPE:=NULL";
break;
case "message":
$action.="select @TYPE:=0";
break;
case "multi-line input":
$action.="select @TYPE:=3";
break;
// TODO continue with other sugar cubes :)
}
debug("action : $action");
}
if (preg_match("/^install task/",$action,$matches)){
$action="insert into Task(id,artifact,title,description,body,checker,checkermsg,type) values (@TASKID,CURRENT_ARTIFACT,@TITLE,@DESCRIPTION,@TEMPLATE,@CHECKER,@CHECKERMSG,@TYPE)";
}
if (preg_match("/^forget profile for current artifact/",$action,$matches)){
$action="delete from Profile where idartifact=CURRENT_ARTIFACT";
}
if (preg_match("/^offer task to anyone/",$action,$matches)){
$action="insert into Profile values (@TASKID,CURRENT_ARTIFACT,1)";
}
if (preg_match("/^skill ([0-9]+) is relevant for the task/",$action,$matches)){
$action="insert into Profile values (@TASKID,CURRENT_ARTIFACT,".$matches[1].")";
}
$action=sugarFree($action);
$action=str_replace("CURRENT_ARTIFACT",$artifactid, $action);
if (preg_match("/^call wirk image annotator as task ([0-9]+) for '(.*)'/",$action,$matches)){
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment