Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrování filtrování podobných pravidel vůči knowledge base #190

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b0d1d20
hledání pravidel z datasource a tabulka pro ukládání známých vazeb
PreVaDu Nov 15, 2016
457685d
porovnání dle atributů, začátek
PreVaDu Jan 19, 2017
9f8eaf1
Merge remote-tracking branch 'refs/remotes/origin/master' into PVD-DP
PreVaDu Jan 19, 2017
4c20ea3
hlubší analýza - po atributy a jejich hodnoty
PreVaDu Jan 23, 2017
a117a1f
kontrola hodnot jen pro stejný atribut
PreVaDu Feb 1, 2017
4a71e36
Merge remote-tracking branch 'refs/remotes/origin/master' into PVD-DP
PreVaDu Feb 1, 2017
b6216ad
hodnocení pravidla v KB pro použití v RC
PreVaDu Feb 1, 2017
a0a0e78
vrácení míry podobnosti i se vztahem podobného pravidla
PreVaDu Feb 7, 2017
f201218
rozdělení porovnání pravidla do více metod pro znovupoužití
PreVaDu Feb 9, 2017
44bb390
ukládání nalezené nejlepší shody
PreVaDu Feb 21, 2017
78d571e
využití uložených nalezených shod pravidel pro zrychlení vrácení výsl…
PreVaDu Feb 21, 2017
8bab288
přidání sloupce ruleset pro uchování nalezených podobnosti
PreVaDu Feb 28, 2017
f058b8d
tabulka se všemi provedenými kontroly podobnosti
PreVaDu Mar 2, 2017
0c8fa34
využití tabulky se všemi provedenými komparacemi
PreVaDu Mar 2, 2017
ff2d9ec
mazání vazeb na smazaná pravidla z KB
PreVaDu Mar 3, 2017
fbf12f7
fix mazání vazeb na více pravidel
PreVaDu Mar 3, 2017
60dc9ca
Merge remote-tracking branch 'refs/remotes/origin/master' into PVD-DP
PreVaDu Mar 22, 2017
9fb8c5d
Merge remote-tracking branch 'refs/remotes/origin/master' into PVD-DP
PreVaDu Mar 28, 2017
855532d
uchovávání dekomponovaného tvaru pravidla v DB
PreVaDu Mar 28, 2017
ae36736
zrušení mazání uchovaných porovnání pro smazaná pravidla z KB
PreVaDu Mar 28, 2017
5dfd64b
reorganizace RuleSetsPresenteru
PreVaDu Apr 1, 2017
c22c461
porovnávání o úroveň výš - preprocessingy namísto atributů
PreVaDu Apr 4, 2017
4661c24
rekurzivní procházení cedentů pro získání atributů
PreVaDu Apr 4, 2017
7ea0f28
využití lastModified Rule setu pro zamezení znovuporovnávání
PreVaDu Apr 7, 2017
62a81c1
vynechání nulové podobnosti jako nejlepší
PreVaDu Apr 14, 2017
a3cf1c6
kontrola, zda je rozložitelný cedent
PreVaDu Apr 19, 2017
e176f74
Merge remote-tracking branch 'refs/remotes/origin/master' into PVD-DP
PreVaDu Jun 13, 2017
301f997
doplnění lokalizace věcí z DP
PreVaDu Jun 13, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 204 additions & 0 deletions app/EasyMinerModule/presenters/RuleSetsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace EasyMinerCenter\EasyMinerModule\Presenters;

use EasyMinerCenter\Model\EasyMiner\Entities\Cedent;
use EasyMinerCenter\Model\EasyMiner\Entities\KnowledgeBaseRuleRelation;
use EasyMinerCenter\Model\EasyMiner\Entities\RuleSet;
use EasyMinerCenter\Model\EasyMiner\Entities\RuleSetRuleRelation;
use EasyMinerCenter\Model\EasyMiner\Facades\KnowledgeBaseFacade;
use EasyMinerCenter\Model\EasyMiner\Facades\RulesFacade;
use EasyMinerCenter\Model\EasyMiner\Facades\RuleSetsFacade;
use EasyMinerCenter\Model\EasyMiner\Facades\UsersFacade;
use EasyMinerCenter\Model\EasyMiner\Repositories\RuleRuleRelationsRepository;
use EasyMinerCenter\Model\EasyMiner\Serializers\AssociationRulesXmlSerializer;
use EasyMinerCenter\Model\EasyMiner\Transformators\XmlTransformator;
use Nette\InvalidArgumentException;
Expand All @@ -24,6 +28,8 @@ class RuleSetsPresenter extends BasePresenter{
private $rulesFacade;
/** @var RuleSetsFacade $ruleSetsFacade */
private $ruleSetsFacade;
/** @var KnowledgeBaseFacade $knowledgeBaseFacade */
private $knowledgeBaseFacade;
/** @var UsersFacade $usersFacade */
private $usersFacade;
/** @var XmlTransformator $xmlTransformator */
Expand Down Expand Up @@ -265,6 +271,198 @@ private function prepareRulesResult($ruleIdsArr, $ruleSet=null){

#endregion actions for manipulation of relations between Rules and RuleSets

#region actions for rule comparing

/**
* Action returning Rule set with rule names and relations from Datasource of Miner
* @param Int $id RuleSet id
* @param Int $miner Miner id
*/
public function actionGetRulesNames($id, $miner){
//najití RuleSetu a kontroly
$ruleSet=$this->ruleSetsFacade->findRuleSet($id);
$this->ruleSetsFacade->checkRuleSetAccess($ruleSet,$this->user->id);
//připravení výstupu
$result=[
'ruleset'=>$ruleSet->getDataArr(),
'rules'=>[]
];
if ($ruleSet->rulesCount>0 || true){
$rules=$this->knowledgeBaseFacade->findRulesByDatasource($ruleSet,$miner);
if (!empty($rules)){
//$result['rules'] = $rules;
foreach($rules as $rule){
$result['rules'][$rule->ruleId]=[
'name' => $rule->text,
'relation' => $rule->relation
];
}
}
}
$this->sendJsonResponse($result);
}

/**
* Method for finding Rule from Rule set with the best similarity to compared Rule
* @param Int $id RuleSet id
* @param Int $rule Rule id
*/
public function actionCompareRuleWithRuleset($id, $rule){
//finding Rule set and checking
$ruleSet=$this->ruleSetsFacade->findRuleSet($id);
$this->ruleSetsFacade->checkRuleSetAccess($ruleSet,$this->user->id);
//prepearing output
$result=[];
$compareResults=[];
try{
$ruleSimilarity = $this->knowledgeBaseFacade->findRuleSimilarity($id, $rule);
}catch (\Exception $e){
$ruleSimilarity = null;
}
if($ruleSimilarity && ($ruleSimilarity->resultDate->getTimestamp() >= $ruleSet->lastModified->getTimestamp())){ // result from past, which is newer than Rule set last modification
$result['max'] = $ruleSimilarity->rate;
$result['rule'] = [
RuleRuleRelationsRepository::COLUMN_RULESET_RULE => $ruleSimilarity->knowledgeBaseRuleId,
RuleRuleRelationsRepository::COLUMN_RELATION => $ruleSimilarity->relation,
RuleRuleRelationsRepository::COLUMN_RATE=> $ruleSimilarity->rate
];
} elseif ($ruleSet->rulesCount>0 && $rule){
if(!$ruleSimilarity){
$ruleSimilarity = $rule;
}
$ruleObj = $this->rulesFacade->findRule($rule);
$ruleParts = [
"ant" => $this->decomposeCedent($ruleObj->antecedent),
"con" => $this->decomposeCedent($ruleObj->consequent)
];
$result['max'] = 0;
$result['rule'] = [
RuleRuleRelationsRepository::COLUMN_RELATION => ''
];

$ruleCompareResults = $this->knowledgeBaseFacade->getRulesComparingResults($rule, $id);
if(count($ruleCompareResults) > 0){
$bestResult = array_slice($ruleCompareResults,0,1)[0];
if($bestResult[RuleRuleRelationsRepository::COLUMN_RATE] > 0){
$result['rule'] = $bestResult;
$result['max'] = $bestResult[RuleRuleRelationsRepository::COLUMN_RATE];
}
}

foreach($this->ruleSetsFacade->findRulesByRuleSet($ruleSet, null) as $ruleSetRule){
if(isset($ruleCompareResults[$ruleSetRule->ruleId])){ // has been already compared and is impossible to have higher rate due to DB ordering
continue;
}
$compareResult = $this->compareRules($ruleParts, $ruleSetRule);
if($compareResult[RuleRuleRelationsRepository::COLUMN_RATE] > $result['max']){
$result['max'] = $compareResult[RuleRuleRelationsRepository::COLUMN_RATE];
$result['rule'] = $compareResult;
}
$compareResult[RuleRuleRelationsRepository::COLUMN_RULE] = $rule;
$compareResult[RuleRuleRelationsRepository::COLUMN_RULE_SET] = $id;
$compareResults[] = $compareResult;
}

if($result['max'] > 0){
try{
$this->knowledgeBaseFacade->addRuleToKBRuleRelation(
$id, $ruleSimilarity,
$result['rule'][RuleRuleRelationsRepository::COLUMN_RULESET_RULE],
$result['rule'][RuleRuleRelationsRepository::COLUMN_RELATION],
$result['max']
);
}catch (\Exception $e){}
}

$this->knowledgeBaseFacade->saveComparingResults($compareResults);
}
$this->sendJsonResponse($result);
}

/**
* Method for comparing two rules
* @param Array $ruleParts parts of Rule for comparing
* @param Array $ruleSetRule Rule from Rule set
* @return Array [ID, relation, comparing rate]
*/
private function compareRules($ruleParts, $ruleSetRule){
$antecedentAttributesCount = $consequentAttributesCount = 0;
$antecedentAttributesSame = $consequentAttributesSame = 0;
$crossAttributesConflict = false;
$ruleSetRuleDecomposed = json_decode($ruleSetRule->decomposed, true);
if(!$ruleSetRuleDecomposed){ // if hasn't rule decomposed yet
$ruleSetRuleDecomposed = [
"ant" => $this->decomposeCedent($ruleSetRule->antecedent),
"con" => $this->decomposeCedent($ruleSetRule->consequent)
];
try{
$this->knowledgeBaseFacade->setDecomposedRuleSetRule($ruleSetRule->ruleId, json_encode($ruleSetRuleDecomposed));
}catch (\Exception $e){}
}
foreach($ruleSetRuleDecomposed['ant'] as $attribute => $value){
if(isset($ruleParts['con'][$attribute]) || array_key_exists($attribute, $ruleParts['con'])){
$crossAttributesConflict = true; // attribute on opposite site of rule
break;
}
if(isset($ruleParts['ant'][$attribute]) || array_key_exists($attribute, $ruleParts['ant'])){
$antecedentAttributesSame++; // same attribute
if($ruleParts['ant'][$attribute] == $value){
$antecedentAttributesSame++; // same value or values bin
}
}
$antecedentAttributesCount++;
}
foreach($ruleSetRuleDecomposed['con'] as $attribute => $value){
if(isset($ruleParts['ant'][$attribute]) || array_key_exists($attribute, $ruleParts['ant'])){
$crossAttributesConflict = true; // attribute on opposite site of rule
break;
}
if(isset($ruleParts['con'][$attribute]) || array_key_exists($attribute, $ruleParts['con'])){
$consequentAttributesSame++; // same attribute
if($ruleParts['con'][$attribute] == $value){
$consequentAttributesSame++; // same value or values bin
}
}
$consequentAttributesCount++;
}
$sameRateAntecedent = $antecedentAttributesSame/($antecedentAttributesCount+count($ruleParts['ant']));
$sameRateConsequent = $consequentAttributesSame/($consequentAttributesCount+count($ruleParts['con']));
$sameRateFinal = $crossAttributesConflict ? 0 : ($sameRateAntecedent+$sameRateConsequent)/2;

return [
RuleRuleRelationsRepository::COLUMN_RULESET_RULE => $ruleSetRule->ruleId,
RuleRuleRelationsRepository::COLUMN_RELATION => $ruleSetRule->relation,
RuleRuleRelationsRepository::COLUMN_RATE => $sameRateFinal
];
}

/**
* Method for decomposing Cedent (mostly used recursively!)
* @param Cedent $cedent for decomposing
* @return Array Attribute => Value
*/
private function decomposeCedent($cedent){
$ruleAttributes = [];
if(!($cedent instanceof Cedent)){
return $ruleAttributes;
}
foreach($cedent->cedents as $childCedent){
$ruleAttributes = $ruleAttributes + $this->decomposeCedent($childCedent);
}
foreach($cedent->ruleAttributes as $ruleAttribute){
if($ruleAttribute->value){
$ruleAttributes[$ruleAttribute->attribute->preprocessing->preprocessingId] = $ruleAttribute->value->valueId;
} elseif($ruleAttribute->valuesBin){
$ruleAttributes[$ruleAttribute->attribute->preprocessing->preprocessingId] = $ruleAttribute->valuesBin->valuesBinId;
} else{
$ruleAttributes[$ruleAttribute->attribute->preprocessing->preprocessingId] = "";
}
}
return $ruleAttributes;
}

#endregion actions for rule comparing

#region injections
/**
* @param RulesFacade $rulesFacade
Expand All @@ -278,6 +476,12 @@ public function injectRulesFacade(RulesFacade $rulesFacade){
public function injectRuleSetsFacade(RuleSetsFacade $ruleSetsFacade){
$this->ruleSetsFacade=$ruleSetsFacade;
}
/**
* @param KnowledgeBaseFacade $knowledgeBaseFacade
*/
public function injectKnowledgeBaseFacade(KnowledgeBaseFacade $knowledgeBaseFacade){
$this->knowledgeBaseFacade=$knowledgeBaseFacade;
}
/**
* @param UsersFacade $usersFacade
*/
Expand Down
10 changes: 10 additions & 0 deletions app/EasyMinerModule/templates/MiningUi/config.latte
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ var Config = new Class({

knowledgeBaseGetRulesUrl: {plink RuleSets:getRules id=>'__RULESETID__',offset=>'-999991999',limit=>'-999992999',order=>'__ORDER__'}.replace('-999991999','__OFFSET__').replace('-999992999','__LIMIT__'),
knowledgeBaseGetRuleSetsUrl: {plink RuleSets:list},
knowledgeBaseGetRulesNamesUrl: {plink RuleSets:getRulesNames id=>'__RULESETID__',miner=>'__MINERID__'},
knowledgeBaseCompareRuleUrl: {plink RuleSets:compareRuleWithRuleset id=>'__RULESETID__',rule=>'__RULE__'},

knowledgeBaseAddRuleSetUrl: {plink RuleSets:new name=>'__NAME__',description=>'__DESCRIPTION__'},
knowledgeBaseRenameRuleSetUrl: {plink RuleSets:rename id=>'__RULESETID__',name=>'__NAME__',description=>'__DESCRIPTION__'},
Expand Down Expand Up @@ -188,6 +190,14 @@ var Config = new Class({
return this.knowledgeBaseGetRulesUrl.replace('__RULESETID__',rulesetId).replace('__OFFSET__',offset).replace('__LIMIT__',limit).replace('__ORDER__',order);
},

getKnowledgeBaseGetRulesNamesUrl: function (rulesetId) {
return this.knowledgeBaseGetRulesNamesUrl.replace('__RULESETID__',rulesetId).replace('__MINERID__',this.getMinerId());
},

getKnowledgeBaseCompareRuleUrl: function (ruleId,rulesetId) {
return this.knowledgeBaseCompareRuleUrl.replace('__RULE__',ruleId).replace('__RULESETID__',rulesetId);
},

getKnowledgeBaseGetRuleSetsUrl: function () {
return this.knowledgeBaseGetRuleSetsUrl;
},
Expand Down
4 changes: 4 additions & 0 deletions app/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ services:
- EasyMinerCenter\Model\EasyMiner\Facades\PreprocessingsFacade
- EasyMinerCenter\Model\EasyMiner\Facades\RuleSetsFacade

- EasyMinerCenter\Model\EasyMiner\Facades\KnowledgeBaseFacade
- EasyMinerCenter\Model\EasyMiner\Repositories\KnowledgeBaseRuleRelationsRepository
- EasyMinerCenter\Model\EasyMiner\Repositories\RuleRuleRelationsRepository

- EasyMinerCenter\Model\EasyMiner\Repositories\DatasourcesRepository
- EasyMinerCenter\Model\EasyMiner\Repositories\DatasourceColumnsRepository
- EasyMinerCenter\Model\EasyMiner\Repositories\MetasourcesRepository
Expand Down
25 changes: 25 additions & 0 deletions app/model/easyminer/entities/KnowledgeBaseRuleRelation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace EasyMinerCenter\Model\EasyMiner\Entities;
use LeanMapper\Entity;

/**
* Class KnowledgeBaseRuleRelation
* @package EasyMinerCenter\Model\EasyMiner\Entities
* @author Přemysl Václav Duben
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*
* @property int $knowledgeBaseRuleRelationId
* @property int $ruleSetId
* @property int $ruleId
* @property int $knowledgeBaseRuleId
* @property string $relation m:Enum(self::RELATION_*)
* @property float $rate
* @property \DateTime|null $resultDate = null
*/
class KnowledgeBaseRuleRelation extends Entity{
const RELATION_POSITIVE='positive';
const RELATION_NEUTRAL='neutral';
const RELATION_NEGATIVE='negative';

}
2 changes: 2 additions & 0 deletions app/model/easyminer/entities/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* @property float|null $lift = null
* @property bool $inRuleClipboard
* @property-read array $basicDataArr
* @property-read string $relation
* @property-read string $decomposed
*/
class Rule extends Entity{

Expand Down
2 changes: 1 addition & 1 deletion app/model/easyminer/entities/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getDataArr(){
'name'=>$this->name,
'description'=>(!empty($this->description)?$this->description:""),
'rulesCount'=>$this->rulesCount,
'lastModified'=>(!empty($this->lastModified))?$this->lastModified->format('c'):null
'lastModified'=>(!empty($this->lastModified))?$this->lastModified->getTimestamp():null
];
}

Expand Down
1 change: 1 addition & 0 deletions app/model/easyminer/entities/RuleSetRuleRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @property Rule $rule m:hasOne
* @property RuleSet $ruleSet m:hasOne
* @property string $relation m:Enum(self::RELATION_*)
* @property string $decomposed
*/
class RuleSetRuleRelation extends Entity{
const RELATION_POSITIVE='positive';
Expand Down
Loading