-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRule.h
More file actions
131 lines (100 loc) · 4.03 KB
/
Rule.h
File metadata and controls
131 lines (100 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* @file Rule.h
* @author Roman Schindlauer, Mushthofa
*
* @brief Rule class.
*
*/
#ifndef _RULE_H
#define _RULE_H
#include <boost/ptr_container/indirect_fun.hpp>
#include <sstream>
#include <cmath>
#include "Literal.h"
#include "Globals.h"
typedef std::vector<AtomPtr > HeadList_t;
typedef std::vector<LiteralPtr> BodyList_t;
typedef enum {TNORM, CO_TNORM, MAX, MIN} FuzzyOp_t;
typedef std::pair<HeadList_t, FuzzyOp_t> HeadExpr_t;
typedef std::pair<BodyList_t, FuzzyOp_t> BodyExpr_t;
class Rule
{
public:
Rule():lineid(0){}
Rule(const HeadExpr_t&, const BodyExpr_t&,
const std::string& = "",
unsigned = 0, bool noVars=false);
virtual ~Rule();
virtual const HeadExpr_t& getHead() const;
virtual const BodyExpr_t& getBody() const;
virtual const BodyExpr_t getPositiveBody() const;
virtual const BodyExpr_t getNegativeBody() const;
virtual void setHead(const HeadExpr_t&);
virtual void setBody(const BodyExpr_t&);
virtual void addHead(AtomPtr);
virtual void addBody(LiteralPtr);
std::set<std::string> getVariables() const;
std::set<std::string> getPosVariables() const;
std::set<std::string> getNegVariables() const;
unsigned numberOfVars() const;
bool isGround() const;
Rule bindVariables(const std::vector<std::string>&);
std::string getFile() const;
unsigned getLine() const;
bool operator==(const Rule&) const;
bool operator!=(const Rule&) const;
bool operator<(const Rule&) const;
bool operator>(const Rule& r2) const
{
return !(*this<r2 || *this==r2);
}
//virtual void accept(BaseVisitor&) const;
bool isDefinite()
{
if(head.first.size() != 1)
return false;
BodyList_t::iterator b_it;
for(b_it = body.first.begin(); b_it!=body.first.end(); ++b_it)
{
if((*b_it)->isNAF())
return false;
}
return true;
}
bool isSmallConstraint(unsigned long maxGr) const
{
if(head.first.size() == 0 && isSmall(maxGr))
return true;
return false;
}
bool isSmall(unsigned long maxGr) const
{
int minRuleVar = Globals::Instance()->intOption("minRuleVar");
if(posVars.size() <= minRuleVar)
return true;
unsigned long numConstants = Atom::constantsTable.size();
double cv = (double)log(maxGr)/ (double)log(posVars.size());
//std::cout << "cv " << cv <<std::endl;
if(numConstants < cv)
return true;
return false;
}
bool isFact() const
{
HeadList_t hl = head.first;
BodyList_t bl = body.first;
return (hl.size() == 1 && bl.size() == 1
&& typeid(*bl[0]->getAtom()) == typeid(ConstantAtom));
}
protected:
HeadExpr_t head;
BodyExpr_t body;
std::string filename;
unsigned lineid;
std::set<std::string> posVars;
};
std::ostream& operator<<(std::ostream&, const Rule&);
bool operator< (const HeadExpr_t& head1, const HeadExpr_t& head2);
bool operator< (const BodyExpr_t& b1, const BodyExpr_t& b2);
#endif
//End