Skip to content

Commit

Permalink
#173 - Rename add_feature to create_feature (#177)
Browse files Browse the repository at this point in the history
- Add create_feature
- Deprecate add_feature
- Update README
  • Loading branch information
jcklie authored Aug 14, 2021
1 parent 9db7617 commit 445fa3a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 37 deletions.
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ Creating types and adding features
typesystem = TypeSystem()
parent_type = typesystem.create_type(name='example.ParentType')
typesystem.add_feature(type_=parent_type, name='parentFeature', rangeTypeName='String')
typesystem.create_feature(type_=parent_type, name='parentFeature', rangeTypeName='String')
child_type = typesystem.create_type(name='example.ChildType', supertypeName=parent_type.name)
typesystem.add_feature(type_=child_type, name='childFeature', rangeTypeName='Integer')
typesystem.create_feature(type_=child_type, name='childFeature', rangeTypeName='Integer')
annotation = child_type(parentFeature='parent', childFeature='child')
Expand Down Expand Up @@ -318,8 +318,8 @@ available as a member variable :code:`self_` or :code:`type_` on the respective
typesystem = TypeSystem()
ExampleType = typesystem.create_type(name='example.Type')
typesystem.add_feature(type_=ExampleType, name='self', rangeTypeName='String')
typesystem.add_feature(type_=ExampleType, name='type', rangeTypeName='String')
typesystem.create_feature(type_=ExampleType, name='self', rangeTypeName='String')
typesystem.create_feature(type_=ExampleType, name='type', rangeTypeName='String')
annotation = ExampleType(self_="Test string1", type_="Test string2")
Expand Down
69 changes: 48 additions & 21 deletions cassis/typesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import IO, Any, Callable, Dict, Iterable, Iterator, List, Optional, Set, Union

import attr
import deprecation
from lxml import etree
from more_itertools import unique_everseen
from toposort import toposort_flatten
Expand Down Expand Up @@ -389,7 +390,7 @@ def __init__(self, add_document_annotation_type: bool = True):

# Array
t = self.create_type(name="uima.cas.ArrayBase", supertypeName="uima.cas.TOP")
self.add_feature(t, name="elements", rangeTypeName="uima.cas.TOP", multipleReferencesAllowed=True)
self.create_feature(t, name="elements", rangeTypeName="uima.cas.TOP", multipleReferencesAllowed=True)

self.create_type(name="uima.cas.FSArray", supertypeName="uima.cas.ArrayBase")
self.create_type(name="uima.cas.BooleanArray", supertypeName="uima.cas.ArrayBase")
Expand All @@ -406,47 +407,47 @@ def __init__(self, add_document_annotation_type: bool = True):
self.create_type(name="uima.cas.FSList", supertypeName="uima.cas.ListBase")
self.create_type(name="uima.cas.EmptyFSList", supertypeName="uima.cas.FSList")
t = self.create_type(name="uima.cas.NonEmptyFSList", supertypeName="uima.cas.FSList")
self.add_feature(t, name="head", rangeTypeName="uima.cas.TOP", multipleReferencesAllowed=True)
self.add_feature(t, name="tail", rangeTypeName="uima.cas.FSList", multipleReferencesAllowed=True)
self.create_feature(t, name="head", rangeTypeName="uima.cas.TOP", multipleReferencesAllowed=True)
self.create_feature(t, name="tail", rangeTypeName="uima.cas.FSList", multipleReferencesAllowed=True)

# FloatList
self.create_type(name="uima.cas.FloatList", supertypeName="uima.cas.ListBase")
self.create_type(name="uima.cas.EmptyFloatList", supertypeName="uima.cas.FloatList")
t = self.create_type(name="uima.cas.NonEmptyFloatList", supertypeName="uima.cas.FloatList")
self.add_feature(t, name="head", rangeTypeName="uima.cas.Float")
self.add_feature(t, name="tail", rangeTypeName="uima.cas.FloatList", multipleReferencesAllowed=True)
self.create_feature(t, name="head", rangeTypeName="uima.cas.Float")
self.create_feature(t, name="tail", rangeTypeName="uima.cas.FloatList", multipleReferencesAllowed=True)

# IntegerList
self.create_type(name="uima.cas.IntegerList", supertypeName="uima.cas.ListBase")
self.create_type(name="uima.cas.EmptyIntegerList", supertypeName="uima.cas.IntegerList")
t = self.create_type(name="uima.cas.NonEmptyIntegerList", supertypeName="uima.cas.IntegerList")
self.add_feature(t, name="head", rangeTypeName="uima.cas.Integer")
self.add_feature(t, name="tail", rangeTypeName="uima.cas.IntegerList", multipleReferencesAllowed=True)
self.create_feature(t, name="head", rangeTypeName="uima.cas.Integer")
self.create_feature(t, name="tail", rangeTypeName="uima.cas.IntegerList", multipleReferencesAllowed=True)

# StringList
self.create_type(name="uima.cas.StringList", supertypeName="uima.cas.ListBase")
self.create_type(name="uima.cas.EmptyStringList", supertypeName="uima.cas.StringList")
t = self.create_type(name="uima.cas.NonEmptyStringList", supertypeName="uima.cas.StringList")
self.add_feature(t, name="head", rangeTypeName="uima.cas.String")
self.add_feature(t, name="tail", rangeTypeName="uima.cas.StringList", multipleReferencesAllowed=True)
self.create_feature(t, name="head", rangeTypeName="uima.cas.String")
self.create_feature(t, name="tail", rangeTypeName="uima.cas.StringList", multipleReferencesAllowed=True)

# Sofa
t = self.create_type(name="uima.cas.Sofa", supertypeName="uima.cas.TOP")
self.add_feature(t, name="sofaNum", rangeTypeName="uima.cas.Integer")
self.add_feature(t, name="sofaID", rangeTypeName="uima.cas.String")
self.add_feature(t, name="mimeType", rangeTypeName="uima.cas.String")
self.add_feature(t, name="sofaArray", rangeTypeName="uima.cas.TOP", multipleReferencesAllowed=True)
self.add_feature(t, name="sofaString", rangeTypeName="uima.cas.String")
self.add_feature(t, name="sofaURI", rangeTypeName="uima.cas.String")
self.create_feature(t, name="sofaNum", rangeTypeName="uima.cas.Integer")
self.create_feature(t, name="sofaID", rangeTypeName="uima.cas.String")
self.create_feature(t, name="mimeType", rangeTypeName="uima.cas.String")
self.create_feature(t, name="sofaArray", rangeTypeName="uima.cas.TOP", multipleReferencesAllowed=True)
self.create_feature(t, name="sofaString", rangeTypeName="uima.cas.String")
self.create_feature(t, name="sofaURI", rangeTypeName="uima.cas.String")

# AnnotationBase
t = self.create_type(name="uima.cas.AnnotationBase", supertypeName="uima.cas.TOP")
self.add_feature(t, name="sofa", rangeTypeName="uima.cas.Sofa")
self.create_feature(t, name="sofa", rangeTypeName="uima.cas.Sofa")

# Annotation
t = self.create_type(name="uima.tcas.Annotation", supertypeName="uima.cas.AnnotationBase")
self.add_feature(t, name="begin", rangeTypeName="uima.cas.Integer")
self.add_feature(t, name="end", rangeTypeName="uima.cas.Integer")
self.create_feature(t, name="begin", rangeTypeName="uima.cas.Integer")
self.create_feature(t, name="end", rangeTypeName="uima.cas.Integer")

if add_document_annotation_type:
self._add_document_annotation_type()
Expand Down Expand Up @@ -598,7 +599,7 @@ def subsumes(self, parent_name: str, child_name: str) -> bool:

return False

def add_feature(
def create_feature(
self,
type_: Type,
name: str,
Expand Down Expand Up @@ -644,6 +645,32 @@ def add_feature(

type_.add_feature(feature)

@deprecation.deprecated(details="Use create_feature")
def add_feature(
self,
type_: Type,
name: str,
rangeTypeName: str,
elementType: str = None,
description: str = None,
multipleReferencesAllowed: bool = None,
):
"""Adds a feature to the given type.
Args:
type_: The type to which the feature will be added
name: The name of the new feature
rangeTypeName: The feature's rangeTypeName specifies the type of value that the feature can take.
elementType: The elementType of a feature is optional, and applies only when the rangeTypeName
is uima.cas.FSArray or uima.cas.FSList The elementType specifies what type of value can be
assigned as an element of the array or list.
description: The description of the new feature
multipleReferencesAllowed: Setting this to true indicates that the array or list may be shared,
so changes to it may affect other objects in the CAS.
Raises:
Exception: If a feature with name `name` already exists in `type_`.
"""
self.create_feature(type_, name, rangeTypeName, elementType, description, multipleReferencesAllowed)

def to_xml(self, path: Union[str, Path, None] = None) -> Optional[str]:
"""Creates a XMI representation of this type system.
Expand Down Expand Up @@ -703,7 +730,7 @@ def _defines_predefined_type(self, type_name):

def _add_document_annotation_type(self):
t = self.create_type(name=_DOCUMENT_ANNOTATION_TYPE, supertypeName="uima.tcas.Annotation")
self.add_feature(t, name="language", rangeTypeName="uima.cas.String")
self.create_feature(t, name="language", rangeTypeName="uima.cas.String")


# Deserializing
Expand Down Expand Up @@ -838,7 +865,7 @@ def deserialize(self, source: Union[IO, str]) -> TypeSystem:
# between type references in inheritance and type references in range or element type.
for t in created_types:
for f in features[t.name]:
ts.add_feature(
ts.create_feature(
t,
name=f.name,
rangeTypeName=f.rangeTypeName,
Expand Down
24 changes: 12 additions & 12 deletions tests/test_typesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_feature_can_be_added():
typesystem = TypeSystem()

test_type = typesystem.create_type(name="test.Type")
typesystem.add_feature(type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature")
typesystem.create_feature(type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature")

actual_type = typesystem.get_type("test.Type")
actual_feature = actual_type.get_feature("testFeature")
Expand All @@ -41,9 +41,9 @@ def test_feature_adding_warns_if_redefined_identically():

test_type = typesystem.create_type(name="test.Type")

typesystem.add_feature(type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature")
typesystem.create_feature(type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature")
with pytest.warns(UserWarning):
typesystem.add_feature(
typesystem.create_feature(
type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature"
)

Expand All @@ -52,10 +52,10 @@ def test_feature_adding_throws_if_redefined_differently():
typesystem = TypeSystem()

test_type = typesystem.create_type(name="test.Type")
typesystem.add_feature(type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature")
typesystem.create_feature(type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature")

with pytest.raises(ValueError):
typesystem.add_feature(
typesystem.create_feature(
type_=test_type, name="testFeature", rangeTypeName="Boolean", description="A test feature"
)

Expand All @@ -75,7 +75,7 @@ def test_type_can_be_created():
def test_type_can_create_instances():
typesystem = TypeSystem()
test_type = typesystem.create_type(name="test.Type")
typesystem.add_feature(type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature")
typesystem.create_feature(type_=test_type, name="testFeature", rangeTypeName="String", description="A test feature")

annotation = test_type(begin=0, end=42, testFeature="testValue")

Expand All @@ -88,10 +88,10 @@ def test_type_can_create_instance_with_inherited_fields():
typesystem = TypeSystem()

parent_type = typesystem.create_type(name="test.ParentType")
typesystem.add_feature(type_=parent_type, name="parentFeature", rangeTypeName="String")
typesystem.create_feature(type_=parent_type, name="parentFeature", rangeTypeName="String")

child_type = typesystem.create_type(name="test.ChildType", supertypeName=parent_type.name)
typesystem.add_feature(type_=child_type, name="childFeature", rangeTypeName="Integer")
typesystem.create_feature(type_=child_type, name="childFeature", rangeTypeName="Integer")

annotation = child_type(parentFeature="parent", childFeature="child")

Expand Down Expand Up @@ -500,7 +500,7 @@ def test_that_merging_compatible_typesystem_works(name, rangeTypeName, elementTy

ts = TypeSystem()
t = ts.create_type("test.ArraysAndListsWithElementTypes", supertypeName="uima.cas.TOP")
ts.add_feature(
ts.create_feature(
type_=t,
name=name,
rangeTypeName=rangeTypeName,
Expand Down Expand Up @@ -533,7 +533,7 @@ def test_that_merging_incompatible_typesystem_throws(name, rangeTypeName, elemen

ts = TypeSystem()
t = ts.create_type("test.ArraysAndListsWithElementTypes", supertypeName="uima.cas.TOP")
ts.add_feature(
ts.create_feature(
type_=t,
name=name,
rangeTypeName=rangeTypeName,
Expand Down Expand Up @@ -596,8 +596,8 @@ def test_typchecking_fs_array():
MyOtherValue = cas.typesystem.create_type(name="test.MyOtherValue", supertypeName="uima.cas.TOP")
MyCollection = cas.typesystem.create_type("test.MyCollection", supertypeName="uima.cas.TOP")

cas.typesystem.add_feature(type_=MyValue, name="value", rangeTypeName="uima.cas.String")
cas.typesystem.add_feature(
cas.typesystem.create_feature(type_=MyValue, name="value", rangeTypeName="uima.cas.String")
cas.typesystem.create_feature(
type_=MyCollection, name="members", rangeTypeName="uima.cas.FSArray", elementType="test.MyValue"
)

Expand Down

0 comments on commit 445fa3a

Please sign in to comment.