Skip to content

Commit

Permalink
add properties for namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
jomifred committed Sep 5, 2024
1 parent 0ed3425 commit 4bfb255
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 29 deletions.
2 changes: 1 addition & 1 deletion jason-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
api 'org.jline:jline:3.22.0'
api 'org.fusesource.jansi:jansi:2.4.0'

api 'org.gradle:gradle-tooling-api:8.8'
api 'org.gradle:gradle-tooling-api:8.10'
//testImplementation group: 'junit', name: 'junit', version: '4.12'

implementation 'org.slf4j:slf4j-nop:1.7.21' // to avoid warning msgs of SLF4J
Expand Down
5 changes: 5 additions & 0 deletions jason-interpreter/src/main/java/jason/bb/BeliefBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

Expand Down Expand Up @@ -53,6 +54,10 @@ public Set<Atom> getNameSpaces() {
return null;
}

public void setNameSpaceProp(Atom ns, Atom key, Term value) {}
public Term getNameSpaceProp(Atom ns, Atom key) { return null; }
public Set<Atom> getNameSpaceProps(Atom ns) { return new HashSet<>(); }

/** Adds a belief in the end of the BB, returns true if succeed.
* The annots of l may be changed to reflect what was changed in the BB,
* for example, if l is p[a,b] in a BB with p[a], l will be changed to
Expand Down
27 changes: 27 additions & 0 deletions jason-interpreter/src/main/java/jason/bb/DefaultBeliefBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import jason.asSyntax.Atom;
import jason.asSyntax.Literal;
import jason.asSyntax.PredicateIndicator;
import jason.asSyntax.Term;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.Serial;
import java.io.Serializable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -20,6 +22,7 @@
*/
public class DefaultBeliefBase extends BeliefBase implements Serializable {

@Serial
private static final long serialVersionUID = 4189725430351480996L;

private static Logger logger = Logger.getLogger(DefaultBeliefBase.class.getSimpleName());
Expand All @@ -31,6 +34,7 @@ public class DefaultBeliefBase extends BeliefBase implements Serializable {
private Map<PredicateIndicator, BelEntry> belsMapDefaultNS = new ConcurrentHashMap<>();

private Map<Atom, Map<PredicateIndicator, BelEntry>> nameSpaces = new ConcurrentHashMap<>();
private Map<Atom, Map<Atom, Term>> nameSpaceProps = new HashMap<>();

private int size = 0;

Expand All @@ -53,6 +57,29 @@ public Set<Atom> getNameSpaces() {
return nameSpaces.keySet();
}

@Override
public void setNameSpaceProp(Atom ns, Atom key, Term value) {
nameSpaceProps
.computeIfAbsent(ns, k -> new HashMap<>())
.put(key,value);
}

@Override
public Term getNameSpaceProp(Atom ns, Atom key) {
if (nameSpaceProps.containsKey(ns))
return nameSpaceProps.get(ns).get(key);
else
return null;
}

@Override
public Set<Atom> getNameSpaceProps(Atom ns) {
if (nameSpaceProps.containsKey(ns))
return nameSpaceProps.get(ns).keySet();
else
return new HashSet<Atom>();
}

@Override
public int size() {
return size;
Expand Down
30 changes: 5 additions & 25 deletions jason-interpreter/src/main/java/jason/stdlib/namespace.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,17 @@
<li>+ arg[0] (any term): the term to be checked.<br/>
</ul>
<p>Examples assuming the BB is currently
{a(10),family::brother(bob),b(f,1)}:
<p>Examples assuming that the BB contains the following beliefs
a(10),family::brother(bob),b(f,1):
<ul>
<li> <code>.namespace(family)</code>: true.
<li> <code>.namespace(any_other)</code>: false.
</ul>
@see jason.stdlib.findall
@see jason.stdlib.setof
@see jason.stdlib.count
@see namespace_set_prop
@see namespace_get_prop
*/
@Manual(
literal=".namespace(argument)",
hint="checks whether the argument is a namespace",
argsHint= {
"the term to be checked"
},
argsType= {
"term"
},
examples= {
".namespace(family), assuming the BB is {a(10),family::brother(bob),b(f,1)}: true",
".namespace(any_other), assuming the BB is {a(10),family::brother(bob),b(f,1)}: false"
},
seeAlso= {
"jason.stdlib.findall",
"jason.stdlib.setof",
"jason.stdlib.count"
}
)
@SuppressWarnings("serial")

public class namespace extends DefaultInternalAction {

@Override public int getMinArgs() {
Expand Down
101 changes: 101 additions & 0 deletions jason-interpreter/src/main/java/jason/stdlib/namespace_get_prop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package jason.stdlib;

import jason.JasonException;
import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.Atom;
import jason.asSyntax.Term;

import java.util.Iterator;

/**
<p>Internal action: <b><code>.namespace_get_prop</code></b>.
<p>Description: gets a property for a namespace.
<p>Parameter:<ul>
<li>+ arg[0] (atom): the namespace id.<br/>
<li>+ arg[1] (atom): the key.<br/>
<li>- arg[2] (var or term): the value.<br/>
<li>+ (optional) arg[3] (term): default value if key does not exit.<br/>
</ul>
<p>Example:
<ul>
<li> <code>.namespace_get_prop(family,uri,X)</code>: X unifies with the value for key uri; internal action fails if no such key.
<li> <code>.namespace_get_prop(family,uri,X,"http://x.com")</code>: X unifies with the value for key uri; unifies with "http://x.com" if no key.
<li> <code>.namespace_get_prop(family,K)</code>: unifies K with any property key (backtracks for all keys).
<li> <code>.namespace_get_prop(family,K,V)</code>: unifies K with any property jey and its value V.
</ul>
@see namespace
@see namespace_set_prop
*/

public class namespace_get_prop extends DefaultInternalAction {

@Override public int getMinArgs() {
return 2;
}
@Override public int getMaxArgs() {
return 4;
}

@Override protected void checkArguments(Term[] args) throws JasonException {
super.checkArguments(args); // check number of arguments
if (!args[0].isAtom())
throw JasonException.createWrongArgument(this,"first argument must be an atom (the namespace id).");
}

@Override public Object execute(final TransitionSystem ts, final Unifier un, final Term[] args) throws Exception {
checkArguments(args);
if (!args[1].isVar()) {
var t = ts.getAg().getBB().getNameSpaceProp((Atom) args[0], (Atom) args[1]);
if (t != null) {
return un.unifies(t, args[2]);
} else if (args.length == 4) {
return un.unifies(args[3], args[2]);
}
} else { // gets all props
return new Iterator<Unifier>() {
Iterator<Atom> i = ts.getAg().getBB().getNameSpaceProps((Atom)args[0]).iterator();
Unifier n = null;

{
next(); // consume the first (and set first n value, i.e. the first solution)
}

public boolean hasNext() {
return n != null;
}

public Unifier next() {
Unifier c = n;

n = un.clone();
if (i.hasNext()) {
var key = i.next();
if (!n.unifiesNoUndo(args[1], key)) {
next();
} else {
if (args.length == 3) {
var value = ts.getAg().getBB().getNameSpaceProp((Atom)args[0], key);
n.unifiesNoUndo(args[2], value);
}
}
} else {
n = null;
}

return c;
}

public void remove() {}
};
}
return true;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package jason.stdlib;

import jason.JasonException;
import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.Atom;
import jason.asSyntax.Term;

/**
<p>Internal action: <b><code>.namespace_set_prop</code></b>.
<p>Description: sets a property for a namespace.
<p>Parameter:<ul>
<li>+ arg[0] (atom): the namespace id.<br/>
<li>+ arg[1] (atom): the key.<br/>
<li>+ arg[2] (term): the value.<br/>
</ul>
<p>Example:
<ul>
<li> <code>.namespace_set_prop(family,uri,"http://hubner.org")</code>: true.
</ul>
@see namespace
@see namespace_get_prop
*/

public class namespace_set_prop extends DefaultInternalAction {

@Override public int getMinArgs() {
return 3;
}
@Override public int getMaxArgs() {
return 3;
}

@Override protected void checkArguments(Term[] args) throws JasonException {
super.checkArguments(args); // check number of arguments
if (!args[0].isAtom())
throw JasonException.createWrongArgument(this,"first argument must be an atom (the namespace id).");
}

@Override public Object execute(final TransitionSystem ts, final Unifier un, final Term[] args) throws Exception {
checkArguments(args);
ts.getAg().getBB().setNameSpaceProp( (Atom)args[0], (Atom) args[1], args[2]);
return true;
}

}
17 changes: 17 additions & 0 deletions jason-interpreter/src/test/jason/asl/stdlib/namespace.asl
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,21 @@

!assert_true( .namespace(family) );
!assert_true( .namespace(financial) );

.namespace_set_prop(family, uri, "http://xxx.com");
.namespace_set_prop(family, size, 4);
.namespace_set_prop(family, root, bob);

.namespace_get_prop(family, uri, URI);
!assert_equals("http://xxx.com", URI);

.namespace_get_prop(family, kk, KK, k1);
!assert_equals(k1, KK);

.findall( K, .namespace_get_prop(family, K), LK);
!assert_equals([uri,root,size], LK);

.findall( [K,V], .namespace_get_prop(family, K, V), LKV);
!assert_equals([[uri,"http://xxx.com"],[root,bob],[size,4]], LKV);

.
10 changes: 7 additions & 3 deletions jason-interpreter/src/test/jason/unit_tests_2.mas2j
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// to be run with (in the directory src/test/jason)
// jason unit_tests_2.mas2j

// without jason, with java:
// ./gradlew config
// export JASON_UJ=$JASON_HOME/jason-cli/build/bin
// java -cp "$JASON_UJ/*" jason.infra.local.RunLocalMAS unit_tests_2.mas2j --log-conf logging.properties
//
MAS unit_tests {

agents:
test_manager [
goals="create_tester_agents(\"./asl\",\"drop_all_desires.asl\")",
goals="create_tester_agents(\"./asl\",\"namespace.asl\")",
beliefs="shutdown_delay(4)"
];

aslSourcePath: "src/test/jason/inc";"src/test/jason/asl";"inc"; "$jasonJar/test/jason/inc";
aslSourcePath: "src/test/jason/inc";"src/test/jason/asl"; "inc"; "$jasonJar/test/jason/inc";
}

0 comments on commit 4bfb255

Please sign in to comment.