Skip to content

Commit f349e94

Browse files
committed
Add GroupLoader to the phrasemes loader, improve light context by LightContextTypeLookup
1 parent 014f8a9 commit f349e94

File tree

15 files changed

+184
-44
lines changed

15 files changed

+184
-44
lines changed

light-core/src/main/java/org/panda_lang/light/framework/design/architecture/linguistic/Context.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public interface Context {
2828

2929
Context fork();
3030

31+
@Nullable Type<?> getType(Class<?> clazz);
32+
3133
@Nullable Type<?> getType(String type);
3234

3335
}

light-core/src/main/java/org/panda_lang/light/framework/language/architecture/linguistic/LightContext.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929

3030
public class LightContext implements Context {
3131

32-
private final Collection<ContextComponent<?>> context;
32+
protected final Collection<ContextComponent<?>> context;
33+
protected final LightContextTypeLookup typeLookup;
3334

3435
private LightContext(Collection<ContextComponent<?>> context) {
35-
this.context = new HashSet<>(context);
36+
this.context = context;
37+
this.typeLookup = new LightContextTypeLookup(this);
3638
}
3739

3840
public LightContext() {
@@ -90,27 +92,13 @@ public LightContext fork() {
9092
}
9193

9294
@Override
93-
@SuppressWarnings("unchecked")
94-
public @Nullable Type<?> getType(String type) {
95-
for (ContextComponent<?> component : context) {
96-
if (Type.class != component.getComponentType()) {
97-
continue;
98-
}
99-
100-
return getType((ContextComponent<? extends Type<?>>) component, type);
101-
}
102-
103-
return null;
95+
public @Nullable Type<?> getType(Class<?> clazz) {
96+
return typeLookup.getType(type -> clazz.isAssignableFrom(type.getAssociated()));
10497
}
10598

106-
private @Nullable Type<?> getType(ContextComponent<? extends Type<?>> types, String type) {
107-
for (Type<?> element : types.getElements()) {
108-
if (element.getClassName().equals(type) || element.getAssociated().getSimpleName().equals(type)) {
109-
return element;
110-
}
111-
}
112-
113-
return null;
99+
@Override
100+
public @Nullable Type<?> getType(String typeName) {
101+
return typeLookup.getType(type -> type.getClassName().equals(typeName) || type.getAssociated().getSimpleName().equals(typeName));
114102
}
115103

116104
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.panda_lang.light.framework.language.architecture.linguistic;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
import org.panda_lang.light.framework.design.architecture.linguistic.ContextComponent;
5+
import org.panda_lang.light.framework.design.architecture.linguistic.type.Type;
6+
7+
import java.util.Collection;
8+
import java.util.function.Predicate;
9+
import java.util.stream.Collectors;
10+
11+
class LightContextTypeLookup {
12+
13+
protected final LightContext lightContext;
14+
15+
protected LightContextTypeLookup(LightContext lightContext) {
16+
this.lightContext = lightContext;
17+
}
18+
19+
protected @Nullable Type<?> getType(Predicate<Type<?>> condition) {
20+
return getType(getTypes(), condition);
21+
}
22+
23+
private @Nullable Type<?> getType(Collection<ContextComponent<? extends Type<?>>> types, Predicate<Type<?>> condition) {
24+
for (ContextComponent<? extends Type<?>> type : types) {
25+
Type<?> matched = getType(type, condition);
26+
27+
if (matched != null) {
28+
return matched;
29+
}
30+
}
31+
32+
return null;
33+
}
34+
35+
private @Nullable Type<?> getType(ContextComponent<? extends Type<?>> types, Predicate<Type<?>> condition) {
36+
for (Type<?> element : types.getElements()) {
37+
if (condition.test(element)) {
38+
return element;
39+
}
40+
}
41+
42+
return null;
43+
}
44+
45+
@SuppressWarnings("unchecked")
46+
private Collection<ContextComponent<? extends Type<?>>> getTypes() {
47+
return lightContext.context.stream()
48+
.filter(component -> component.getComponentType() == Type.class)
49+
.map(component -> (ContextComponent<? extends Type<?>>) component)
50+
.collect(Collectors.toList());
51+
}
52+
53+
}

light-core/src/main/java/org/panda_lang/light/framework/language/architecture/linguistic/phraseme/LightPhrasemeLinguisticAct.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import org.panda_lang.light.framework.design.architecture.linguistic.type.Type;
66
import org.panda_lang.panda.framework.design.runtime.ExecutableBranch;
77

8-
public class LightPhrasemeLinguisticAct implements LinguisticAct {
8+
class LightPhrasemeLinguisticAct implements LinguisticAct {
99

1010
private final PhrasemeCallback callback;
1111
private final Type<?> returnType;
1212

13-
public LightPhrasemeLinguisticAct(PhrasemeCallback callback, Type<?> returnType) {
13+
LightPhrasemeLinguisticAct(PhrasemeCallback callback, Type<?> returnType) {
1414
this.callback = callback;
1515
this.returnType = returnType;
1616
}

light-core/src/main/java/org/panda_lang/light/framework/language/architecture/linguistic/phraseme/PhrasemeLoaderUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.panda_lang.light.framework.language.architecture.linguistic.phraseme;
22

3-
public class PhrasemeLoaderUtils {
3+
class PhrasemeLoaderUtils {
44

5-
public static Object[] cast(Object[] parameters, Class<?>[] types) {
5+
static Object[] cast(Object[] parameters, Class<?>[] types) {
66
for (int i = 0; i < parameters.length; i++) {
77
parameters[i] = types[i].cast(parameters[i]);
88
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.panda_lang.light.framework.language.architecture.linguistic.phraseme.loader;
2+
3+
import org.panda_lang.light.framework.design.architecture.linguistic.Context;
4+
import org.panda_lang.light.framework.design.architecture.linguistic.phraseme.Phraseme;
5+
import org.panda_lang.light.framework.language.architecture.linguistic.phraseme.loader.annotations.PhrasemeGroup;
6+
import org.panda_lang.light.framework.language.architecture.linguistic.phraseme.loader.annotations.PhrasemeVariant;
7+
import org.panda_lang.panda.utilities.annotations.AnnotationsScannerProcess;
8+
import org.panda_lang.panda.utilities.commons.ReflectionUtils;
9+
10+
import java.lang.reflect.Method;
11+
import java.util.ArrayList;
12+
import java.util.Collection;
13+
14+
class PhrasemeGroupLoader {
15+
16+
protected Collection<Phraseme> load(Context context, AnnotationsScannerProcess process) throws Exception {
17+
Collection<Phraseme> phrasemes = new ArrayList<>();
18+
19+
Collection<Class<?>> selected = process.createSelector()
20+
.selectTypesAnnotatedWith(PhrasemeGroup.class);
21+
22+
for (Class<?> clazz : selected) {
23+
Phraseme phraseme = loadPhrasemeGroup(context, clazz);
24+
//phrasemes.add(phraseme);
25+
}
26+
27+
return phrasemes;
28+
}
29+
30+
// TODO: \o/
31+
private Phraseme loadPhrasemeGroup(Context context, Class<?> clazz) throws Exception {
32+
String phrasemeGroup = clazz.getAnnotation(PhrasemeGroup.class).value();
33+
34+
Collection<Method> variantMethods = ReflectionUtils.getMethodsAnnotatedWith(clazz, PhrasemeVariant.class);
35+
36+
return null;
37+
}
38+
39+
}

light-core/src/main/java/org/panda_lang/light/framework/language/architecture/linguistic/phraseme/registry/PhrasemeRepresentationLoader.java renamed to light-core/src/main/java/org/panda_lang/light/framework/language/architecture/linguistic/phraseme/loader/PhrasemeRepresentationLoader.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package org.panda_lang.light.framework.language.architecture.linguistic.phraseme.registry;
1+
package org.panda_lang.light.framework.language.architecture.linguistic.phraseme.loader;
22

33
import org.panda_lang.light.LightException;
44
import org.panda_lang.light.framework.design.architecture.linguistic.Context;
55
import org.panda_lang.light.framework.design.architecture.linguistic.phraseme.Phraseme;
66
import org.panda_lang.light.framework.design.interpreter.pattern.linguistic.LinguisticPattern;
77
import org.panda_lang.light.framework.language.architecture.linguistic.phraseme.LightPhraseme;
88
import org.panda_lang.light.framework.language.architecture.linguistic.phraseme.PhrasemeCallback;
9+
import org.panda_lang.light.framework.language.architecture.linguistic.phraseme.loader.annotations.PhrasemeRepresentation;
910
import org.panda_lang.panda.framework.design.runtime.ExecutableBranch;
1011
import org.panda_lang.panda.utilities.annotations.AnnotationsScannerProcess;
1112
import org.panda_lang.panda.utilities.commons.objects.StringUtils;
@@ -15,21 +16,17 @@
1516
import java.util.ArrayList;
1617
import java.util.Collection;
1718

18-
public class PhrasemeRepresentationLoader {
19+
class PhrasemeRepresentationLoader {
1920

20-
public Collection<Phraseme> load(Context context, AnnotationsScannerProcess process) {
21+
protected Collection<Phraseme> load(Context context, AnnotationsScannerProcess process) throws Exception {
2122
Collection<Phraseme> phrasemes = new ArrayList<>();
2223

2324
Collection<Method> methods = process.createSelector()
2425
.selectMethodsAnnotatedWith(PhrasemeRepresentation.class);
2526

2627
for (Method method : methods) {
27-
try {
28-
Phraseme phraseme = load(context, method);
29-
phrasemes.add(phraseme);
30-
} catch (Exception e) {
31-
throw new LightException(e);
32-
}
28+
Phraseme phraseme = load(context, method);
29+
phrasemes.add(phraseme);
3330
}
3431

3532
return phrasemes;
@@ -62,7 +59,8 @@ public Object call(ExecutableBranch branch, Object[] convertedParameters) {
6259
}
6360
};
6461

65-
return new LightPhraseme(pattern, callback, context.getType(method.getReturnType().getSimpleName()));
62+
return new LightPhraseme(pattern, callback, context.getType(method.getReturnType()));
6663
}
6764

65+
6866
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.panda_lang.light.framework.language.architecture.linguistic.phraseme.loader;
2+
3+
import org.panda_lang.light.LightException;
4+
import org.panda_lang.light.framework.design.architecture.linguistic.Context;
5+
import org.panda_lang.light.framework.design.architecture.linguistic.phraseme.Phraseme;
6+
import org.panda_lang.panda.utilities.annotations.AnnotationsScannerProcess;
7+
8+
import java.util.Collection;
9+
10+
public class PhrasemesLoader {
11+
12+
public Collection<Phraseme> load(Context context, AnnotationsScannerProcess process) {
13+
try {
14+
return loadAll(context, process);
15+
} catch (Exception e) {
16+
throw new LightException("Cannot load phrasemes: " + e.getMessage(), e);
17+
}
18+
}
19+
20+
private Collection<Phraseme> loadAll(Context context, AnnotationsScannerProcess process) throws Exception {
21+
Collection<Phraseme> phrasemes;
22+
23+
PhrasemeRepresentationLoader representationLoader = new PhrasemeRepresentationLoader();
24+
phrasemes = representationLoader.load(context, process);
25+
26+
PhrasemeGroupLoader groupLoader = new PhrasemeGroupLoader();
27+
phrasemes.addAll(groupLoader.load(context, process));
28+
29+
return phrasemes;
30+
}
31+
32+
}

light-core/src/main/java/org/panda_lang/light/framework/language/architecture/linguistic/phraseme/registry/Id.java renamed to light-core/src/main/java/org/panda_lang/light/framework/language/architecture/linguistic/phraseme/loader/annotations/Id.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.panda_lang.light.framework.language.architecture.linguistic.phraseme.registry;
1+
package org.panda_lang.light.framework.language.architecture.linguistic.phraseme.loader.annotations;
22

33
import java.lang.annotation.ElementType;
44
import java.lang.annotation.Retention;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.panda_lang.light.framework.language.architecture.linguistic.phraseme.loader.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target(ElementType.TYPE)
9+
@Retention(RetentionPolicy.RUNTIME)
10+
public @interface PhrasemeGroup {
11+
12+
String value();
13+
14+
}

0 commit comments

Comments
 (0)