File: STConversation.m

package info (click to toggle)
steptalk 0.10.0%2Bgit20200629-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,732 kB
  • sloc: objc: 12,182; yacc: 400; makefile: 40; sh: 34; csh: 4; awk: 3; lisp: 3
file content (176 lines) | stat: -rw-r--r-- 4,680 bytes parent folder | download
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
    STConversation
 
    Copyright (c) 2002 Free Software Foundation
 
    Written by: Stefan Urbanek
    Date: 2003 Sep 21
   
    This file is part of the StepTalk project.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 */

#import "STConversation.h"

#import <Foundation/NSDebug.h>
#import <Foundation/NSException.h>

#import "STEnvironment.h"
#import "STEngine.h"
#import "STLanguageManager.h"

@implementation STConversation
/** Creates a new conversation with environment created using default 
    description and default language. */
+ conversation
{
    STEnvironment *env = [STEnvironment environmentWithDefaultDescription];
    
    NSLog(@"DEPRECATED");
    return AUTORELEASE([[self alloc] initWithContext:env language:nil]);
}

+ conversationWithContext:(STContext *)aContext
		 language:(NSString *)aLanguage
{
    return AUTORELEASE([[self alloc] initWithContext:aContext
					    language:aLanguage]);
}

- (bycopy id)resultByCopy
{
    return [self result];
}

- (id)runScriptFromString:(NSString *)aString
{
    NSLog(@"Warning: runScriptFromString: in STConversation is deprecated,"
	  @" use -interpretScript: and -result instead.");
    [self interpretScript:aString];
    return [self result];
}

/** Returns all languages that are known in the receiver. Should be used by
    remote calls instead of NSLanguage query which gives local list of
    languages. */
- (bycopy NSArray *)knownLanguages
{
    return [[STLanguageManager defaultManager] availableLanguages];
}

/** Creates a new conversation with environment created using default 
    description and language with name <var>langName</var>. */
+ conversationWithEnvironment:(STEnvironment *)env 
                     language:(NSString *)langName
{
    STConversation *c;
 
    NSLog(@"WARNING: +[STConversation conversationWithEnvironment:language:]"
          @" is deprecated, use conversationWithContext:language: instead.");
 
    c = [[self alloc] initWithContext:env language:langName];
    return AUTORELEASE(c);
}

- initWithEnvironment:(STEnvironment *)env 
             language:(NSString *)langName
{
    NSLog(@"WARNING: -[STConversation initWithEnvironment:language:]"
          @" is deprecated, use initWithContext:language: instead.");

    return [self initWithContext:env language:langName];
}

- initWithContext:(STContext *)aContext
         language:(NSString *)aLanguage
{
    if ((self = [super init]) != nil)
    {
        STLanguageManager  *manager = [STLanguageManager defaultManager];
        NSDebugLLog(@"STConversation",@"Creating conversation %@", self);

        if(!aLanguage || [aLanguage isEqual:@""])
        {
            languageName = RETAIN([manager defaultLanguage]);
        }
        else
        {
            languageName = RETAIN(aLanguage);
        }
    
        context = RETAIN(aContext);
    }
    return self;
}

- (void)dealloc
{
    NSDebugLLog(@"STConversation",@"Deallocating conversation %@", self);
    RELEASE(languageName);
    RELEASE(context);
    RELEASE(engine);
    RELEASE(returnValue);
    [super dealloc];
}

- (void)_createEngine
{
    ASSIGN(engine,[STEngine engineForLanguage:languageName]);
}

- (void)setLanguage:(NSString *)newLanguage
{
    if(![newLanguage isEqual:languageName])
    {
        RELEASE(engine);
        engine = nil;
        ASSIGN(languageName, newLanguage);
    }
}

/** Return name of the language used in the receiver conversation */
- (NSString *)language
{
    return languageName;
}
- (STEnvironment *)environment
{
    NSLog(@"WARNING: -[STConversation environment] is deprecated,"
          @" use -context instead.");

    return (STEnvironment *)context;
}
- (STContext *)context
{
    return context;
}

- (void)interpretScript:(bycopy NSString *)aString
{
    if(!engine)
    {
        [self _createEngine];
    }
    
    ASSIGN(returnValue, [engine interpretScript: aString 
                                      inContext: context]);
}
- (id)result
{
    return returnValue;
}
@end