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

« back to all changes in this revision

Viewing changes to src/dg/hypergraph.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 2001, Universiteit Twente.
 
5
// Author: Rik Eshuis (eshuis@cs.utwente.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 "hypergraph.h"
 
23
#include "node.h"
 
24
#include "hyperedge.h"
 
25
#include "util.h"
 
26
#include "outputfile.h"
 
27
 
 
28
HyperGraph::HyperGraph() {
 
29
        nodes = new List<Node *>;
 
30
        hedges = new List<HyperEdge *>;
 
31
        nodeTypes = new int[static_cast<int>(MAX_TYPES)];
 
32
        edgeTypes = new int[static_cast<int>(MAX_TYPES)];
 
33
        int m = MAX_TYPES;
 
34
        for (int i=0;i<m;i++)
 
35
                for (int j=0;j<m;j++)
 
36
                        for (int k=0;k<m;k++)
 
37
                                connections [i][j][k]= False;
 
38
        counter = 1;
 
39
        indexPrefix = "";
 
40
}
 
41
 
 
42
HyperGraph::~HyperGraph() {
 
43
        nodes->clear();
 
44
        hedges->clear();
 
45
        delete nodes;
 
46
        delete hedges;
 
47
        delete[] nodeTypes;
 
48
        delete[] edgeTypes;
 
49
}
 
50
 
 
51
 
 
52
 
 
53
 
 
54
void HyperGraph::AddNode(Node *node) {
 
55
        if (!HasNode(node)) nodes->add(node);}
 
56
 
 
57
void HyperGraph::AddHyperEdge(HyperEdge *edge) {
 
58
        if (!HasHyperEdge(edge)) hedges->add(edge);
 
59
}
 
60
 
 
61
void HyperGraph::WriteSubjects(OutputFile *f) {
 
62
        (*f) << "# GRAPH NODES\n\n";
 
63
        for (nodes->first(); !nodes->done(); nodes->next()) {
 
64
                if (check(nodes->cur()))
 
65
                        nodes->cur()->Write(f);
 
66
        }
 
67
        (*f) << "# GRAPH HYPEREDGES\n\n";
 
68
        for (hedges->first(); !hedges->done(); hedges->next()) {
 
69
                if (check(hedges->cur()))
 
70
                        hedges->cur()->WriteMembers(f);
 
71
        }
 
72
}
 
73
 
 
74
 
 
75
 
 
76
void HyperGraph::GetNodes(List <Subject *> *l){
 
77
        for (nodes->first(); !nodes->done(); nodes->next()) {
 
78
                if (check(nodes->cur() && !nodes->cur()->IsHyperEdge()))
 
79
                        l->add(nodes->cur());
 
80
        }
 
81
}
 
82
 
 
83
 
 
84
void HyperGraph::GetNodes(List<Subject *> *l, int t) {
 
85
        // int c = l->count();
 
86
        for (nodes->first(); !nodes->done(); nodes->next()) {
 
87
                Subject *s = nodes->cur();
 
88
                if (check(s && !s->IsEdge())) {
 
89
                        if (s->GetClassType() == t)
 
90
                                l->add(s);
 
91
                }
 
92
        }
 
93
}
 
94
 
 
95
void HyperGraph::RemoveNode(Node *node){
 
96
        nodes->remove(node);
 
97
        for (hedges->first();!hedges->done(); ) {
 
98
        HyperEdge *e = hedges->cur();
 
99
        if (check(e)) {
 
100
                        List <Subject *> *n1=e->GetSubject1();
 
101
                        if (n1->contains(node)) hedges->removecur();
 
102
                             else{
 
103
                               List <Subject *> *n2=e->GetSubject2();
 
104
                               if (n2->contains(node)) hedges->removecur();
 
105
                               else hedges->next();
 
106
                        }
 
107
             }
 
108
        else hedges->next();
 
109
        }
 
110
}
 
111
 
 
112
void HyperGraph::GetHyperEdges(List <Subject *> *l){
 
113
        for (hedges->first(); !hedges->done(); hedges->next()) {
 
114
                HyperEdge *e = hedges->cur();
 
115
                if (check(e)) 
 
116
                        l->add(e);
 
117
        }
 
118
}
 
119
 
 
120
 
 
121
void HyperGraph::GetHyperEdgesFrom(List <Subject *> *l,Subject *s){
 
122
        for (hedges->first(); !hedges->done(); hedges->next()) {
 
123
                HyperEdge *e = hedges->cur();
 
124
                if (check(e)) {
 
125
                        if (e->IsDirected() && e->GetSubject1()->contains(s))
 
126
                                l->add(e);
 
127
                }
 
128
        }
 
129
}
 
130
 
 
131
void HyperGraph::GetHyperEdgesTo(List <Subject *> *l,Subject *s){
 
132
        for (hedges->first(); !hedges->done(); hedges->next()) {
 
133
                HyperEdge *e = hedges->cur();
 
134
                if (check(e)) {
 
135
                        if (e->IsDirected() && e->GetSubject2()->contains(s))
 
136
                                l->add(e);
 
137
 
 
138
                }
 
139
        }
 
140
}
 
141
 
 
142
 
 
143
void HyperGraph::GetHyperEdgesFrom(List <HyperEdge *> *l,List <Subject *> *cfg){
 
144
        for (hedges->first(); !hedges->done(); hedges->next()) {
 
145
                HyperEdge *e = hedges->cur();
 
146
                if ((check(e)) && (e->IsDirected())){
 
147
                  List <Subject *> *source=e->GetSubject1();
 
148
                  bool Found=True;
 
149
                  for (source->first();!source->done();source->next()){
 
150
                    if (cfg->find(source->cur())<0) { // source->cur() not in cfg
 
151
                      Found=False;
 
152
                      break; // jump out of the for-loop
 
153
                    }
 
154
                  }
 
155
                  if (Found) // all sources of e are in cfg
 
156
                    l->add(e);
 
157
                }
 
158
        }
 
159
}
 
160
 
 
161
bool HyperGraph::ExistsSimilarHyperEdge(HyperEdge *he){
 
162
  for (hedges->first(); !hedges->done(); hedges->next()) {
 
163
    HyperEdge *e = hedges->cur();
 
164
    if (check(e)){
 
165
      if (*e==*he) return True;
 
166
    }
 
167
  }  
 
168
  return False;
 
169
}
 
170