Schema XML
XMLParserLib
ExtarctionRuleLib
apply_rule_Discrete
apply_rule_Continuous
Schema XML
<?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XMLSpy v2007 sp1 (http://www.altova.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="CT_Vincolo">
<xs:sequence>
<xs:element ref="Node" minOccurs="1" maxOccurs="unbounded" /> </xs:sequence>
</xs:complexType>
<xs:complexType name="CT_Rule"> <xs:sequence>
<xs:element ref="Node" minOccurs="2" maxOccurs="2" /> <xs:element ref="Probability" />
<xs:element ref="Vincolo" minOccurs="0" /> <xs:element ref="EndRule" />
</xs:sequence>
<xs:attribute ref="typeR" use="required" /> </xs:complexType>
<xs:complexType name="CT_Probability"> <xs:attribute ref="val" use="required" /> </xs:complexType>
<xs:complexType name="CT_Node"> <xs:attribute ref="val2" /> <xs:attribute ref="val_st" /> <xs:attribute ref="val1" />
<xs:attribute ref="type" use="required" /> <xs:attribute ref="typeValue" use="required" /> <xs:attribute ref="sign2" />
<xs:attribute ref="sign1" use="required" /> <xs:attribute ref="name" use="required" /> </xs:complexType>
<xs:complexType name="CT_EndRule" />
<xs:complexType name="CT_Bayesian_causal_map"> <xs:sequence>
<xs:element ref="Rule" maxOccurs="unbounded" /> </xs:sequence>
<xs:attribute ref="number_of_attributes" use="required" /> </xs:complexType>
<xs:attribute name="val_st" type="xs:string" /> <xs:attribute name="val2" type="xs:float" /> <xs:attribute name="val1" type="xs:float" /> <xs:attribute name="val" type="xs:float" /> <xs:attribute name="typeValue"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="string" /> <xs:enumeration value="float" /> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="type"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="discrete" /> <xs:enumeration value="continuous" /> </xs:restriction>
</xs:simpleType> </xs:attribute> <xs:attribute name="typeR"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="constrained" /> <xs:enumeration value="normal" /> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="sign2"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="<" /> <xs:enumeration value=">" /> <xs:enumeration value="=" /> <xs:enumeration value="<=" /> <xs:enumeration value=">=" /> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="sign1"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="<" /> <xs:enumeration value=">" /> <xs:enumeration value="=" /> <xs:enumeration value="<=" /> <xs:enumeration value=">=" /> </xs:restriction> </xs:simpleType> </xs:attribute>
<xs:attribute name="number_of_attributes" type="xs:integer" /> <xs:attribute name="name" type="xs:string" />
<xs:element name="Vincolo" type="CT_Vincolo" /> <xs:element name="Rule" type="CT_Rule" />
<xs:element name="Probability" type="CT_Probability" /> <xs:element name="Node" type="CT_Node" />
<xs:element name="EndRule" type="CT_EndRule" />
<xs:element name="Bayesian_causal_map" type="CT_Bayesian_causal_map" />
XMLParserLib
XMLParser.h
#pragma once
#include "..\..\src\XMLParser\Stdafx.h" #include <ExtractionRule.h>
using namespace ExtractionRule; namespace XMLParser {
class MyParserXML { private :
//file da parsare const char * filename; //nome nodo implicante string namenode1; //nome nodo implicato string namenode2; //Hash restituita Hash *myTH; //Regola Rule *rule; //Nodo Node *node;
//utilizzata durante l'inserimento di un nodo //nella regola per sapere se tale nodo
//va inserito come implicante o come implicato bool first_time;
//true il prossimo nodo va inserito come nella regola //come vincolo
bool vincolo;
//numero regole inserite correttamente int count;
//Parsing del file
void ExtractItemFromDomTree(DOMNode *n); public :
//Costruttore
MyParserXML(const char * filename);
//Inizializza il parser e crea la tabella Hash //contenete la mappa causale
Hash * Initialize_And_Parsing(); };
XMLParser.cpp
#include "stdafx.h"
#include "..\..\include\XMLParser\XMLParser.h"
XMLParser::MyParserXML::MyParserXML(const char *filename) { this->filename=filename; } void XMLParser::MyParserXML::ExtractItemFromDomTree(DOMNode *n) { DOMNode *child; string name_Attribute; string value_Attribute; DOMNamedNodeMap * pAttributes; int nSize=0; string name_Node; if (n) { if (n->getNodeType() == DOMNode::ELEMENT_NODE) { name_Node = XMLString::transcode(n->getNodeName()); if(name_Node=="Bayesian_causal_map") { pAttributes = n->getAttributes(); DOMAttr * pAttributeNode=(DOMAttr*)pAttributes->item(0); // get attribute name
name_Attribute = XMLString::transcode(pAttributeNode->getName());
// get attribute value
value_Attribute = XMLString::transcode(pAttributeNode->getValue());
//creo tabella hash
myTH = new Hash((int)(atof(value_Attribute.c_str()))); }
if(name_Node=="Rule"){
pAttributes = n->getAttributes();
DOMAttr *pAttributeNode=(DOMAttr*)pAttributes->item(0); // get attribute name
name_Attribute = XMLString::transcode(pAttributeNode->getName());
// get attribute value
value_Attribute = XMLString::transcode(pAttributeNode->getValue()); rule->set_Normal_Constrained(value_Attribute); } if(name_Node=="Node") { pAttributes = n->getAttributes(); int nSize = pAttributes->getLength(); for(int i=0;i<nSize;++i) {
DOMAttr * pAttributeNode = (DOMAttr*) pAttributes->item(i);
// get attribute name
XMLString::transcode(pAttributeNode->getName());
// get attribute value
value_Attribute = XMLString::transcode(pAttributeNode->getValue()); if(name_Attribute=="name")node->set_Name(value_Attribute); if (name_Attribute=="type") node->set_Type(value_Attribute); if (name_Attribute=="sign1") node->set_Sign1(value_Attribute); if (name_Attribute=="sign2") node->set_Sign2(value_Attribute); if (name_Attribute=="typeValue")node->set_TypeValue(value_Attribute); if (name_Attribute=="val1") node->set_Value1((float) (atof(value_Attribute.c_str()))); if (name_Attribute=="val_st") node->set_Value1(value_Attribute); if (name_Attribute=="val2") node->set_Value2((float)(atof(value_Attribute.c_str()))); } if(vincolo) { rule->set_Node_constrained(*node); node->reset_node(); } else { if(first_time){ rule->set_Node1(*node); namenode1=node->get_Name(); first_time=false; node->reset_node(); } else { rule->set_Node2(*node); namenode2=node->get_Name(); first_time=true; node->reset_node(); } } } if(name_Node=="Probability") { pAttributes = n->getAttributes();
DOMAttr * pAttributeNode = (DOMAttr*) pAttributes->item(0);
// get attribute name
name_Attribute = XMLString::transcode(pAttributeNode->getName());
// get attribute value
value_Attribute = XMLString::transcode(pAttributeNode->getValue()); rule->set_Prob((float)(atof(value_Attribute.c_str()))); } if(name_Node=="Vincolo"){ //rule->add_Constrained(); vincolo=true; } if(name_Node=="EndRule"){ //cout<<namenode1<<endl; //cout<<namenode2<<endl; //rule->printRule();
if(rule->is_Correct()) { myTH->insert_implicante(namenode1,rule); myTH->insert_implicato(namenode2,rule); count=count+1; } else {
cout<<"The Rule is not Correct : "; rule->ToTEXT(); delete rule; } namenode1="null"; namenode2="null"; vincolo=false; rule =new Rule(); } } for (child=n->getFirstChild();child!=0;child=child->getNextSibling()) MyParserXML::ExtractItemFromDomTree(child); } } ExtractionRule::Hash* XMLParser::MyParserXML::Initialize_And_Parsing(){ const char* xmlFile = 0;
node = new Node(); rule =new Rule(); count=0; first_time=true; vincolo=false; //validazione di DEFALUT /* AbstractDOMParser::ValSchemes valScheme = AbstractDOMParser::Val_Auto;
bool doNamespaces = false; bool doSchema = false;
bool schemaFullChecking = flase; */
//VALIDA SEMPRE IN BASE ALLO SCHEMA SCRITTO NEL FILE XML AbstractDOMParser::ValSchemes valScheme =
AbstractDOMParser::Val_Always; bool doNamespaces = true; bool doSchema = true;
bool schemaFullChecking = true; bool doList = false;
bool errorOccurred = false; bool recognizeNEL = false; char localeStr[64];
memset(localeStr, 0, sizeof localeStr); // Initialize the XML4C system
try {
if (strlen(localeStr)) {
} else { XMLPlatformUtils::Initialize(); } if (recognizeNEL) { XMLPlatformUtils::recognizeNEL(recognizeNEL); } }
catch (const XMLException& toCatch) {
cerr << "Error during initialization! :\n" << StrX(toCatch.getMessage()) <<endl;
//return 1; }
// Instantiate the DOM parser.
static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull }; DOMImplementation *impl =
DOMImplementationRegistry::getDOMImplementation(gLS); DOMBuilder *parser = ((DOMImplementationLS*)impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0); //Grammar *g=parser->loadGrammar("prova.xsd",0); parser->setFeature(XMLUni::fgDOMNamespaces, doNamespaces); parser->setFeature(XMLUni::fgXercesSchema, doSchema); parser->setFeature(XMLUni::fgXercesSchemaFullChecking, schemaFullChecking); if (valScheme == AbstractDOMParser::Val_Auto) { parser->setFeature(XMLUni::fgDOMValidateIfSchema, true); }
else if (valScheme == AbstractDOMParser::Val_Never) {
parser->setFeature(XMLUni::fgDOMValidation, false); }
else if (valScheme == AbstractDOMParser::Val_Always) {
parser->setFeature(XMLUni::fgDOMValidation, true); }
// enable datatype normalization - default is off
parser->setFeature(XMLUni::fgDOMDatatypeNormalization, true); // And create our error handler and install it
DOMCountErrorHandler errorHandler; parser->setErrorHandler(&errorHandler);
// Get the starting time and kick off the parse of the indicated // file. Catch any exceptions that might propogate out of it. //ifstream fin;
xmlFile =filename;
errorHandler.resetErrors();
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0; try
{
// reset document pool
parser->resetDocumentPool(); doc = parser->parseURI(xmlFile); }
catch (const XMLException& toCatch) {
cerr << "\nError during parsing: '" << xmlFile << "'\n" << "Exception message is: \n"
<< StrX(toCatch.getMessage()) << "\n" <<endl; errorOccurred = true;
}
catch (const DOMException& toCatch) {
const unsigned int maxChars = 2047; XMLCh errText[maxChars + 1];
cerr << "\nDOM Error during parsing: '" << xmlFile << "'\n"
<< "DOMException code is: " << toCatch.code << endl;
if (DOMImplementation::loadDOMExceptionMsg(toCatch.code, errText, maxChars))
cerr << "Message is: " << StrX(errText) << endl; errorOccurred = true;
}
catch (...) {
cerr << "\nUnexpected exception during parsing: '" << xmlFile << "'\n";
errorOccurred = true; }
// Extract the DOM tree, get the list of all the elements and report the
// length as the count of elements. if (errorHandler.getSawErrors()) {
cout << "\nErrors occurred. \n" << endl;
cout<<"Tree build without Domain Rule. \n"<<endl; errorOccurred = true; } else { if (doc) { MyParserXML::ExtractItemFromDomTree((DOMNode*)doc->getDocumentElement()); cout<<"---"<<endl; cout<<"Number of rule inserted : "<<count<<endl; cout<<"---"<<endl;
} }
// Delete the parser itself. Must be done prior to calling Terminate, below.
parser->release();
// And call the termination method XMLPlatformUtils::Terminate(); delete node;
delete rule;
if(errorOccurred ) myTH=NULL; if((count==0)&&(!errorOccurred)) {
cout<<"All RULE are not correct.\n"<<endl;
cout<<"Tree build without Domain Rule. \n"<<endl; delete myTH; myTH=NULL; } return myTH; } DOMCountErrorHandler::DOMCountErrorHandler(): fSawErrors(false) { } DOMCountErrorHandler::~DOMCountErrorHandler() { } // ---// DOMCountHandlers: Overrides of the DOM ErrorHandler interface // ---bool DOMCountErrorHandler::handleError(const DOMError& domError) {
fSawErrors = true;
if (domError.getSeverity() == DOMError::DOM_SEVERITY_WARNING) cerr << "\nWarning at file ";
else if (domError.getSeverity() == DOMError::DOM_SEVERITY_ERROR) cerr << "\nError at file ";
else
cerr << "\nFatal Error at file ";
cerr << StrX(domError.getLocation()->getURI())
<< ", line " << domError.getLocation()->getLineNumber() << ", char " << domError.getLocation()->getColumnNumber() << "\n Message: " << StrX(domError.getMessage()) << endl; return true; } void DOMCountErrorHandler::resetErrors() { fSawErrors = false; }
ExtractionRuleLib
ExtractionRule.h
#pragma once
#include "..\..\src\ExtractionRule\Stdafx.h" namespace ExtractionRule {
//Definisce il tipo del valore che può assumere //un attributo
struct Value { string st; float f; };
//struct contenete la probabilità, l'indice(DV) //dell'attributo implicato nella regola
//e l'indice della regola in esame struct PossibleRulesDiscrete {
float prob;
unsigned int index_of_attribute; int index_rule;
};
//come sopra ma per attributidi tipo continuo //con doppia limitazione
struct PossibleRulesContinuos_2 { float prob; int index_min_value1_of_attribute; int index_min_value2_of_attribute; int index_equal1; int index_equal2; string sign1; string sign2; int index_rule;
vector<unsigned int> number_of_istances_for_value; };
//come sopra ma per attributidi tipo continuo //con singola limitazione
struct PossibleRulesContinuos_1 { float prob; int index_min_value_of_attribute; int index_equal; string sign; int index_rule;
vector<unsigned int> number_of_istances_for_value; };
//Classe rappresentante l'attributo class Node {
private :
string name;
//tipo attributo (discrete, conituous) string type;
//tipo valore attributo (float, string) string type_value;
//segno attributo (<,<=,=,>,>=) string sign1;
//segno attributo (<,<=,=,>,>=)
//(in caso di attributo discreto o continuo // con singola limitazione non significativo) string sign2;
//valore1 attributo Value value1;
//valore1 attributo (in caso di attributo discreto o continuo // con singola limitazione non significativo)
float value2; public :
//construttore
Node(string name="null",string type_value="null",string
sign1="null", string value1 ="null",string type="null",string sign2="null",float value2=INIT);
//Distruttore ~Node();
//Setta il nomme dell attributo void set_Name(string n);
//Setta il tipo del valore dell attributo void set_TypeValue(string t);
//Setta il tipo dell attributo void set_Type(string t);
//Setta il valore 1 dell attributo come float void set_Value1(float v1);
//Setta il valore 1 dell attributo come stringa void set_Value1(string v1);
//Setta il valore 2 dell attributo void set_Value2(float v2);
//Setta il segno 1 dell attributo void set_Sign1(string s1);
//Setta il segno 2 dell attributo void set_Sign2(string s2);
//Resetta il node, mettendo tutti i suoi campi a null o e zero void reset_node();
//controlla se un node è nullo o meno bool is_Null();
//Restituisce il nome dell attributo string get_Name();
//Restituisce il tipo del valore dell attributo string get_TypeValue();
//Restituisce il tipo dell attributo string get_Type(); //Restituisce il segno1 string get_Sign1(); //Restituisce il valore 1 Value get_Value1(); //Restituisce il segno2 string get_Sign2(); //Restituisce il valore 2 float get_Value2();
//Controlla se un node è corretto o meno bool is_Correct();
//Controlla se un node è discreto bool is_Discrete();
//Stampa il node void printNode();
//Viualizza il node nella forma semplice (nomenodo segno valore) void ToTEXT();
//Compatta il node void compact_Node();
//Controlla se il node ha doppia limitazione o meno bool has_double_Limitation();
//Controlla se due nodi sono discordi o concordi static bool are_Discordant(Node *node1,Node *node2);
//Verifica se l'insieme dei valori di node contiene totalmente i valori che current_root puo assumere
static bool Is_Contained(Node *node,Node *c_root); //Verifica se due nodi sono uguali
static bool Is_Equal(Node *node1,Node *node2);
//Intersezione in cui almmeno uno dei due nodi ha segno " = " static bool Intersection_S_E(Node *node_sign_uguale, Node *nodeAnts);
//Calcola l intersezione aventi attributi entrambi con doppia limitazione
static bool Intersection_D_L(Node *nodeImplicato,Node *nodeBelongAntecedents);
//Intersezione di due insiemi con limitazioni diversa, uno con doppia limitazione l altro con limitazione
//singola
static bool Intersection_Dif_L(Node *nodeSingleLimitation, Node * nodeDoubleLimitation);
//Calcola l intersezione di attributi aventi entrambi limitazione singola
static bool Intersection_S_L(Node *nodeImplicato,Node *nodeBelongAntecedents);
//Verifico se i valori dell attributo implicato hanno un INTERSEZIONE vuota (o non vuota ma ovvia)
//con l insieme dei valori che l attributo puo assumere rispetto al cammino considerato
//esempoio: B<=b nei PREDECESSORI e A<=a -> B<b1, con b1>b - intersezione non vuota ma ovvia
-//caso in cui un nodo appare solo una volta nell insieme dei predecessori
static bool Intersection_Empty_Or_Obvious(Node *nodeImplicato, Node *nodeBelongAntecedents);
};
//classe rappresentante la regola di dominio class Rule { private : //implicanti vector<Node> node1; //possibili vincoli vector<Node> node_constrained; //implicato Node node2; //porbabilità
float prob;
//tipo regola (normal, constrained) string typeR;
public :
//costruttore regola con vincoli
Rule(Node node1,Node node2,float prob,string typeR,Node node_constrained);
//costruttore regola senza vincoli
Rule(Node node1,Node node2,float prob,string typeR); //cosrtuttore regola vuota
Rule(); ~Rule();
//setta l impicante della regola void set_Node1(Node node);
//setta l implicato della regola void set_Node2(Node node);
//aggiunge un vincolo alla regola void set_Node_constrained(Node node); //setta la probabilità della regola void set_Prob(float prob);
//setta il tipo della regola
void set_Normal_Constrained(string n_c); //resetta la regola tutto a null
void reset_Rule();
//restitusice gli implicanti di una regola vector<Node> get_Node1();
//restituisce l implicato della regola Node get_Node2();
//restituisce la probabiltà della regola float get_Prob();
//restituisce i vincoli della regola vector<Node> get_Node_constrained(); //restituisce il tipo della regola string get_Type();
//stampa la regola void printRule();
//viualizza la regola nella forma semplice (nomenodo segno valore - > nomenodo segno valore)
void ToTEXT();
//bool controlla se la regola è di tipo normal o meno bool is_Normal();
//controlla che la regola sia corretta bool is_Correct();
//controlla che i vincoli della regola siano corretti bool constrained_is_Correct();
//aggiunge il vector per i vincoli void add_Constrained();
//elimina i vincoli di una regola void delete_Constrained();
};
//classe rappresentante la tabella Hash class Hash {
struct HashTableItem { string name;
//regole in cui l'elemnto selezionato è presente come implicato
vector<Rule*> *implicato;
//regole in cui l'elemnto selezionato è presente come implicante vector<Rule*> *implicante; }; private: //tabella Hash vector<vector<HashTableItem>> *myHT; //vector temporaneo vector<Rule*> *vector_empty; //funzione di hash
int hashvalue(string key); //dimensione tabella hash int size_hash ;
//hash value int hv ; //indice int index;
//usato per la ricerca di un elemento int flag ;
//posizione elemento hash int pos_find ;
//true se un elemento è già stato inserito nella hash o no bool exist; public: //Costruttore Hash(int size_hash); //Distruttore ~Hash();
//Restituisce tutte le regole aventi l attributo key come implicato
vector<Rule*>* get_implicato(string key);
//Restituisce tutte le regole aventi l attributo key come implicante
vector<Rule*>* get_implicante(string key);
//Inserisce la regola nel Vector degli implicanti dell attributo key
void insert_implicante(string key,Rule *rule);
//Inserisce la regola nel Vector degli implicati dell attributo key
void insert_implicato(string key,Rule *rule); //Restituisce la dimensione della tabella Hash int get_SizeHash();
} ;
//struct rappresentante gli implicanti memorizzati e da inserire
//nella testa di una simple implications struct Implying_Complex{ //node implicante Node implyingNode; //probabilità regola float probability; }; private:
//radice del sottoalbero corrente Node *current_root;
//nodo nullo
//se diverso da null Node node_null;
vector<Node>* antecedents; Hash *tableHash;
unsigned int pos;
//Date due regole restituisce true se sono uguali flase altrimenti
bool Is_Equal_Rule(Rule *r1,Rule *r2);
//Dati due nodi restituisce true se sono identici altrimenti false
//bool Is_Equal(Node *node1,Node *node2);
//Verfica se node contiene totalmente i valori in c_root //bool Is_Contained(Node *node,Node *c_root);
//Verfica che il vincolo node sia verificato nei predecessori
bool Constraint_Control(Node *node);
//Verifica se node appartiene o meno all insieme dei predecessori
bool Belong_Antecedents(Node *node);
//Verifica se l insieme dei vincoli è verificato bool Constraint_Is_Verified(vector<Node>* nodev); bool Intersection_Empty_Or_Obvious(Node
*nodeImplicato,vector<Rule> *set_rule);
//Verfica se node appartiene come implicato ad un insieme di regole
bool Belong_Implied(Node *node,vector<Rule> *simple_complex_Rule);
//Verfica se node appartiene come implicato ad un
insieme di regole, esclusa un regola data come parametro bool Belong_Indirect(vector<Rule> *extended_Rule,Rule rule1,Rule *rule);
//Trova node fra i predecessori restituendo quello coi valori piu ristretti
Node Find_Node(Node *node);
//Calcola la probabilità di una complex float Compute_Prob(vector<Implying_Complex>* vectorOfImplyingComplex);
float computeFixedValue(vector<int>* eventString,vector<Implying_Complex>* vectorOfImplyingComplex);
//Calcola le combinazioni di n eventi ad i ad i int get_CombinationNumber(int n,int i);
//Calcola il fattoriale di unNumero int fattoriale (int unNumero); //Calcola la potenza di un numero
int pow(int numero,int esponente);
//esegue la compattazione di un node continuo coi valori assunti nei predecessori
void Compact(Node *node);
//cerca ed estrae tutte le possibili Simple Implications vector<Rule>* Find_Simple_Implications();
//cerca ed estrae tutte le possibili Complex Implications
vector<Rule>* Find_Complex_Implications(vector<Rule> *e_Rule);
//cerca ed estrae tutte le possibili Extended Simple Implications
vector<Rule>*
Find_Extended_Simple_Implications(vector<Rule> *simple_complex_Rule);
//cerca ed estrae tutte le possibili Indirect Complex //Implications vector<Rule>* Find_Indirect_Complex_Implications(vector<Rule> *extended_Rule); public: //costruttore FindRule(Node *current_root,vector<Node>* antecedents,Hash* tableHash); //distruttore ~FindRule();
//Restituisce il node radice del sotto albero corrente Node* get_CurrentRoot();
//restituisce node_null, contiene il node risultao dalla //ricerca fra i predecessori
Node get_Node_null(); //setta il node_null
void set_Node_null(Node node);
//restituisce l insieme dei predecessori vector<Node>* get_Antecedents();
//restituisce la tabella hash rappresentante la mappa Hash* get_Table();
//restituisce tutte le regole vector<Rule>* Find_Rules();
//restituisce le regole stampandole
//nella forma semplice implicante=valore -> //implicato=valore, prob=p
vector<Rule>* Find_Rules_NO_PRINT(); };
}
#include "stdafx.h"
#include "..\..\include\ExtractionRule\ExtractionRule.h"
ExtractionRule::Node::Node(string name,string type_value,string sign1,string value1,string type,string sign2,float value2){
this->name=name; this->type=type; this->type_value=type_value; if(type_value=="float"){ this->value1.f=(float)atof(value1.c_str()); this->value1.st="null"; } else { this->value1.st=value1; this->value1.f=INIT; } this->value2=value2; this->sign1=sign1; this->sign2=sign2; } ExtractionRule::Node::~Node(){} void ExtractionRule::Node::printNode( ){ cout<<"nome :"<<this->name<<endl; cout<<"tipo :"<<this->type<<endl;
cout<<"tipo valore :"<<this->type_value<<endl; cout<<"segno1 :"<<this->sign1<<endl; if(this->type_value=="float") cout<<"valore1 :"<<this->value1.f<<endl; else cout<<"valore1 :"<<this->value1.st<<endl; if(this->has_double_Limitation()){ cout<<"segno2 :"<<this->sign2<<endl; cout<<"valore2 :"<<this->value2<<endl; } } void ExtractionRule::Node::ToTEXT(){ if(!this->has_double_Limitation()) if(this->type_value=="float")
cout<<this->name<<" "<<this->sign1<<" "<<this->value1.f; else
cout<<this->name<<" "<<this->sign1<<" "<<this->value1.st; else { cout<<this->value1.f; if(this->sign1==">") cout<<" < "; else cout<<" <= "; cout<<this->name; if(this->sign2=="<") cout<<" < "; else cout<<" <= ";
cout<<this->value2; } } void ExtractionRule::Node::set_Name(string n){ this->name=n; } void ExtractionRule::Node::reset_node() { this->name="null"; this->type="null"; //if (this->type_value=="float") this->value1.f=INIT; // else this->value1.st="null"; this->value2=INIT; this->sign1="null"; this->sign2="null"; } void ExtractionRule::Node::set_TypeValue(string t) { this->type_value=t; } void ExtractionRule::Node::set_Type(string t) { this->type=t; } void ExtractionRule::Node::set_Value1(float v1) { this->value1.f=v1; } void ExtractionRule::Node::set_Value1(string v1) { this->value1.st=v1; } void ExtractionRule::Node::set_Value2(float v2) { this->value2=v2; } void ExtractionRule::Node::set_Sign1(string s1) { this->sign1=s1; } void ExtractionRule::Node::set_Sign2(string s2) { this->sign2=s2; } string ExtractionRule::Node::get_Name() { return this->name; } string ExtractionRule::Node::get_TypeValue(){
return this->type_value; } string ExtractionRule::Node::get_Type(){ return this->type; } ExtractionRule::Value ExtractionRule::Node::get_Value1(){ return this->value1; } float ExtractionRule::Node::get_Value2(){ return this->value2; } string ExtractionRule::Node::get_Sign1() { return this->sign1; } string ExtractionRule::Node::get_Sign2() { return this->sign2; } bool ExtractionRule::Node::is_Discrete(){ if(this->type=="discrete") return true; else return false;
} bool ExtractionRule::Node::is_Correct(){ if((this->type_value=="string")&&(this->value1.f!=INIT)) return false; if((this->type_value=="float")&&(this->value1.st!="null")) return false; if((this->value1.f==INIT)&&(this->value1.st=="null")) return false; if(this->is_Discrete()) if((this->sign2=="null")&&(this->value2==INIT)&&(this->sign1=="=")&&(this->type_value!="null")) return true; else return false;
else // nodo continuo
if(this->type_value=="string") return false; if(this->has_double_Limitation())
if(((>sign1==">")||(>sign1==">="))&&((>sign2=="<")||(>sign2=="<=")) && (>value1.f < this->value2)) return true;
else return false; else return true;
}
bool ExtractionRule::Node::has_double_Limitation(){ if(this->sign2=="null") return false;
else return true; }
bool ExtractionRule::Node::are_Discordant(Node *node1,Node *node2){ if ( (((node1->get_Sign1()=="<")||(node1->get_Sign1()=="<="))&& ((node2->get_Sign1()==">")||(node2->get_Sign1()==">="))) || (((node1->get_Sign1()==">")||(node1->get_Sign1()==">="))&& ((node2->get_Sign1()=="<")||(node2->get_Sign1()=="<="))) ) return true;
else return false; }
//esempio x >= 15 e x < 15.1 - > x = 15 void ExtractionRule::Node::compact_Node(){ if(this->type_value=="float"){
if ((this->sign1==">=")&&(this->sign2=="<")){
if((float)(this->value1.f + CIFRE_DECIMALI) == this->value2 ) { this->set_Sign1("="); this->set_Sign2("null"); this->set_Value2(INIT); } } if((this->sign1 ==">")&&(this->sign2 =="<=")){
if((float)(this->value1.f+ CIFRE_DECIMALI) == this->value2) { this->set_Value1((float)(this->value1.f+ CIFRE_DECIMALI)); this->set_Sign1("="); this->set_Sign2("null"); this->set_Value2(INIT); } } } } bool ExtractionRule::Node::is_Null(){
if(>name=="null")&& >type=="null")&& >type_value=="null")&& >value2==INIT)&& (this->sign1=="null")&&
(this->sign2=="null")&&(this->value1.st=="null")&&(this->value1.f==INIT)) return true; else return false;
}
//sono esattamente uguali
if(Is_Equal(c_root,node)) return true; else { if((node->get_Sign1()=="=")&&(c_root- >get_Sign1()=="=")&&(node->get_Value1().f==c_root->get_Value1().f))return true; if((node->get_Sign1()=="=")||(c_root->get_Sign1()=="=")){ if(c_root->get_Sign1()=="=") return !Intersection_S_E(c_root,node); else return false; }else{
//entrambi con SINGOLA limitazione
if (((!c_root->has_double_Limitation())&&(!node->has_double_Limitation()))){
//controllo se hanno lo stesso segno ovvero entrambi minore o //maggiore
if((node->get_Sign1()==c_root->get_Sign1())) {
if(((node->get_Sign1()==">")||(node->get_Sign1()==">="))&& (c_root->get_Value1().f>=node->get_Value1().f)) return true;
if(((node->get_Sign1()=="<")||(node->get_Sign1()=="<="))&& (c_root->get_Value1().f<=node->get_Value1().f)) return true;
return false; }
//segno diverso ovvero uno < l altro <= o col //maggiore
else {
//************************************************************ // il caso in cui hanno limitazione SINGOLA e segno DISCORDE //non viene
//considerato perchè in tal caso il risultato è sempre FALSE //************************************************************ //ese mpio node <=19 , c_root < ? basta che c_root sia minore di 19+CIFRE_DECIMALI è contenuto in node
//node <= root <
if((((node->get_Sign1()=="<=")&&(c_root->get_Sign1()=="<")))){ if(c_root->get_Value1().f<=(float)(node->get_Value1().f + CIFRE_DECIMALI) ) return true;
else return false; }
else {
//node < root <=
if((((node->get_Sign1()=="<")&&(c_root->get_Sign1()=="<=")))){ if(c_root->get_Value1().f<node->get_Value1().f) return true; else return false;
} else {
//node >= root >
if(c_root->get_Value1().f >= (float)(node->get_Value1().f – CIFRE_DECIMALI)) return true;
else return false; }
else {
//node > root >=
if((((node->get_Sign1()==">")&&(c_root->get_Sign1()==">=")))){ if(c_root->get_Value1().f>node->get_Value1().f) return true; else return false;
}
else return false; } } } } } else { //---TUTTI I CASI CON ENTRAMBI DOPPIA LIMITAZIONE FUNZIONANO
CORRETTAMENTE
//----hanno DOPPIA LIMITAZIONE ENTRAMBI ---if
(((c_root->has_double_Limitation())&&(node->has_double_Limitation()))) { //hanno gli stessi segni
if((node->get_Sign1()==c_root->get_Sign1())&&(node->get_Sign2()==c_root->get_Sign2())){
if ((c_root->get_Value1().f>=node->get_Value1().f)&&(c_root->get_Value2()<=node->get_Value2())) return true;
else return false; } else { //node <x< if((node->get_Sign1()==">")&&(node->get_Sign2()=="<")){ //c_root <=x<= if((c_root->get_Sign1()==">=")&&(c_root->get_Sign2()=="<=")){ if((c_root->get_Value1().f>node->get_Value1().f)&&(c_root->get_Value2()<node->get_Value2())) return true;
else return false; }
//c_root <=x<
if((c_root->get_Sign1()==">=")&&(c_root->get_Sign2()=="<")){ if((c_root->get_Value1().f>node->get_Value1().f)&&(c_root->get_Value2()<=node->get_Value2())) return true;
else return false; }
//c_root <x<=
if((c_root->get_Sign1()==">")&&(c_root->get_Sign2()=="<=")){ if((c_root->get_Value1().f>=node->get_Value1().f)&&(c_root->get_Value2()<node->get_Value2())) return true;
else return false; }else return false; }
//node <=x<
if((node->get_Sign1()==">=")&&(node->get_Sign2()=="<")){ //c_root <x<=
if((c_root->get_Value1().f >= (float)(node->get_Value1().f -
CIFRE_DECIMALI))&&(c_root->get_Value2()<node->get_Value2())) return true;
else return false; } //c_root <x< if((c_root->get_Sign1()==">")&&(c_root->get_Sign2()=="<")){ if((c_root->get_Value1().f >= (float)(node->get_Value1().f - CIFRE_DECIMALI))&&(c_root->get_Value2()<=node->get_Value2())) return true;
else return false; } //c_root <=x<= if((c_root->get_Sign1()==">=")&&(c_root->get_Sign2()=="<=")){ if((c_root->get_Value1().f>=node- >get_Value1().f)&&(c_root->get_Value2()<node->get_Value2())) return true;
else return false; }else return false; } //node <x<= if((node->get_Sign1()==">")&&(node->get_Sign2()=="<=")){ //c_root <=x< if((c_root->get_Sign1()==">=")&&(c_root->get_Sign2()=="<")){ if((c_root->get_Value1().f>node->get_Value1().f)&&(c_root->get_Value2() <=(float)(node->get_Value2() + CIFRE_DECIMALI))) return true;
else return false; }
//c_root <x<
if((c_root->get_Sign1()==">")&&(c_root->get_Sign2()=="<")){ if((c_root->get_Value1().f>=node->get_Value1().f) &&(c_root->get_Value2() <= (float)(node-&&(c_root->get_Value2() + CIFRE_DECIMALI))) return true;
else return false; }
//c_root <=x<=
if((c_root->get_Sign1()==">=")&&(c_root->get_Sign2()=="<=")){ if((c_root->get_Value1().f>node->get_Value1().f)&&(c_root->get_Value2()<=node->get_Value2())) return true;
else return false; }else return false; } //node <=x<= if((node->get_Sign1()==">=")&&(node->get_Sign2()=="<=")){ //c_root <=x< if((c_root->get_Sign1()==">=")&&(c_root->get_Sign2()=="<")){ if((c_root->get_Value1().f>=node->get_Value1().f)&&(c_root->get_Value2() <= (float)(node-if((c_root->get_Value1().f>=node->get_Value1().f)&&(c_root->get_Value2() + CIFRE_DECIMALI))) return true;
}
//c_root <x<
if((c_root->get_Sign1()==">")&&(c_root->get_Sign2()=="<")){ if((c_root->get_Value1().f>=>get_Value1().f-CIFRE_DECIMALI))&&(c_root->get_Value2() <= (float)(node->get_Value2() + CIFRE_DECIMALI))) return true;
else return false; } //c_root <x<= if((c_root->get_Sign1()==">")&&(c_root->get_Sign2()=="<=")){ if((c_root->get_Value1().f >= (float)(node->get_Value1().f - CIFRE_DECIMALI))&&(c_root->get_Value2()<=node->get_Value2())) return true;
else return false; }else return false; } else return false; }
//-- --- FINE DOPPIA LIMITAZIONE PER ENTRAMBI ---}
//--- - c_root DOPPIA LIMITAZIONE // node LIMITAZIONE SINGOLA -else {
if(!node->has_double_Limitation()){
//se in un nodo con doppia limitazione il primo segno è< 0 <= il nodo è considerato nullo
//if((c_root->get_Sign1()=="<")||(c_root->get_Sign1()=="<=")) return false;
if(node->get_Sign1()=="<"){
if((c_root->get_Sign2()=="<")&&(c_root->get_Value2()<=node->get_Value1().f)) return true;
if((c_root->get_Sign2()=="<=")&&(c_root->get_Value2()<node->get_Value1().f)) return true;
return false; }
if(node->get_Sign1()=="<="){
if((c_root->get_Sign2()=="<")&&(c_root->get_Value2()<= (float) (node->get_Value1().f+CIFRE_DECIMALI))) return true;
if((c_root->get_Sign2()=="<=")&&(c_root->get_Value2()<=node->get_Value1().f)) return true;
return false; }
if(node->get_Sign1()==">"){
if((c_root->get_Sign1()==">")&&(c_root->get_Value1().f>=node->get_Value1().f)) return true;
if((c_root->get_Sign1()==">=")&&(c_root->get_Value1().f>node->get_Value1().f)) return true;
return false; }
if(node->get_Sign1()==">="){
if((c_root->get_Sign1()==">")&&(c_root->get_Value1().f >= (float) (node->get_Value1().f-CIFRE_DECIMALI))) return true;
if((c_root->get_Sign1()==">=")&&(c_root->get_Value1().f>=node->get_Value1().f)) return true;
return false; }
else return false; }
else return false; // caso in cui node doppio limitato e root con singola limitazione
// node non puo mai contenere root }
} }
}//ELSE PRINCIPALE QUELLO DOPO IS EQUAL }
bool ExtractionRule::Node::Is_Equal(Node *node1,Node *node2) { // sono identici if(>get_Name()==node2->get_Name())&& (node1- >get_Type()==node2->get_Type())&&(node1->get_Sign1()==node2->get_Sign1())&&(node1->get_Sign2()==node2->get_Sign2()) &&(node1->get_Value2()==node2->get_Value2())&&(node1->get_TypeValue()==node2->get_TypeValue())) if(node1->get_TypeValue()=="string") if(node1->get_Value1().st==node2->get_Value1().st) return true;
else return false; else
if(node1->get_Value1().f==node2->get_Value1().f) return true;
else return false; else
return false; }
bool ExtractionRule::Node::Intersection_S_E(Node *node_sign_uguale, Node *nodeAnts){
if(!nodeAnts->has_double_Limitation()){ if (nodeAnts->get_Sign1()==">=") {
if(nodeAnts->get_Value1().f <= node_sign_uguale->get_Value1().f) return false;
else return true; }
if (nodeAnts->get_Sign1()==">") {
if(nodeAnts->get_Value1().f <= (float)(node_sign_uguale->get_Value1().f - CIFRE_DECIMALI)) return false;
else return true; }
if(nodeAnts->get_Value1().f >= node_sign_uguale->get_Value1().f) return false;
else return true; }
if (nodeAnts->get_Sign1()=="<") {
if(nodeAnts->get_Value1().f >= (float)(node_sign_uguale->get_Value1().f + CIFRE_DECIMALI)) return false;
else return true; } else return true; }
else {
if((node_sign_uguale->get_Value1().f >
nodeAnts->get_Value1().f)&&(node_sign_uguale->get_Value1().f < nodeAnts->get_Value2())) return false;
else {
if((nodeAnts->get_Sign1()==">")&&(nodeAnts->get_Sign2()=="<")) {
if(nodeAnts->get_Value1().f == (float)(node_sign_uguale->get_Value1().f - CIFRE_DECIMALI)) return false;
if(nodeAnts->get_Value2() == (float)(node_sign_uguale->get_Value1().f + CIFRE_DECIMALI)) return false;
else return true; }
if((nodeAnts->get_Sign1()==">=")&&(nodeAnts->get_Sign2()=="<=")) {
if(nodeAnts->get_Value1().f == node_sign_uguale->get_Value1().f) return false;
if(nodeAnts->get_Value2() == node_sign_uguale->get_Value1().f) return false;
else return true; }
if((nodeAnts->get_Sign1()==">")&&(nodeAnts-get_Sign2()=="<=")) {
if(nodeAnts->get_Value1().f == (float)(node_sign_uguale->get_Value1().f - CIFRE_DECIMALI)) return false;
if(nodeAnts->get_Value2() == node_sign_uguale->get_Value1().f) return false;
else return true; }
if((nodeAnts->get_Sign1()==">=")&&(nodeAnts-get_Sign2()=="<")) {
if(nodeAnts->get_Value1().f == node_sign_uguale->get_Value1().f) return false;
if(nodeAnts->get_Value2() == (float)(node_sign_uguale->get_Value1().f + CIFRE_DECIMALI)) return false;
else return true; } else return true; }
} }
bool ExtractionRule::Node::Intersection_D_L(Node *nodeImplicato,Node *nodeBelongAntecedents){ if ((nodeImplicato->get_Value1().f>nodeBelongAntecedents- >get_Value1().f)&&(nodeImplicato->get_Value2()<nodeBelongAntecedents->get_Value2())) return false; if((nodeImplicato->get_Value1().f>nodeBelongAntecedents- >get_Value1().f)&&(nodeImplicato->get_Value1().f<nodeBelongAntecedents->get_Value2())) return false; if((nodeImplicato->get_Value2()<nodeBelongAntecedents- >get_Value2())&&(nodeImplicato->get_Value2()>nodeBelongAntecedents->get_Value1().f)) return false; else{
//hanno gli stessi segni
if((nodeBelongAntecedents->get_Sign1()==nodeImplicato- >get_Sign1())&&(nodeBelongAntecedents->get_Sign2()==nodeImplicato->get_Sign2())){ if ((nodeImplicato->get_Value1().f==nodeBelongAntecedents- >get_Value1().f)&&(nodeImplicato->get_Value2()<nodeBelongAntecedents->get_Value2()))return false; if ((nodeImplicato->get_Value2()==nodeBelongAntecedents- >get_Value2())&&(nodeImplicato->get_Value1().f>nodeBelongAntecedents->get_Value1().f))return false; if (nodeImplicato->get_Value1().f==nodeBelongAntecedents->get_Value2()) return false;
if (nodeImplicato->get_Value2()==nodeBelongAntecedents->get_Value1().f)return false;
else return true; }
//non hanno gli stessi segni else { //nodeB <= x <= if((nodeBelongAntecedents- >get_Sign1()==">=")&&(nodeBelongAntecedents->get_Sign2()=="<=")) { //node < x < if((nodeImplicato->get_Sign1()==">")&&(nodeImplicato->get_Sign2()=="<")) { if((nodeImplicato->get_Value1().f==(float) (nodeBelongAntecedents->get_Value1().f- CIFRE_DECIMALI))&&(nodeImplicato->get_Value2()<nodeBelongAntecedents->get_Value2()))return false; if((nodeImplicato->get_Value2()==(float) (nodeBelongAntecedents->get_Value2() +CIFRE_DECIMALI))&&(nodeImplicato->get_Value1().f>=nodeBelongAntecedents->get_Value1().f)) return false;
if(nodeImplicato->get_Value1().f== (float)
(nodeBelongAntecedents->get_Value2()-CIFRE_DECIMALI)) return false;
if(nodeImplicato->get_Value2()==(float)(nodeBelongAntecedents->get_Value1().f-CIFRE_DECIMALI)) return false;
else return true; }else { //node <= x < if((nodeImplicato->get_Sign1()==">=")&&(nodeImplicato->get_Sign2()=="<")) { if((nodeImplicato->get_Value1().f==nodeBelongAntecedents- >get_Value1().f)&&(nodeImplicato->get_Value2()<nodeBelongAntecedents->get_Value2()))return false; if((nodeImplicato->get_Value2()==(float) (nodeBelongAntecedents->get_Value2() +CIFRE_DECIMALI))&&(nodeImplicato->get_Value1().f>nodeBelongAntecedents->get_Value1().f)) return false; if(nodeImplicato->get_Value1().f==nodeBelongAntecedents->get_Value2()) return false;
if(nodeImplicato->get_Value2()==(float)(nodeBelongAntecedents->get_Value1().f-CIFRE_DECIMALI)) return false;
else return true; }else { //node < x <= if((nodeImplicato->get_Sign1()==">")&&(nodeImplicato->get_Sign2()=="<=")) { if((nodeImplicato->get_Value1().f==(float) (nodeBelongAntecedents->get_Value1().f- CIFRE_DECIMALI))&&(nodeImplicato->get_Value2()<nodeBelongAntecedents->get_Value2()))return false; if((nodeImplicato->get_Value2()==nodeBelongAntecedents- >get_Value2())&&(nodeImplicato->get_Value1().f>=nodeBelongAntecedents->get_Value1().f)) return false; if(nodeImplicato->get_Value1().f==(float) (nodeBelongAntecedents->get_Value2()-CIFRE_DECIMALI)) return false; if(nodeImplicato->get_Value2()==nodeBelongAntecedents->get_Value1().f) return false;
else return true; }else return true; } } } //nodeB <= x < if((nodeBelongAntecedents->get_Sign1()==">=")&&(nodeBelongAntecedents->get_Sign2()=="<")) { //node < x < if((nodeImplicato->get_Sign1()==">")&&(nodeImplicato->get_Sign2()=="<")) {
if((nodeImplicato->get_Value1().f==(float)(nodeBelongAntecedents- >get_Value1().f-CIFRE_DECIMALI))&&(nodeImplicato->get_Value2()<nodeBelongAntecedents->get_Value2()))return false; if((nodeImplicato->get_Value2()==nodeBelongAntecedents- >get_Value2())&&(nodeImplicato->get_Value1().f>=nodeBelongAntecedents->get_Value1().f)) return false; if(nodeImplicato->get_Value1().f==nodeBelongAntecedents->get_Value2()) return false;
if(nodeImplicato->get_Value2()==(float)(nodeBelongAntecedents->get_Value1().f-CIFRE_DECIMALI)) return false;
else return true; }else { //node <= x <= if((nodeImplicato->get_Sign1()==">=")&&(nodeImplicato->get_Sign2()=="<=")) { if((nodeImplicato->get_Value1().f==nodeBelongAntecedents->get_Value1().f)&&(nodeImplicato->get_Value2()<(float) (nodeBelongAntecedents->get_Value2()-CIFRE_DECIMALI)))return false; if((nodeImplicato->get_Value2()==(float)(nodeBelongAntecedents- >get_Value2()-CIFRE_DECIMALI))&&(nodeImplicato->get_Value1().f>nodeBelongAntecedents->get_Value1().f)) return false; if(nodeImplicato->get_Value1().f==(float)(nodeBelongAntecedents->get_Value2()-CIFRE_DECIMALI)) return false;
if(nodeImplicato->get_Value2()==nodeBelongAntecedents->get_Value1().f) return false;
else return true; }else { //node < x <= if((nodeImplicato->get_Sign1()==">")&&(nodeImplicato->get_Sign2()=="<=")) { if((nodeImplicato->get_Value1().f==(float)(nodeBelongAntecedents- >get_Value1().f-CIFRE_DECIMALI))&&(nodeImplicato- >get_Value2()<(float)(nodeBelongAntecedents->get_Value2()-CIFRE_DECIMALI)))return false; if((nodeImplicato->get_Value2()==(float)(nodeBelongAntecedents- >get_Value2()-CIFRE_DECIMALI))&&(nodeImplicato->get_Value1().f>=nodeBelongAntecedents->get_Value1().f)) return false; if(nodeImplicato->get_Value1().f==nodeBelongAntecedents->get_Value2()) return false;
if(nodeImplicato->get_Value2()==nodeBelongAntecedents->get_Value1().f) return false;
else return true; }else return true; } } } //nodeB < x <= if((nodeBelongAntecedents->get_Sign1()==">")&&(nodeBelongAntecedents->get_Sign2()=="<=")) {
//node < x < if((nodeImplicato->get_Sign1()==">")&&(nodeImplicato->get_Sign2()=="<")) { if((nodeImplicato->get_Value1().f==nodeBelongAntecedents- >get_Value1().f)&&(nodeImplicato->get_Value2()<nodeBelongAntecedents->get_Value2()))return false; if((nodeImplicato->get_Value2()==(float)(nodeBelongAntecedents- >get_Value2()+CIFRE_DECIMALI))&&(nodeImplicato->get_Value1().f>nodeBelongAntecedents->get_Value1().f)) return false; if(nodeImplicato->get_Value1().f==(float)(nodeBelongAntecedents->get_Value2()-CIFRE_DECIMALI)) return false;
if(nodeImplicato->get_Value2()==nodeBelongAntecedents->get_Value1().f) return false;
else return true; }else { //node <= x <= if((nodeImplicato->get_Sign1()==">=")&&(nodeImplicato->get_Sign2()=="<=")) { if((nodeImplicato->get_Value1().f==(float)(nodeBelongAntecedents- >get_Value1().f+CIFRE_DECIMALI))&&(nodeImplicato->get_Value2()<nodeBelongAntecedents->get_Value2()))return false; if((nodeImplicato->get_Value2()==nodeBelongAntecedents->get_Value2())&&(nodeImplicato->get_Value1().f>(float) (nodeBelongAntecedents->get_Value1().f+CIFRE_DECIMALI))) return false; if(nodeImplicato->get_Value1().f==nodeBelongAntecedents->get_Value2()) return false;
if(nodeImplicato->get_Value2()==(float)(nodeBelongAntecedents->get_Value1().f-1)) return false;
else return true; }else { //node <= x < if((nodeImplicato->get_Sign1()==">=")&&(nodeImplicato->get_Sign2()=="<")) { if((nodeImplicato->get_Value1().f==(float)(nodeBelongAntecedents- >get_Value1().f+CIFRE_DECIMALI))&&(nodeImplicato->get_Value2()<nodeBelongAntecedents->get_Value2()))return false; if((nodeImplicato->get_Value2()==(float)(nodeBelongAntecedents- >get_Value2()+CIFRE_DECIMALI))&&(nodeImplicato- >get_Value1().f>(float)(nodeBelongAntecedents->get_Value1().f+CIFRE_DECIMALI))) return false;
if(nodeImplicato->get_Value1().f==nodeBelongAntecedents->get_Value2()) return false;
if(nodeImplicato->get_Value2()==nodeBelongAntecedents->get_Value1().f) return false;
else return true; }else return true; }
} }
if(((nodeBelongAntecedents->get_Sign1()==">")&&(nodeBelongAntecedents->get_Sign2()=="<"))){ //node <= x <= if((nodeImplicato->get_Sign1()==">=")&&(nodeImplicato->get_Sign2()=="<=")) { if((nodeImplicato->get_Value1().f==(float)(nodeBelongAntecedents- >get_Value1().f+CIFRE_DECIMALI))&&(nodeImplicato- >get_Value2()<(float)(nodeBelongAntecedents->get_Value2()-CIFRE_DECIMALI)))return false; if((nodeImplicato->get_Value2()==(float)(nodeBelongAntecedents- >get_Value2()-CIFRE_DECIMALI))&&(nodeImplicato- >get_Value1().f>(float)(nodeBelongAntecedents->get_Value1().f+CIFRE_DECIMALI))) return false;
if(nodeImplicato->get_Value1().f==(float)(nodeBelongAntecedents->get_Value2()-CIFRE_DECIMALI)) return false;
if(nodeImplicato->get_Value2()==(float)(nodeBelongAntecedents->get_Value1().f-CIFRE_DECIMALI)) return false;
else return true; } else { //node <= x < if((nodeImplicato->get_Sign1()==">=")&&(nodeImplicato->get_Sign2()=="<")) { if((nodeImplicato->get_Value1().f==(float)(nodeBelongAntecedents- >get_Value1().f+CIFRE_DECIMALI))&&(nodeImplicato->get_Value2()<nodeBelongAntecedents->get_Value2()))return false; if((nodeImplicato->get_Value2()==nodeBelongAntecedents->get_Value2())&&(nodeImplicato->get_Value1().f>(float) (nodeBelongAntecedents->get_Value1().f+CIFRE_DECIMALI))) return false; if(nodeImplicato->get_Value1().f==(float)(nodeBelongAntecedents->get_Value2()-CIFRE_DECIMALI)) return false;
if(nodeImplicato->get_Value2()==nodeBelongAntecedents->get_Value1().f) return false;
else return true; }else { //node < x <= if((nodeImplicato->get_Sign1()==">")&&(nodeImplicato->get_Sign2()=="<=")) { if((nodeImplicato->get_Value1().f==nodeBelongAntecedents->get_Value1().f)&&(nodeImplicato->get_Value2()<(float) (nodeBelongAntecedents->get_Value2()-CIFRE_DECIMALI)))return false; if((nodeImplicato->get_Value2()==(float)(nodeBelongAntecedents- >get_Value2()-CIFRE_DECIMALI))&&(nodeImplicato->get_Value1().f>nodeBelongAntecedents->get_Value1().f)) return false; if(nodeImplicato->get_Value1().f==nodeBelongAntecedents->get_Value2()) return false;
if(nodeImplicato->get_Value2()==(float)(nodeBelongAntecedents->get_Value1().f-CIFRE_DECIMALI)) return false;
else return true; } else return true; }
} }
else return true; }
}
bool ExtractionRule::Node::Intersection_Dif_L(Node *nodeImplicato, Node *nodeBelongAntecedents){
//node belong doppialimitazione
if(nodeBelongAntecedents->has_double_Limitation()) { //node belong < x < if((nodeBelongAntecedents->get_Sign1()==">")&&(nodeBelongAntecedents->get_Sign2()=="<")){ if((nodeImplicato->get_Sign1()==">") ||(nodeImplicato->get_Sign1()=="<=")){ if((nodeImplicato->get_Value1().f>nodeBelongAntecedents->get_Value1().f)&&(nodeImplicato->get_Value1().f< (float)
(nodeBelongAntecedents->get_Value2() - CIFRE_DECIMALI) )) return false;
else return true; }
if((nodeImplicato->get_Sign1()=="<")||(nodeImplicato->get_Sign1()==">=") ){
if((nodeImplicato->get_Value1().f> (float)(nodeBelongAntecedents->get_Value1().f +
CIFRE_DECIMALI))&&(nodeImplicato->get_Value1().f<nodeBelongAntecedents->get_Value2())) return false; else return true;
}else return true; } //node belong <= x <= if((nodeBelongAntecedents->get_Sign1()==">=")&&(nodeBelongAntecedents->get_Sign2()=="<=")){ if((nodeImplicato->get_Sign1()==">")||(nodeImplicato->get_Sign1()=="<=")){ if((nodeImplicato->get_Value1().f> (float)(nodeBelongAntecedents-
>get_Value1().f-CIFRE_DECIMALI))&&(nodeImplicato->get_Value1().f<nodeBelongAntecedents->get_Value2())) return false; else return true;
}
if((nodeImplicato->get_Sign1()=="<")||(nodeImplicato->get_Sign1()==">=")){
if((nodeImplicato->get_Value1().f>nodeBelongAntecedents->get_Value1().f)&&(nodeImplicato->get_Value1().f<(float)
(nodeBelongAntecedents->get_Value2() + CIFRE_DECIMALI))) return false;
else return true; }else return true; } //node belong <= x < if((nodeBelongAntecedents->get_Sign1()==">=")&&(nodeBelongAntecedents->get_Sign2()=="<")){ if((nodeImplicato->get_Sign1()==">")||(nodeImplicato->get_Sign1()=="<=")){ if((nodeImplicato->get_Value1().f> (float)(nodeBelongAntecedents->get_Value1().f - CIFRE_DECIMALI)
)&&(nodeImplicato->get_Value1().f<(float)(nodeBelongAntecedents->get_Value2() - CIFRE_DECIMALI) ))return false;
else return true; } if((nodeImplicato->get_Sign1()=="<")||(nodeImplicato->get_Sign1()==">=")) if((nodeImplicato->get_Value1().f>nodeBelongAntecedents- >get_Value1().f)&&(nodeImplicato->get_Value1().f<nodeBelongAntecedents->get_Value2()))return false; else return true;
else return true; } //node belong < x <= if((nodeBelongAntecedents->get_Sign1()==">")&&(nodeBelongAntecedents->get_Sign2()=="<=")){ if((nodeImplicato->get_Sign1()==">")||(nodeImplicato->get_Sign1()=="<=")) if((nodeImplicato->get_Value1().f>nodeBelongAntecedents- >get_Value1().f)&&(nodeImplicato->get_Value1().f<nodeBelongAntecedents->get_Value2()))return false; else return true;
if((nodeImplicato->get_Sign1()=="<")||(nodeImplicato->get_Sign1()==">=")) if((nodeImplicato->get_Value1().f>(float)(nodeBelongAntecedents->get_Value1().f + CIFRE_DECIMALI))&&(nodeImplicato->get_Value1().f<(float)(nodeBelongAntecedents->get_Value2() + CIFRE_DECIMALI)))return false;
else return true; else return true; }
else return true; }else return true; }
//---//verifico se i valori dell attributo implicato hanno un
INTERSEZIONE vuota (o non vuota ma ovvia)
//con l insieme dei valori che l attributo puo assumere rispetto al cammino considerato
//esempoio: B<=b nei PREDECESSORI e A<=a -> B<b1, con b1>b - intersezione non vuota ma ovvia
-//caso in cui un nodo appare solo una volta nell insieme dei predecessori
//---bool ExtractionRule::Node::Intersection_Empty_Or_Obvious(Node
*nodeImplicato, Node *nodeBelongAntecedents){
//il node da confrontare con nodeimplicato è node_null
//CONSIDERO CHE CI SAI UN SOLO NODE APPARTENENTE AI PREDECESSORI CHE MI INTERESSA
if(!nodeBelongAntecedents->is_Null()) { //sono esattamente uguali
else {
//uno dei due ha segno uguale o tutti e due
if((nodeImplicato->get_Sign1()=="=")&&(nodeBelongAntecedents->get_Sign1()=="="))return true; if((nodeImplicato->get_Sign1()=="=")||(nodeBelongAntecedents->get_Sign1()=="=")){ if(nodeImplicato->get_Sign1()=="=") return Intersection_S_E(nodeImplicato,nodeBelongAntecedents); else return Intersection_S_E(nodeBelongAntecedents,nodeImplicato); }else{
//entrambi con SINGOLA LIMITAZIONE
if (((!nodeImplicato->has_double_Limitation())&&(! nodeBelongAntecedents->has_double_Limitation())))
return Intersection_S_L(nodeImplicato,nodeBelongAntecedents); //NON sono entrambi SINGOLARMENTE limitati
else {
//controllo se hanno DOPPIA LIMITAZIONE entrambi o meno if
(((nodeImplicato- >has_double_Limitation())&&(nodeBelongAntecedents->has_double_Limitation())))
return Intersection_D_L(nodeImplicato,nodeBelongAntecedents); else {
if(nodeImplicato->has_double_Limitation())//uno dei due è LIMITATO SINGOLARMENTE
return Intersection_Dif_L(nodeBelongAntecedents,nodeImplicato); else if(nodeBelongAntecedents->has_double_Limitation())
return Intersection_Dif_L(nodeImplicato,nodeBelongAntecedents); else return true;
} } } } }
else return true; }
bool ExtractionRule::Node::Intersection_S_L(Node *nodeImplicato,Node *nodeBelongAntecedents){
//controllo se hanno lo stesso segno ovvero entrambi minore o maggiore if((nodeBelongAntecedents->get_Sign1()==nodeImplicato->get_Sign1())) { if(((nodeBelongAntecedents->get_Sign1()==">")|| (nodeBelongAntecedents->get_Sign1()==">="))&& (nodeImplicato->get_Value1().f>nodeBelongAntecedents->get_Value1().f)) return false; if(((nodeBelongAntecedents->get_Sign1()=="<")|| (nodeBelongAntecedents->get_Sign1()=="<="))&& (nodeImplicato->get_Value1().f<nodeBelongAntecedents->get_Value1().f)) return false;
return true; }
//segno diverso ovvero (sempre singola limitazione) else { //nodebelong <= if(nodeBelongAntecedents->get_Sign1()=="<="){ if(nodeImplicato->get_Sign1()=="<"){ if(nodeImplicato->get_Value1().f<=nodeBelongAntecedents->get_Value1().f)return false;
else return true; }else {
if(nodeImplicato->get_Sign1()==">"){
if(nodeImplicato->get_Value1().f<nodeBelongAntecedents->get_Value1().f)return false;
else return true; }else {
if(nodeImplicato->get_Sign1()==">="){
if(nodeImplicato->get_Value1().f<=nodeBelongAntecedents->get_Value1().f)return false;
else return true; }else return true; } } } //node belong < if(nodeBelongAntecedents->get_Sign1()=="<"){ if(nodeImplicato->get_Sign1()==">"){ if(nodeImplicato->get_Value1().f< (float)(nodeBelongAntecedents->get_Value1().f-CIFRE_DECIMALI))return false;
else return true; }else {
if(nodeImplicato->get_Sign1()==">="){
if(nodeImplicato->get_Value1().f<nodeBelongAntecedents->get_Value1().f)return false;
else return true; }else {
if(nodeImplicato->get_Sign1()=="<="){
if(nodeImplicato->get_Value1().f<(float)(nodeBelongAntecedents->get_Value1().f-CIFRE_DECIMALI))return false;
else return true; }else return true; } } } //node belong >= if(nodeBelongAntecedents->get_Sign1()==">="){ if(nodeImplicato->get_Sign1()==">" ){ if(nodeImplicato->get_Value1().f>=nodeBelongAntecedents->get_Value1().f)return false;
else return true; }else{
if(nodeImplicato->get_Sign1()=="<" ){
if(nodeImplicato->get_Value1().f>nodeBelongAntecedents->get_Value1().f)return false;
else return true; }else {
if(nodeImplicato->get_Sign1()=="<=" ){
if(nodeImplicato->get_Value1().f>=nodeBelongAntecedents->get_Value1().f)return false;
else return true; }else return true; } } } //node belong > if(nodeBelongAntecedents->get_Sign1()==">"){ if( nodeImplicato->get_Sign1()==">=" ){ if(nodeImplicato->get_Value1().f>(float)(nodeBelongAntecedents->get_Value1().f+CIFRE_DECIMALI))return false;
else return true; }else {
if( nodeImplicato->get_Sign1()=="<" ){
if(nodeImplicato->get_Value1().f>(float)(nodeBelongAntecedents->get_Value1().f+CIFRE_DECIMALI))return false;
else return true; }else {
if( nodeImplicato->get_Sign1()=="<=" ){
if(nodeImplicato->get_Value1().f>nodeBelongAntecedents->get_Value1().f)return false;
else return true; }else return true; }
}
}else return true; }
}
////--- R U L E --- //---//costruttore regola con vincoli
ExtractionRule::Rule::Rule(Node node1,Node node2,float prob,string typeR,Node node_contrained){ this->node1.push_back(node1); this->node2 = node2; this->prob = prob; this->typeR = typeR; this->node_constrained.push_back(node_contrained); }
//costruttore regola senza vincoli
ExtractionRule::Rule::Rule(Node node1,Node node2,float prob,string typeR){
this->node1.push_back(node1); this->node2 = node2;
this->prob = prob; this->typeR = typeR;
}
//crea regola vuota da inizializzare, ExtractionRule::Rule::Rule() { this->prob = 0; this->typeR = "null"; } //Distruttore ExtractionRule::Rule::~Rule(){}
void ExtractionRule::Rule::set_Node1(Node node){ this->node1.push_back(node);
}
void ExtractionRule::Rule::set_Node2(Node node){ this->node2=node;
}
void ExtractionRule::Rule::set_Prob(float prob){ this->prob=prob;
}
void ExtractionRule::Rule::set_Node_constrained(Node node){ this->node_constrained.push_back(node); } void ExtractionRule::Rule::set_Normal_Constrained(string n_c){ this->typeR=n_c; } vector<ExtractionRule::Node> ExtractionRule::Rule::get_Node1(){ return this->node1; } ExtractionRule::Node ExtractionRule::Rule::get_Node2(){ return this->node2; } vector<ExtractionRule::Node> ExtractionRule::Rule::get_Node_constrained(){ return this->node_constrained; } float ExtractionRule::Rule::get_Prob(){ return this->prob; } string ExtractionRule::Rule::get_Type(){
return this->typeR; } void ExtractionRule::Rule::reset_Rule(){ this->node1.clear(); this->node2.reset_node(); this->node_constrained.clear(); this->prob=0; this->typeR="null"; }
//bool controlla se la regola è di tipo normal o meno bool ExtractionRule::Rule::is_Normal(){
if(this->typeR=="normal") return true; else return false;
}
bool ExtractionRule::Rule::constrained_is_Correct(){ for(unsigned int i=0;i<this->node_constrained.size();i++)
if(!this->node_constrained.at(i).is_Correct()) return false; return true; } bool ExtractionRule::Rule::is_Correct(){ if (this->is_Normal()) if(this->node_constrained.empty()) if ((this->node1.at(0).is_Correct())&&(this->node2.is_Correct())) return true;
else return false; else return false; else
if(this->node_constrained.empty()) return false; else
if((this->node1.at(0).is_Correct())&&(this->node2.is_Correct())&&(this->constrained_is_Correct()))return true;
else return false; }
void ExtractionRule::Rule::printRule(){
cout<<"---"<<endl;
cout<<"NODO IMPLICANTE :"<<endl;
cout<<"---"<<endl;
for(unsigned int i=0;i<this->node1.size();i++){ cout<<i<<" implicante"<<endl;
this->node1.at(i).printNode(); }
cout<<"---"<<endl;
cout<<"NODO IMPLICATO :"<<endl;
cout<<"---"<<endl;
cout<<"---"<<endl; cout<<"PROBABILITA' : "; cout<<this->prob<<endl; cout<<"---"<<endl; cout<<"TIPO : "; cout<<this->typeR<<endl; cout<<"---"<<endl; if(!this->is_Normal()) {
cout<<"NODI VINCOLO :"<<endl;
for(unsigned int i=0;i<this->node_constrained.size();i++){ cout<<i<<" vincolo"<<endl; this->node_constrained.at(i).printNode(); } } } void ExtractionRule::Rule::ToTEXT() { if(node1.size() > 1)
for(unsigned int i=0;i<this->node1.size();i++){ //cout<<i<<" implicante"<<endl; this->node1.at(i).ToTEXT(); cout<<" ; "; } else this->node1.at(FIRST).ToTEXT(); cout<<" - > "; this->node2.ToTEXT(); cout<<" , p = "; cout<<this->prob; if(!this->is_Normal()) { if(!this->node_constrained.empty()){
for(unsigned int i=0;i<this->node_constrained.size();i++){ cout<<" ,"; this->node_constrained.at(i).ToTEXT(); } } } cout<<endl; } void ExtractionRule::Rule::delete_Constrained(){ //delete this->node_constrained; this->node_constrained.clear(); typeR="normal"; } void ExtractionRule::Rule::add_Constrained(){
//this->node_constrained=new vector<Node>(); } //---//---H A S H --- //---ExtractionRule::Hash::Hash(int size_hash){ this->hv=0; ; this->index=0; this->flag=0 ; this->pos_find=0; this->size_hash= size_hash;
//creo un vector di dimensione size_hash di oggetti vector this->myHT=new vector<vector<HashTableItem>> (size_hash); } ExtractionRule::Hash::~Hash(){ delete myHT; } int ExtractionRule::Hash::get_SizeHash(){ return size_hash; }
int ExtractionRule::Hash::hashvalue(string key){ hv = 0;
for(unsigned int i=0;i<key.size();i++) hv += key[i]%size_hash;
return hv % size_hash; }
void ExtractionRule::Hash::insert_implicante(string key, Rule *rule) {
flag=0;
index = Hash::hashvalue(key); HashTableItem item;
if (myHT->at(index).size()==0) { //vector vuoto item.implicante=new vector <Rule*>();
item.implicato=NULL; item.implicante->push_back(rule); item.name=key; myHT->at(index).push_back(item); } else {
for (unsigned int i=0;i<myHT->at(index).size();i++){
if (myHT->at(index).at(i).name==key){//aggiungo una regola alla struct dell elemento key
myHT->at(index).at(i).implicante=new vector <Rule*>(); myHT->at(index).at(i).implicante->push_back(rule); flag=1; break; } }
if (flag==0) {//aggiungo una struct per il nuovo elemento key item.implicato=NULL;
item.implicante=new vector <Rule*>(); item.implicante->push_back(rule); item.name=key; myHT->at(index).push_back(item); } } }
void ExtractionRule::Hash::insert_implicato(string key, Rule* rule){ flag=0;
index = Hash::hashvalue(key); Hash::HashTableItem item;
if (myHT->at(index).size()==0) { //vector vuoto item.implicato=new vector <Rule*>();
item.implicante=NULL; item.implicato->push_back(rule); item.name=key; myHT->at(index).push_back(item); } else {
for (unsigned int i=0;i<myHT->at(index).size();i++){
if (myHT->at(index).at(i).name==key){ //aggiungo una regola alla struct dell elemento key
if(myHT->at(index).at(i).implicato==NULL)
myHT->at(index).at(i).implicato=new vector <Rule*>(); myHT->at(index).at(i).implicato->push_back(rule); flag=1;
break; } }
if (flag==0) { //aggiungo una struct per il nuovo elemento key item.implicato=new vector <Rule*>();
item.implicante=NULL; item.implicato->push_back(rule); item.name=key; myHT->at(index).push_back(item); } } } vector<ExtractionRule::Rule*>* ExtractionRule::Hash::get_implicante(string key ){ exist=false; pos_find=0;