~ubuntu-branches/ubuntu/intrepid/tcm/intrepid

« back to all changes in this revision

Viewing changes to src/sd/dv/ergraph.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2003-07-03 20:08:21 UTC
  • Revision ID: james.westby@ubuntu.com-20030703200821-se4xtqx25e5miczi
Tags: upstream-2.20
ImportĀ upstreamĀ versionĀ 2.20

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
////////////////////////////////////////////////////////////////////////////////
 
2
//
 
3
// This file is part of Toolkit for Conceptual Modeling (TCM).
 
4
// (c) copyright 1995, Vrije Universiteit Amsterdam.
 
5
// Author: Frank Dehne (frank@cs.vu.nl).
 
6
//
 
7
// TCM is free software; you can redistribute it and/or modify
 
8
// it under the terms of the GNU General Public License as published by
 
9
// the Free Software Foundation; either version 2 of the License, or
 
10
// (at your option) any later version.
 
11
//
 
12
// TCM is distributed in the hope that it will be useful,
 
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
// GNU General Public License for more details.
 
16
//
 
17
// You should have received a copy of the GNU General Public License
 
18
// along with TCM; if not, write to the Free Software
 
19
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
20
// 02111-1307, USA.
 
21
////////////////////////////////////////////////////////////////////////////////
 
22
#include "ergraph.h"
 
23
#include "edge.h"
 
24
#include "util.h"
 
25
 
 
26
ERGraph::ERGraph(): Graph() {
 
27
        int i=0;
 
28
        int j=0;
 
29
        nodeTypes[i++] = Code::COMMENT;
 
30
        nodeTypes[i++] = Code::ENTITY_TYPE;
 
31
        nodeTypes[i++] = Code::VALUE_TYPE;
 
32
        nodeTypes[i++] = Code::TAXONOMY_JUNCTION;
 
33
        nodeTypes[i++] = Code::RELATIONSHIP_NODE;
 
34
        nodeTypes[i++] = 0;
 
35
 
 
36
        edgeTypes[j++] = Code::BINARY_RELATIONSHIP;
 
37
        edgeTypes[j++] = Code::FUNCTION;
 
38
        edgeTypes[j++] = Code::EMPTY_EDGE;
 
39
        edgeTypes[j++] = Code::ISA_RELATIONSHIP;
 
40
        edgeTypes[j++] = 0;
 
41
}
 
42
 
 
43
Subject *ERGraph::GetGeneralization(Subject *subj) {
 
44
        for (edges->first(); !edges->done(); edges->next()) {
 
45
                Edge *edge = edges->cur();
 
46
                if (edge->GetClassType()==Code::ISA_RELATIONSHIP) {
 
47
                        if (edge->GetSubject1() == subj)
 
48
                                return edge->GetSubject2();
 
49
                }
 
50
                else if (edge->GetClassType()==Code::EMPTY_EDGE) {
 
51
                        Subject *junction = 0;
 
52
                        if (edge->GetSubject1() == subj)
 
53
                                junction = edge->GetSubject2();
 
54
                        else if (edge->GetSubject2() == subj)
 
55
                                junction = edge->GetSubject1();
 
56
                        if (junction) {
 
57
                                List<Edge *> edges2 = *edges;
 
58
                                for (edges2.first(); !edges2.done(); edges2.next()) {
 
59
                                        Edge *edge2 = edges2.cur();
 
60
                                        if (edge2->GetClassType()==Code::ISA_RELATIONSHIP) {
 
61
                                                if (edge2->GetSubject1() == junction)
 
62
                                                        return edge2->GetSubject2();
 
63
                                        }
 
64
                                }
 
65
                        }
 
66
                }
 
67
        }
 
68
        return 0;
 
69
}
 
70
 
 
71
bool ERGraph::IsaLeaves(Subject *subj) {
 
72
        for (edges->first(); !edges->done(); edges->next()) {
 
73
                Edge *edge = edges->cur();
 
74
                Subject *n1 = edge->GetSubject1();
 
75
                if (n1 == subj && edge->GetClassType()==Code::ISA_RELATIONSHIP) {
 
76
                        return True;
 
77
                }
 
78
        }
 
79
        return False;
 
80
}
 
81
 
 
82
bool ERGraph::IsaPathExists(Subject *subj1, Subject *subj2) {
 
83
        List<Edge *> path;
 
84
        return IsaPathExists(subj1, subj2, &path);
 
85
}
 
86
 
 
87
bool ERGraph::IsaPathExists(Subject *subj1, Subject *subj2, List<Edge *> *path) {
 
88
        // should be made more generic !!!
 
89
        for (edges->first(); !edges->done(); edges->next()) {
 
90
                Edge *edge = edges->cur();
 
91
                Subject *n1 = edge->GetSubject1();
 
92
                Subject *n2 = edge->GetSubject2();
 
93
                bool step = False;
 
94
 
 
95
                // is_a: only n1 -> n2.
 
96
                int edgetype = edge->GetClassType();
 
97
                int subj1type = subj1->GetClassType();
 
98
                if (n1 == subj1 && edgetype==Code::ISA_RELATIONSHIP) {
 
99
                        step = True;
 
100
                }
 
101
                else if (edgetype==Code::EMPTY_EDGE && 
 
102
                         (subj1type==Code::ENTITY_TYPE||subj1type==Code::CLASS_NODE) &&
 
103
                         (n1 == subj1 || n2 == subj1)) {
 
104
 
 
105
                        step = True;
 
106
                        if (subj1==n2) {
 
107
                                Subject *tmp = n1;
 
108
                                n1 = n2;
 
109
                                n2 = tmp;
 
110
                        }
 
111
                }
 
112
                if (step) {
 
113
                        if (n2==subj2) {
 
114
                                path->add(edge);
 
115
                                return True;
 
116
                        }
 
117
                        else if (path->find(edge) == -1) {
 
118
                                path->add(edge);
 
119
                                if (IsaPathExists(n2, subj2, path))
 
120
                                        return True;
 
121
                                path->remove(edge);
 
122
                                if (!edges->setcur(edge))
 
123
                                        error("%s %d: internal graph error\n", 
 
124
                                                __FILE__, __LINE__);
 
125
                        }
 
126
                        step = False;
 
127
                }
 
128
        } 
 
129
        return False;
 
130
}
 
131
 
 
132
void ERGraph::InitConnections () {
 
133
        int ENT = Code::GetIndex(Code::ENTITY_TYPE, nodeTypes);
 
134
        int TAX = Code::GetIndex(Code::TAXONOMY_JUNCTION, nodeTypes);
 
135
        int VAL = Code::GetIndex(Code::VALUE_TYPE, nodeTypes);
 
136
        int REL = Code::GetIndex(Code::RELATIONSHIP_NODE, nodeTypes);
 
137
        int FUN = Code::GetIndex(Code::FUNCTION, edgeTypes);
 
138
        int MAN = Code::GetIndex(Code::BINARY_RELATIONSHIP, edgeTypes);
 
139
        int EMP = Code::GetIndex(Code::EMPTY_EDGE, edgeTypes);
 
140
        int ISA = Code::GetIndex(Code::ISA_RELATIONSHIP, edgeTypes);
 
141
 
 
142
        connections[ENT][ENT][FUN] = True;
 
143
        connections[ENT][ENT][MAN] = True;
 
144
        connections[ENT][ENT][ISA] = True;
 
145
 
 
146
        connections[ENT][VAL][FUN] = True;
 
147
        connections[ENT][TAX][EMP] = True;
 
148
 
 
149
        connections[TAX][ENT][EMP] = True;
 
150
        connections[TAX][ENT][ISA] = True;
 
151
 
 
152
        connections[REL][ENT][FUN] = True;
 
153
        connections[REL][VAL][FUN] = True;
 
154
}