Skip to content

Commit 243d0f0

Browse files
committed
Merge pull request #9 from alan-wu/master
Number of bug fixes and improvements
2 parents d557c0b + 3c5d96e commit 243d0f0

File tree

9 files changed

+119
-28
lines changed

9 files changed

+119
-28
lines changed

core/src/FieldmlDOM.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <libxml/parser.h>
5050
#include <libxml/xmlmemory.h>
5151
#include <libxml/xmlschemas.h>
52+
#include <libxml/parserInternals.h>
5253

5354
#include "ErrorContextAutostack.h"
5455
#include "Util.h"
@@ -113,6 +114,24 @@ static void addContextError( void *context, const char *msg, ... )
113114

114115
//========================================================================
115116

117+
xmlExternalEntityLoader defaultLoader = 0;
118+
119+
120+
xmlParserInputPtr
121+
xmlMyExternalEntityLoader(const char *URL, const char *ID,
122+
xmlParserCtxtPtr ctxt) {
123+
xmlParserInputPtr ret = 0;
124+
125+
if (0 == strcmp(URL, "http://www.cellml.org/tools/cellml_1_1_schema/common/xlink-href.xsd"))
126+
ret = xmlNewStringInputStream(ctxt, (const xmlChar *)HREF_STRING_XSD);
127+
if (ret != NULL)
128+
return(ret);
129+
if (defaultLoader != NULL)
130+
ret = defaultLoader(URL, ID, ctxt);
131+
return(ret);
132+
}
133+
134+
116135
static int validate( FieldmlErrorHandler *errorHandler, xmlParserInputBufferPtr buffer, const char *resourceName )
117136
{
118137
xmlSchemaPtr schemas = NULL;
@@ -123,6 +142,11 @@ static int validate( FieldmlErrorHandler *errorHandler, xmlParserInputBufferPtr
123142

124143
xmlSubstituteEntitiesDefault( 1 );
125144

145+
if (!defaultLoader)
146+
defaultLoader = xmlGetExternalEntityLoader();
147+
148+
xmlSetExternalEntityLoader(xmlMyExternalEntityLoader);
149+
126150
if( buffer == NULL )
127151
{
128152
return 1;
@@ -671,8 +695,11 @@ class ReferenceEvaluatorParser :
671695
{
672696
const char *name = getStringAttribute( objectNode, NAME_ATTRIB );
673697
FmlObjectHandle sourceEvaluator = getObjectAttribute( objectNode, EVALUATOR_ATTRIB, state );
674-
675-
FmlObjectHandle evaluator = Fieldml_CreateReferenceEvaluator( state.session, name, sourceEvaluator );
698+
FmlObjectHandle valueType = getObjectAttribute( objectNode, VALUE_TYPE_ATTRIB, state );
699+
if (valueType == FML_INVALID_HANDLE)
700+
valueType = Fieldml_GetValueType( state.session, sourceEvaluator );
701+
702+
FmlObjectHandle evaluator = Fieldml_CreateReferenceEvaluator( state.session, name, sourceEvaluator, valueType );
676703
if( evaluator == FML_INVALID_HANDLE )
677704
{
678705
state.errorHandler->logError( "ReferenceEvaluator creation failed", name );

core/src/SimpleMap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ template <typename K, typename V> class SimpleMap
9797

9898
int size()
9999
{
100-
return pairs.size();
100+
return static_cast<int>(pairs.size());
101101
}
102102

103103

core/src/String_InternalXSD.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@
4141

4242
#include "String_InternalXSD.h"
4343

44+
const char * const HREF_STRING_XSD = "<schema targetNamespace=\"http://www.w3.org/1999/xlink\" \
45+
xmlns:xlink=\"http://www.w3.org/1999/xlink\" \
46+
xmlns=\"http://www.w3.org/2001/XMLSchema\"> \
47+
<annotation> \
48+
<documentation xml:lang=\"en\"> \
49+
This schema provides the XLink href attribute for use in the MathML2 \
50+
schema. Written by Max Froumentin, W3C. \
51+
</documentation> \
52+
</annotation> \
53+
\
54+
<attribute name=\"href\" type=\"anyURI\"/>\
55+
</schema> \
56+
";
57+
58+
4459
const char * const FML_STRING_FIELDML_XSD_LOCATION = "http://www.fieldml.org/resources/xml/0.5/FieldML_0.5.xsd";
4560

4661
const char * const FML_STRING_FIELDML_XSD = "<?xml version=\"1.0\" encoding=\"utf-8\"?> \
@@ -49,8 +64,8 @@ const char * const FML_STRING_FIELDML_XSD = "<?xml version=\"1.0\" encoding=\"ut
4964
xmlns:xlink=\"http://www.w3.org/1999/xlink\" \
5065
> \
5166
\
52-
<xs:import namespace=\"http://www.w3.org/1999/xlink\" \
53-
schemaLocation=\"http://www.cellml.org/tools/cellml_1_1_schema/common/xlink-href.xsd\" /> \
67+
<xs:import namespace=\"http://www.w3.org/1999/xlink\" \
68+
schemaLocation=\"http://www.cellml.org/tools/cellml_1_1_schema/common/xlink-href.xsd\" /> \
5469
\
5570
<xs:complexType name=\"FieldmlRdfTargetType\"> \
5671
<xs:attribute name=\"id\" type=\"xs:string\" form=\"qualified\" use=\"optional\"/> \

core/src/String_InternalXSD.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#ifndef H_STRING_INTERNAL_XSD_H
4343
#define H_STRING_INTERNAL_XSD_H
4444

45+
extern const char * const HREF_STRING_XSD;
46+
4547
extern const char * const FML_STRING_FIELDML_XSD_LOCATION;
4648

4749
extern const char * const FML_STRING_FIELDML_XSD;

core/src/fieldml_api.cpp

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,10 @@ static vector<FmlObjectHandle> getArgumentList( FieldmlSession *session, FmlObje
237237
{
238238
for( set<FmlObjectHandle>::const_iterator i = used.begin(); i != used.end(); i++ )
239239
{
240-
// Performance wise, I am not sure I should remove all elems from
241-
// unbound in used or using find as shown below.
242-
if (unbound.end() == unbound.find(*i))
243-
args.push_back( *i );
240+
// Performance wise, I am not sure I should remove all elems from
241+
// unbound in used or using find as shown below.
242+
if (unbound.end() == unbound.find(*i))
243+
args.push_back( *i );
244244
}
245245
return args;
246246
}
@@ -2158,6 +2158,11 @@ FmlEnsembleValue Fieldml_GetEvaluatorElement( FmlSessionHandle handle, FmlObject
21582158
return -1;
21592159
}
21602160

2161+
if ( ( evaluatorIndex < 1 ) || ( evaluatorIndex > map->size() ) )
2162+
{
2163+
return -1;
2164+
}
2165+
21612166
return map->getKey( evaluatorIndex - 1 );
21622167
}
21632168

@@ -2179,6 +2184,11 @@ FmlObjectHandle Fieldml_GetEvaluator( FmlSessionHandle handle, FmlObjectHandle o
21792184
return FML_INVALID_HANDLE;
21802185
}
21812186

2187+
if ( ( evaluatorIndex < 1 ) || ( evaluatorIndex > map->size() ) )
2188+
{
2189+
return FML_INVALID_HANDLE;
2190+
}
2191+
21822192
return map->getValue( evaluatorIndex - 1 );
21832193
}
21842194

@@ -2204,7 +2214,7 @@ FmlObjectHandle Fieldml_GetElementEvaluator( FmlSessionHandle handle, FmlObjectH
22042214
}
22052215

22062216

2207-
FmlObjectHandle Fieldml_CreateReferenceEvaluator( FmlSessionHandle handle, const char * name, FmlObjectHandle sourceEvaluator )
2217+
FmlObjectHandle Fieldml_CreateReferenceEvaluator( FmlSessionHandle handle, const char * name, FmlObjectHandle sourceEvaluator, FmlObjectHandle valueType )
22082218
{
22092219
FieldmlSession *session = FieldmlSession::handleToSession( handle );
22102220
ERROR_AUTOSTACK( session );
@@ -2224,7 +2234,26 @@ FmlObjectHandle Fieldml_CreateReferenceEvaluator( FmlSessionHandle handle, const
22242234
return session->getLastError();
22252235
}
22262236

2227-
FmlObjectHandle valueType = Fieldml_GetValueType( handle, sourceEvaluator );
2237+
if( !checkLocal( session, valueType ) )
2238+
{
2239+
return session->getLastError();
2240+
}
2241+
2242+
FmlObjectHandle sourceEvaluatorValueType = Fieldml_GetValueType( handle, sourceEvaluator );
2243+
if( valueType != sourceEvaluatorValueType)
2244+
{
2245+
if( !checkIsValueType( session, valueType, true, false, false, false ) )
2246+
{
2247+
session->setError( FML_ERR_INVALID_PARAMETER_4, valueType, "Cannot create reference evaluator with value type cast to non-scalar non-continuous type." );
2248+
return FML_INVALID_HANDLE;
2249+
}
2250+
2251+
if ( Fieldml_GetTypeComponentCount( handle, valueType ) != 1 )
2252+
{
2253+
session->setError( FML_ERR_INVALID_PARAMETER_4, valueType, "Cannot create reference evaluator with value type cast to non-scalar continuous type." );
2254+
return FML_INVALID_HANDLE;
2255+
}
2256+
}
22282257

22292258
ReferenceEvaluator *referenceEvaluator = new ReferenceEvaluator( name, sourceEvaluator, valueType, false );
22302259

@@ -2377,6 +2406,11 @@ FmlObjectHandle Fieldml_GetBindArgument( FmlSessionHandle handle, FmlObjectHandl
23772406
return FML_INVALID_HANDLE;
23782407
}
23792408

2409+
if ( ( bindIndex < 1 ) || ( bindIndex > map->size() ) )
2410+
{
2411+
return FML_INVALID_HANDLE;
2412+
}
2413+
23802414
return map->getKey( bindIndex - 1 );
23812415
}
23822416

@@ -2397,6 +2431,11 @@ FmlObjectHandle Fieldml_GetBindEvaluator( FmlSessionHandle handle, FmlObjectHand
23972431
return FML_INVALID_HANDLE;
23982432
}
23992433

2434+
if ( ( bindIndex < 1 ) || ( bindIndex > map->size() ) )
2435+
{
2436+
return FML_INVALID_HANDLE;
2437+
}
2438+
24002439
return map->getValue( bindIndex - 1 );
24012440
}
24022441

@@ -3640,7 +3679,7 @@ FmlObjectHandle Fieldml_GetDataSource( FmlSessionHandle handle, FmlObjectHandle
36403679

36413680
return FML_INVALID_HANDLE;
36423681
}
3643-
3682+
36443683

36453684
FmlObjectHandle Fieldml_GetKeyDataSource( FmlSessionHandle handle, FmlObjectHandle objectHandle )
36463685
{
@@ -3690,7 +3729,7 @@ int Fieldml_GetDataSourceCount( FmlSessionHandle handle, FmlObjectHandle objectH
36903729
return resource->dataSources.size();
36913730
}
36923731

3693-
3732+
36943733
FmlObjectHandle Fieldml_GetDataSourceByIndex( FmlSessionHandle handle, FmlObjectHandle objectHandle, int index )
36953734
{
36963735
FieldmlSession *session = FieldmlSession::handleToSession( handle );
@@ -3746,7 +3785,7 @@ FmlObjectHandle Fieldml_GetDataSourceResource( FmlSessionHandle handle, FmlObjec
37463785
return session->region->getNamedObject( source->resource->name );
37473786
}
37483787

3749-
3788+
37503789
char * Fieldml_GetArrayDataSourceLocation( FmlSessionHandle handle, FmlObjectHandle objectHandle )
37513790
{
37523791
FieldmlSession *session = FieldmlSession::handleToSession( handle );

core/src/fieldml_api.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -991,11 +991,11 @@ FmlObjectHandle Fieldml_GetIndexEvaluator( FmlSessionHandle handle, FmlObjectHan
991991
* Creates a reference evaluator. Reference evaluators delegate their evaluation directly to another evaluator, but can bind
992992
* argument evaluators before doing so.
993993
*
994-
* \note A reference evaluator's value type is the same as the value type of its source evaluator.
994+
* \note The value type may be either that of the source evaluator, OR for a scalar continuous source evaluator, it may be 'cast' to any scalar continuous type.
995995
*
996996
* \see Fieldml_GetReferenceSourceEvaluator
997997
*/
998-
FmlObjectHandle Fieldml_CreateReferenceEvaluator( FmlSessionHandle handle, const char * name, FmlObjectHandle sourceEvaluator );
998+
FmlObjectHandle Fieldml_CreateReferenceEvaluator( FmlSessionHandle handle, const char * name, FmlObjectHandle sourceEvaluator, FmlObjectHandle valueType );
999999

10001000

10011001
/**
@@ -1057,7 +1057,8 @@ int Fieldml_GetBindCount( FmlSessionHandle handle, FmlObjectHandle objectHandle
10571057

10581058

10591059
/**
1060-
* \return The argument evaulator used by the nth bind in the given evaluator.
1060+
* \return The argument evaulator used by the nth bind in the given evaluator. FML_INVALID_HANDLE will be returned
1061+
* if object at bind index cannot be found.
10611062
*
10621063
* \see Fieldml_GetBindCount
10631064
* \see Fieldml_SetBind

core/src/fieldml_write.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static int writeBinds( xmlTextWriterPtr writer, FmlSessionHandle handle, FmlObje
167167

168168
static int writeArguments( xmlTextWriterPtr writer, FmlSessionHandle handle, FmlObjectHandle object )
169169
{
170-
int count = Fieldml_GetArgumentCount( handle, object, 1, 1 );
170+
int count = Fieldml_GetArgumentCount( handle, object, /*isBound*/0, /*isUsed*/1 );
171171
if( count <= 0 )
172172
{
173173
return 0;
@@ -177,7 +177,7 @@ static int writeArguments( xmlTextWriterPtr writer, FmlSessionHandle handle, Fml
177177

178178
for( int i = 1; i <= count; i++ )
179179
{
180-
FmlObjectHandle argument = Fieldml_GetArgument( handle, object, i, 1, 1 );
180+
FmlObjectHandle argument = Fieldml_GetArgument( handle, object, i, /*isBound*/0, /*isUsed*/1 );
181181
if( argument == FML_INVALID_HANDLE )
182182
{
183183
continue;
@@ -478,6 +478,7 @@ static int writeReferenceEvaluator( xmlTextWriterPtr writer, FmlSessionHandle ha
478478

479479
writeObjectName( writer, NAME_ATTRIB, handle, object );
480480
writeObjectName( writer, EVALUATOR_ATTRIB, handle, Fieldml_GetReferenceSourceEvaluator( handle, object ) );
481+
writeObjectName( writer, VALUE_TYPE_ATTRIB, handle, Fieldml_GetValueType( handle, object ) );
481482

482483
writeBinds( writer, handle, object );
483484

@@ -581,10 +582,13 @@ static void writeParameterIndexes( xmlTextWriterPtr writer, FmlSessionHandle han
581582
xmlTextWriterStartElement( writer, INDEX_EVALUATOR_TAG );
582583
xmlTextWriterWriteAttribute( writer, EVALUATOR_ATTRIB, (const xmlChar*)Fieldml_GetObjectName( handle, index ) );
583584

584-
FmlObjectHandle order = Fieldml_GetParameterIndexOrder( handle, object, i );
585-
if( order != FML_INVALID_HANDLE )
585+
if( !isSparse )
586586
{
587-
xmlTextWriterWriteAttribute( writer, ORDER_ATTRIB, (const xmlChar*)Fieldml_GetObjectName( handle, order ) );
587+
FmlObjectHandle order = Fieldml_GetParameterIndexOrder( handle, object, i );
588+
if( order != FML_INVALID_HANDLE )
589+
{
590+
xmlTextWriterWriteAttribute( writer, ORDER_ATTRIB, (const xmlChar*)Fieldml_GetObjectName( handle, order ) );
591+
}
588592
}
589593

590594
xmlTextWriterEndElement( writer );

io/src/OutputStream.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ FmlIoErrorNumber FileOutputStream::writeDouble( double value )
174174
return FML_IOERR_RESOURCE_CLOSED;
175175
}
176176

177-
int err = fprintf( file, "%.8g ", value );
177+
int err = fprintf( file, "%.17g ", value );
178178

179179
if( err < 0 )
180180
{
@@ -268,8 +268,9 @@ FmlIoErrorNumber StringOutputStream::writeDouble( double value )
268268
{
269269
return FML_IOERR_RESOURCE_CLOSED;
270270
}
271-
272-
buffer << value << " ";
271+
char tmpValueString[50];
272+
sprintf(tmpValueString, "%.17g ", value);
273+
buffer << tmpValueString;
273274

274275
return FML_IOERR_NO_ERROR;
275276
}

test/src/fieldml_test.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,15 +512,17 @@ int testCycles()
512512

513513
FmlObjectHandle type = Fieldml_CreateContinuousType( session, "test.type" );
514514

515+
FmlObjectHandle type2 = Fieldml_CreateContinuousType( session, "test.type.2" );
516+
515517
FmlObjectHandle ensemble = Fieldml_CreateEnsembleType( session, "test.ensemble" );
516518
Fieldml_SetEnsembleMembersRange( session, ensemble, 1, 20, 1 );
517519

518520
FmlObjectHandle arg1 = Fieldml_CreateArgumentEvaluator( session, "test.arg1", type );
519521

520522
FmlObjectHandle external = Fieldml_CreateExternalEvaluator( session, "test.external", type );
521523

522-
FmlObjectHandle ref1 = Fieldml_CreateReferenceEvaluator( session, "test.reference1", external );
523-
FmlObjectHandle ref2 = Fieldml_CreateReferenceEvaluator( session, "test.reference2", ref1 );
524+
FmlObjectHandle ref1 = Fieldml_CreateReferenceEvaluator( session, "test.reference1", external , type);
525+
FmlObjectHandle ref2 = Fieldml_CreateReferenceEvaluator( session, "test.reference2", ref1 , type2);
524526

525527
if( Fieldml_SetBind( session, ref1, arg1, ref2 ) != FML_ERR_CYCLIC_DEPENDENCY )
526528
{
@@ -531,7 +533,7 @@ int testCycles()
531533
FmlObjectHandle param = Fieldml_CreateParameterEvaluator( session, "test.parameter", ensemble );
532534
Fieldml_SetParameterDataDescription( session, param, FML_DATA_DESCRIPTION_DOK_ARRAY );
533535

534-
FmlObjectHandle ref3 = Fieldml_CreateReferenceEvaluator( session, "test.reference3", param );
536+
FmlObjectHandle ref3 = Fieldml_CreateReferenceEvaluator( session, "test.reference3", param , type);
535537

536538
if( Fieldml_AddDenseIndexEvaluator( session, param, ref3, FML_INVALID_HANDLE ) != FML_ERR_CYCLIC_DEPENDENCY )
537539
{

0 commit comments

Comments
 (0)