------------------------------------------------------------------------------- Copyright 2013, Open Universiteit Nederland. This file is distributed-- under the terms of the GNU General Public License. For more information,-- see the file "LICENSE.txt", which is included in the distribution.------------------------------------------------------------------------------- |-- Maintainer : bastiaan.heeren@ou.nl-- Stability : provisional-- Portability : portable (depends on ghc)---- Run a feedbackscript-------------------------------------------------------------------------------moduleIdeas.Service.FeedbackScript.Run(Script,Environment(..),newEnvironment,feedbackDiagnosis,feedbackHint,feedbackHints,ruleToString,feedbackIds,attributeIds,conditionIds,eval)whereimportControl.MonadimportData.ListimportData.MaybeimportData.MonoidimportIdeas.Common.Libraryhiding(ready,Environment)importIdeas.Common.Strategy.Abstract(LabelInfo)importIdeas.Service.BasicServicesimportIdeas.Service.DiagnoseimportIdeas.Service.FeedbackScript.SyntaximportIdeas.Service.StatedataEnvironmenta=Env{oldReady::Bool,expected::Maybe(Rule(Contexta)),recognized::Maybe(Rule(Contexta)),actives::Maybe[LabelInfo],diffPair::Maybe(String,String),before::MaybeTerm,after::MaybeTerm,afterText::MaybeString}newEnvironment::Statea->EnvironmentanewEnvironmentst=newEnvironmentForstnextwherenext=either(constNothing)Just(onefirstst)newEnvironmentFor::Statea->Maybe((Rule(Contexta),b,c),Statea)->EnvironmentanewEnvironmentForstnext=Env{oldReady=readyst,expected=fmap(\((x,_,_),_)->x)next,recognized=Nothing,actives=listToMaybe(stateLabelsst),diffPair=Nothing,before=fst,after=liftMsndnext>>=f,afterText=liftMsndnext>>=g}wherefs=fmap(`build`stateTerms)(hasTermView(exercises))gs=return$prettyPrinter(exercises)(stateTerms)toText::Environmenta->Script->Text->MaybeTexttoTextenvscript=evalenvscript.RightruleToString::Environmenta->Script->Ruleb->StringruleToStringenvscriptr=letf=evalenvscript.Left.getIdinmaybe(showIdr)show(fr)eval::Environmenta->Script->EitherIdText->MaybeTextevalenvscript=either(return.findIdRef)evalTextwhereevalText::Text->MaybeTextevalText=liftMmconcat.mapMunref.textItemswhereunref(TextRefa)|a==expectedId=fmap(findIdRef.getId)(expectedenv)|a==recognizedId=fmap(findIdRef.getId)(recognizedenv)|a==diffbeforeId=fmap(TextString.fst)(diffPairenv)|a==diffafterId=fmap(TextString.snd)(diffPairenv)|a==beforeId=fmapTextTerm(beforeenv)|a==afterId=fmapTextTerm(afterenv)|a==afterTextId=fmapTextString(afterTextenv)|otherwise=findRef(==a)unreft=JusttevalBool::Condition->BoolevalBool(RecognizedIsa)=maybeFalse(eqIda.getId)(recognizedenv)evalBool(CondNotc)=not(evalBoolc)evalBool(CondConstb)=bevalBool(CondRefa)|a==oldreadyId=oldReadyenv|a==hasexpectedId=isJust(expectedenv)|otherwise=Falsenamespaces=nub$mempty:[a|NameSpaceas<-scriptDeclsscript,a<-as]-- equality with namespaceseqId::Id->Id->BooleqIdab=any(\n->n#a==b)namespacesfindIdRef::Id->TextfindIdRefx=fromMaybe(TextString(showIdx))(findRef(`eqId`x))findRef::(Id->Bool)->MaybeTextfindRefp=listToMaybe$catMaybes[evalTextt|(as,c,t)<-allDecls,anypas&&evalBoolc]allDecls=letf(Simple_ast)=[(as,CondConstTrue,t)]f(Guarded_asxs)=[(as,c,t)|(c,t)<-xs]f_=[]inconcatMapf(scriptDeclsscript)feedbackDiagnosis::Diagnosisa->Environmenta->Script->TextfeedbackDiagnosisdiagnosisenv=casediagnosisofBuggy_r->makeWrong"buggy"env{recognized=Justr}NotEquivalent->makeWrong"noteq"envExpected__r->makeOk"ok"env{recognized=Justr}Similar__->makeOk"same"envDetour___r->makeOk"detour"env{recognized=Justr}Correct__->makeOk"unknown"envwheremakeOk=makeDefault"Well done!"makeWrong=makeDefault"This is incorrect."makeDefaultdtse=fromMaybe(TextStringdt).make(newIds)efeedbackHint::Id->Environmenta->Script->TextfeedbackHintfeedbackIdenvscript=fromMaybe(defaultHintenvscript)$makefeedbackIdenvscriptfeedbackHints::Id->[((Rule(Contexta),b,c),Statea)]->Statea->Script->[Text]feedbackHintsfeedbackIdnextsstatescript=map(\env->fromMaybe(defaultHintenvscript)$makefeedbackIdenvscript)envswhereenvs=map(newEnvironmentForstate.Just)nextsdefaultHint::Environmenta->Script->TextdefaultHintenvscript=makeText$caseexpectedenvofJustr->ruleToStringenvscriptrNothing->"Sorry, not hint available."make::Id->Environmenta->Script->MaybeTextmakefeedbackIdenvscript=toTextenvscript(TextReffeedbackId)feedbackIds::[Id]feedbackIds=mapnewId["same","noteq","unknown","ok","buggy","detour","hint","step","label"]attributeIds::[Id]attributeIds=[expectedId,recognizedId,diffbeforeId,diffafterId,beforeId,afterId,afterTextId]conditionIds::[Id]conditionIds=[oldreadyId,hasexpectedId]expectedId,recognizedId,diffbeforeId,diffafterId,beforeId,afterId,afterTextId::IdexpectedId=newId"expected"recognizedId=newId"recognized"diffbeforeId=newId"diffbefore"diffafterId=newId"diffafter"beforeId=newId"before"afterId=newId"after"afterTextId=newId"aftertext"oldreadyId,hasexpectedId::IdoldreadyId=newId"oldready"hasexpectedId=newId"hasexpected"