Skip to content

Commit

Permalink
Made fixes to interpreter infrastructure, got rid of sick hack, lists…
Browse files Browse the repository at this point in the history
… are interpretting correctly (ish) now
  • Loading branch information
Tom White committed Mar 20, 2013
1 parent 86b9223 commit ec8a1fd
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 36 deletions.
25 changes: 23 additions & 2 deletions simplCore/src/simpl/descriptions/FieldCategorizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import simpl.annotations.dbal.simpl_map;
import simpl.annotations.dbal.simpl_scalar;
import simpl.core.ElementState;
import simpl.platformspecifics.SimplPlatformSpecifics;
import simpl.tools.XMLTools;
import simpl.types.TypeRegistry;

Expand Down Expand Up @@ -39,8 +40,10 @@ public FieldType categorizeField(Field thatField)
// THIS WILL NOT BE A PERMANENT SOLUTION.
if (FieldCategorizer.isEnumCollection(thatField)) {
// Enums are scalars at the moment.
fieldType = FieldType.COLLECTION_ELEMENT;
} else {
fieldType = FieldType.COLLECTION_SCALAR;
} else if(FieldCategorizer.isScalarCollection(thatField)){
fieldType = FieldType.COLLECTION_SCALAR;
}else{
fieldType = FieldType.COLLECTION_ELEMENT;
}
} else if (FieldCategorizer.representAsMap(thatField)) {
Expand All @@ -49,7 +52,25 @@ public FieldType categorizeField(Field thatField)

return fieldType;
}




public static boolean isScalarCollection(Field f) {
if(representAsCollection(f))
{
ArrayList<Class<?>> classes = XMLTools.getGenericParameters(f);
if(classes.isEmpty())
{
return false;
}else{
return TypeRegistry.containsScalarTypeFor(classes.get(0));
}
}else{
return false;
}
}

public static boolean isEnumCollection(Field f) {
if(representAsCollection(f))
{
Expand Down
15 changes: 8 additions & 7 deletions simplCore/src/simpl/descriptions/FieldDescriptors.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ else if(classIsMap(toDescribe.getType()))

}

nfd.setListType(new ListType(collectionType));
nfd.setMapType(new MapType(collectionType));
}
else if(classIsCollection(toDescribe.getType()))
{
Class<?> mapType = toDescribe.getType();
if(classIsInterfaceOrAbstract(mapType))
Class<?> listType = toDescribe.getType();
if(classIsInterfaceOrAbstract(listType))
{
// we need to obtain the type via creating an instance of the CD.
try
Expand All @@ -121,15 +121,15 @@ else if(classIsCollection(toDescribe.getType()))
throw new RuntimeException("Fields which are defined with an interface must have an instance initialized by their public constructor in order to be simpl serialized!");
}

mapType = instance.getClass();
listType = instance.getClass();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}

nfd.setMapType(new MapType(mapType));
nfd.setListType(new ListType(listType));
}
else
{
Expand Down Expand Up @@ -259,10 +259,11 @@ private static boolean classIsInterfaceOrAbstract(Class<?> toDescribe)
}

private static boolean classIsMap(Class<?> type) {
return type.isAssignableFrom(Map.class);
return Map.class.isAssignableFrom(type);
}

private static boolean classIsCollection(Class<?> type) {
return type.isAssignableFrom(Collection.class) && !type.isAssignableFrom(Map.class);
//type.
return Collection.class.isAssignableFrom(type) && !Map.class.isAssignableFrom(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ public JsonDeserializer()
{
}

private CompositeInterpretation(String tagOrField, ClassDescriptor contextCD, ISimplTypesScope sts)
{

}

public CompositeInterpretation deserialize(String objRepr, ISimplTypesScope sts) throws SIMPLTranslationException
{
Object val = JSONValue.parse(objRepr);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package simpl.interpretation;

public interface InterpreterInstanceSelector {
boolean selectInstance(Object obj);
SimplInterpretation obtainInterpreter();
}
38 changes: 35 additions & 3 deletions simplCore/src/simpl/interpretation/ListInterpretation.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

import simpl.descriptions.ClassDescriptor;
import simpl.descriptions.ClassDescriptors;
import simpl.descriptions.FieldCategorizer;
import simpl.descriptions.FieldDescriptor;
import simpl.exceptions.SIMPLTranslationException;
import simpl.tools.ReflectionTools;
import simpl.types.ListType;

public class ListInterpretation implements SimplInterpretation {
Expand All @@ -18,6 +20,11 @@ public class ListInterpretation implements SimplInterpretation {

ListType ourListType;

public ListType getListType()
{
return this.ourListType;
}

public String getFieldName()
{
return this.fieldName;
Expand All @@ -30,12 +37,23 @@ public void setFieldName(String s)

List<SimplInterpretation> interps;

public List<SimplInterpretation> getInterpretations()
{
return this.interps;
}

public ListInterpretation()
{
this.interps = new LinkedList<SimplInterpretation>();
this.ourListType = new ListType();
}

public ListInterpretation(Class<?> listType)
{
this.interps = new LinkedList<SimplInterpretation>();
this.ourListType = new ListType(listType);
}

public void addItemInterpretation(SimplInterpretation si)
{
this.interps.add(si);
Expand Down Expand Up @@ -85,14 +103,28 @@ public SimplInterpretation interpret(Object context, FieldDescriptor field,
InterpretationContext interpretationContext)
throws SIMPLTranslationException {

return null;
ListInterpretation li = (ListInterpretation) interpretObject(ReflectionTools.getFieldValue(field.getField(), context), interpretationContext);

li.setFieldName(field.getName());

return li;
}

@Override
public SimplInterpretation interpretObject(Object theObject,
InterpretationContext interpretationContext) throws SIMPLTranslationException {
// TODO Auto-generated method stub
return null;

ListInterpretation li = new ListInterpretation(theObject.getClass());

Collection theCollection = (Collection) theObject;

for(Object o: theCollection)
{
SimplInterpretation si = interpretationContext.interpretObject(o);
li.addItemInterpretation(si);
}

return li;
}

}
99 changes: 85 additions & 14 deletions simplCore/src/simpl/interpretation/SimplInterpreter.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package simpl.interpretation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import simpl.descriptions.ClassDescriptor;
import simpl.descriptions.ClassDescriptors;
import simpl.descriptions.FieldDescriptor;
import simpl.descriptions.FieldType;
import simpl.exceptions.SIMPLTranslationException;
import simpl.types.ScalarType;
import simpl.types.TypeRegistry;

public class SimplInterpreter {

Expand All @@ -16,30 +22,95 @@ public class SimplInterpreter {
public SimplInterpreter()
{
this.thisInterpContext= new InterpretationContext(this);
this.initializeInterpreters();
}


static CompositeInterpretation compositeInterpReference = new CompositeInterpretation("");

public SimplInterpretation interpretInstance(Object obj) throws SIMPLTranslationException
{
return compositeInterpReference.interpretObject(obj, thisInterpContext);
return selectInterpretationForObject(obj).interpretObject(obj, this.thisInterpContext);
}

public SimplInterpretation interpretField(Object obj, FieldDescriptor fd) throws SIMPLTranslationException
private Map<FieldType, SimplInterpretation> interpreterByFieldType = new HashMap<FieldType, SimplInterpretation>();
private List<InterpreterInstanceSelector> interpreterBySelection = new ArrayList<InterpreterInstanceSelector>(4);

private SimplInterpretation selectInterpretationForObject(Object obj) throws SIMPLTranslationException
{
int size = interpreterBySelection.size();
for(int i = 0; i < size; i++)
{
if(interpreterBySelection.get(i).selectInstance(obj))
{
return interpreterBySelection.get(i).obtainInterpreter();
}
}
throw new SIMPLTranslationException("No interpreter matches the given object type! " + obj.getClass().getName());
}

private void initializeInterpreters()
{
// Register fieldtype level mappings
interpreterByFieldType.put(FieldType.SCALAR, new ScalarInterpretation());
interpreterByFieldType.put(FieldType.COMPOSITE_ELEMENT, new CompositeInterpretation(""));
interpreterByFieldType.put(FieldType.COLLECTION_ELEMENT, new ListInterpretation());
interpreterByFieldType.put(FieldType.COLLECTION_SCALAR, new ListInterpretation());
interpreterByFieldType.put(FieldType.MAP_SCALAR, new MapInterpretation());
interpreterByFieldType.put(FieldType.MAP_ELEMENT, new MapInterpretation());

// Register type level mappings
interpreterBySelection.add(new InterpreterInstanceSelector() {
@Override public boolean selectInstance(Object obj) {
return TypeRegistry.containsScalarTypeFor(obj.getClass()) || obj.getClass().isEnum();
}

@Override public SimplInterpretation obtainInterpreter() {
return new ScalarInterpretation();
}});

interpreterBySelection.add(new InterpreterInstanceSelector() {
@Override public boolean selectInstance(Object obj) {
return Map.class.isAssignableFrom(obj.getClass());
}

@Override public SimplInterpretation obtainInterpreter() {
return new MapInterpretation();
}});

interpreterBySelection.add(new InterpreterInstanceSelector() {
@Override public boolean selectInstance(Object obj) {
return !Map.class.isAssignableFrom(obj.getClass()) && Collection.class.isAssignableFrom(obj.getClass());
}

@Override public SimplInterpretation obtainInterpreter() {
return new ListInterpretation();
}});

interpreterBySelection.add(new InterpreterInstanceSelector() {
@Override public boolean selectInstance(Object obj) {
return true;
}

@Override public SimplInterpretation obtainInterpreter() {
return new CompositeInterpretation("");
}});
}

private SimplInterpretation getInterpreterByFieldType(FieldType ft) throws SIMPLTranslationException
{
// TODO: ELIMINATE THIS HIDEOUS HACK. :)
switch(fd.getType())
SimplInterpretation interp = interpreterByFieldType.get(ft);
if(interp == null)
{
case SCALAR:
return new ScalarInterpretation().interpret(obj, fd, this.thisInterpContext);
case COMPOSITE_ELEMENT:
return new CompositeInterpretation("").interpret(obj, fd, this.thisInterpContext);
default:
break;

throw new SIMPLTranslationException("Type currently not supported! " + ft.name());
}

return null;
return interp;
}

public SimplInterpretation interpretField(Object obj, FieldDescriptor fd) throws SIMPLTranslationException
{
return this.getInterpreterByFieldType(fd.getType()).interpret(obj, fd, this.thisInterpContext);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import simpl.descriptions.ClassDescriptor;
import simpl.descriptions.ClassDescriptors;
import simpl.descriptions.FieldDescriptor;
import simpl.descriptions.FieldType;
import simpl.interpretation.listOfScalars;
import simpl.types.ListType;

Expand Down Expand Up @@ -66,6 +68,11 @@ public void testListTypeCreatedForDeclaredListType()
assertNotNull("Should have a list type!", lt);
Object instance = lt.createInstance();
assertEquals(instance.getClass(), LinkedList.class);

FieldDescriptor fd = cd.allFieldDescriptors().get(0);
assertEquals(FieldType.COLLECTION_SCALAR, fd.getType());


}

@Test
Expand Down
28 changes: 23 additions & 5 deletions simplTests/test/simpl/interpretation/InterpretingListsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,35 @@ public void testInterpretScalarList() throws Exception {
los.myList.add(1);
los.myList.add(2);
los.myList.add(3);

los.myString = "string";

CompositeInterpretation ci = new CompositeInterpretation("list_of_scalars");

ListInterpretation li = new ListInterpretation(los.myList.getClass());
li.addItemInterpretation(new ScalarInterpretation("", "0", "IntegerType"));
li.addItemInterpretation(new ScalarInterpretation("", "1", "IntegerType"));
li.addItemInterpretation(new ScalarInterpretation("", "2", "IntegerType"));
li.addItemInterpretation(new ScalarInterpretation("", "3", "IntegerType"));
li.setFieldName("myList");

ci.addInterpretation(ci);

SimplInterpreter interpreter = new SimplInterpreter();

SimplUnderstander understander = new SimplUnderstander(context);

// Object result = understander.understandInterpretation(interpreter.interpretInstance(los), "list_of_scalars");
CompositeInterpretation rootInterp = (CompositeInterpretation)interpreter.interpretInstance(los);
assertEquals(2,rootInterp.interpretations.size());
ListInterpretation listInterp = (ListInterpretation)rootInterp.interpretations.get(0);

assertEquals(4, listInterp.getInterpretations().size());
assertEquals("list_of_scalars",rootInterp.getTagName());



Object result = understander.understandInterpretation(rootInterp);

Object result = null;
fail("reimplement w/ new structure");


assertNotNull(result);

listOfScalars theResult = (listOfScalars)result;
Expand Down

0 comments on commit ec8a1fd

Please sign in to comment.